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
15 changes: 14 additions & 1 deletion CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,18 @@ Changelog corresponds with a tagged and signed Git commit. This marks the chang
A tagged commit may or may not have a corresponding binary version available.
Format: Tag: `<Corresponding Tag>`

* Version 1.06 (Tag v1.016)
* Added support in Synthesiser for strings longer than 99 characters (Credits to @Skylion007)

* Version 1.05 (Tag: v1.015)
* Improved language support for recognizer (Credits to @duncanj)
* Add support for multiple responses for recognizer (Credits to @duncanj)
* Add profanity filter toggle support for recognizer (Credits to @duncanj)

* Version 1.01 (Tag: v1.01)
* Fixed state functions for Microphones
* Fixed encoding single byte frames
* Support Multiple Languages

* Version 1.00 (Tag: v1.00)
* Initial Release
* Initial Release
3 changes: 3 additions & 0 deletions CREDITS.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ The following people/organizations have helped provide functionality for the API
* Allows for text to speech translation
* Homepage: http://google.com

* JSON-lib: http://json-lib.sourceforge.net/
* JUnit: http://junit.org/

I would like to thank the above so much for your work, this wrapper/API could not have been
created without it.
13 changes: 0 additions & 13 deletions java-speech-api.iml

This file was deleted.

14 changes: 11 additions & 3 deletions src/com/darkprograms/speech/recognizer/FlacEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.io.File;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* Class that contains methods to encode Wave files to FLAC files
Expand All @@ -31,7 +33,7 @@ public FlacEncoder() {
* @param inputFile Input wave file
* @param outputFile Output FLAC file
*/
public void convertWaveToFlac(File inputFile, File outputFile) {
public void convertWaveToFlac(File inputFile, File outputFile) throws Exception {


StreamConfiguration streamConfiguration = new StreamConfiguration();
Expand Down Expand Up @@ -80,7 +82,9 @@ public void convertWaveToFlac(File inputFile, File outputFile) {
flacOutputStream.close();

} catch (Exception ex) {
ex.printStackTrace();
Logger.getLogger(FlacEncoder.class.getName()).log(Level.SEVERE, ex.getClass().getName(), ex);

throw ex;
}
}

Expand All @@ -93,7 +97,11 @@ public void convertWaveToFlac(File inputFile, File outputFile) {
* @param outputFile Output FLAC file
*/
public void convertWaveToFlac(String inputFile, String outputFile) {
convertWaveToFlac(new File(inputFile), new File(outputFile));
try {
convertWaveToFlac(new File(inputFile), new File(outputFile));
} catch (Exception ex) {
Logger.getLogger(FlacEncoder.class.getName()).log(Level.SEVERE, null, ex);
}
}


Expand Down
37 changes: 34 additions & 3 deletions src/com/darkprograms/speech/recognizer/GoogleResponse.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.darkprograms.speech.recognizer;
package com.darkprograms.speech.recognizer;

import java.util.ArrayList;
import java.util.List;

/**
* Class that holds the response and confidence of a Google recognizer request
*
* @author Luke Kuza, Duncan Jauncey
* @author Luke Kuza, Duncan Jauncey, Víctor Martín Molina
*/
public class GoogleResponse {

Expand All @@ -22,7 +22,7 @@ public class GoogleResponse {
/**
* List that holds other possible responses for this request.
*/
private List<String> otherPossibleResponses = new ArrayList(20);
private List<String> otherPossibleResponses = new ArrayList<String>(20);

/**
* Constructor
Expand Down Expand Up @@ -76,4 +76,35 @@ public List<String> getOtherPossibleResponses() {
return otherPossibleResponses;
}

@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof GoogleResponse)) {
return false;
}
GoogleResponse googleResponse = (GoogleResponse) obj;
if (!response.equals(googleResponse.response)) {
return false;
}
if (!confidence.equals(googleResponse.confidence)) {
return false;
}
if (!otherPossibleResponses.equals(googleResponse.otherPossibleResponses)) {
return false;
}

return true;
}

@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + (this.response != null ? this.response.hashCode() : 0);
hash = 59 * hash + (this.confidence != null ? this.confidence.hashCode() : 0);
hash = 59 * hash + (this.otherPossibleResponses != null ? this.otherPossibleResponses.hashCode() : 0);
return hash;
}

}
112 changes: 46 additions & 66 deletions src/com/darkprograms/speech/recognizer/Recognizer.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
package com.darkprograms.speech.recognizer;
package com.darkprograms.speech.recognizer;

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

/**
* Class that submits FLAC audio and retrieves recognized text
*
* @author Luke Kuza, Duncan Jauncey
* @author Luke Kuza, Duncan Jauncey, Víctor Martín Molina
*/
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";

private static final String CONFIDENCE = "confidence";

private boolean profanityFilter = true;
private String language = null;
Expand All @@ -28,6 +35,14 @@ public class Recognizer {
public Recognizer() {
}

/**
*
* @return Google's profanity filter
*/
public boolean isProfanityFilter() {
return profanityFilter;
}

/**
* Enable/disable Google's profanity filter (on by default).
* @param profanityFilter
Expand All @@ -36,6 +51,13 @@ public void setProfanityFilter(boolean profanityFilter) {
this.profanityFilter = profanityFilter;
}

/**
* @return Language code. This language code must match the language of the speech to be recognized. ex. en-US ru-RU
*/
public String getLanguage() {
return language;
}

/**
* Language code. This language code must match the language of the speech to be recognized. ex. en-US ru-RU
* Setting this to null will make Google use it's own language detection.
Expand Down Expand Up @@ -161,49 +183,34 @@ public GoogleResponse getRecognizedDataForFlac(String flacFile) throws Exception
* 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
* @return Returns the parsed response.
*/
private void parseResponse(String rawResponse, GoogleResponse googleResponse) {
if (!rawResponse.contains("utterance"))
return;

String array = substringBetween(rawResponse, "[", "]");
String[] parts = array.split("}");
System.out.println(parts.length);

boolean first = true;
for( String s : parts ) {
if( first ) {
first = false;
String utterancePart = s.split(",")[0];
String confidencePart = s.split(",")[1];

String utterance = utterancePart.split(":")[1];
String confidence = confidencePart.split(":")[1];

utterance = stripQuotes(utterance);
confidence = stripQuotes(confidence);

if( utterance.equals("null") ) {
utterance = null;
}
if( confidence.equals("null") ) {
confidence = null;
void parseResponse(String rawResponse, GoogleResponse googleResponse) {
try {
JSONObject json = JSONObject.fromObject(rawResponse);
int status = json.getInt("status");

if (status == 0) {
JSONArray hypotheses = json.getJSONArray("hypotheses");

for (int index = 0; index < hypotheses.size(); index++) {
JSONObject hypothese = (JSONObject) hypotheses.get(index);

if (hypothese.containsKey(CONFIDENCE)) {
googleResponse.setResponse(hypothese.getString("utterance"));
googleResponse.setConfidence(hypothese.getDouble(CONFIDENCE) + "");
} else {
googleResponse.getOtherPossibleResponses().add(hypothese.getString("utterance"));
}
}

googleResponse.setResponse(utterance);
googleResponse.setConfidence(confidence);
} else {
String utterance = s.split(":")[1];
utterance = stripQuotes(utterance);
if( utterance.equals("null") ) {
utterance = null;
}
googleResponse.getOtherPossibleResponses().add(utterance);
Logger.getLogger(Recognizer.class.getName()).log(Level.WARNING, "status: {0}", status);
}
} catch (JSONException e) {
Logger.getLogger(Recognizer.class.getName()).log(Level.WARNING, e.getLocalizedMessage(), e);
}
}

/**
* Performs the request to Google with a file <br>
* Request is buffered
Expand Down Expand Up @@ -271,31 +278,4 @@ private String rawRequest(File inputFile, int maxResults) throws Exception {

}

private String substringBetween(String s, String part1, String part2) {
String sub = null;

int i = s.indexOf(part1);
int j = s.indexOf(part2, i + part1.length());

if (i != -1 && j != -1) {
int nStart = i + part1.length();
sub = s.substring(nStart, j);
}

return sub;
}

private String stripQuotes(String s) {
int start = 0;
if( s.startsWith("\"") ) {
start = 1;
}
int end = s.length();
if( s.endsWith("\"") ) {
end = s.length() - 1;
}
return s.substring(start, end);
}


}
Loading