【问题标题】:How to access the audio stream recorded by Microsoft Speech SDK如何访问 Microsoft Speech SDK 录制的音频流
【发布时间】:2020-04-04 10:34:39
【问题描述】:

我正在使用 Microsoft 的 Speech SDK for JavaScript 转录麦克风流。录制和转录都是使用语音 SDK 完成的,我无法找到在录制完成后如何访问和保存录制的音频文件的方法。

用于创建记录器和记录的代码

recognizer = new SpeechSDK.SpeechRecognizer(speechConfig, audioConfig);
// to start the recording
recognizer.startContinuousRecognitionAsync(
    () => {
      portFromCS.postMessage({ type: "started", data: "" });
    },
    err => {
      recognizer.close();
    },
  );
// used after user input to stop the recording
recognizer.stopContinuousRecognitionAsync(
    () => {
      window.console.log("successfully stopped");
      // TODO: somehow need to save the file
    },
    err => {
      window.console.log("error on stop", err);
    },
  );

documentation 相当糟糕,我无法找到如何使用他们的 SDK 访问原始音频的内置方法。我唯一的选择是使用两个音频流进行录制并使用单独的录制流保存文件吗?这意味着什么?

【问题讨论】:

    标签: javascript audio-recording speech-to-text microsoft-cognitive


    【解决方案1】:

    SDK 不保存音频,也没有内置功能。

    在 1.11.0 版中,向连接对象添加了一个新的 API,让您可以查看发送到服务的消息,您可以从中提取音频并自己组装波形文件。

    这里有一些打字稿可以做到这一点:

    import * as SpeechSdk from "microsoft-cognitiveservices-speech-sdk";
    import * as fs from "fs";
    
    const filename: string = "input.wav";
    const outputFileName: string = "out.wav";
    const subscriptionKey: string = "<SUBSCRIPTION_KEY>";
    const region: string = "<SUBSCRIPTION_REGION>";
    
    const speechConfig: SpeechSdk.SpeechConfig = SpeechSdk.SpeechConfig.fromSubscription(subscriptionKey, region);
    
    // Load the audio from a file, alternately you could use 
    // const audioConfig:SpeechSdk.AudioConfig = SpeechSdk.AudioConfig.fromDefaultMicrophone() in a browser();
    const fileContents: Buffer = fs.readFileSync(filename);
    const inputStream: SpeechSdk.PushAudioInputStream = SpeechSdk.AudioInputStream.createPushStream();
    const audioConfig: SpeechSdk.AudioConfig = SpeechSdk.AudioConfig.fromStreamInput(inputStream);
    inputStream.write(fileContents);
    inputStream.close();
    
    const r: SpeechSdk.SpeechRecognizer = new SpeechSdk.SpeechRecognizer(speechConfig, audioConfig);
    const con: SpeechSdk.Connection = SpeechSdk.Connection.fromRecognizer(r);
    
    let wavFragmentCount: number = 0;
    
    const wavFragments: { [id: number]: ArrayBuffer; } = {};
    
    con.messageSent = (args: SpeechSdk.ConnectionMessageEventArgs): void => {
        // Only record outbound audio mesages that have data in them.
        if (args.message.path === "audio" && args.message.isBinaryMessage && args.message.binaryMessage !== null) {
            wavFragments[wavFragmentCount++] = args.message.binaryMessage;
        }
    };
    
    r.recognizeOnceAsync((result: SpeechSdk.SpeechRecognitionResult) => {
        // Find the length of the audio sent.
        let byteCount: number = 0;
        for (let i: number = 0; i < wavFragmentCount; i++) {
            byteCount += wavFragments[i].byteLength;
        }
    
        // Output array.
        const sentAudio: Uint8Array = new Uint8Array(byteCount);
    
        byteCount = 0;
        for (let i: number = 0; i < wavFragmentCount; i++) {
            sentAudio.set(new Uint8Array(wavFragments[i]), byteCount);
            byteCount += wavFragments[i].byteLength;
        }
    
        // Set the file size in the wave header:
        const view = new DataView(sentAudio.buffer);
        view.setUint32(4, byteCount, true);
        view.setUint32(40, byteCount, true);
    
        // Write the audio back to disk.
        fs.writeFileSync(outputFileName, sentAudio);
        r.close();
    });
    

    它从一个文件中加载,所以我可以在 NodeJS 而不是浏览器中进行测试,但核心部分是相同的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-19
      • 2020-05-18
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多