如何从sphinx4取回重新编码的音频

时间:2018-10-30 18:56:17

标签: java speech-recognition cmusphinx sphinx4

嗨,我正在尝试从sphinx4获取音频文件,有什么方法可以将音频文件保存到本地吗?让用户说“确定sphinx告诉我时间”,我需要保存音频文件,其中包含“确定sphinx4告诉我时间”的语法,因此我可以将此音频文件用于其他目的。

1 个答案:

答案 0 :(得分:0)

这是如何从解码器取回音频文件

    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);


}

然后进入onResult回调

@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();
        }

    }

您可以播放此音频

   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();
    }