如何从 sphinx4 取回重新编码的音频
How to get back recoded audio from sphinx4
您好,我正在尝试从 sphinx4 获取音频文件,有什么方法可以将音频文件保存到本地吗?让看到用户说 "OK sphinx tell me time " 我需要保存音频文件 包含 "ok sphinx4 tell me time " 语法,所以我可以将这个音频文件用于其他目的。
这是从解码器取回音频文件的方法
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();
}
您好,我正在尝试从 sphinx4 获取音频文件,有什么方法可以将音频文件保存到本地吗?让看到用户说 "OK sphinx tell me time " 我需要保存音频文件 包含 "ok sphinx4 tell me time " 语法,所以我可以将这个音频文件用于其他目的。
这是从解码器取回音频文件的方法
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();
}