Added voice control

Former-commit-id: 6f69079bf44f0d8f9ae40de6b0f1638d103464c2
This commit is contained in:
Ziver Koc 2015-05-13 21:14:10 +00:00
parent 35c92407a3
commit 53da641909
863 changed files with 192681 additions and 0 deletions

View file

@ -0,0 +1,126 @@
package se.koc.hal.stt;
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.LineUnavailableException;
import com.darkprograms.speech.microphone.MicrophoneAnalyzer;
import com.darkprograms.speech.recognizer.FlacEncoder;
import com.darkprograms.speech.recognizer.GSpeechDuplex;
import com.darkprograms.speech.recognizer.GSpeechResponseListener;
import com.darkprograms.speech.recognizer.GoogleResponse;
import se.koc.hal.intf.HalSpeachToText;
import zutil.io.file.FileUtil;
public class GoogleSTTClient implements HalSpeachToText, GSpeechResponseListener {
private MicrophoneAnalyzer mic;
private File audioFile;
private File flacFile;
private int ambientVolume;
private GSpeechDuplex google;
private String response;
@Override
public void initSTT() {
try{
this.mic = new MicrophoneAnalyzer(AudioFileFormat.Type.WAVE);
this.audioFile = File.createTempFile("input", "wav");
this.flacFile = File.createTempFile("input", "flac");
this.google = new GSpeechDuplex("AIzaSyBGAQ29aMvts9MObj739_HYa-tvNeEI0X8");
this.google.addResponseListener(this);
/*System.out.println("Messuring ambient noise...");
mic.captureAudioToFile(audioFile);
Thread.sleep(300);
ambientVolume = mic.getAudioVolume();
Thread.sleep(300);
mic.close();*/
}catch(Exception e){
e.printStackTrace();
}
}
@Override
public synchronized String listen() {
try {
String request = null;
while(request == null){
/*mic.captureAudioToFile(audioFile);
try{Thread.sleep(2000);}catch(Exception e){}
mic.close();*/
FlacEncoder flacEncoder = new FlacEncoder();
//flacEncoder.convertWaveToFlac(audioFile, flacFile);
flacEncoder.convertWaveToFlac(FileUtil.find("edu/cmu/sphinx/demo/speakerid/test.wav"), flacFile);
response = null;
google.recognize(flacFile, 8000);
this.wait();
flacFile.delete();
audioFile.delete();
}
}catch(Exception e){
e.printStackTrace();
}
return response;
}
public String thresholdListen(){
try{
boolean speaking = false;
while(!speaking){
mic.captureAudioToFile(audioFile);
System.out.print("_");
final int THRESHOLD = 10; //YOUR THRESHOLD VALUE.
mic.open();
int speakingVolume = -2;
do{
int volume = mic.getAudioVolume();
if(volume>ambientVolume+THRESHOLD){
speakingVolume = volume;
speaking = true;
System.out.print(".");
Thread.sleep(1000);
}
if(speaking && volume+THRESHOLD<speakingVolume){
break;
}
Thread.sleep(100);
}while(speaking);
mic.close();
}
mic.close();
FlacEncoder flacEncoder = new FlacEncoder();
flacEncoder.convertWaveToFlac(audioFile, flacFile);
response = null;
google.recognize(flacFile, 8000);
this.wait();
flacFile.delete();
}catch(Exception e){
e.printStackTrace();
}
return response;
}
@Override
public void onResponse(GoogleResponse googleResponse) {
response = googleResponse.getResponse();
this.notifyAll();
}
}

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2015 ezivkoc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.koc.hal.stt;
import se.koc.hal.intf.HalSpeachToText;
import java.io.*;
public class ManualSTTClient implements HalSpeachToText {
private BufferedReader in;
@Override
public void initSTT() {
// open up standard input
in = new BufferedReader(new InputStreamReader(System.in));
}
@Override
public String listen() {
try {
return in.readLine();
} catch (IOException ioe) {
ioe.printStackTrace();
}
return null;
}
}

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.koc.hal.stt;
import edu.cmu.sphinx.api.Configuration;
import edu.cmu.sphinx.api.LiveSpeechRecognizer;
import edu.cmu.sphinx.api.SpeechResult;
import se.koc.hal.intf.HalSpeachToText;
import java.io.IOException;
/**
* Created by Ziver on 2015-05-08.
*/
public class Sphinx4STTClient implements HalSpeachToText {
private LiveSpeechRecognizer recognizer;
@Override
public void initSTT() {
Configuration configuration = new Configuration();
// Set path to acoustic model.
configuration.setAcousticModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us");
// Set path to dictionary.
configuration.setDictionaryPath("resource:/edu/cmu/sphinx/models/en-us/cmudict-en-us.dict");
// Set language model.
configuration.setLanguageModelPath("resource:/edu/cmu/sphinx/models/en-us/en-us.lm.dmp");
try {
recognizer = new LiveSpeechRecognizer(configuration);
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
@Override
public String listen() {
// Start recognition process pruning previously cached data.
recognizer.startRecognition(true);
SpeechResult result = recognizer.getResult();
// Pause recognition process. It can be resumed then with startRecognition(false).
recognizer.stopRecognition();
return result.getHypothesis();
}
}

View file

@ -0,0 +1,101 @@
/*
* Copyright (c) 2013 ezivkoc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.koc.hal.tts;
import com.darkprograms.speech.synthesiser.SynthesiserV2;
import javafx.embed.swing.JFXPanel;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import se.koc.hal.intf.HalTextToSpeach;
import java.io.*;
import java.net.URISyntaxException;
import java.net.URL;
/**
* Created with IntelliJ IDEA.
* User: ezivkoc
* Date: 2013-12-17
* Time: 13:45
*/
public class GoogleTTSClient implements HalTextToSpeach {
private static final String API_KEY = "AIzaSyBGAQ29aMvts9MObj739_HYa-tvNeEI0X8";
private SynthesiserV2 synthesiser;
private File tmpAudioFile;
public void initTTS() {
try {
// JavaFX should be initialized
JFXPanel fxPanel = new JFXPanel();
synthesiser = new SynthesiserV2(API_KEY);
tmpAudioFile = File.createTempFile("tts", "tmp");
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void speak(String msg) {
try{
String language = synthesiser.detectLanguage(msg);
InputStream is = synthesiser.getMP3Data(msg);
BufferedInputStream buff = new BufferedInputStream(is);
DataInputStream di = new DataInputStream(buff);
FileOutputStream fos = new FileOutputStream(tmpAudioFile);
while(di.available() != 0){
fos.write(di.readByte());
}
fos.close();
URL resource = tmpAudioFile.toURI().toURL();
Media media = new Media(resource.toURI().toString());
final MediaPlayer mediaPlayer = new MediaPlayer(media);
mediaPlayer.play();
mediaPlayer.setOnEndOfMedia(new Runnable() {
@Override public void run() {
mediaPlayer.stop();
}
});
while(mediaPlayer.getStatus() != MediaPlayer.Status.STOPPED){
try{Thread.sleep(200);}catch(Exception e){}
}
}catch(URISyntaxException e){
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,73 @@
/*
* Copyright (c) 2015 Ziver
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.koc.hal.tts;
import marytts.LocalMaryInterface;
import marytts.MaryInterface;
import marytts.client.RemoteMaryInterface;
import marytts.exceptions.MaryConfigurationException;
import marytts.exceptions.SynthesisException;
import marytts.util.data.audio.AudioPlayer;
import se.koc.hal.intf.HalTextToSpeach;
import javax.sound.sampled.AudioInputStream;
import java.io.IOException;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* User: ezivkoc
* Date: 2013-12-17
* Time: 14:36
*/
public class MaryLocalTTSClient implements HalTextToSpeach {
private MaryInterface marytts;
@Override
public void initTTS() {
try {
marytts = new LocalMaryInterface();
Set<String> voices = marytts.getAvailableVoices();
marytts.setVoice(voices.iterator().next());
} catch (MaryConfigurationException e) {
e.printStackTrace();
System.exit(1);
}
}
@Override
public void speak(String msg) {
try {
AudioInputStream audio = marytts.generateAudio(msg);
AudioPlayer player = new AudioPlayer(audio);
player.start();
player.join();
} catch (SynthesisException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View file

@ -0,0 +1,72 @@
/*
* Copyright (c) 2013 ezivkoc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package se.koc.hal.tts;
import marytts.MaryInterface;
import marytts.client.RemoteMaryInterface;
import marytts.exceptions.SynthesisException;
import marytts.util.data.audio.AudioPlayer;
import se.koc.hal.intf.HalTextToSpeach;
import javax.sound.sampled.AudioInputStream;
import java.io.IOException;
import java.util.Set;
/**
* Created with IntelliJ IDEA.
* User: ezivkoc
* Date: 2013-12-17
* Time: 14:36
*/
public class MaryRemoteTTSClient implements HalTextToSpeach {
private MaryInterface marytts;
@Override
public void initTTS() {
try {
marytts = new RemoteMaryInterface("127.0.0.1", 59125);
Set<String> voices = marytts.getAvailableVoices();
marytts.setVoice(voices.iterator().next());
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
@Override
public synchronized void speak(String msg) {
try {
AudioInputStream audio = marytts.generateAudio(msg);
AudioPlayer player = new AudioPlayer(audio);
player.start();
player.join();
} catch (SynthesisException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}