Last active
March 23, 2018 07:36
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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