如何访问Microsoft Speech SDK记录的音频流

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

我正在使用Microsoft的Speech SDK for JavaScript转录麦克风流。录制和转录都是使用Speech 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个回答
0
投票

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

在版本1.11.0中,向连接对象添加了新的API,使您可以查看发送到服务的消息,从中您可以拉出音频并自己组装wave文件。

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

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而不是浏览器中进行测试,但是核心部分是相同的。

© www.soinside.com 2019 - 2024. All rights reserved.