Azure Speech SDK 转录音频问题

问题描述 投票:0回答:1

我正在尝试使用 Azure 语音转文本 SDK 添加另一种将语音转录为文本的方法到此 npm lib

https://github.com/Rei-x/discord-speech-recognition
。我尝试使用 api 但也得到了相同的结果。这是我的代码以及它的结果

const sdk = require("microsoft-cognitiveservices-speech-sdk");

function getAzureRequestOptions(options) {
    const speechConfig = sdk.SpeechConfig.fromSubscription(options.key, options.region);
    if (options.lang) {
        speechConfig.speechRecognitionLanguage = options.lang;
    }
    return speechConfig;
}

function resolveSpeechWithAzureSpeechToText(audioBuffer, options) {
    console.log('resolveSpeechWithAzureSpeechToText called with audioBuffer length:', audioBuffer.length);
    const pushStream = sdk.AudioInputStream.createPushStream();

    pushStream.write(audioBuffer);
    pushStream.close();

    const speechConfig = getAzureRequestOptions(options);
    const audioConfig = sdk.AudioConfig.fromStreamInput(pushStream);
    const recognizer = new sdk.SpeechRecognizer(speechConfig, audioConfig);

    return new Promise((resolve, reject) => {
        console.log('Starting recognition...');
        recognizer.recognizeOnceAsync(
            (result) => {
                console.log('Recognition succeeded');
                recognizer.close();
                resolve(result.text);
                console.log(result);
            },
            (err) => {
                console.log('Recognition failed');
                recognizer.close();
                reject(err);
                console.log(err);
            }
        );
    });
}

module.exports.resolveSpeechWithAzureSpeechToText = resolveSpeechWithAzureSpeechToText;

结果,我只得到音频缓冲区的长度,没有文本。

控制台视图:

enter image description here

node.js azure discord azure-cognitive-services speech-to-text
1个回答
0
投票

为了记录,问题在于音频缓冲区的格式(PCM 单声道,48khz)。

根据文档,默认音频流格式为 WAV(16 kHz 或 8 kHz、16 位和单声道 PCM)

问题可能是之前的转换尝试可能未正确实现或针对不同的格式。

因此,要解决此问题,您可以使用 wave-resamper packageffmpeg 将输入音频转换为支持的格式。

const waveResampler = require('wave-resampler');
// resample 48kHz samples to 16kHz
const resampledAudio = waveResampler.resample(inputAudio, 48000, 16000);
© www.soinside.com 2019 - 2024. All rights reserved.