-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsfx.js
197 lines (165 loc) · 6.31 KB
/
sfx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var exec = require("child_process").exec,
fs = require("fs");
const DEBUG = true;
const LIBRARY = __dirname + "/sounds/";
var sfx = {
library: {},
config: {},
proc: null,
/**
* Play a sound.
* @param {String} sound The sound file or name of sound in library
* @param {Number} volume Volume between 0-100
* @param {Number} rate Rate at which the sound should play
*/
play: function(sound, _volume, _rate, _callback) {
// Sort out the arguments
var volume, rate, callback;
if (typeof _volume == "function") callback = _volume;
else if (typeof _rate == "function") callback = _rate, volume = _volume;
else volume = _volume, rate = _rate, callback = _callback;
var config = sfx.config,
command = config.play.command;
// Play a random sound
if (sound == "random") sound = Object.keys(sfx.library)[Math.floor(Math.random() * Object.keys(sfx.library).length)];
// Volume
if (volume && config.play.options.volume) {
var v = config.play.options.volume;
command += " " + sfx.format(v.syntax, {
// Denormalize via the range
volume: (v.range[1] - v.range[0]) * (volume / 100)
});
}
// Rate
if (rate && config.play.options.rate) command += " " + sfx.format(config.play.options.rate.syntax, {
rate: rate
});
// Play the sound
sfx.getSound(sound, function(err, path) {
command += " \"" + path + "\"";
sfx.run(command, callback);
});
},
/**
* Quick helper function to format a string of variables
* @private
* @param {String} string String with variables in the form of ":varName"
* @param {Object} values Object containing variable values
* @return {String} Formatted string
*/
format: function(string, values) {
return string.replace(/\:(\w+)/g, function(match, value) {
return values[value];
});
},
/**
* Retrieve and test a sound via a path or name.
* Works kindof like `require`:
* 1. Check if sound is at path
* 2. Check if sound is in ./sound
*
* @param {String} path Path or name of sound
* @param {Function} callback Callback with (err, soundPath)
*/
getSound: function(path, callback) {
fs.exists(path, function(exists) {
// Sound exists at given path
if (exists) callback(null, path);
else {
// Check if the sound is in the library
if (sfx.library[path]) callback(null, LIBRARY + sfx.library[path]);
else callback(new Error("Sound not found."));
}
});
},
/**
* Say a message. Mac/Darwin only.
* @param {String} message Message to say aloud
* @param {String} voice Voice. Accepts "random" and will play with a random voice
*/
say: function(message, _voice, _callback) {
var voice, callback;
if(typeof _voice == "function") callback = _voice;
else voice = _voice, callback = _callback;
if(process.platform !== "darwin") {
if(callback) return callback();
else return console.log("`say` is a darwin only feature. Sorry, for now.");
}
var voices = "Agnes,Albert,Alex,Bad News,Bahh,Bells,Boing,Bruce,Bubbles,Cellos,Deranged,Fred,Good News,Hysterical,Junior,Kathy,Pipe Organ,Princess,Ralph,Trinoids,Vicki,Victoria,Whisper,Zarvox".split(",").map(function(v) {
return v.toLowerCase();
});
if(voice == "random") voice = voices[Math.floor(Math.random() * voice.length)];
else if(voice && voices.indexOf(voice) == -1) throw new Error("Voice does not exist!");
sfx.run("say " + (voice ? "-v " + voice + " " : "") + message, callback);
},
/**
* Run a command.
* @param {String} command Command string
* @return {[type]} [description]
*/
run: function(command, callback) {
DEBUG && console.log("Running command: ", command);
sfx.stop(); // Stop any running processes
sfx.proc = exec(command); // Execute the command
// Add the callback
if(callback) {
sfx.proc.on("exit", callback);
sfx.proc.on("error", callback);
}
},
/**
* Stop the current sound.
* @return {[type]} [description]
*/
stop: function() {
if(sfx.proc) sfx.proc.kill("SIGKILL");
},
/**
* Verify an inputted configuration.
* @param {Object} config
* @return {Object} The configuation object
*/
verifyConfig: function(config) {
Object.keys(config).forEach(function(command) {
var c = config[command];
if (!c.command) throw new Error("sfx: Bad configuration. Please specify a command attribute in the `" + command + "`");
if (!c.options) return c.options = {};
// Loop over the options
Object.keys(c.options).forEach(function(option) {
var o = c.options[option];
if (!o.syntax) throw new Error("sfx: Bad configuration. Please specify the syntax attribute in the option `" + option + "` in the `" + command + "` command");
});
});
return config;
},
/**
* Initilize sfx
* @return {sfx}
*/
init: function() {
// Get and verify the configuration
try {
sfx.config = sfx.verifyConfig(require("./platform/" + process.platform + ".json"));
} catch (err) {
if (err.code == "ENOENT") console.log("sfx: Platform not supported yet.");
else {
throw err;
}
}
// Populate the library
// Syncronous to prevent unpopulated library from being used.
var files = fs.readdirSync(LIBRARY);
files.forEach(function(file) {
var split = file.split(".");
split.pop(); // Pop off the extension
var name = split.join(".").toLowerCase();
sfx.library[name] = file;
// Bind the to sfx
sfx[name] = sfx.play.bind(sfx, name);
});
// Random
sfx.random = sfx.play.bind(sfx, "random");
return sfx;
}
};
module.exports = sfx.init();