Commit b765416f authored by Martin LOISEAU's avatar Martin LOISEAU 🎄
Browse files

Fix multiple speech response (still slow to load)

parent f8dc1876
......@@ -50,6 +50,8 @@ public class MainActivity extends AppCompatActivity {
public MainThread mainThread;
private CaptureQR QRActionRequesting;
private CaptureSpeech SpeechRequest;
private Intent recognizerIntent;
private listener listener;
//game logic
public ArrayList<Atom> Atoms;
......@@ -73,6 +75,7 @@ public class MainActivity extends AppCompatActivity {
}
mainThread = new MainThread(this);
SetUpListener();
Atoms = new ArrayList<Atom>();
Atoms.add(new ItemAtom(0));
......@@ -198,7 +201,7 @@ public class MainActivity extends AppCompatActivity {
public void onClick(View view) {
Snackbar.make(view, "Should listen to what's being said using speech recognition API", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
DemoListener();
// DemoListener();
}
});
......@@ -251,22 +254,6 @@ public class MainActivity extends AppCompatActivity {
return true;
}
public boolean DemoListener()
{
requestRecordAudioPermission();
Intent recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,5);
listener listener = new listener(this);
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(listener);
sr.startListening(recognizerIntent);
return true;
}
public boolean DemoQR()
{
requestPhotoPermission();
......@@ -298,14 +285,6 @@ public class MainActivity extends AppCompatActivity {
if(QRActionRequesting != null) QRActionRequesting.GiveQRResult(data.getStringExtra("QR_CONTENT"));
}
public void SetListeningResults(String result) // to display listening results
{
Log.d("Debug", "Best confidence result (from main activity): " + result);
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
if(SpeechRequest != null) SpeechRequest.GiveSpeechResult(result);
}
private void requestRecordAudioPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
String requiredPermission = Manifest.permission.RECORD_AUDIO;
......@@ -330,8 +309,32 @@ public class MainActivity extends AppCompatActivity {
}
}
private void SetUpListener()
{
requestRecordAudioPermission();
recognizerIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
recognizerIntent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE,"voice.recognition.test");
recognizerIntent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS,2);
}
public void GetSpeech(CaptureSpeech calling_action) {
this.SpeechRequest = calling_action;
DemoListener();
listener = new listener(this);
sr = SpeechRecognizer.createSpeechRecognizer(this);
sr.setRecognitionListener(listener);
Log.d("Debug2", "Start listening speech recognizer");
sr.startListening(recognizerIntent);
}
public void SetListeningResults(String result) // to display listening results
{
Log.d("Debug2", "Best confidence result (from main activity): " + result);
//Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
if(SpeechRequest != null) SpeechRequest.GiveSpeechResult(result);
}
}
package com.example.qrquest.actions;
import android.util.Log;
import com.example.qrquest.MainActivity;
import com.example.qrquest.atom.SpeechAtom;
......@@ -10,6 +12,7 @@ public class CaptureSpeech extends Action {
public void Do()
{
Log.d("Debug2", "Capture speed action started");
super.Do();
mainActivity.GetSpeech(this);
}
......@@ -17,6 +20,7 @@ public class CaptureSpeech extends Action {
public void GiveSpeechResult(String result)
{
mainActivity.Atoms.add(new SpeechAtom(result));
Log.d("Debug2", "Capture speech action added speech atom \"" + result + "\"");
End();
}
}
......@@ -25,10 +25,9 @@ public class Checker extends Action {
super.Do();
for (Atom a: mainActivity.Atoms
) {
Log.d("Debug2","QR checker action reads " + a.GetStringValue());
Log.d("Debug2","Checker action reads " + a.GetStringValue());
if(a.getClass() == AtomType)
{
Log.d("Debug2","QR checker detected a QRAtom");
if(a.GetStringValue().equals(WaitedValue)) mainActivity.OpenNodes.add(mainActivity.GetNode(SuccessNodeID));
else mainActivity.OpenNodes.add(mainActivity.GetNode(FailNodeID));
mainActivity.Atoms.remove(a);
......
......@@ -10,6 +10,7 @@ import java.util.ArrayList;
class listener implements RecognitionListener
{
boolean result_available;
public boolean already_gave_results;
ArrayList<String> result_strings; // ordered by likeliness
MainActivity mainActivity;
......@@ -18,11 +19,13 @@ class listener implements RecognitionListener
result_available = false;
result_strings = null;
this.mainActivity = mainActivity;
Log.d("Debug2", "Listener created");
}
public void onReadyForSpeech(Bundle params)
{
Log.d("Debug", "onReadyForSpeech");
Log.d("Debug2", "listener ready for speech");
}
public void onBeginningOfSpeech()
{
......@@ -30,7 +33,7 @@ class listener implements RecognitionListener
}
public void onRmsChanged(float rmsdB)
{
Log.d("Debug", "onRmsChanged");
//Log.d("Debug", "onRmsChanged"); // this is spammed for some reason
}
public void onBufferReceived(byte[] buffer)
{
......@@ -43,6 +46,8 @@ class listener implements RecognitionListener
}
public void onResults(Bundle results)
{
if(already_gave_results) return; // this is because of an oppen issue on google's end https://issuetracker.google.com/issues/152628934'
else already_gave_results = true;
if (results == null) {
Log.d("Debug", "no speech detected " + results);
return;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment