gpt4 book ai didi

speech-recognition - 如何使用 Codename One 中的 Google Speech API?

转载 作者:行者123 更新时间:2023-12-04 07:59:33 28 4
gpt4 key购买 nike

我想从手机录制音频,然后将其发送到谷歌语音非流媒体 API。我可以使用 Capture.captureAudio() 进行录音,但是我不知道音频编码和采样率是什么,因为它们是必需的 for the api request .
如何获取音频编码和采样率,以便我可以将它们与我的 API 请求一起发送?

最佳答案

如果您查看 sources在 Android 上,它记录在 AMR-WB 中

        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
recorder.setOutputFile(temp.getAbsolutePath());

如果您正确使用 Google 语音 API 接受 AMR-WB set audio format .

另一个问题是文件在3GPP容器中被记录为AMR-WB,所以你需要一个自定义代码从3GPP中提取音频数据,你可以找到它 here :
// #!AMR\n
private static byte[] AMR_MAGIC_HEADER = {0x23, 0x21, 0x41, 0x4d, 0x52, 0x0a};


public byte[] convert3gpDataToAmr(byte[] data) {
if (data == null) {
return null;
}

ByteArrayInputStream bis = new ByteArrayInputStream(data);
// read FileTypeHeader
FileTypeBox ftypHeader = new FileTypeBox(bis);
// You can check if it is correct here
// read MediaDataHeader
MediaDataBox mdatHeader = new MediaDataBox(bis);
// You can check if it is correct here
int rawAmrDataLength = mdatHeader.getDataLength();
int fullAmrDataLength = AMR_MAGIC_HEADER.length + rawAmrDataLength;
byte[] amrData = new byte[fullAmrDataLength];
System.arraycopy(AMR_MAGIC_HEADER, 0, amrData, 0, AMR_MAGIC_HEADER.length);
bis.read(amrData, AMR_MAGIC_HEADER.length, rawAmrDataLength);
return amrData;
}

另请注意,AMR-WB 的准确度略低,因此您可能需要考虑使用更详细的 API 而非代号的原始音频捕获。

关于speech-recognition - 如何使用 Codename One 中的 Google Speech API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39433368/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com