Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor for Lex integration #42

Merged
merged 5 commits into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.darkprograms.speech.recognizer.vad;

import com.darkprograms.speech.microphone.Microphone;
import com.darkprograms.speech.microphone.MicrophoneAnalyzer;

import javax.sound.sampled.AudioInputStream;
Expand All @@ -25,6 +26,7 @@ public abstract class AbstractVAD implements VoiceActivityDetector, Runnable {
MicrophoneAnalyzer mic;
private VoiceActivityListener listener;
private VadState state;
private Thread thread;

private int maxSpeechMs;
private int maxSpeechWindows;
Expand All @@ -41,13 +43,46 @@ public void detectVoiceActivity(MicrophoneAnalyzer mic, VoiceActivityListener li
}

/** Initialise the VAD and start a thread */
@Override
public void detectVoiceActivity(MicrophoneAnalyzer mic, int maxSpeechMs, VoiceActivityListener listener) {
this.listener = listener;
this.mic = mic;
this.audio = mic.captureAudioToStream();
this.maxSpeechMs = maxSpeechMs;
maxSpeechWindows = maxSpeechMs / WINDOW_MILLIS;
new Thread(this, "JARVIS-VAD").start();

if (this.mic != null) {
if (this.mic == mic) {
// re-open the same mic
if (mic.getState() == Microphone.CaptureState.CLOSED) {
mic.open();
}
return;
} else {
// swap mics
this.audio = mic.captureAudioToStream();
this.mic.close();
}
} else {
this.audio = mic.captureAudioToStream();
}

this.mic = mic;
}

@Override
public void setVoiceActivityListener(VoiceActivityListener listener) {
this.listener = listener;
}

@Override
public void start() {
thread = new Thread(this, "JARVIS-VAD");
thread.start();
}

@Override
public void terminate() {
// state = VadState.CLOSED;
thread.interrupt();
}

/**
Expand All @@ -67,7 +102,7 @@ public void run() {

state = VoiceActivityDetector.VadState.LISTENING;

while (true) {
while (state != VadState.CLOSED) {
try {
int bytesRead = this.audio.read(audioData, 0, bytesToRead);
boolean speechDetected = sampleForSpeech(audioData);
Expand Down Expand Up @@ -139,5 +174,4 @@ protected AudioInputStream createVoiceActivityStream(ByteArrayOutputStream outBu
System.out.println("speech: " + mic.getAudioFormat().getFrameSize() * mic.getNumOfFrames(outBuffer.size()));
return new AudioInputStream(new ByteArrayInputStream(outBuffer.toByteArray()), audio.getFormat(), mic.getNumOfFrames(outBuffer.size()));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ enum VadState {
CLOSED
}

void start();
void terminate();

// TODO: optionally provide PipedInputStream to support streaming recognition on Google
void detectVoiceActivity(MicrophoneAnalyzer mic, VoiceActivityListener listener);
void detectVoiceActivity(MicrophoneAnalyzer mic, int maxSpeechMs, VoiceActivityListener listener);
void setVoiceActivityListener(VoiceActivityListener listener);
}
Loading