Adding new headers to support multi-lingual, turn on/off the storing of samples and transcriptions, a tag name for the client

This commit is contained in:
Andre Natal 2018-10-04 01:26:18 -07:00 коммит произвёл Andre Natal
Родитель 39509a8bee
Коммит 81e9c919ae
5 изменённых файлов: 120 добавлений и 25 удалений

Просмотреть файл

@ -11,7 +11,9 @@ import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
@ -23,9 +25,8 @@ import com.mozilla.speechmodule.R;
import static android.support.constraint.Constraints.TAG;
public class MainActivity extends AppCompatActivity implements ISpeechRecognitionListener {
public class MainActivity extends AppCompatActivity implements ISpeechRecognitionListener, CompoundButton.OnCheckedChangeListener {
private Button mButtonStart, mButtonCancel;
private MozillaSpeechService mMozillaSpeechService;
private GraphView mGraph;
private long mDtstart;
@ -42,6 +43,11 @@ public class MainActivity extends AppCompatActivity implements ISpeechRecognitio
private void initialize() {
Button buttonStart, buttonCancel;
EditText txtProdutTag, txtLanguage;
Switch switchTranscriptions = findViewById(R.id.switchTranscriptions);
Switch switchSamples = findViewById(R.id.switchSamples);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
@ -55,15 +61,20 @@ public class MainActivity extends AppCompatActivity implements ISpeechRecognitio
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
124);
}
mButtonStart = findViewById(R.id.button_start);
mButtonCancel = findViewById(R.id.button_cancel);
buttonStart = findViewById(R.id.button_start);
buttonCancel = findViewById(R.id.button_cancel);
txtProdutTag = findViewById(R.id.txtProdutTag);
txtLanguage = findViewById(R.id.txtLanguage);
mPlain_text_input = findViewById(R.id.plain_text_input);
mButtonStart.setOnClickListener((View v) -> {
buttonStart.setOnClickListener((View v) -> {
try {
mMozillaSpeechService.addListener(this);
mDtstart = System.currentTimeMillis();
mSeries1.resetData(new DataPoint[0]);
mMozillaSpeechService.setLanguage(txtLanguage.getText().toString());
mMozillaSpeechService.setProductTag(txtProdutTag.getText().toString());
mMozillaSpeechService.start(getApplicationContext());
} catch (Exception e) {
Log.d(TAG, e.getLocalizedMessage());
@ -71,7 +82,7 @@ public class MainActivity extends AppCompatActivity implements ISpeechRecognitio
}
});
mButtonCancel.setOnClickListener((View v) -> {
buttonCancel.setOnClickListener((View v) -> {
try {
mMozillaSpeechService.cancel();
} catch (Exception e) {
@ -80,6 +91,11 @@ public class MainActivity extends AppCompatActivity implements ISpeechRecognitio
}
});
switchTranscriptions.setOnCheckedChangeListener(this);
switchSamples.setOnCheckedChangeListener(this);
mMozillaSpeechService.storeTranscriptions(false);
mMozillaSpeechService.storeSamples(false);
mGraph = findViewById(R.id.graph);
mSeries1 = new LineGraphSeries<>(new DataPoint[0]);
mGraph.addSeries(mSeries1);
@ -134,4 +150,12 @@ public class MainActivity extends AppCompatActivity implements ISpeechRecognitio
mMozillaSpeechService.removeListener(this);
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.equals(findViewById(R.id.switchTranscriptions))) {
mMozillaSpeechService.storeTranscriptions(isChecked);
} else {
mMozillaSpeechService.storeSamples(isChecked);
}
}
}

Просмотреть файл

@ -35,18 +35,56 @@
android:layout_toEndOf="@+id/button_start"
android:text="Cancel" />
<EditText
android:id="@+id/plain_text_input"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="@+id/button_start"
android:layout_marginStart="15dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:singleLine="false"
android:gravity="top"
android:inputType="textMultiLine"
android:enabled="false" />
<EditText
android:id="@+id/plain_text_input"
android:layout_width="match_parent"
android:layout_height="123dp"
android:layout_below="@+id/button_start"
android:layout_marginBottom="10dp"
android:layout_marginEnd="10dp"
android:layout_marginStart="15dp"
android:layout_marginTop="120dp"
android:enabled="false"
android:gravity="top"
android:inputType="textMultiLine"
android:singleLine="false" />
<Switch
android:id="@+id/switchTranscriptions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:text="Store Transcriptions" />
<Switch
android:id="@+id/switchSamples"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_below="@+id/graph"
android:text="Store Samples" />
<EditText
android:id="@+id/txtProdutTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/plain_text_input"
android:layout_alignStart="@+id/graph"
android:layout_marginBottom="-71dp"
android:ems="10"
android:inputType="textPersonName"
android:text="ProductTag" />
<EditText
android:id="@+id/txtLanguage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/plain_text_input"
android:layout_alignStart="@+id/graph"
android:layout_marginBottom="-120dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Language" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>

Просмотреть файл

@ -14,6 +14,7 @@ public class MozillaSpeechService {
private ArrayList<ISpeechRecognitionListener> mListeners;
private Context mContext;
private boolean isIdle = true;
NetworkSettings mNetworkSettings;
public enum SpeechState
{
@ -32,6 +33,7 @@ public class MozillaSpeechService {
private MozillaSpeechService() {
mVad = new Vad();
mNetworkSettings = new NetworkSettings();
}
public void start(Context aContext) {
@ -45,7 +47,8 @@ public class MozillaSpeechService {
if (retVal < 0) {
notifyListeners(SpeechState.ERROR, "Error Initializing VAD " + String.valueOf(retVal));
} else {
this.mSpeechRecognition = new SpeechRecognition(SAMPLERATE, CHANNELS, mVad, aContext, this);
this.mSpeechRecognition = new SpeechRecognition(SAMPLERATE, CHANNELS, mVad, aContext,
this, mNetworkSettings);
Thread audio_thread = new Thread(this.mSpeechRecognition);
audio_thread.start();
isIdle = false;
@ -84,4 +87,20 @@ public class MozillaSpeechService {
}
}
public void storeSamples(boolean yesOrNo) {
this.mNetworkSettings.mStoreSamples = yesOrNo;
}
public void storeTranscriptions(boolean yesOrNo) {
this.mNetworkSettings.mStoreTranscriptions = yesOrNo;
}
public void setLanguage(String language) {
this.mNetworkSettings.mLanguage = language;
}
public void setProductTag(String tag) {
this.mNetworkSettings.mProductTag = tag;
}
}

Просмотреть файл

@ -23,7 +23,7 @@ public class Networking {
this.mSpeechService = aSpeechService;
}
protected void doSTT(final ByteArrayOutputStream baos) {
protected void doSTT(final ByteArrayOutputStream baos, NetworkSettings mNetworkSettings) {
if (cancelled) {
mSpeechService.notifyListeners(MozillaSpeechService.SpeechState.CANCELED, null);
@ -34,7 +34,12 @@ public class Networking {
Looper.prepare();
ByteArrayEntity byteArrayEntity = new ByteArrayEntity(baos.toByteArray());
SyncHttpClient client = new SyncHttpClient();
client.post(mContext,STT_ENDPOINT, byteArrayEntity, "audio/3gpp",
client.addHeader("Accept-Language-STT", mNetworkSettings.mLanguage);
client.addHeader("Store-Transcription", mNetworkSettings.mStoreTranscriptions ? "1": "0" );
client.addHeader("Store-Sample", mNetworkSettings.mStoreSamples ? "1": "0");
client.addHeader("Product-Tag", mNetworkSettings.mProductTag);
client.post(mContext, STT_ENDPOINT, byteArrayEntity, "audio/3gpp",
new AsyncHttpResponseHandler() {
@Override
@ -92,3 +97,9 @@ public class Networking {
}
}
class NetworkSettings {
boolean mStoreSamples = true;
boolean mStoreTranscriptions = true;
String mLanguage;
String mProductTag = "moz-android-speech-lib";
}

Просмотреть файл

@ -27,13 +27,16 @@ class SpeechRecognition implements Runnable {
int mChannels;
MozillaSpeechService mService;
Networking network;
NetworkSettings mNetworkSettings;
protected SpeechRecognition(int aSampleRate, int aChannels, Vad aVad, Context aContext, MozillaSpeechService aService) {
protected SpeechRecognition(int aSampleRate, int aChannels, Vad aVad, Context aContext,
MozillaSpeechService aService, NetworkSettings mNetworkSettings) {
this.mVad = aVad;
this.mContext = aContext;
this.mSampleRate = aSampleRate;
this.mChannels = aChannels;
this.mService = aService;
this.mNetworkSettings = mNetworkSettings;
}
public void run() {
@ -94,13 +97,13 @@ class SpeechRecognition implements Runnable {
if (finishedvoice) {
this.done = true;
network.doSTT(baos);
network.doSTT(baos, mNetworkSettings);
}
if ((dtdepois - dtantes)/1000 > mUpperLimit ) {
this.done = true;
if (touchedvoice) {
network.doSTT(baos);
network.doSTT(baos, mNetworkSettings);
}
else {
raisenovoice = true;