@euirim/microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
136 lines (134 loc) • 4.43 kB
JavaScript
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
import { createNoDashGuid } from "../../../src/common/Guid";
import { ChunkedArrayBufferStream, PromiseHelper, } from "../../common/Exports";
import { AudioStreamFormatImpl } from "./AudioStreamFormat";
export const bufferSize = 4096;
/**
* Represents audio input stream used for custom audio input configurations.
* @class AudioInputStream
*/
export class AudioOutputStream {
/**
* Creates and initializes an instance.
* @constructor
*/
constructor() { }
/**
* Creates a memory backed PullAudioOutputStream with the specified audio format.
* @member AudioInputStream.createPullStream
* @function
* @public
* @param {AudioStreamFormat} format - The audio data format in which audio will be
* written to the push audio stream's write() method (currently only support 16 kHz 16bit mono PCM).
* @returns {PullAudioOutputStream} The audio input stream being created.
*/
static createPullStream(format) {
return PullAudioOutputStream.create(format);
}
}
/**
* Represents memory backed push audio input stream used for custom audio input configurations.
* @class PullAudioOutputStream
*/
// tslint:disable-next-line:max-classes-per-file
export class PullAudioOutputStream extends AudioOutputStream {
/**
* Creates a memory backed PullAudioOutputStream with the specified audio format.
* @member PullAudioOutputStream.create
* @function
* @public
* @param {AudioStreamFormat} format - The audio data format in which audio will be written to the
* push audio stream's write() method (currently only support 16 kHz 16bit mono PCM).
* @returns {PullAudioOutputStream} The push audio input stream being created.
*/
static create(format) {
return new PullAudioOutputStreamImpl(bufferSize, format);
}
}
/**
* Represents memory backed push audio input stream used for custom audio input configurations.
* @private
* @class PullAudioOutputStreamImpl
*/
// tslint:disable-next-line:max-classes-per-file
export class PullAudioOutputStreamImpl extends PullAudioOutputStream {
/**
* Creates and initalizes an instance with the given values.
* @constructor
* @param {AudioStreamFormat} format - The audio stream format.
*/
constructor(chunkSize, format) {
super();
if (format === undefined) {
this.privFormat = AudioStreamFormatImpl.getDefaultInputFormat();
}
else {
this.privFormat = format;
}
this.privId = createNoDashGuid();
this.privStream = new ChunkedArrayBufferStream(chunkSize);
this.streamReader = this.privStream.getReader();
}
/**
* Format information for the audio
*/
get format() {
return this.privFormat;
}
/**
* Checks if the stream is closed
* @member PullAudioOutputStreamImpl.prototype.isClosed
* @property
* @public
*/
get isClosed() {
return this.privStream.isClosed;
}
/**
* Gets the id of the stream
* @member PullAudioOutputStreamImpl.prototype.id
* @property
* @public
*/
get id() {
return this.privId;
}
/**
* Reads data from the buffer
* @member PullAudioOutputStreamImpl.prototype.read
* @function
* @public
* @param {ArrayBuffer} dataBuffer - The audio buffer of which this function will make a copy.
*/
read() {
return this.streamReader.read()
.onSuccessContinueWithPromise((chunk) => {
return PromiseHelper.fromResult(chunk.buffer);
});
}
/**
* Writes the audio data specified by making an internal copy of the data.
* @member PullAudioOutputStreamImpl.prototype.write
* @function
* @public
* @param {ArrayBuffer} dataBuffer - The audio buffer of which this function will make a copy.
*/
write(dataBuffer) {
this.privStream.writeStreamChunk({
buffer: dataBuffer,
isEnd: false,
timeReceived: Date.now()
});
}
/**
* Closes the stream.
* @member PullAudioOutputStreamImpl.prototype.close
* @function
* @public
*/
close() {
this.privStream.close();
}
}
//# sourceMappingURL=AudioOutputStream.js.map