Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

voice recognition - doing actions after sound recognization in android

please give me suggestion about this condition. is it possible in android that the device waits for special sound, and after it recognizes it, some actions happens. please tell me about your ideas.Thanks for your help!

UPDATE i try pocket sphinx and do lots of searches about 'defining new keyword", but i can't do it. I use this code:

public class PracticeActivity  extends Activity implements RecognitionListener, edu.cmu.pocketsphinx.RecognitionListener {


 // private static final String KWS_SEARCH = "wakeup";
   // private static final String KEYPHRASE = "listen"; //adjust this keyphrase!
   //3-
   private static final String DIGITS_SEARCH = "digits";
private edu.cmu.pocketsphinx.SpeechRecognizer recognizer;
private MediaPlayer mediaPlayer;
@Override
public void onCreate(Bundle state) {
    super.onCreate(state);
    setContentView(R.layout.practice);
    ((TextView) findViewById(R.id.caption_text))
            .setText("Preparing the recognizer");

    new AsyncTask<Void, Void, Exception>() {
        @Override
        protected Exception doInBackground(Void... params) {
            try {
                Assets assets = new Assets(PracticeActivity.this);
                File assetDir = assets.syncAssets();
                setupRecognizer(assetDir);
            } catch (IOException e) {
                return e;
            }
            return null;
        }

        @Override
        protected void onPostExecute(Exception result) {
            if (result != null) {
                ((TextView) findViewById(R.id.caption_text))
                        .setText("Failed to init recognizer " + result);
            } else {
                recognizer.startListening(DIGITS_SEARCH);
            }
        }
    }.execute();
}

@Override
public void onDestroy() {
    super.onDestroy();
    recognizer.cancel();
    recognizer.shutdown();
}

/**
 * In partial result we get quick updates about current hypothesis. In
 * keyword spotting mode we can react here, in other modes we need to wait
 * for final result in onResult.
 */
@Override
public void onPartialResult(Hypothesis hypothesis) {
    if (hypothesis == null)
        return;

    String text = hypothesis.getHypstr();
    if (text.equals("my phone")) {
        ((TextView) findViewById(R.id.result_text))
                .setText(text);
        findViewById(R.id.result).setVisibility(View.VISIBLE);
       /* recognizer.cancel();
        recognizer.startListening(KWS_SEARCH);*/
    }else if (text.equals("where is my phone")){
        ((TextView) findViewById(R.id.caption_text))
                .setText(text);
        ((TextView) findViewById(R.id.result_text))
                .setText("i am here");
    }else
        recognizer.startListening(DIGITS_SEARCH);
}

@Override
public void onResult(Hypothesis hypothesis) {

}

@Override
public void onReadyForSpeech(Bundle params) {

}

@Override
public void onBeginningOfSpeech() {
}

@Override
public void onRmsChanged(float rmsdB) {

}

@Override
public void onBufferReceived(byte[] buffer) {

}

@Override
public void onEndOfSpeech() {
}

@Override
public void onError(int error) {

}

@Override
public void onResults(Bundle results) {

}

@Override
public void onPartialResults(Bundle partialResults) {

}

@Override
public void onEvent(int eventType, Bundle params) {

}

@Override
public void onTimeout() {
}

private void setupRecognizer(File assetsDir) throws IOException {
    File modelsDir = new File(assetsDir, "models");
    // The recognizer can be configured to perform multiple searches
    // of different kind and switch between them

    recognizer = defaultSetup()
            .setAcousticModel(new File(assetsDir, "en-us-ptm"))
            .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
            .getRecognizer();
    recognizer.addListener(this);


    /*/2-
    File digitsGrammar = new File(modelsDir, "grammar/digits.gram");
    recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);*/

    //3-
    File digitsGrammar = new File(modelsDir, "grammar/digits.gram");
    recognizer.addKeywordSearch(DIGITS_SEARCH, digitsGrammar);

}

@Override
public void onError(Exception error) {
    ((TextView) findViewById(R.id.caption_text)).setText(error.getMessage());
}

and this is for digits.gram

my phone /1e-1/

this happens as running:

06-08 15:37:38.300 27871-27909/phone_finder.maxsoft.com.whereismyphone I/OpenGLRenderer: Initialized EGL, version 1.4
06-08 15:37:38.310 27871-27909/phone_finder.maxsoft.com.whereismyphone I/OpenGLRenderer: HWUI protection enabled for context ,  &this =0x7f73c27be0 ,&mEglDisplay = 1 , &mEglConfig = 1943265968 
06-08 15:37:38.310 27871-27909/phone_finder.maxsoft.com.whereismyphone D/OpenGLRenderer: Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
06-08 15:37:38.310 27871-27909/phone_finder.maxsoft.com.whereismyphone D/OpenGLRenderer: Enabling debug mode 0
06-08 15:37:38.310 27871-27909/phone_finder.maxsoft.com.whereismyphone D/mali_winsys: new_window_surface returns 0x3000,  [1440x2560]-format:1
06-08 15:37:38.320 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(320): Allocating 137521 * 32 bytes (4297 KiB) for word entries
06-08 15:37:38.320 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(333): Reading main dictionary: /storage/emulated/0/Android/data/phone_finder.maxsoft.com.whereismyphone/files/sync/cmudict-en-us.dict
06-08 15:37:38.390 27871-27871/phone_finder.maxsoft.com.whereismyphone I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@2596fa2 time:22196983
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(213): Allocated 1007 KiB for strings, 1662 KiB for phones
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(336): 133420 words read
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(358): Reading filler dictionary: /storage/emulated/0/Android/data/phone_finder.maxsoft.com.whereismyphone/files/sync/en-us-ptm/noisedict
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(213): Allocated 0 KiB for strings, 0 KiB for phones
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict.c(361): 5 words read
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict2pid.c(396): Building PID tables for dictionary
06-08 15:37:38.560 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict2pid.c(406): Allocating 42^3 * 2 bytes (144 KiB) for word-initial triphones
06-08 15:37:38.590 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict2pid.c(132): Allocated 42672 bytes (41 KiB) for word-final triphones
06-08 15:37:38.590 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: dict2pid.c(196): Allocated 42672 bytes (41 KiB) for single-phone word triphones
06-08 15:37:38.600 27871-27908/phone_finder.maxsoft.com.whereismyphone I/cmusphinx: INFO: kws_search.c(420): KWS(beam: -1080, plp: -23, default threshold 0, delay 10)
06-08 15:37:38.600 27871-27908/phone_finder.maxsoft.com.whereismyphone E/cmusphinx: ERROR: "kws_search.c", line 351: Failed to open keyword file '/storage/emulated/0/Android/data/phone_finder.maxsoft.com.whereismyphone/files/sync/models/grammar/digits.gram': No such file or directory
06-08 15:37:38.600 27871-27908/phone_finder.maxsoft.com.whereismyphone E/cmusphinx: ERROR: "kws_search.c", line 424: Failed to create kws search
06-08 15:37:38.600 27871-27908/phone_finder.maxsoft.com.whereismyphone E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                         Process: phone_finder.maxsoft.com.whereismyphone, PID: 27871
                                                                                         java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                             at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                                                             at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                                             at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                             at java.lang.Thread.run(Thread.java:818)
                                                                                          Caused by: java.lang.RuntimeException: Decoder_setKws returned -1
                                                                                             at edu.cmu.pocketsphinx.PocketSphinxJNI.Decoder_setKws(Native Method)
                                                                                             at edu.cmu.pocketsphinx.Decoder.setKws(Decoder.java:151)
                                                                                             at edu.cmu.pocketsphinx.SpeechRecognizer.addKeywordSearch(SpeechRecognizer.java:276)
                                                                                             at phone_finder.maxsoft.com.whereismyphone.PracticeActivity.setupRecognizer(PracticeActivity.java:169)
                                                                                             at phone_finder.maxsoft.com.whereismyphone.PracticeActivity.access$000(PracticeActivity.java:25)
                                                                                             at phone_finder.maxsoft.com.whereismyphone.PracticeActivity$1.doInBackground(PracticeActivity.java:47)
                                                                                             at phone_finder.maxsoft.com.whereismyphone.PracticeActivity$1.doInBackground(PracticeActivity.java:41)
                                                                                             at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                             at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)?
                                                                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)?
                                                                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)?
                                                                                             at java.lang.Th

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It is called 'Continuous Speech Recognition'. If you try to use Android's built-in SpeechRecognizer api with infinite loop to wait for certain words or commands, it will drain to much battery.

You can look at CMUSphinx tutorial, it's much faster and device-friendly as far as I know:

http://cmusphinx.sourceforge.net/wiki/tutorialandroid


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...