-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
216 additions
and
1 deletion.
There are no files selected for viewing
Binary file not shown.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package com.company; | ||
|
||
import com.darkprograms.speech.recognizer.GoogleResponse; | ||
import info.debatty.java.stringsimilarity.JaroWinkler; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Created by user on 21.11.16. | ||
*/ | ||
public class CommandsRecognizer implements ResponseListener { | ||
|
||
JaroWinkler jaroWinkler; | ||
|
||
private static final String startTraining = "начать обучение"; | ||
private static final String poseType = "название новой позы"; | ||
private static final String capturePose = "фиксация"; | ||
private static final String completeTraining = "закончить обучение"; | ||
|
||
|
||
public CommandsRecognizer() { | ||
jaroWinkler = new JaroWinkler(); | ||
} | ||
|
||
|
||
@Override | ||
public void onResponce(GoogleResponse response) { | ||
List<String> responces = response.getOtherPossibleResponses(); | ||
|
||
double startTrainingSimilarity = 0; | ||
double poseTypeSimilarity = 0; | ||
double capturePoseSimilarity = 0; | ||
double completeTrainingSimilarity = 0; | ||
|
||
|
||
for(String responce : responces ) { | ||
if(responce != null) { | ||
|
||
if(startTrainingSimilarity < jaroWinkler.similarity(responce, startTraining)) { | ||
startTrainingSimilarity = jaroWinkler.similarity(responce, startTraining); | ||
} | ||
|
||
if(poseTypeSimilarity < jaroWinkler.similarity(responce, poseType)) { | ||
poseTypeSimilarity = jaroWinkler.similarity(responce, poseType); | ||
} | ||
|
||
if(capturePoseSimilarity < jaroWinkler.similarity(responce, capturePose)) { | ||
capturePoseSimilarity = jaroWinkler.similarity(responce, capturePose); | ||
} | ||
|
||
if(completeTrainingSimilarity < jaroWinkler.similarity(responce, completeTraining)) { | ||
completeTrainingSimilarity = jaroWinkler.similarity(responce, completeTraining); | ||
} | ||
} | ||
} | ||
|
||
System.out.println(startTrainingSimilarity); | ||
System.out.println(poseTypeSimilarity); | ||
System.out.println(capturePoseSimilarity); | ||
System.out.println(completeTrainingSimilarity); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package com.company; | ||
|
||
import com.darkprograms.speech.microphone.MicrophoneAnalyzer; | ||
import com.darkprograms.speech.recognizer.GoogleResponse; | ||
import com.darkprograms.speech.recognizer.Recognizer; | ||
import javaFlacEncoder.FLACFileWriter; | ||
|
||
import java.io.File; | ||
import java.util.ArrayList; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by user on 20.11.16. | ||
*/ | ||
public class RecordingThread extends Thread { | ||
|
||
private String apiKey = "AIzaSyDMRFZsdncfP2udmTbozAQ2owJuL5RRm34"; | ||
|
||
private boolean debug = true; | ||
|
||
private int minimumVolumeToStartrecording = 40; | ||
private int volumeToStopRecording = 20; | ||
private int checkVolumeSampleTime = 10; | ||
private int sampleTime = 1000; | ||
|
||
private MicrophoneAnalyzer microphone; | ||
private File tempAudioFile; | ||
private Recognizer recognizer; | ||
|
||
private List<ResponseListener> listeners; | ||
|
||
public RecordingThread() { | ||
|
||
microphone = new MicrophoneAnalyzer(FLACFileWriter.FLAC); | ||
tempAudioFile = new File("temp.flac"); | ||
microphone.setAudioFile(tempAudioFile); | ||
recognizer = new Recognizer(Recognizer.Languages.RUSSIAN, apiKey); | ||
listeners = new ArrayList<ResponseListener>(); | ||
|
||
} | ||
|
||
public void addResponceListener(ResponseListener listener) { | ||
listeners.add(listener); | ||
} | ||
|
||
@Override | ||
public void run() { | ||
|
||
while (true) { | ||
microphone.open(); | ||
|
||
try { | ||
microphone.captureAudioToFile(microphone.getAudioFile()); | ||
Thread.sleep(checkVolumeSampleTime * 3); | ||
|
||
int volume = microphone.getAudioVolume(checkVolumeSampleTime); | ||
boolean isSpeaking = (volume > minimumVolumeToStartrecording); | ||
|
||
if (isSpeaking) { | ||
|
||
DebugLog("RECORDING..."); | ||
|
||
do { | ||
Thread.sleep(sampleTime);//Updates every second | ||
} while (microphone.getAudioVolume(sampleTime) > volumeToStopRecording); | ||
|
||
|
||
DebugLog("Recording Complete!"); | ||
DebugLog("Recognizing..."); | ||
|
||
GoogleResponse response = recognizer.getRecognizedDataForFlac(microphone.getAudioFile(), 3); | ||
notifyListeners(response); | ||
|
||
DebugLog("Looping back");//Restarts loops | ||
|
||
} | ||
|
||
} catch (Exception e) { | ||
// TODO Auto-generated catch block | ||
e.printStackTrace(); | ||
System.out.println("Error Occured"); | ||
} finally { | ||
microphone.close();//Makes sure microphone closes on exit. | ||
} | ||
} | ||
|
||
} | ||
|
||
private void notifyListeners (GoogleResponse response) { | ||
for(ResponseListener listener : listeners) { | ||
if(listener != null) { | ||
listener.onResponce(response); | ||
} | ||
} | ||
} | ||
|
||
|
||
private void DebugLog(String message) { | ||
if (debug) { | ||
System.out.println(message); | ||
} | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.company; | ||
|
||
import com.darkprograms.speech.recognizer.GoogleResponse; | ||
|
||
/** | ||
* Created by user on 21.11.16. | ||
*/ | ||
public interface ResponseListener { | ||
void onResponce(GoogleResponse response); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.company; | ||
|
||
import com.darkprograms.speech.recognizer.GoogleResponse; | ||
|
||
/** | ||
* Created by user on 21.11.16. | ||
*/ | ||
public class temp implements ResponseListener { | ||
@Override | ||
public void onResponce(GoogleResponse response) { | ||
|
||
displayResponse(response); | ||
|
||
} | ||
|
||
|
||
private static void displayResponse(GoogleResponse gr) { | ||
if (gr.getResponse() == null) { | ||
System.out.println((String) null); | ||
return; | ||
} | ||
System.out.println("Google Response: " + gr.getResponse()); | ||
System.out.println("Google is " + Double.parseDouble(gr.getConfidence()) * 100 + "% confident in" | ||
+ " the reply"); | ||
System.out.println("Other Possible responses are: "); | ||
for (String s : gr.getOtherPossibleResponses()) { | ||
System.out.println("\t" + s); | ||
} | ||
} | ||
} |