首页 文章

保存Android Stock语音识别引擎的音频输入

提问于
浏览
38

我试图在文件中保存由android语音识别服务监听的音频数据 .

实际上我按照这里的解释实现 RecognitionListenerSpeech to Text on Android

将数据保存到缓冲区中,如下所示:Capturing audio sent to Google's speech recognition server

并将缓冲区写入Wav文件,如此处所示 . Android Record raw bytes into WAVE file for Http Streaming

我的问题是如何获得适当的音频设置以保存在wav文件的 Headers 中 . 事实上当我播放wav文件时,只听到奇怪的噪音,有了这个参数,

short nChannels=2;// audio channels
int sRate=44100;    // Sample rate
short bSamples = 16;// byteSample

或者没有这个:

short nChannels=1;// audio channels
int sRate=8000;    // Sample rate
short bSamples = 16;// byteSample

令人困惑的是,从logcat查看语音识别任务的参数我首先找到 Set PLAYBACK sample rate to 44100 HZ

12-20 14:41:34.007: DEBUG/AudioHardwareALSA(2364): Set PLAYBACK PCM format to S16_LE (Signed 16 bit Little Endian)
    12-20 14:41:34.007: DEBUG/AudioHardwareALSA(2364): Using 2 channels for PLAYBACK.
    12-20 14:41:34.007: DEBUG/AudioHardwareALSA(2364): Set PLAYBACK sample rate to 44100 HZ
    12-20 14:41:34.007: DEBUG/AudioHardwareALSA(2364): Buffer size: 2048
    12-20 14:41:34.007: DEBUG/AudioHardwareALSA(2364): Latency: 46439

然后 aInfo.SampleRate = 8000 当它播放要发送到谷歌服务器的文件时:

12-20 14:41:36.152: DEBUG/(2364): PV_Wav_Parser::InitWavParser
12-20 14:41:36.152: DEBUG/(2364): File open Succes
12-20 14:41:36.152: DEBUG/(2364): File SEEK End Succes
...
12-20 14:41:36.152: DEBUG/(2364): PV_Wav_Parser::ReadData
12-20 14:41:36.152: DEBUG/(2364): Data Read buff = RIFF?
12-20 14:41:36.152: DEBUG/(2364): Data Read = RIFF?
12-20 14:41:36.152: DEBUG/(2364): PV_Wav_Parser::ReadData
12-20 14:41:36.152: DEBUG/(2364): Data Read buff = fmt 
...
12-20 14:41:36.152: DEBUG/(2364): PVWAVPARSER_OK
12-20 14:41:36.156: DEBUG/(2364): aInfo.AudioFormat = 1
12-20 14:41:36.156: DEBUG/(2364): aInfo.NumChannels = 1
12-20 14:41:36.156: DEBUG/(2364): aInfo.SampleRate = 8000
12-20 14:41:36.156: DEBUG/(2364): aInfo.ByteRate = 16000
12-20 14:41:36.156: DEBUG/(2364): aInfo.BlockAlign = 2
12-20 14:41:36.156: DEBUG/(2364): aInfo.BitsPerSample = 16
12-20 14:41:36.156: DEBUG/(2364): aInfo.BytesPerSample = 2
12-20 14:41:36.156: DEBUG/(2364): aInfo.NumSamples = 2258

那么,我怎样才能找到合适的参数来将音频缓冲区保存在一个好的wav音频文件中呢?

3 回答

  • 6

    您没有包含实际写出PCM数据的代码,因此难以诊断,但如果您听到奇怪的噪音,那么当您编写数据或错误的数字时,您很可能看错endian通道 . 获取采样率错误只会导致音频声音变慢或变快,但如果听起来完全乱码,则可能是指定通道数或字节流的字节顺序错误 .

    要确切知道,只需将您的字节直接流式传输到没有任何标头的文件(原始PCM数据) . 这样,您可以在编写文件头时排除任何错误 . 然后使用Audacity导入原始数据,尝试不同的选项(位深度,字节序,通道),直到你得到一个听起来正确的音频文件(只有一个是正确的) . 您可以从文件 - >导入 - >原始数据执行此操作...

    一旦您以这种方式识别了字节格式,您只需要担心是否正确设置了 Headers . 您可能希望将此引用引用http://www-mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html作为文件格式 . 或者在编写音频文件Java - reading, manipulating and writing WAV filesFMJ时查看现有Java解决方案的以下链接 . 虽然我猜这些可能在Android上无法使用 .

    如果您不得不自己编写WAV / RIFF编写器,请记住Java的数据类型是big-endian,因此您写入文件的任何多字节基元必须写入reverse byte order以匹配RIFF的小端内容 .

  • 0

    8000 ,小端, 16 bit PCM ,单声道做了伎俩

  • 2

    在最新版本的onBufferReceived不起作用时,您可以使用record/save audio from voice recognition intent代替 .

相关问题