Reputation: 129
I am transcribing microphone stream with Microsoft's Speech SDK for JavaScript. Both the recording and the transcription is done using the Speech SDK and I have not been able to find a way how to access and save the recorded audio file once the recording has finished.
Code for creating the recorder and recording
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);
},
);
The documentation is fairly bad and I was not able to find a built-in way how to access the raw audio using their SDK. Is my only option to have two audio streams for recording and save the file using a separate recording stream? What are the implications of that?
Upvotes: 2
Views: 1667
Reputation: 81
The SDK doesn't save the audio, nor have functionality to do so built in.
In version 1.11.0 a new API was added to the connection object to allow you to see the messages sent to the service, from that you can pull the audio out and assemble a wave file yourself.
Here's some typescript that does this:
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();
});
It loads from a file so I could test in NodeJS instead of a browser, but the core part's the same.
Upvotes: 1