Skip to content

Commit

Permalink
Добавлено тестирование из файлов
Browse files Browse the repository at this point in the history
  • Loading branch information
3XclusiVe committed Feb 9, 2017
1 parent 030ded7 commit 70d0469
Show file tree
Hide file tree
Showing 97 changed files with 215 additions and 31 deletions.
1 change: 1 addition & 0 deletions .classpath
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<classpathentry exported="true" kind="lib" path="lib/jcommon-1.0.23.jar"/>
<classpathentry exported="true" kind="lib" path="lib/jfreechart-1.0.19.jar"/>
<classpathentry exported="true" kind="lib" path="lib/JTransforms-3.1-with-dependencies.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/jflac-1.3"/>
<classpathentry kind="output" path="out/production/test_google_speach_api"/>
</classpath>
Binary file removed all_records/Converted/10.flac
Binary file not shown.
Binary file added all_records/Converted/10.wav
Binary file not shown.
Binary file removed all_records/Converted/11.flac
Binary file not shown.
Binary file added all_records/Converted/11.wav
Binary file not shown.
Binary file removed all_records/Converted/12.flac
Binary file not shown.
Binary file added all_records/Converted/12.wav
Binary file not shown.
Binary file removed all_records/Converted/13.flac
Binary file not shown.
Binary file added all_records/Converted/13.wav
Binary file not shown.
Binary file removed all_records/Converted/14.flac
Binary file not shown.
Binary file added all_records/Converted/14.wav
Binary file not shown.
Binary file removed all_records/Converted/15.flac
Binary file not shown.
Binary file added all_records/Converted/15.wav
Binary file not shown.
Binary file removed all_records/Converted/16.flac
Binary file not shown.
Binary file added all_records/Converted/16.wav
Binary file not shown.
Binary file removed all_records/Converted/17.flac
Binary file not shown.
Binary file added all_records/Converted/17.wav
Binary file not shown.
Binary file removed all_records/Converted/18.flac
Binary file not shown.
Binary file added all_records/Converted/18.wav
Binary file not shown.
Binary file removed all_records/Converted/19.flac
Binary file not shown.
Binary file added all_records/Converted/19.wav
Binary file not shown.
Binary file removed all_records/Converted/20.flac
Binary file not shown.
Binary file added all_records/Converted/20.wav
Binary file not shown.
Binary file removed all_records/Converted/21.flac
Binary file not shown.
Binary file added all_records/Converted/21.wav
Binary file not shown.
Binary file removed all_records/Converted/22.flac
Binary file not shown.
Binary file added all_records/Converted/22.wav
Binary file not shown.
Binary file removed all_records/Converted/23.flac
Binary file not shown.
Binary file added all_records/Converted/23.wav
Binary file not shown.
Binary file removed all_records/Converted/24.flac
Binary file not shown.
Binary file added all_records/Converted/24.wav
Binary file not shown.
Binary file removed all_records/Converted/25.flac
Binary file not shown.
Binary file added all_records/Converted/25.wav
Binary file not shown.
Binary file removed all_records/Converted/26.flac
Binary file not shown.
Binary file added all_records/Converted/26.wav
Binary file not shown.
Binary file removed all_records/Converted/27.flac
Binary file not shown.
Binary file added all_records/Converted/27.wav
Binary file not shown.
Binary file removed all_records/Converted/28.flac
Binary file not shown.
Binary file added all_records/Converted/28.wav
Binary file not shown.
Binary file removed all_records/Converted/29.flac
Binary file not shown.
Binary file added all_records/Converted/29.wav
Binary file not shown.
Binary file removed all_records/Converted/3.flac
Binary file not shown.
Binary file added all_records/Converted/3.wav
Binary file not shown.
Binary file removed all_records/Converted/30.flac
Binary file not shown.
Binary file added all_records/Converted/30.wav
Binary file not shown.
Binary file removed all_records/Converted/31.flac
Binary file not shown.
Binary file added all_records/Converted/31.wav
Binary file not shown.
Binary file removed all_records/Converted/32.flac
Binary file not shown.
Binary file added all_records/Converted/32.wav
Binary file not shown.
Binary file removed all_records/Converted/33.flac
Binary file not shown.
Binary file added all_records/Converted/33.wav
Binary file not shown.
Binary file removed all_records/Converted/34.flac
Binary file not shown.
Binary file added all_records/Converted/34.wav
Binary file not shown.
Binary file removed all_records/Converted/35.flac
Binary file not shown.
Binary file added all_records/Converted/35.wav
Binary file not shown.
Binary file removed all_records/Converted/36.flac
Binary file not shown.
Binary file added all_records/Converted/36.wav
Binary file not shown.
Binary file removed all_records/Converted/37.flac
Binary file not shown.
Binary file added all_records/Converted/37.wav
Binary file not shown.
Binary file removed all_records/Converted/38.flac
Binary file not shown.
Binary file added all_records/Converted/38.wav
Binary file not shown.
Binary file removed all_records/Converted/39.flac
Binary file not shown.
Binary file added all_records/Converted/39.wav
Binary file not shown.
Binary file removed all_records/Converted/4.flac
Binary file not shown.
Binary file added all_records/Converted/4.wav
Binary file not shown.
Binary file removed all_records/Converted/40.flac
Binary file not shown.
Binary file added all_records/Converted/40.wav
Binary file not shown.
Binary file removed all_records/Converted/41.flac
Binary file not shown.
Binary file added all_records/Converted/41.wav
Binary file not shown.
Binary file removed all_records/Converted/42.flac
Binary file not shown.
Binary file added all_records/Converted/42.wav
Binary file not shown.
Binary file removed all_records/Converted/43.flac
Binary file not shown.
Binary file removed all_records/Converted/44.flac
Binary file not shown.
Binary file removed all_records/Converted/45.flac
Binary file not shown.
Binary file removed all_records/Converted/46.flac
Binary file not shown.
Binary file removed all_records/Converted/47.flac
Binary file not shown.
Binary file removed all_records/Converted/48.flac
Binary file not shown.
Binary file removed all_records/Converted/49.flac
Binary file not shown.
Binary file removed all_records/Converted/5.flac
Binary file not shown.
Binary file added all_records/Converted/5.wav
Binary file not shown.
Binary file removed all_records/Converted/50.flac
Binary file not shown.
Binary file removed all_records/Converted/51.flac
Binary file not shown.
Binary file removed all_records/Converted/52.flac
Binary file not shown.
Binary file removed all_records/Converted/6.flac
Binary file not shown.
Binary file added all_records/Converted/6.wav
Binary file not shown.
Binary file removed all_records/Converted/7.flac
Binary file not shown.
Binary file added all_records/Converted/7.wav
Binary file not shown.
Binary file removed all_records/Converted/8.flac
Binary file not shown.
Binary file added all_records/Converted/8.wav
Binary file not shown.
Binary file removed all_records/Converted/9.flac
Binary file not shown.
Binary file added all_records/Converted/9.wav
Binary file not shown.
Binary file added lib/jflac-1.3.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions src/com/company/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
public class Main {

public static void main (String[] args) throws LineUnavailableException, IOException, UnsupportedAudioFileException {
//RecordingThread recordingThread = new RecordingThread();
//File AudioStream = new File("/Users/user/IdeaProjects/test_google_speach_api/all_records/Converted/4.wav");
RecordingThread recordingThread = new RecordingThread();
CommandsRecognizer commandsRecognizer = new CommandsRecognizer();

Expand Down
130 changes: 99 additions & 31 deletions src/com/company/RecordingThread/RecordingThread.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public class RecordingThread extends Thread {

private List<ResponseListener> listeners;

private File AudioFileStream;

public RecordingThread() {

microphone = new MicrophoneAnalyzer(FLACFileWriter.FLAC);
Expand All @@ -47,6 +49,17 @@ public RecordingThread() {

}

public RecordingThread(File AudioFile) {

microphone = new MicrophoneAnalyzer(FLACFileWriter.FLAC);

recognizer = new Recognizer(Recognizer.Languages.RUSSIAN, apiKey);
listeners = new ArrayList<ResponseListener>();

this.AudioFileStream = AudioFile;

}

private double CalculateNoiseLevel(int TimeInMsToCalculateNoiseLevel) {

tempAudioFile = new File("DisplayAllReponce.flac");
Expand Down Expand Up @@ -94,56 +107,111 @@ public void addResponceListener(ResponseListener listener) {
public void run() {

int curSample = 0;
if(this.AudioFileStream == null) {

while (true) {
microphone.open();
while (true) {
microphone.open();

try {
tempAudioFile = new File("DisplayAllReponce.flac");
microphone.setAudioFile(tempAudioFile);
microphone.captureAudioToFile(microphone.getAudioFile());

try {
tempAudioFile = new File("DisplayAllReponce.flac");
microphone.setAudioFile(tempAudioFile);
microphone.captureAudioToFile(microphone.getAudioFile());

Thread.sleep(checkVolumeSampleTime * 3);
Thread.sleep(checkVolumeSampleTime * 3);

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

//int magnitude = microphone.getAudioVolume(checkVolumeSampleTime);
System.out.println(magnitude);
//boolean isSpeaking = (volume > minimumVolumeToStartrecording);
boolean isSpeaking = (magnitude > 200);
//int magnitude = microphone.getAudioVolume(checkVolumeSampleTime);
System.out.println(magnitude);
//boolean isSpeaking = (volume > minimumVolumeToStartrecording);
boolean isSpeaking = (magnitude > 200);

if (isSpeaking) {
if (isSpeaking) {

DebugLog("Start RECORDING...");
DebugLog("Start RECORDING...");

do {
DebugLog("RECORDING proc...");
Thread.sleep(sampleTime);//Updates every second
} while (microphone.magnitude(120, 122) > 100);
do {
DebugLog("RECORDING proc...");
Thread.sleep(sampleTime);//Updates every second
} while (microphone.magnitude(120, 122) > 100);


DebugLog("Recording Complete!");
microphone.close();
DebugLog("Recording Complete!");
microphone.close();

DebugLog("Recognizing...");
DebugLog("Recognizing...");

GoogleResponse response = recognizer.getRecognizedDataForFlac(microphone.getAudioFile(), 1);
notifyListeners(response);
GoogleResponse response = recognizer.getRecognizedDataForFlac(microphone.getAudioFile(), 1);
notifyListeners(response);

DebugLog("Looping back");//Restarts loops
DebugLog("Looping back");//Restarts loops


}
microphone.getAudioFile().delete();


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Error Occured");
} finally {
microphone.close();//Makes sure microphone closes on exit.
}
microphone.getAudioFile().delete();
}
} else {

while (true) {
microphone.open();

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Error Occured");
} finally {
microphone.close();//Makes sure microphone closes on exit.
try {
tempAudioFile = new File("DisplayAllReponce.flac");
microphone.setAudioFile(tempAudioFile);
microphone.captureAudioFromFileToFile(microphone.getAudioFile(), AudioFileStream);

Thread.sleep(checkVolumeSampleTime * 3);

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

//int magnitude = microphone.getAudioVolume(checkVolumeSampleTime);
System.out.println(magnitude);
//boolean isSpeaking = (volume > minimumVolumeToStartrecording);
boolean isSpeaking = (magnitude > 200);

if (isSpeaking) {

DebugLog("Start RECORDING...");

do {
DebugLog("RECORDING proc...");
Thread.sleep(sampleTime);//Updates every second
} while (microphone.magnitude(120, 122) > 100);

DebugLog("Recording Complete!");
microphone.close();

DebugLog("Recognizing...");

GoogleResponse response = recognizer.getRecognizedDataForFlac(microphone.getAudioFile(), 1);
notifyListeners(response);

DebugLog("Looping back");//Restarts loops


}
microphone.getAudioFile().delete();


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("Error Occured");
} finally {
microphone.close();//Makes sure microphone closes on exit.
}
}

}

}
Expand Down
51 changes: 51 additions & 0 deletions src/com/company/microphone/Microphone.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.company.microphone;

import org.kc7bfi.jflac.sound.spi.FlacAudioFileReader;

import javax.sound.sampled.*;

import java.io.Closeable;
Expand Down Expand Up @@ -132,6 +134,29 @@ public void captureAudioToFile(File audioFile) throws LineUnavailableException {
new Thread(new CaptureThread()).start();


}

protected File audioFileStream;
/**
* Captures audio from the microphone and saves it a file
*
* @param audioFile The File to save the audio to
* @throws LineUnavailableException
*/
public void captureAudioFromFileToFile(File audioFile, File audioFileStream) throws LineUnavailableException {

setState(CaptureState.STARTING_CAPTURE);
setAudioFile(audioFile);
this.audioFileStream = audioFileStream;

if(getTargetDataLine() == null){
initTargetDataLine();
}

//Get Audio
new Thread(new CaptureFromFileThread()).start();


}

/**
Expand Down Expand Up @@ -209,6 +234,7 @@ private class CaptureThread implements Runnable {
* Run method for thread
*/
public void run() {

try {
AudioFileFormat.Type fileType = getFileType();
File audioFile = getAudioFile();
Expand All @@ -221,4 +247,29 @@ public void run() {
}
}

/**
* Thread to capture the audio from the microphone and save it to a file
*/
private class CaptureFromFileThread implements Runnable {

/**
* Run method for thread
*/
public void run() {

try {
AudioFileFormat.Type fileType = getFileType();
File audioFile = getAudioFile();
File audioStream = audioFileStream;
open();

AudioInputStream inputStream = AudioSystem.getAudioInputStream(audioFileStream);
AudioSystem.write(inputStream, fileType, audioFile);
//Will write to File until it's closed.
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

}
59 changes: 59 additions & 0 deletions src/com/company/microphone/MicrophoneAnalyzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.darkprograms.speech.util.Complex;
import com.darkprograms.speech.util.FFT;
import org.kc7bfi.jflac.sound.spi.FlacAudioFileReader;

import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;


/********************************************************************************************
Expand Down Expand Up @@ -208,6 +211,62 @@ public double magnitude (int minFreq, int maxFreq) {

}

static int numOfBytesTotalOffset = 0;

public double magnitudeInFile (int minFreq, int maxFreq) throws IOException, UnsupportedAudioFileException {

int numOfBytes = 2048;

numOfBytesTotalOffset += numOfBytes;
FlacAudioFileReader flacAudioFileReader = new FlacAudioFileReader();
AudioInputStream stream = flacAudioFileReader.getAudioInputStream(this.getAudioFile());

byte[] data = new byte[numOfBytes+1];//One byte is lost during conversion
try {
stream.read(data, 0, numOfBytes);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


double[] audioData = this.bytesToDoubleArray(data);
audioData = applyHanningWindow(audioData);
Complex[] complex = new Complex[audioData.length];
for(int i = 0; i<complex.length; i++){
complex[i] = new Complex(audioData[i], 0);
}
Complex[] fftData = FFT.fft(complex);


int N = 3;

if(N<=0 || fftData == null){ return -1; } //error case

final int LENGTH = fftData.length;//Used to calculate bin size
fftData = removeNegativeFrequencies(fftData);

Complex[][] comdata = new Complex[N][fftData.length/N];
for(int i = 0; i<N; i++){
for(int j = 0; j<comdata[0].length; j++){
comdata[i][j] = fftData[j*(i+1)];
}
}

Complex[] result = new Complex[fftData.length/N];//Combines the arrays
for(int i = 0; i<result.length; i++){
Complex tmp = new Complex(1,0);
for(int j = 0; j<N; j++){
tmp = tmp.times(comdata[j][i]);
}
result[i] = tmp;
}


return findMeanBetween(minFreq, maxFreq, fftData);

}

private double findMeanBetween(int minFreq, int maxFreq, Complex[] result) {

float sampleRate = getAudioFormat().getSampleRate();
Expand Down
3 changes: 3 additions & 0 deletions test_google_speach_api.eml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
<lib name="JTransforms-3.1-with-dependencies.jar" scope="COMPILE">
<relative-module-cls project-related="jar://$PROJECT_DIR$/lib/JTransforms-3.1-with-dependencies.jar!/"/>
</lib>
<levels>
<level name="jflac-1.3" value="project"/>
</levels>
</component>

0 comments on commit 70d0469

Please sign in to comment.