Index: content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java |
diff --git a/content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java b/content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..92ac8166d77e5a46b0c2ecde39c234dbc438604d |
--- /dev/null |
+++ b/content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java |
@@ -0,0 +1,173 @@ |
+// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.content.browser; |
+ |
+import android.content.Context; |
+import android.content.Intent; |
+import android.os.Bundle; |
+import android.speech.RecognitionListener; |
+import android.speech.RecognizerIntent; |
+import android.speech.SpeechRecognizer; |
+ |
+import org.chromium.base.CalledByNative; |
+import org.chromium.base.JNINamespace; |
+import org.chromium.content.browser.SpeechRecognitionError; |
+ |
+import java.util.ArrayList; |
+ |
+/** |
+ * This class uses Android's SpeechRecognizer to perform speech recognition for |
+ * the Web Speech API on Android. Using Android's platform recognizer offers |
+ * several benefits, like good quality and good local fallback when no data |
+ * connection is available. |
+ */ |
+@JNINamespace("content") |
+class SpeechRecognition { |
+ |
+ private boolean mContinuous; |
+ private SpeechRecognizer mRecognizer; |
bulach
2013/06/11 07:24:48
nit: put the final members first
janx
2013/06/12 14:47:14
Done.
|
+ private final Context mContext; |
+ private final Intent mIntent; |
+ private final int mNativeSpeechRecognizerImplAndroid; |
+ private final RecognitionListener mListener; |
+ |
+ class Listener implements RecognitionListener { |
+ @Override |
+ public void onBeginningOfSpeech() { |
+ nativeOnSoundStart(mNativeSpeechRecognizerImplAndroid); |
+ } |
+ |
+ @Override |
+ public void onBufferReceived(byte[] buffer) { } |
+ |
+ @Override |
+ public void onEndOfSpeech() { |
+ nativeOnSoundEnd(mNativeSpeechRecognizerImplAndroid); |
+ // Since Android doesn't have a dedicated event for when audio |
+ // capture is finished, we fire it after speech has ended. |
+ nativeOnAudioEnd(mNativeSpeechRecognizerImplAndroid); |
+ } |
+ |
+ @Override |
+ public void onError(int error) { |
Primiano Tucci (use gerrit)
2013/06/11 13:34:51
In which order (relatively to onEndOfSpeech) is th
janx
2013/06/12 14:47:14
The order is the same as on desktop except in one
|
+ int code = -1; |
+ |
+ // Translate Android speech recognition errors to Web Speech API errors. |
+ switch(error) { |
+ case SpeechRecognizer.ERROR_AUDIO: |
+ code = SpeechRecognitionError.AUDIO; |
+ break; |
+ case SpeechRecognizer.ERROR_CLIENT: |
+ code = SpeechRecognitionError.ABORTED; |
+ break; |
+ case SpeechRecognizer.ERROR_RECOGNIZER_BUSY: |
+ case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS: |
+ code = SpeechRecognitionError.NOT_ALLOWED; |
+ break; |
+ case SpeechRecognizer.ERROR_NETWORK_TIMEOUT: |
+ case SpeechRecognizer.ERROR_NETWORK: |
+ case SpeechRecognizer.ERROR_SERVER: |
+ code = SpeechRecognitionError.NETWORK; |
+ break; |
+ case SpeechRecognizer.ERROR_NO_MATCH: |
+ code = SpeechRecognitionError.NO_MATCH; |
+ break; |
+ case SpeechRecognizer.ERROR_SPEECH_TIMEOUT: |
+ code = SpeechRecognitionError.NO_SPEECH; |
+ break; |
+ default: |
+ assert false; |
+ return; |
+ } |
+ |
+ nativeOnRecognitionError(mNativeSpeechRecognizerImplAndroid, code); |
+ terminate(); |
+ } |
+ |
+ @Override |
+ public void onEvent(int event, Bundle bundle) { } |
+ |
+ @Override |
+ public void onPartialResults(Bundle bundle) { |
+ handleResults(bundle, true); |
+ } |
+ |
+ @Override |
+ public void onReadyForSpeech(Bundle bundle) { |
+ nativeOnAudioStart(mNativeSpeechRecognizerImplAndroid); |
+ } |
+ |
+ @Override |
+ public void onResults(Bundle bundle) { |
+ handleResults(bundle, false); |
+ if (!mContinuous) |
+ terminate(); |
+ } |
+ |
+ @Override |
+ public void onRmsChanged(float rms) { } |
+ |
+ private void handleResults(Bundle bundle, boolean provisional) { |
+ ArrayList<String> list = |
+ bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION); |
+ String[] results = list.toArray(new String[list.size()]); |
+ float[] scores = bundle.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES); |
+ nativeOnRecognitionResults(mNativeSpeechRecognizerImplAndroid, results, scores, |
+ provisional); |
+ } |
+ } |
+ |
+ private SpeechRecognition(final Context context, int nativeSpeechRecognizerImplAndroid) { |
+ mContext = context; |
+ mContinuous = false; |
+ mNativeSpeechRecognizerImplAndroid = nativeSpeechRecognizerImplAndroid; |
+ mListener = new Listener(); |
+ mIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); |
+ mRecognizer = SpeechRecognizer.createSpeechRecognizer(mContext); |
+ mRecognizer.setRecognitionListener(mListener); |
+ } |
+ |
+ private void terminate() { |
+ mRecognizer.destroy(); |
+ mRecognizer = null; |
+ nativeOnRecognitionEnd(mNativeSpeechRecognizerImplAndroid); |
Primiano Tucci (use gerrit)
2013/06/11 13:34:51
At this point you should reset the native pointer
janx
2013/06/12 14:47:14
Done.
|
+ } |
+ |
+ @CalledByNative |
+ private static SpeechRecognition createSpeechRecognition(Context context, |
+ int nativeSpeechRecognizerImplAndroid) { |
+ return new SpeechRecognition(context, nativeSpeechRecognizerImplAndroid); |
+ } |
+ |
+ @CalledByNative |
+ private void startRecognition(boolean continuous, boolean interim_results) { |
+ mContinuous = continuous; |
+ mIntent.putExtra("android.speech.extra.DICTATION_MODE", continuous); |
+ mIntent.putExtra(RecognizerIntent.EXTRA_PARTIAL_RESULTS, interim_results); |
+ mRecognizer.startListening(mIntent); |
+ } |
+ |
+ @CalledByNative |
+ private void abortRecognition() { |
+ mRecognizer.cancel(); |
+ terminate(); |
+ } |
+ |
+ @CalledByNative |
+ private void stopRecognition() { |
+ mContinuous = false; |
+ mRecognizer.stopListening(); |
+ } |
+ |
+ private native void nativeOnAudioStart(int nativeSpeechRecognizerImplAndroid); |
+ private native void nativeOnSoundStart(int nativeSpeechRecognizerImplAndroid); |
+ private native void nativeOnSoundEnd(int nativeSpeechRecognizerImplAndroid); |
+ private native void nativeOnAudioEnd(int nativeSpeechRecognizerImplAndroid); |
+ private native void nativeOnRecognitionResults(int nativeSpeechRecognizerImplAndroid, |
+ String[] results, float[] scores, boolean provisional); |
+ private native void nativeOnRecognitionError(int nativeSpeechRecognizerImplAndroid, |
+ int error); |
+ private native void nativeOnRecognitionEnd(int nativeSpeechRecognizerImplAndroid); |
+} |