Skip to content
Open
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
1 change: 1 addition & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The J.A.R.V.I.S. Speech API is designed to be simple and efficient, using the sp
The API currently provides the following functionality,

* Microphone Capture API (Wrapped around the current Java API for simplicity)
* Voice Activity Detector
* A speech recognizer using Google's recognizer service
* Converts WAVE files from microphone input to FLAC (using existing API, see CREDITS)
* Retrieves Response from Google, including confidence score and text
Expand Down
13 changes: 12 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@

<groupId>com.darkprograms.speech</groupId>
<artifactId>java-speech-api</artifactId>
<version>1.13.0-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<name>java-speech-api</name>
Expand Down Expand Up @@ -69,6 +71,10 @@
<id>https://github.com/AranHase</id>
<name>AranHase</name>
</developer>
<developer>
<id>https://github.com/nalbion</id>
<name>nalbion</name>
</developer>
</developers>

<dependencies>
Expand All @@ -82,6 +88,11 @@
<artifactId>json</artifactId>
<version>20150729</version>
</dependency>
<dependency>
<groupId>com.amazonaws</groupId>
<artifactId>aws-java-sdk-lex</artifactId>
<version>1.11.160</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.darkprograms.speech.recognizer;
package com.darkprograms.speech.encoding;

import net.sourceforge.javaflacencoder.FLACEncoder;
import net.sourceforge.javaflacencoder.FLACFileOutputStream;
Expand Down Expand Up @@ -32,8 +32,6 @@ public FlacEncoder() {
* @param outputFile Output FLAC file
*/
public void convertWaveToFlac(File inputFile, File outputFile) {


StreamConfiguration streamConfiguration = new StreamConfiguration();
streamConfiguration.setSampleRate(8000);
streamConfiguration.setBitsPerSample(16);
Expand Down
90 changes: 79 additions & 11 deletions src/main/java/com/darkprograms/speech/microphone/Microphone.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public enum CaptureState {
*/
private File audioFile;

private AudioInputStream audioStream;
private float sampleRate;

/**
* Constructor
*
Expand All @@ -52,6 +55,15 @@ public Microphone(AudioFileFormat.Type fileType) {
initTargetDataLine();
}

/**
* Constructor for use with {@link #captureAudioToStream()}
* @param sampleRate samples per second - 16_000 (recommended) or 8_000
*/
public Microphone(float sampleRate) {
setState(CaptureState.CLOSED);
initTargetDataLine(sampleRate);
}

/**
* Gets the current state of Microphone
*
Expand Down Expand Up @@ -100,18 +112,33 @@ public void setTargetDataLine(TargetDataLine targetDataLine) {
/**
* Initializes the target data line.
*/
private void initTargetDataLine(){
private TargetDataLine initTargetDataLine() {
return initTargetDataLine(8_000F);
}
private TargetDataLine initTargetDataLine(float sampleRate) {
this.sampleRate = sampleRate;
DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat());
try {
setTargetDataLine((TargetDataLine) AudioSystem.getLine(dataLineInfo));
TargetDataLine targetDataLine = (TargetDataLine)AudioSystem.getLine(dataLineInfo);
setTargetDataLine(targetDataLine);
return targetDataLine;
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
return null;
}

}

public AudioInputStream captureAudioToStream() {
setState(CaptureState.STARTING_CAPTURE);
if(getTargetDataLine() == null) {
initTargetDataLine();
}

open();
audioStream = new AudioInputStream(getTargetDataLine());
return audioStream;
}

/**
* Captures audio from the microphone and saves it a file
Expand All @@ -129,8 +156,6 @@ public void captureAudioToFile(File audioFile) throws LineUnavailableException {

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


}

/**
Expand All @@ -144,14 +169,19 @@ public void captureAudioToFile(String audioFile) throws LineUnavailableException
captureAudioToFile(file);
}


/**
* The audio format to save in
*
* @return Returns AudioFormat to be used later when capturing audio from microphone
*/
public AudioFormat getAudioFormat() {
float sampleRate = 8000.0F;
return getAudioFormat(sampleRate);
}

/**
* @param sampleRate set to 16_000.0F for AWS Lex
*/
public AudioFormat getAudioFormat(float sampleRate) {
//8000,11025,16000,22050,44100
int sampleSizeInBits = 16;
//8,16
Expand All @@ -172,18 +202,47 @@ public void open(){
if(getTargetDataLine()==null){
initTargetDataLine();
}
if(!getTargetDataLine().isOpen() && !getTargetDataLine().isRunning() && !getTargetDataLine().isActive()){
TargetDataLine targetDataLine = getTargetDataLine();
if(!targetDataLine.isOpen() && !targetDataLine.isRunning() && !targetDataLine.isActive()) {
try {
setState(CaptureState.PROCESSING_AUDIO);

try {
System.out.println("???????????????????????????????????????????????????????????");
System.out.println("???????????????????????????????????????????????????????????");System.out.println("???????????????????????????????????????????????????????????");System.out.println("???????????????????????????????????????????????????????????");
System.out.println("???????????????????????????????????????????????????????????");


if (targetDataLine.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Setting gain!!!!!!!!!!!!!!");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Setting gain!!!!!!!!!!!!!!");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Setting gain!!!!!!!!!!!!!!");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Setting gain!!!!!!!!!!!!!!");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Setting gain!!!!!!!!!!!!!!");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Setting gain!!!!!!!!!!!!!!");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Setting gain!!!!!!!!!!!!!!");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Setting gain!!!!!!!!!!!!!!");
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Setting gain!!!!!!!!!!!!!!");
FloatControl gainControl = (FloatControl) getTargetDataLine().getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(40);
}
} catch (Exception e) {
try {
FloatControl gainControl = (FloatControl) getTargetDataLine().getControl(FloatControl.Type.VOLUME);
gainControl.setValue(-10);
} catch (Exception e1) {
e1.printStackTrace();
}
}
getTargetDataLine().open(getAudioFormat());

getTargetDataLine().start();
} catch (LineUnavailableException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return;
}
}

}

/**
Expand All @@ -203,7 +262,6 @@ public void close() {
* Thread to capture the audio from the microphone and save it to a file
*/
private class CaptureThread implements Runnable {

/**
* Run method for thread
*/
Expand All @@ -220,4 +278,14 @@ public void run() {
}
}

/*private class ListenThread implements Runnable {
public void run() {
try {
open();
audioStream = new AudioInputStream(getTargetDataLine());
} catch (Exception ex) {
ex.printStackTrace();
}
}
}*/
}
Loading