Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于想实时获取录音的buffers文件进行语音转文本遇到的问题 #234

Open
chriscencen opened this issue Jul 29, 2024 · 2 comments

Comments

@chriscencen
Copy link

大佬,请教一下,我想实现实时对录制的音频进行语音转文本,sendAudioData方法是处理语音转文本的逻辑,但是现在试了Recorder.SampleData和开启takeoffEncodeChunk都无法拿到一片一片的buffers进行转写,只能一整句话一整句话的转是什么原因呢?以下为代码片段,麻烦帮忙看一下哈

`this.recorder = Recorder(
{
type: 'mp3', sampleRate: 16000, bitRate: 16, //mp3格式,指定采样率hz、比特率kbps,其他参数使用默认配置;注意:是数字的参数必须提供数字,不要用字符串;需要使用的type类型,需提前把格式支持文件加载进来,比如使用wav格式需要提前加载wav.js编码引
onProcess: function (buffers, powerLevel, bufferDuration, bufferSampleRate, newBufferIdx, asyncEnd) {
if (that.isRecording) {
// that._sendAudioData(new Uint8Array(buffers[buffers.length - 1].buffer)); // 这样可以录音,但是是一句话说完才能拿到音频进行转写,无法实时转写
let prevChunk = null;
const chunk = Recorder.SampleData(buffers, bufferSampleRate, bufferSampleRate, prevChunk);
const bufferData = chunk.data;
// that._sendAudioData(bufferData); // 这样无法实现
}
},
takeoffEncodeChunk: function (chunkBytes) {
that._sendAudioData(chunkBytes, false); // 这样无法实现
}
},

    );
    this.recorder.open(() => {
      console.log('打开录音成功');
    }, (msg, isUserNotAllow) => {//用户拒绝未授权或不支持
      Toast('无法录音:' + msg);
      console.log((isUserNotAllow ? 'UserNotAllow,' : '') + '无法录音:' + msg);
    });`
@xiangyuecn
Copy link
Owner

Recorder.SampleData(buffers, bufferSampleRate, 16000, prevChunk)
把buffers转成你需要的采样率,语音识别采样率一般是16000,第三个参数不要乱填

也可以用takeoffEncodeChunk,你必须提供你需要的格式type:"pcm",这样takeoffEncodeChunk里面得到的chunkBytes就是16000采样率的pcm二进制数据

@chriscencen
Copy link
Author

takeoffEncodeChunk因为在最终停止录音的时候拿不到完整的音频不太符合我的需求所以舍弃了这种方法,然后Recorder.SampleData我试了一下把第三个参数换成了16000,感觉可以比较实时的进行音频获取了,但是获取的buffers去用我们自己的语音转文本方法转出来不是我录的,比如我说的中文给我转出来一堆英文或者数字,我最终需要的是Uint8Array格式的buffers,转了格式还是不太行呢。
let prevChunk = null;
const chunk = Recorder.SampleData(buffers, bufferSampleRate, 16000, prevChunk);
const bufferData = chunk.data;
// that._sendAudioData(bufferData);
// that._sendAudioData(new Uint8Array(bufferData))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants