Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/ClusterM/java-speech-api.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTheShadow <admin@dark-programs.com>2012-01-09 04:38:58 +0400
committerTheShadow <admin@dark-programs.com>2012-01-09 04:38:58 +0400
commit0a8d626f7a8a9d8bb80571ac6b1faf404c3476ca (patch)
tree4d67c7aed5559dd28348b99ba26e84363ea2c47c
Initial commit of code
-rw-r--r--java-speech-api.iml13
-rw-r--r--src/META-INF/MANIFEST.MF2
-rw-r--r--src/com/darkprograms/speech/microphone/Microphone.java202
-rw-r--r--src/com/darkprograms/speech/recognizer/FlacEncoder.java98
-rw-r--r--src/com/darkprograms/speech/recognizer/GoogleResponse.java64
-rw-r--r--src/com/darkprograms/speech/recognizer/Recognizer.java169
-rw-r--r--src/com/darkprograms/speech/synthesiser/Synthesiser.java48
7 files changed, 596 insertions, 0 deletions
diff --git a/java-speech-api.iml b/java-speech-api.iml
new file mode 100644
index 0000000..3f65ddf
--- /dev/null
+++ b/java-speech-api.iml
@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module type="JAVA_MODULE" version="4">
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
+ <exclude-output />
+ <content url="file://$MODULE_DIR$">
+ <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
+ </content>
+ <orderEntry type="jdk" jdkName="1.6.0_30" jdkType="JavaSDK" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ <orderEntry type="library" name="JavaFlacEncoder" level="project" />
+ </component>
+</module>
+
diff --git a/src/META-INF/MANIFEST.MF b/src/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..59499bc
--- /dev/null
+++ b/src/META-INF/MANIFEST.MF
@@ -0,0 +1,2 @@
+Manifest-Version: 1.0
+
diff --git a/src/com/darkprograms/speech/microphone/Microphone.java b/src/com/darkprograms/speech/microphone/Microphone.java
new file mode 100644
index 0000000..0093e3e
--- /dev/null
+++ b/src/com/darkprograms/speech/microphone/Microphone.java
@@ -0,0 +1,202 @@
+package com.darkprograms.speech.microphone;
+
+import javax.sound.sampled.*;
+import java.io.File;
+
+/**
+ * Microphone class that contains methods to capture audio from microphone
+ *
+ * @author Luke Kuza
+ */
+public class Microphone {
+
+ /**
+ * TargetDataLine variable to receive data from microphone
+ */
+ private TargetDataLine targetDataLine;
+
+ /**
+ * Enum for current Microphone state
+ */
+ private enum CaptureState {
+ PROCESSING_AUDIO, STARTING_CAPTURE, CLOSED
+ }
+
+ /**
+ * Variable for enum
+ */
+ CaptureState state;
+
+ /**
+ * Variable for the audios saved file type
+ */
+ private AudioFileFormat.Type fileType;
+
+ /**
+ * Variable that holds the saved audio file
+ */
+ private File audioFile;
+
+ /**
+ * Gets the current state of Microphone
+ *
+ * @return PROCESSING_AUDIO is returned when the Thread is recording Audio and/or saving it to a file<br>
+ * STARTING_CAPTURE is returned if the Thread is setting variables<br>
+ * CLOSED is returned if the Thread is not doing anything/not capturing audio
+ */
+ public String getState() {
+ switch (state) {
+ case PROCESSING_AUDIO:
+ return "PROCESSING_AUDIO";
+ case STARTING_CAPTURE:
+ return "STARTING_CAPTURE";
+ case CLOSED:
+ return "CLOSED";
+
+ default:
+ return "CLOSED";
+ }
+ }
+
+ /**
+ * Sets the current state of Microphone
+ *
+ * @param state State from enum
+ */
+ private void setState(CaptureState state) {
+ this.state = state;
+ }
+
+ public File getAudioFile() {
+ return audioFile;
+ }
+
+ public void setAudioFile(File audioFile) {
+ this.audioFile = audioFile;
+ }
+
+ public AudioFileFormat.Type getFileType() {
+ return fileType;
+ }
+
+ public void setFileType(AudioFileFormat.Type fileType) {
+ this.fileType = fileType;
+ }
+
+ public TargetDataLine getTargetDataLine() {
+ return targetDataLine;
+ }
+
+ public void setTargetDataLine(TargetDataLine targetDataLine) {
+ this.targetDataLine = targetDataLine;
+ }
+
+
+ /**
+ * Constructor
+ *
+ * @param fileType File type to save the audio in<br>
+ * Example, to save as WAVE use AudioFileFormat.Type.WAVE
+ */
+ public Microphone(AudioFileFormat.Type fileType) {
+ setState(CaptureState.CLOSED);
+ setFileType(fileType);
+ }
+
+
+ /**
+ * Captures audio from the microphone and saves it a file
+ *
+ * @param audioFile The File to save the audio to
+ * @throws Exception Throws an exception if something went wrong
+ */
+ public void captureAudioToFile(File audioFile) throws Exception {
+ setAudioFile(audioFile);
+ setState(CaptureState.STARTING_CAPTURE);
+
+ DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat());
+ setTargetDataLine((TargetDataLine) AudioSystem.getLine(dataLineInfo));
+
+
+ //Get Audio
+ new Thread(new CaptureThread()).start();
+
+
+ }
+
+ /**
+ * Captures audio from the microphone and saves it a file
+ *
+ * @param audioFile The fully path (String) to a file you want to save the audio in
+ * @throws Exception Throws an exception if something went wrong
+ */
+ public void captureAudioToFile(String audioFile) throws Exception {
+ File file = new File(audioFile);
+ setAudioFile(file);
+ setState(CaptureState.STARTING_CAPTURE);
+
+ DataLine.Info dataLineInfo = new DataLine.Info(TargetDataLine.class, getAudioFormat());
+ setTargetDataLine((TargetDataLine) AudioSystem.getLine(dataLineInfo));
+
+
+ //Get Audio
+ new Thread(new CaptureThread()).start();
+
+
+ }
+
+ /**
+ * The audio format to save in
+ *
+ * @return Returns AudioFormat to be used later when capturing audio from microphone
+ */
+ private AudioFormat getAudioFormat() {
+ float sampleRate = 8000.0F;
+ //8000,11025,16000,22050,44100
+ int sampleSizeInBits = 16;
+ //8,16
+ int channels = 1;
+ //1,2
+ boolean signed = true;
+ //true,false
+ boolean bigEndian = false;
+ //true,false
+ return new AudioFormat(sampleRate, sampleSizeInBits, channels, signed, bigEndian);
+ }
+
+ /**
+ * Close the microphone capture, saving all processed audio to the specified file.<br>
+ * If already closed, this does nothing
+ */
+ public void close() {
+ if (getState().equals("CLOSED")) {
+ } else {
+ setState(CaptureState.CLOSED);
+ getTargetDataLine().stop();
+ getTargetDataLine().close();
+ }
+ }
+
+ /**
+ * Thread to capture the audio from the microphone and save it to a file
+ */
+ private class CaptureThread implements Runnable {
+
+ /**
+ * Run method for thread
+ */
+ public void run() {
+ try {
+ state = CaptureState.PROCESSING_AUDIO;
+ AudioFileFormat.Type fileType = getFileType();
+ File audioFile = getAudioFile();
+ getTargetDataLine().open(getAudioFormat());
+ getTargetDataLine().start();
+ AudioSystem.write(new AudioInputStream(getTargetDataLine()), fileType, audioFile);
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+ }
+
+}
diff --git a/src/com/darkprograms/speech/recognizer/FlacEncoder.java b/src/com/darkprograms/speech/recognizer/FlacEncoder.java
new file mode 100644
index 0000000..4343cea
--- /dev/null
+++ b/src/com/darkprograms/speech/recognizer/FlacEncoder.java
@@ -0,0 +1,98 @@
+package com.darkprograms.speech.recognizer;
+
+import javaFlacEncoder.FLACEncoder;
+import javaFlacEncoder.FLACFileOutputStream;
+import javaFlacEncoder.StreamConfiguration;
+
+import javax.sound.sampled.AudioFormat;
+import javax.sound.sampled.AudioInputStream;
+import javax.sound.sampled.AudioSystem;
+import java.io.File;
+import java.nio.ByteBuffer;
+import java.nio.ByteOrder;
+
+/**
+ * Class that contains methods to encode Wave files to FLAC files
+ * THIS IS THANKS TO THE javaFlacEncoder Project created here: http://sourceforge.net/projects/javaflacencoder/
+ */
+public class FlacEncoder {
+
+ /**
+ * Constructor
+ */
+ public FlacEncoder() {
+
+ }
+
+ /**
+ * Converts a wave file to a FLAC file(in order to POST the data to Google and retrieve a response) <br>
+ * Sample Rate is 8000 by default
+ *
+ * @param inputFile Input wave file
+ * @param outputFile Output FLAC file
+ */
+ public void convertWaveToFlac(File inputFile, File outputFile) {
+
+
+ StreamConfiguration streamConfiguration = new StreamConfiguration();
+ streamConfiguration.setSampleRate(8000);
+ streamConfiguration.setBitsPerSample(16);
+ streamConfiguration.setChannelCount(1);
+
+
+ try {
+ AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputFile);
+ AudioFormat format = audioInputStream.getFormat();
+
+ int frameSize = format.getFrameSize();
+
+ FLACEncoder flacEncoder = new FLACEncoder();
+ FLACFileOutputStream flacOutputStream = new FLACFileOutputStream(outputFile);
+
+ flacEncoder.setStreamConfiguration(streamConfiguration);
+ flacEncoder.setOutputStream(flacOutputStream);
+
+ flacEncoder.openFLACStream();
+
+ int[] sampleData = new int[(int) audioInputStream.getFrameLength()];
+ byte[] samplesIn = new byte[frameSize];
+
+ int i = 0;
+
+ while (audioInputStream.read(samplesIn, 0, frameSize) != -1) {
+
+ ByteBuffer bb = ByteBuffer.wrap(samplesIn);
+ bb.order(ByteOrder.LITTLE_ENDIAN);
+ short shortVal = bb.getShort();
+
+ sampleData[i] = shortVal;
+
+ i++;
+ }
+
+ flacEncoder.addSamples(sampleData, i);
+ flacEncoder.encodeSamples(i, false);
+ flacEncoder.encodeSamples(flacEncoder.samplesAvailableToEncode(), true);
+
+ audioInputStream.close();
+ flacOutputStream.close();
+
+ } catch (Exception ex) {
+ ex.printStackTrace();
+ }
+ }
+
+
+ /**
+ * Converts a wave file to a FLAC file(in order to POST the data to Google and retrieve a response) <br>
+ * Sample Rate is 8000 by default
+ *
+ * @param inputFile Input wave file
+ * @param outputFile Output FLAC file
+ */
+ public void convertWaveToFlac(String inputFile, String outputFile) {
+ convertWaveToFlac(new File(inputFile), new File(outputFile));
+ }
+
+
+}
diff --git a/src/com/darkprograms/speech/recognizer/GoogleResponse.java b/src/com/darkprograms/speech/recognizer/GoogleResponse.java
new file mode 100644
index 0000000..0f03c87
--- /dev/null
+++ b/src/com/darkprograms/speech/recognizer/GoogleResponse.java
@@ -0,0 +1,64 @@
+package com.darkprograms.speech.recognizer;
+
+/**
+ * Class that holds the response and confidence of a Google recognizer request
+ *
+ * @author Luke Kuza
+ */
+public class GoogleResponse {
+
+ /**
+ * Variable that holds the response
+ */
+ private String response;
+ /**
+ * Variable that holds the confidence score
+ */
+ private String confidence;
+
+ /**
+ * Constructor
+ */
+ public GoogleResponse() {
+
+ }
+
+
+ /**
+ * Gets the response text of what was said in the submitted Audio to Google
+ *
+ * @return String representation of what was said
+ */
+ public String getResponse() {
+ return response;
+ }
+
+ /**
+ * Set the response
+ *
+ * @param response The response
+ */
+ protected void setResponse(String response) {
+ this.response = response;
+ }
+
+ /**
+ * Gets the confidence score for the specific request
+ *
+ * @return The confidence score, ex .922343324323
+ */
+ public String getConfidence() {
+ return confidence;
+ }
+
+ /**
+ * Set the confidence score for this request
+ *
+ * @param confidence The confidence score
+ */
+ protected void setConfidence(String confidence) {
+ this.confidence = confidence;
+ }
+
+
+}
diff --git a/src/com/darkprograms/speech/recognizer/Recognizer.java b/src/com/darkprograms/speech/recognizer/Recognizer.java
new file mode 100644
index 0000000..ea41c6a
--- /dev/null
+++ b/src/com/darkprograms/speech/recognizer/Recognizer.java
@@ -0,0 +1,169 @@
+package com.darkprograms.speech.recognizer;
+
+import java.io.*;
+import java.net.URL;
+import java.net.URLConnection;
+
+/**
+ * Class that submits FLAC audio and retrieves recognized text
+ */
+public class Recognizer {
+
+ /**
+ * URL to POST audio data and retrieve results
+ */
+ private static final String GOOGLE_RECOGNIZER_URL = "https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&lang=en-US";
+
+ /**
+ * Constructor
+ */
+ public Recognizer() {
+
+ }
+
+ /**
+ * Get recognized data from a Wave file. This method will encode the wave file to a FLAC
+ *
+ * @param waveFile Wave file to recognize
+ * @return Returns a GoogleResponse, with the response and confidence score
+ * @throws Exception Throws exception if something goes wrong
+ */
+ public GoogleResponse getRecognizedDataForWave(File waveFile) throws Exception {
+ FlacEncoder flacEncoder = new FlacEncoder();
+ File flacFile = new File(waveFile + ".flac");
+
+ flacEncoder.convertWaveToFlac(waveFile, flacFile);
+
+ String response = rawRequest(flacFile);
+
+ //Delete converted FLAC data
+ flacFile.delete();
+
+ String[] parsedResponse = parseResponse(response);
+
+
+ GoogleResponse googleResponse = new GoogleResponse();
+
+
+ googleResponse.setResponse(parsedResponse[0]);
+ googleResponse.setConfidence(parsedResponse[1]);
+
+ return googleResponse;
+ }
+
+ /**
+ * Get recognized data from a Wave file. This method will encode the wave file to a FLAC
+ *
+ * @param waveFile Wave file to recognize
+ * @return Returns a GoogleResponse, with the response and confidence score
+ * @throws Exception Throws exception if something goes wrong
+ */
+ public GoogleResponse getRecognizedDataForWave(String waveFile) throws Exception {
+ return getRecognizedDataForWave(new File(waveFile));
+ }
+
+ /**
+ * Get recognized data from a FLAC file.
+ *
+ * @param flacFile FLAC file to recognize
+ * @return Returns a GoogleResponse, with the response and confidence score
+ * @throws Exception Throws exception if something goes wrong
+ */
+ public GoogleResponse getRecognizedDataForFlac(File flacFile) throws Exception {
+ String response = rawRequest(flacFile);
+ String[] parsedResponse = parseResponse(response);
+
+ GoogleResponse googleResponse = new GoogleResponse();
+
+
+ googleResponse.setResponse(parsedResponse[0]);
+ googleResponse.setConfidence(parsedResponse[1]);
+
+ return googleResponse;
+ }
+
+ /**
+ * Get recognized data from a FLAC file.
+ *
+ * @param flacFile FLAC file to recognize
+ * @return Returns a GoogleResponse, with the response and confidence score
+ * @throws Exception Throws exception if something goes wrong
+ */
+ public GoogleResponse getRecognizedDataForFlac(String flacFile) throws Exception {
+ return getRecognizedDataForFlac(new File(flacFile));
+ }
+
+ /**
+ * Parses the raw response from Google
+ *
+ * @param rawResponse The raw, unparsed response from Google
+ * @return Returns the parsed response. Index 0 is response, Index 1 is confidence score
+ */
+ private String[] parseResponse(String rawResponse) {
+ String[] parsedResponse = new String[2];
+
+ String[] strings = rawResponse.split(":");
+
+ parsedResponse[0] = strings[4].split("\"")[1];
+ parsedResponse[1] = strings[5].replace("}]}", "");
+
+ return parsedResponse;
+ }
+
+ /**
+ * Performs the request to Google with a file <br>
+ * Request is buffered
+ *
+ * @param inputFile Input files to recognize
+ * @return Returns the raw, unparsed response from Google
+ * @throws Exception Throws exception if something went wrong
+ */
+ private String rawRequest(File inputFile) throws Exception {
+ URL url;
+ URLConnection urlConn;
+ OutputStream outputStream;
+ BufferedReader br;
+
+ // URL of Remote Script.
+ url = new URL(GOOGLE_RECOGNIZER_URL);
+
+ // Open New URL connection channel.
+ urlConn = url.openConnection();
+
+ // we want to do output.
+ urlConn.setDoOutput(true);
+
+ // No caching
+ urlConn.setUseCaches(false);
+
+ // Specify the header content type.
+ urlConn.setRequestProperty("Content-Type", "audio/x-flac; rate=8000");
+
+ // Send POST output.
+ outputStream = urlConn.getOutputStream();
+
+
+ FileInputStream fileInputStream = new FileInputStream(inputFile);
+
+ byte[] buffer = new byte[256];
+
+ while ((fileInputStream.read(buffer, 0, 256)) != -1) {
+ outputStream.write(buffer, 0, 256);
+ }
+
+ fileInputStream.close();
+ outputStream.close();
+
+ // Get response data.
+ br = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
+
+ String response = br.readLine();
+
+ br.close();
+
+ return response;
+
+ }
+
+
+}
diff --git a/src/com/darkprograms/speech/synthesiser/Synthesiser.java b/src/com/darkprograms/speech/synthesiser/Synthesiser.java
new file mode 100644
index 0000000..bbedb0d
--- /dev/null
+++ b/src/com/darkprograms/speech/synthesiser/Synthesiser.java
@@ -0,0 +1,48 @@
+package com.darkprograms.speech.synthesiser;
+
+import java.io.InputStream;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLEncoder;
+
+/**
+ * Synthesiser class that connects to Google's unoffical API to retreive data
+ *
+ * @author Luke Kuza
+ */
+public class Synthesiser {
+
+ /**
+ * URL to query for Google synthesiser
+ */
+ private final static String GOOGLE_SYNTHESISER_URL = "http://translate.google.com/translate_tts?tl=en&q=";
+
+ /**
+ * Constructor
+ */
+ public Synthesiser() {
+
+ }
+
+ /**
+ * Gets an input stream to MP3 data for the returned information from a request
+ *
+ * @param synthText Text you want to be synthesized into MP3 data
+ * @return Returns an input stream of the MP3 data that is returned from Google
+ * @throws Exception Throws exception if it can not complete the request
+ */
+ public InputStream getMP3Data(String synthText) throws Exception {
+ String encoded = URLEncoder.encode(synthText, "UTF-8"); //Encode
+
+ URL url = new URL(GOOGLE_SYNTHESISER_URL + encoded); //create url
+
+ // Open New URL connection channel.
+ URLConnection urlConn = url.openConnection(); //Open connection
+
+
+ urlConn.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0) Gecko/20100101 Firefox/4.0"); //Adding header for user agent is required
+
+ return urlConn.getInputStream();
+ }
+
+}