Skip to content

Commit

Permalink
Добавлена функция тестирования из файлов.
Browse files Browse the repository at this point in the history
  • Loading branch information
3XclusiVe committed Feb 8, 2017
1 parent b00b17b commit 4cd95e7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
33 changes: 27 additions & 6 deletions src/Tests/TestFromDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.company.CommandsRecognizers.CommandsRecognizer;
import com.company.microphone.MicrophoneAnalyzer;
import com.company.testInterfaceCommandsListener;
import com.darkprograms.speech.microphone.Microphone;
import com.darkprograms.speech.recognizer.GoogleResponse;
import com.darkprograms.speech.recognizer.Recognizer;
import javaFlacEncoder.FLACFileWriter;
Expand All @@ -14,7 +16,7 @@
*/
public class TestFromDirectory {

public static String pathToDirectoryWithAudio = "";
public static String pathToDirectoryWithAudio = "/Users/user/Desktop/no_Converted/Converted";

public static CommandsRecognizer commandsRecognizer = new CommandsRecognizer();

Expand All @@ -23,12 +25,14 @@ public class TestFromDirectory {
public static void main (String[]args) throws IOException {

commandsRecognizer.addListener(testCommandCounterAndStatistic);
commandsRecognizer.addListener(new testInterfaceCommandsListener());

File f = new File(pathToDirectoryWithAudio);
File[] directory = f.listFiles();

for (File file : directory) {
testFromFile(file);
System.out.println(file.getName());
}

testCommandCounterAndStatistic.printStats();
Expand All @@ -37,14 +41,31 @@ public static void main (String[]args) throws IOException {

public static void testFromFile(File audioFile) throws IOException {

MicrophoneAnalyzer mic = new MicrophoneAnalyzer(FLACFileWriter.FLAC);
Microphone mic = new Microphone(FLACFileWriter.FLAC);

mic.setAudioFile(audioFile);

Recognizer rec = new Recognizer(Recognizer.Languages.RUSSIAN, "AIzaSyDMRFZsdncfP2udmTbozAQ2owJuL5RRm34");
GoogleResponse response = rec.getRecognizedDataForFlac(mic.getAudioFile(), 10);
Recognizer recognizer = new Recognizer (Recognizer.Languages.RUSSIAN, "AIzaSyDMRFZsdncfP2udmTbozAQ2owJuL5RRm34");
//Although auto-detect is available, it is recommended you select your region for added accuracy.
try {
int maxNumOfResponses = 1;
System.out.println("Sample rate is: " + (int) mic.getAudioFormat().getSampleRate());
GoogleResponse response = recognizer.getRecognizedDataForFlac (audioFile, maxNumOfResponses, (int) mic.getAudioFormat().getSampleRate ());
System.out.println ("Google Response: " + response.getResponse ());
System.out.println ("Google is " + Double.parseDouble (response.getConfidence ()) * 100 + "% confident in" + " the reply");
System.out.println ("Other Possible responses are: ");
for (String s:response.getOtherPossibleResponses ()) {
System.out.println ("\t" + s);
}

commandsRecognizer.onResponce(response);
}
catch (Exception ex) {
// TODO Handle how to respond if Google cannot be contacted
System.out.println ("ERROR: Google cannot be contacted");
ex.printStackTrace ();
}


commandsRecognizer.onResponce(response);

}

Expand Down
2 changes: 1 addition & 1 deletion src/Tests/TestFromFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void main (String[]args) {


Microphone mic = new Microphone(FLACFileWriter.FLAC);
File file = new File ("/Users/user/Desktop/22/DisplayAllReponce.flac"); //Name your file whatever you want
File file = new File ("/Users/user/Desktop/1/1.flac"); //Name your file whatever you want


Recognizer recognizer = new Recognizer (Recognizer.Languages.RUSSIAN, "AIzaSyDMRFZsdncfP2udmTbozAQ2owJuL5RRm34");
Expand Down
4 changes: 2 additions & 2 deletions src/com/company/RecordingThread/RecordingThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public RecordingThread() {
recognizer = new Recognizer(Recognizer.Languages.RUSSIAN, apiKey);
listeners = new ArrayList<ResponseListener>();

int TimeInMsToCalculateNoiseLevel = 1000;
int TimeInMsToCalculateNoiseLevel = 10;
System.out.println("Start Calculating Noise Level");
CurrentNoiseLevel = CalculateNoiseLevel(TimeInMsToCalculateNoiseLevel);
System.out.println("Noise Level = " + CurrentNoiseLevel);
Expand Down Expand Up @@ -113,7 +113,7 @@ public void run() {

double magnitude = microphone.magnitude(120, 122);

int volume = microphone.getAudioVolume(checkVolumeSampleTime);
//int magnitude = microphone.getAudioVolume(checkVolumeSampleTime);
System.out.println(magnitude);
//boolean isSpeaking = (volume > minimumVolumeToStartrecording);
boolean isSpeaking = (magnitude > 200);
Expand Down
2 changes: 1 addition & 1 deletion src/com/company/microphone/MicrophoneAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public int getFrequency(byte[] bytes){

public double magnitude (int minFreq, int maxFreq) {

int numOfBytes = 4096;
int numOfBytes = 2048;

if(getTargetDataLine() == null){
return -1;
Expand Down

0 comments on commit 4cd95e7

Please sign in to comment.