-
-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2abd4d7
commit 779b3ce
Showing
11 changed files
with
346 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
app/src/common/shared/org/mozilla/vrbrowser/speech/MozillaSpeechRecognizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package org.mozilla.vrbrowser.speech; | ||
|
||
import android.content.Context; | ||
import android.util.Log; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.mozilla.speechlibrary.SpeechResultCallback; | ||
import com.mozilla.speechlibrary.SpeechService; | ||
import com.mozilla.speechlibrary.SpeechServiceSettings; | ||
import com.mozilla.speechlibrary.stt.STTResult; | ||
|
||
import org.mozilla.geckoview.GeckoWebExecutor; | ||
import org.mozilla.vrbrowser.browser.engine.EngineProvider; | ||
import org.mozilla.vrbrowser.ui.widgets.dialogs.VoiceSearchWidget; | ||
import org.mozilla.vrbrowser.utils.SystemUtils; | ||
|
||
public class MozillaSpeechRecognizer implements SpeechRecognizer, SpeechResultCallback { | ||
protected final String LOGTAG = SystemUtils.createLogtag(this.getClass()); | ||
private SpeechService mMozillaSpeechService; | ||
private Context mContext; | ||
private @Nullable SpeechRecognizer.Callback mCallback; | ||
private static int MAX_CLIPPING = 10000; | ||
private static int MAX_DB = 130; | ||
private static int MIN_DB = 50; | ||
|
||
public MozillaSpeechRecognizer(Context context) { | ||
mContext = context; | ||
mMozillaSpeechService = new SpeechService(context); | ||
} | ||
|
||
@Override | ||
public void start(@NonNull Settings settings, @Nullable GeckoWebExecutor executor, @NonNull Callback callback) { | ||
SpeechServiceSettings.Builder builder = new SpeechServiceSettings.Builder() | ||
.withLanguage(settings.locale) | ||
.withStoreSamples(settings.storeData) | ||
.withStoreTranscriptions(settings.storeData) | ||
.withProductTag(settings.productTag); | ||
mCallback = callback; | ||
mMozillaSpeechService.start(builder.build(), EngineProvider.INSTANCE.getDefaultGeckoWebExecutor(mContext), this); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
mMozillaSpeechService.stop(); | ||
mCallback = null; | ||
} | ||
|
||
@Override | ||
public boolean shouldDisplayStoreDataPrompt() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isSpeechError(int code) { | ||
return code == VoiceSearchWidget.State.SPEECH_ERROR.ordinal(); | ||
} | ||
|
||
// SpeechResultCallback | ||
@Override | ||
public void onStartListen() { | ||
if (mCallback != null) { | ||
mCallback.onStartListening(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onMicActivity(double fftsum) { | ||
if (mCallback != null) { | ||
double db = (double)fftsum * -1; // the higher the value, quieter the user/environment is | ||
db = db == Double.POSITIVE_INFINITY ? MAX_DB : db; | ||
int level = (int)(MAX_CLIPPING - (((db - MIN_DB) / (MAX_DB - MIN_DB)) * MAX_CLIPPING)); | ||
Log.d(LOGTAG, "===> db: " + db); | ||
Log.d(LOGTAG, "===> level " + level); | ||
mCallback.onMicActivity(level); | ||
} | ||
} | ||
|
||
@Override | ||
public void onDecoding() { | ||
if (mCallback != null) { | ||
mCallback.onDecoding(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onSTTResult(@Nullable @org.jetbrains.annotations.Nullable STTResult result) { | ||
if (mCallback == null) { | ||
return; | ||
} | ||
|
||
if (result != null) { | ||
mCallback.onResult(result.mTranscription, result.mConfidence); | ||
} else { | ||
mCallback.onResult("", 0); | ||
} | ||
} | ||
|
||
@Override | ||
public void onNoVoice() { | ||
if (mCallback != null) { | ||
mCallback.onNoVoice(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(int errorType, @Nullable @org.jetbrains.annotations.Nullable String error) { | ||
if (mCallback != null) { | ||
mCallback.onError(errorType, error); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
app/src/common/shared/org/mozilla/vrbrowser/speech/SpeechRecognizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.mozilla.vrbrowser.speech; | ||
|
||
import android.telecom.Call; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
|
||
import com.mozilla.speechlibrary.SpeechResultCallback; | ||
|
||
import org.mozilla.geckoview.GeckoWebExecutor; | ||
|
||
public interface SpeechRecognizer { | ||
class Settings { | ||
public String locale; | ||
public boolean storeData; | ||
public String productTag; | ||
} | ||
|
||
interface Callback { | ||
void onStartListening(); | ||
void onMicActivity(int level); | ||
void onDecoding(); | ||
void onResult(String transcription, float confidence); | ||
void onNoVoice(); | ||
void onError(@SpeechResultCallback.ErrorType int errorType, @Nullable String error); | ||
} | ||
|
||
void start(@NonNull Settings settings, @Nullable GeckoWebExecutor executor, @NonNull Callback callback); | ||
void stop(); | ||
boolean shouldDisplayStoreDataPrompt(); | ||
boolean isSpeechError(int code); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.