Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Unified Diff: content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java

Issue 15907012: Implement SpeechRecognizerImplAndroid (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix non-android builds by excluding speech_recognizer_impl_android Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..f374c7d61479b7983f22bd0730109233414e49e6
--- /dev/null
+++ b/content/public/android/java/src/org/chromium/content/browser/SpeechRecognition.java
@@ -0,0 +1,189 @@
+// 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 final Context mContext;
+ private final Intent mIntent;
+ private final RecognitionListener mListener;
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 Ultra-nit: can you move it to line 39, just below
janx 2013/06/13 16:08:53 This ultra-nit conflicts with: "bulach 2013/06/11
+ private final int STATE_IDLE = 0;
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 Nit: +static (i.e. private static final int). Here
Peter Beverloo 2013/06/13 11:46:23 Agreed. Please also add a comment that they are eq
Primiano Tucci (use gerrit) 2013/06/13 13:50:04 Hmm I think they're not really the same and btw th
janx 2013/06/13 16:08:53 Done.
+ private final int STATE_AWAITING_SPEECH = 1;
+ private final int STATE_CAPTURING_SPEECH = 2;
+ private int mState;
+ private int mNativeSpeechRecognizerImplAndroid;
+ private boolean mContinuous;
+ private SpeechRecognizer mRecognizer;
+
+ class Listener implements RecognitionListener {
Peter Beverloo 2013/06/13 11:46:23 You never replied to my comment about method in th
janx 2013/06/13 16:08:53 I did add a little more documentation and recycled
+ @Override
+ public void onBeginningOfSpeech() {
+ mState = STATE_CAPTURING_SPEECH;
+ 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);
+ mState = STATE_IDLE;
+ }
+
+ @Override
+ public void onError(int error) {
+ int code = SpeechRecognitionError.NONE;
+
+ // Translate Android speech recognition errors to Web Speech API errors.
+ switch(error) {
+ case SpeechRecognizer.ERROR_AUDIO:
Peter Beverloo 2013/06/13 11:46:23 nit: indent, should be four spaces per level.
janx 2013/06/13 16:08:53 Done.
+ 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;
+ }
+
+ terminate(code);
+ }
+
+ @Override
+ public void onEvent(int event, Bundle bundle) { }
+
+ @Override
+ public void onPartialResults(Bundle bundle) {
+ handleResults(bundle, true);
+ }
+
+ @Override
+ public void onReadyForSpeech(Bundle bundle) {
+ mState = STATE_AWAITING_SPEECH;
+ nativeOnAudioStart(mNativeSpeechRecognizerImplAndroid);
+ }
+
+ @Override
+ public void onResults(Bundle bundle) {
+ handleResults(bundle, false);
+ if (!mContinuous)
+ terminate(SpeechRecognitionError.NONE);
+ }
+
+ @Override
+ public void onRmsChanged(float rms) { }
+
+ private void handleResults(Bundle bundle, boolean provisional) {
+ ArrayList<String> list =
+ bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
Peter Beverloo 2013/06/13 11:46:23 nit: should have eight space indent (continuation)
janx 2013/06/13 16:08:53 Done.
+ String[] results = list.toArray(new String[list.size()]);
+ float[] scores = bundle.getFloatArray(SpeechRecognizer.CONFIDENCE_SCORES);
+ nativeOnRecognitionResults(mNativeSpeechRecognizerImplAndroid, results, scores,
+ provisional);
Peter Beverloo 2013/06/13 11:46:23 nit: should have eight space indent (continuation)
janx 2013/06/13 16:08:53 Done.
+ }
+ }
+
+ 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(int error) {
+ if (mState != STATE_IDLE) {
+ if (mState == STATE_CAPTURING_SPEECH) {
+ nativeOnSoundEnd(mNativeSpeechRecognizerImplAndroid);
+ }
+ nativeOnAudioEnd(mNativeSpeechRecognizerImplAndroid);
+ mState = STATE_IDLE;
+ }
+ if (error != SpeechRecognitionError.NONE)
+ nativeOnRecognitionError(mNativeSpeechRecognizerImplAndroid, error);
+ mRecognizer.destroy();
+ mRecognizer = null;
+ nativeOnRecognitionEnd(mNativeSpeechRecognizerImplAndroid);
+ mNativeSpeechRecognizerImplAndroid = 0;
+ }
+
+ @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();
Primiano Tucci (use gerrit) 2013/06/13 10:05:05 I fear that if someone (i.e. the S.R. manager) cal
janx 2013/06/13 16:08:53 Done.
+ terminate(SpeechRecognitionError.ABORTED);
+ }
+
+ @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);
+}

Powered by Google App Engine
This is Rietveld 408576698