Skip to content

Instantly share code, notes, and snippets.

@toolbits
Last active December 13, 2016 01:44
Show Gist options
  • Save toolbits/3c03020ac943c5d38af2a2954dd1855a to your computer and use it in GitHub Desktop.
Save toolbits/3c03020ac943c5d38af2a2954dd1855a to your computer and use it in GitHub Desktop.
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