Skip to content

Instantly share code, notes, and snippets.

@bschlenk
Last active March 23, 2018 07:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bschlenk/f4f7a84d0f042a4803542c5b9af7de32 to your computer and use it in GitHub Desktop.
Save bschlenk/f4f7a84d0f042a4803542c5b9af7de32 to your computer and use it in GitHub Desktop.
Call macOS's "say" program with every possible voice at the same time with the given phrase.
#!/usr/bin/env node
/**
* Call macOS's `say` program with every possible voice at the same
* time with the given phrase. Scare your pets.
*/
const { exec } = require('child_process');
function getVoices(callback) {
const command = 'say -v ?';
console.log('attempting to list the available voices');
return new Promise((resolve, reject) => {
console.log('starting the say process with %s', command);
exec(command, (err, stdout, stderr) => {
if (err) {
reject(err);
return;
}
stdout = stdout.toString('utf8');
const lines = stdout.split('\n');
const voices = lines.map(l => l.split(' ')[0])
resolve(voices);
});
})
}
function saySomething(phrase, voice) {
const command = `say -v ${voice} ${phrase}`;
console.log('executing command "%s', command);
return new Promise((resolve, reject) => {
exec(command).on('exit', (code, signal) => {
console.log('%s exiting', voice);
resolve();
});
});
}
function parseArgs() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log('Please specify something to say!');
process.exit(1);
}
let synchronous = false;
if (['-s', '--synchronous'].includes(args[0])) {
synchronous = true;
args.splice(0, 1);
}
const phrase = args.join(' ');
return { phrase, synchronous };
}
async function main() {
const { phrase, synchronous } = parseArgs();
const voices = await getVoices();
if (synchronous) {
for (const voice of voices) {
await saySomething(phrase, voice);
}
} else {
voices.forEach(voice => saySomething(phrase, voice));
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment