Skip to content

Commit

Permalink
Ui improvements and demo for words grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
nshmyrev committed May 7, 2020
1 parent a4bea3a commit 571a928
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
20 changes: 15 additions & 5 deletions app/src/main/java/org/kaldi/demo/KaldiActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
Expand All @@ -48,8 +50,9 @@ public class KaldiActivity extends Activity implements

static private final int STATE_START = 0;
static private final int STATE_READY = 1;
static private final int STATE_FILE = 2;
static private final int STATE_MIC = 3;
static private final int STATE_DONE = 2;
static private final int STATE_FILE = 3;
static private final int STATE_MIC = 4;

/* Used to handle permission request */
private static final int PERMISSIONS_REQUEST_RECORD_AUDIO = 1;
Expand Down Expand Up @@ -141,7 +144,7 @@ protected String doInBackground(Void... params) {
long startTime = System.currentTimeMillis();
StringBuilder result = new StringBuilder();
try {
rec = new KaldiRecognizer(activityReference.get().model, 16000.f);
rec = new KaldiRecognizer(activityReference.get().model, 16000.f, "oh zero one two three four five six seven eight nine");

InputStream ais = activityReference.get().getAssets().open("10001-90210-01803.wav");
if (ais.skip(44) != 44) {
Expand Down Expand Up @@ -223,6 +226,7 @@ private void setUiState(int state) {
switch (state) {
case STATE_START:
resultView.setText(R.string.preparing);
resultView.setMovementMethod(new ScrollingMovementMethod());
findViewById(R.id.recognize_file).setEnabled(false);
findViewById(R.id.recognize_mic).setEnabled(false);
break;
Expand All @@ -232,13 +236,19 @@ private void setUiState(int state) {
findViewById(R.id.recognize_file).setEnabled(true);
findViewById(R.id.recognize_mic).setEnabled(true);
break;
case STATE_DONE:
((Button) findViewById(R.id.recognize_mic)).setText(R.string.recognize_microphone);
findViewById(R.id.recognize_file).setEnabled(true);
findViewById(R.id.recognize_mic).setEnabled(true);
break;
case STATE_FILE:
resultView.append(getString(R.string.starting));
resultView.setText(getString(R.string.starting));
findViewById(R.id.recognize_mic).setEnabled(false);
findViewById(R.id.recognize_file).setEnabled(false);
break;
case STATE_MIC:
((Button) findViewById(R.id.recognize_mic)).setText(R.string.stop_microphone);
resultView.setText(getString(R.string.say_something));
findViewById(R.id.recognize_file).setEnabled(false);
findViewById(R.id.recognize_mic).setEnabled(true);
break;
Expand All @@ -259,7 +269,7 @@ public void recognizeFile() {

public void recognizeMicrophone() {
if (recognizer != null) {
setUiState(STATE_READY);
setUiState(STATE_DONE);
recognizer.cancel();
recognizer = null;
} else {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
<string name="failed">Failed to init recognizer %s\n</string>
<string name="elapsed">Result %s elapsed %d milliseconds\n</string>
<string name="starting">Starting\n</string>
<string name="say_something">Say something\n</string>

</resources>

0 comments on commit 571a928

Please sign in to comment.