Skip to content

Commit

Permalink
Implement HVR ASR (#3974)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored Nov 12, 2021
1 parent 2abd4d7 commit 779b3ce
Show file tree
Hide file tree
Showing 11 changed files with 346 additions and 39 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,8 @@ tools/webgfx-tests-fxr/
# Room schemas
app/schemas/

# HVR app connect
app/agconnect-services.json

# Token files
.mls_token
12 changes: 12 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ apply from: "$project.rootDir/tools/gradle/versionCode.gradle"
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "org.mozilla.telemetry.glean-gradle-plugin"
apply plugin: 'com.huawei.agconnect'

deps.telemetry.glean_unittests = "org.mozilla.telemetry:glean-forUnitTests:$project.ext.glean_version"

Expand Down Expand Up @@ -61,6 +62,13 @@ def getOpenXRCMakeFlags = { ->
return ""
}

def getHVRMLApiKey = { ->
if (gradle.hasProperty("userProperties.HVR_ML_API_KEY")) {
return gradle."userProperties.HVR_ML_API_KEY"
}
return ""
}

// Glean: Generate markdown docs for the collected metrics.
ext.gleanGenerateMarkdownDocs = true
ext.gleanDocsDirectory = "$rootDir/docs"
Expand Down Expand Up @@ -234,6 +242,7 @@ android {
arguments "-DVR_SDK_LIB=hvr-lib", "-DHVR=ON", "-DOPENXR=ON"
}
}
buildConfigField "String", "HVR_ML_API_KEY", "\"${getHVRMLApiKey()}\""
}

noapi {
Expand Down Expand Up @@ -522,6 +531,9 @@ dependencies {

// HVR
hvrImplementation fileTree(dir: "${project.rootDir}/third_party/hvr", include: ['*.jar'])
hvrImplementation 'com.huawei.agconnect:agconnect-core-harmony:1.0.0.300'
hvrImplementation 'com.huawei.agconnect:agconnect-core:1.6.0.300'
hvrImplementation 'com.huawei.hms:ml-computer-voice-asr:3.1.0.300'
}

if (findProject(':servo')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

import androidx.annotation.NonNull;

import com.mozilla.speechlibrary.SpeechService;

import org.mozilla.vrbrowser.browser.Accounts;
import org.mozilla.vrbrowser.browser.Addons;
import org.mozilla.vrbrowser.browser.LoginStorage;
Expand All @@ -24,6 +22,8 @@
import org.mozilla.vrbrowser.db.AppDatabase;
import org.mozilla.vrbrowser.db.DataRepository;
import org.mozilla.vrbrowser.downloads.DownloadsManager;
import org.mozilla.vrbrowser.speech.MozillaSpeechRecognizer;
import org.mozilla.vrbrowser.speech.SpeechRecognizer;
import org.mozilla.vrbrowser.telemetry.GleanMetricsService;
import org.mozilla.vrbrowser.ui.adapters.Language;
import org.mozilla.vrbrowser.ui.widgets.AppServicesProvider;
Expand All @@ -43,7 +43,7 @@ public class VRBrowserApplication extends Application implements AppServicesProv
private Places mPlaces;
private Accounts mAccounts;
private DownloadsManager mDownloadsManager;
private SpeechService mSpeechService;
private SpeechRecognizer mSpeechRecognizer;
private EnvironmentsManager mEnvironmentsManager;
private Addons mAddons;
private ConnectivityReceiver mConnectivityManager;
Expand Down Expand Up @@ -83,7 +83,7 @@ protected void onActivityCreate(@NonNull Context activityContext) {
mSessionStore.setLocales(LocaleUtils.getPreferredLanguageTags(activityContext));
mDownloadsManager = new DownloadsManager(activityContext);
mDownloadsManager.init();
mSpeechService = new SpeechService(activityContext);
mSpeechRecognizer = new MozillaSpeechRecognizer(activityContext);
mBitmapCache = new BitmapCache(activityContext, mAppExecutors.diskIO(), mAppExecutors.mainThread());
mEnvironmentsManager = new EnvironmentsManager(activityContext);
mEnvironmentsManager.init();
Expand Down Expand Up @@ -142,8 +142,12 @@ public DownloadsManager getDownloadsManager() {
}

@Override
public SpeechService getSpeechService() {
return mSpeechService;
public SpeechRecognizer getSpeechRecognizer() {
return mSpeechRecognizer;
}

public void setSpeechRecognizer(SpeechRecognizer customRecognizer) {
mSpeechRecognizer = customRecognizer;
}

@Override
Expand Down
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);
}
}
}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.mozilla.vrbrowser.db.AppDatabase;
import org.mozilla.vrbrowser.db.DataRepository;
import org.mozilla.vrbrowser.downloads.DownloadsManager;
import org.mozilla.vrbrowser.speech.SpeechRecognizer;
import org.mozilla.vrbrowser.utils.BitmapCache;
import org.mozilla.vrbrowser.utils.ConnectivityReceiver;
import org.mozilla.vrbrowser.utils.EnvironmentsManager;
Expand All @@ -27,7 +28,7 @@ public interface AppServicesProvider {
BitmapCache getBitmapCache();
Accounts getAccounts();
DownloadsManager getDownloadsManager();
SpeechService getSpeechService();
SpeechRecognizer getSpeechRecognizer();
EnvironmentsManager getEnvironmentsManager();
LoginStorage getLoginStorage();
Addons getAddons();
Expand Down
Loading

0 comments on commit 779b3ce

Please sign in to comment.