Search code examples
javaspeech-recognitioncmusphinxsphinx4

How to get back recoded audio from sphinx4


Hi I am trying to get the audio file from sphinx4, is there any way to save audiofile to local? let see user say "OK sphinx tell me time " I need to save audio file witch contain "ok sphinx4 tell me time " syntax, so i can use this audio file for other purposes.


Solution

  • This how you can get back audio file from decoder

        recognizer = SpeechRecognizerSetup.defaultSetup()
                .setAcousticModel(new File(assetsDir, "en-us-ptm"))
                .setDictionary(new File(assetsDir, "cmudict-en-us.dict"))
                .getRecognizer();
    
        recognizer.addListener(mRecognitionListener);
        recognizer.getDecoder().setRawdataSize(300000);// don't forget to set size
    
        recognizer.addKeyphraseSearch(KWS_SEARCH, KEYPHRASE);
    
    
    }
    

    and then inside your onResult callback

    @override
    public void onResult(Hypothesis hypothesis) {
    Log.d(TAG, "onResult: " + + recognizer.getDecoder().getRawdata().length);
    
            if (hypothesis != null) {
                String text = hypothesis.getHypstr();
            }
          Decoder decoder= recognizer.getDecoder();
            short[] data = decoder.getRawdata();
            try {
                DataOutputStream dos = new DataOutputStream(new FileOutputStream(Utils.getPublicStorageDir("recoding",".raw")));
                for (int i = 0; i < data.length; i++) {
                    dos.writeShort(data[i]);
                }
                dos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    

    And you can play this audio

       public static void playAudioFromRaw(short[] data){
            int bufsize = AudioTrack.getMinBufferSize(
                    8000,
                    AudioFormat.CHANNEL_IN_STEREO,
                    AudioFormat.ENCODING_PCM_16BIT
            );
            AudioTrack trackplayer = new AudioTrack(
                    AudioManager.STREAM_MUSIC, 8000,
                    AudioFormat.CHANNEL_IN_STEREO,
                    AudioFormat.ENCODING_PCM_16BIT,
                    bufsize,
                    AudioTrack.MODE_STREAM
            );
            trackplayer.play();
            trackplayer.write(data, 0, data.length);
            trackplayer.stop();
            trackplayer.release();
        }