Last active
December 13, 2016 01:44
-
-
Save toolbits/3c03020ac943c5d38af2a2954dd1855a to your computer and use it in GitHub Desktop.
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
void setup() | |
{ | |
String[] device; | |
int i; | |
device = getSpeechDevice(); | |
for (i = 0; i < device.length; ++i) { | |
println(device[i]); | |
} | |
// simple version, default device | |
speechText("hello", true); | |
// explicitly select a device | |
speechText("hello", true, device[0]); | |
return; | |
} | |
import java.io.InputStreamReader; | |
String[] getSpeechDevice() | |
{ | |
Process proc; | |
BufferedReader reader; | |
String line; | |
ArrayList<String> result = new ArrayList(); | |
try { | |
proc = Runtime.getRuntime().exec(new String[]{"say", "-a", "?"}); | |
proc.waitFor(); | |
reader = new BufferedReader(new InputStreamReader(proc.getInputStream())); | |
while ((line = reader.readLine()) != null) { | |
line = line.trim(); | |
line = line.substring(line.indexOf(' ') + 1); | |
result.add(line); | |
} | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
result.clear(); | |
} | |
return result.toArray(new String[0]); | |
} | |
void speechText(String message, boolean wait) | |
{ | |
Process proc; | |
try { | |
proc = Runtime.getRuntime().exec(new String[]{"say", message}); | |
if (wait) { | |
proc.waitFor(); | |
} | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return; | |
} | |
void speechText(String message, boolean wait, String device) | |
{ | |
String[] param; | |
Process proc; | |
if (!device.isEmpty()) { | |
param = new String[]{"say", message, "-a", device}; | |
} else { | |
param = new String[]{"say", message}; | |
} | |
try { | |
proc = Runtime.getRuntime().exec(param); | |
if (wait) { | |
proc.waitFor(); | |
} | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment