Programs may run for a long time. It is useful to say something when the program is finished in macOS.
Say is a utility program to convert text to audible speech. Linux alternative: spd-say
Try to make better Hello World
examples using different programming languages:
import os
def say(m = "Finish", v = "Victoria"):
os.system(f'say -v {v} {m}')
Usage in Python:
say() # English
say(m="完了吧,如無意外",v="Sin-ji") # Cantonese
say(m="完結",v="Ting-Ting") # Chinese
say(m="終わり",v="Kyoko") # Japanese
say(m="종료",v="Yuna") # Korean
say(m="fin",v="Monica") # Spanish
say(m="finir",v="Amelie") # French
import Foundation
func say(m: String, v: String) {
let p = Process()
p.executableURL = URL(fileURLWithPath: "/usr/bin/env")
p.arguments = ["say", "-v", v, m]
do {
try p.run()
p.waitUntilExit()
} catch {
print("Unexpected error: \(error).")
}
}
Usage in Swift:
say(m:"Finish", v:"Victoria") // English
say(m:"完了吧,如無意外",v:"Sin-ji") // Cantonese
say(m:"完結",v:"Ting-Ting") // Chinese
say(m:"終わり",v:"Kyoko") // Japanese
say(m:"종료",v:"Yuna") // Korean
say(m:"fin",v:"Monica") // Spanish
say(m:"finir",v:"Amelie") // French
import "os/exec"
func say(m string, v string) {
exec.Command("say", "-v", v, m).Run()
}
Usage in Go:
say("Finish", "Victoria") // English
say("完了吧,如無意外", "Sin-ji") // Cantonese
say("完結", "Ting-Ting") // Chinese
say("終わり", "Kyoko") // Japanese
say("종료", "Yuna") // Korean
say("fin", "Monica") // Spanish
say("finir", "Amelie") // French
// By default, Javascript runs in a parallel (async/await is needed)
const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function say(m = "Finish", v = "Victoria") {
await exec(`say -v ${v} ${m}`)
}
Usage in NodeJS:
// Note: without using async/await,
// all sentences will be spoken at the same time
async function main() {
await say() // English
await say(m="完了吧,如無意外",v="Sin-ji") // Cantonese
await say(m="完結",v="Ting-Ting") // Chinese
await say(m="終わり",v="Kyoko") // Japanese
await say(m="종료",v="Yuna") // Korean
await say(m="fin",v="Monica") // Spanish
await say(m="finir",v="Amelie") // French
}
main()
#include <stdio.h>
#include <stdlib.h>
void say(char *m, char *v) {
char cmd[1000];
sprintf(cmd, "say -v %s %s\n", v, m);
system(cmd);
}
Usage in C/C++:
- u8 prefix: (UTF-8) encoding is needed for non-ASCII string
int main() {
say("Finish","Victoria"); // English
say(u8"完了吧,如無意外", "Sin-ji"); // Cantonese
say(u8"完結", "Ting-Ting"); // Chinese
say(u8"終わり", "Kyoko" ); // Japanese
say(u8"종료","Yuna"); // Korean
say("fin","Monica"); // Spanish
say("finir","Amelie"); // French
return 0;
}
public class Say
{
public static void say(String m, String v) throws Exception {
String cmd = "say -v " + v + " " + m;
Runtime.getRuntime().exec(cmd).waitFor(); // wait until it finishes
}
}
Usage in Java:
Say.say("Finish","Victoria"); // English
Say.say("完了吧,如無意外", "Sin-ji"); // Cantonese
Say.say("完結", "Ting-Ting"); // Chinese
Say.say("終わり", "Kyoko"); // Japanese
Say.say("종료", "Yuna"); // Korean
Say.say("fin", "Monica"); // Spanish
Say.say("finir", "Amelie"); // French
-
Using the Web Speech API
-
Note: speechSynthesis.speak() without user activation is no longer allowed since M71, around December 2018.
-
See https://www.chromestatus.com/feature/5687444770914304 for more details
-
Implementation: src/vanilla_js
// say.js (vanilla)
let voices = []; // store the voices
let synth = window.speechSynthesis;
function say(m = "Finish", v = "Victoria") {
const speak = () => {
let utterThis = new SpeechSynthesisUtterance(m);
for ( let voice of voices ) {
if ( voice.name === v ) {
utterThis.voice = voice;
break;
}
}
synth.speak(utterThis);
}
if ( voices.length === 0 ) { // For the first time without a voice list
// Note: synth.getVoices() - use setInterval trick to get the voice list from the server
let id = setInterval(() => {
voices = synth.getVoices();
if (voices.length !== 0) {
clearInterval(id);
speak();
}
}, 10);
}
else { // call the speak() handler if the voice list is ready
speak();
}
}
Usage in Vanilla JS
<script src="say.js"></script>
<button onclick="say('Finish','Victoria');">English</button>
<button onclick="say('完了吧,如無意外','Sin-ji');" >Cantonese</button>
<button onclick="say('完結','Ting-Ting');">Chinese</button>
<button onclick="say('終わり','Kyoko');">Japanese</button>
<button onclick="say('종료','Yuna');">Korean</button>
<button onclick="say('fin','Monica');">Spanish</button>
<button onclick="say('finir','Amelie');">French</button>
say
function with different default values for the supporting human languages can be founded in src/py
import os
def say(m = "完結", v = "Ting-Ting"):
os.system(f'say -v {v} {m}')
import os
def say(m = "完結", v = "Sin-ji"):
os.system(f'say -v {v} {m}')
import os
def say(m = "fin", v = "Juan"):
os.system(f'say -v {v} {m}')
import os
def say(m = "終わり", v = "Kyoko"):
os.system(f'say -v {v} {m}')
import os
def say(m = "종료", v = "Yuna"):
os.system(f'say -v {v} {m}')