microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 12.1 kB
Source Map (JSON)
{"version":3,"sources":["src/sdk/Audio/AudioOutputStream.ts"],"names":[],"mappings":"AAKA,OAAO,EAEH,iBAAiB,EAGpB,MAAM,yBAAyB,CAAC;AAEjC,OAAO,EACH,iBAAiB,EACjB,6BAA6B,EAChC,MAAM,eAAe,CAAC;AAGvB;;;GAGG;AACH,8BAAsB,iBAAiB;IAEnC;;;OAGG;IACH,SAAS;IAIT;;;;OAIG;IACH,aAAoB,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAAE;IAEtD;;;;;;OAMG;WACW,gBAAgB,IAAI,qBAAqB;IAIvD;;;;;OAKG;aACa,KAAK,IAAI,IAAI;CAChC;AAED;;;GAGG;AACH,8BAAsB,qBAAsB,SAAQ,iBAAiB;IAEjE;;;;;;OAMG;WACW,MAAM,IAAI,qBAAqB;IAI7C;;;;;;;OAOG;aACa,IAAI,CAAC,UAAU,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;IAE9D;;;;;OAKG;aACa,KAAK,IAAI,IAAI;CAChC;AAED;;;;GAIG;AACH,qBAAa,yBAA0B,SAAQ,qBAAsB,YAAW,iBAAiB;IAC7F,OAAO,CAAC,UAAU,CAAwB;IAC1C,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,UAAU,CAAsB;IACxC,OAAO,CAAC,iBAAiB,CAAY;IAErC;;;OAGG;;IAOH;;;OAGG;IACH,IAAW,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAK1C;IAED;;OAEG;IACH,IAAW,MAAM,IAAI,iBAAiB,CAErC;IAED;;;;;OAKG;IACH,IAAW,QAAQ,IAAI,OAAO,CAE7B;IAED;;;;;OAKG;IACI,EAAE,IAAI,MAAM;IAInB;;;;;;;OAOG;IACU,IAAI,CAAC,UAAU,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;IAmC3D;;;;;;OAMG;IACI,KAAK,CAAC,UAAU,EAAE,WAAW,GAAG,IAAI;IAS3C;;;;;OAKG;IACI,KAAK,IAAI,IAAI;CAGvB;AAMD,8BAAsB,qBAAsB,SAAQ,iBAAiB;IACjE;;;OAGG;IACH,SAAS;IAIT;;;;;;;;;OASG;WACW,MAAM,CAAC,QAAQ,EAAE,6BAA6B,GAAG,qBAAqB;IAIpF;;;;;OAKG;aACa,KAAK,IAAI,IAAI;CAEhC;AAED;;;;GAIG;AACH,qBAAa,yBAA0B,SAAQ,qBAAsB,YAAW,iBAAiB;IAC7F,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;IAChC,OAAO,CAAC,YAAY,CAAgC;IAEpD;;;;;;OAMG;gBACgB,QAAQ,EAAE,6BAA6B;IAO1D,IAAW,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAAK;IAEzC,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IAMhC,KAAK,IAAI,IAAI;IAMb,EAAE,IAAI,MAAM;CAGtB","file":"AudioOutputStream.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\n/* eslint-disable max-classes-per-file */\r\n\r\nimport {\r\n createNoDashGuid,\r\n IAudioDestination,\r\n IStreamChunk,\r\n Stream,\r\n} from \"../../common/Exports.js\";\r\nimport { Contracts } from \"../Contracts.js\";\r\nimport {\r\n AudioStreamFormat,\r\n PushAudioOutputStreamCallback\r\n} from \"../Exports.js\";\r\nimport { AudioOutputFormatImpl } from \"./AudioOutputFormat.js\";\r\n\r\n/**\r\n * Represents audio output stream used for custom audio output configurations.\r\n * @class AudioOutputStream\r\n */\r\nexport abstract class AudioOutputStream {\r\n\r\n /**\r\n * Creates and initializes an instance.\r\n * @constructor\r\n */\r\n protected constructor() {\r\n return;\r\n }\r\n\r\n /**\r\n * Sets the format of the AudioOutputStream\r\n * Note: the format is set by the synthesizer before writing. Do not set it before passing it to AudioConfig\r\n * @member AudioOutputStream.prototype.format\r\n */\r\n public abstract set format(format: AudioStreamFormat);\r\n\r\n /**\r\n * Creates a memory backed PullAudioOutputStream with the specified audio format.\r\n * @member AudioOutputStream.createPullStream\r\n * @function\r\n * @public\r\n * @returns {PullAudioOutputStream} The audio output stream being created.\r\n */\r\n public static createPullStream(): PullAudioOutputStream {\r\n return PullAudioOutputStream.create();\r\n }\r\n\r\n /**\r\n * Explicitly frees any external resource attached to the object\r\n * @member AudioOutputStream.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public abstract close(): void;\r\n}\r\n\r\n/**\r\n * Represents memory backed push audio output stream used for custom audio output configurations.\r\n * @class PullAudioOutputStream\r\n */\r\nexport abstract class PullAudioOutputStream extends AudioOutputStream {\r\n\r\n /**\r\n * Creates a memory backed PullAudioOutputStream with the specified audio format.\r\n * @member PullAudioOutputStream.create\r\n * @function\r\n * @public\r\n * @returns {PullAudioOutputStream} The push audio output stream being created.\r\n */\r\n public static create(): PullAudioOutputStream {\r\n return new PullAudioOutputStreamImpl();\r\n }\r\n\r\n /**\r\n * Reads audio data from the internal buffer.\r\n * @member PullAudioOutputStream.prototype.read\r\n * @function\r\n * @public\r\n * @param {ArrayBuffer} dataBuffer - An ArrayBuffer to store the read data.\r\n * @returns {Promise<number>} Audio buffer length has been read.\r\n */\r\n public abstract read(dataBuffer: ArrayBuffer): Promise<number>;\r\n\r\n /**\r\n * Closes the stream.\r\n * @member PullAudioOutputStream.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public abstract close(): void;\r\n}\r\n\r\n/**\r\n * Represents memory backed push audio output stream used for custom audio output configurations.\r\n * @private\r\n * @class PullAudioOutputStreamImpl\r\n */\r\nexport class PullAudioOutputStreamImpl extends PullAudioOutputStream implements IAudioDestination {\r\n private privFormat: AudioOutputFormatImpl;\r\n private privId: string;\r\n private privStream: Stream<ArrayBuffer>;\r\n private privLastChunkView: Int8Array;\r\n\r\n /**\r\n * Creates and initializes an instance with the given values.\r\n * @constructor\r\n */\r\n public constructor() {\r\n super();\r\n this.privId = createNoDashGuid();\r\n this.privStream = new Stream<ArrayBuffer>();\r\n }\r\n\r\n /**\r\n * Sets the format information to the stream. For internal use only.\r\n * @param {AudioStreamFormat} format - the format to be set.\r\n */\r\n public set format(format: AudioStreamFormat) {\r\n if (format === undefined || format === null) {\r\n this.privFormat = AudioOutputFormatImpl.getDefaultOutputFormat();\r\n }\r\n this.privFormat = format as AudioOutputFormatImpl;\r\n }\r\n\r\n /**\r\n * Format information for the audio\r\n */\r\n public get format(): AudioStreamFormat {\r\n return this.privFormat;\r\n }\r\n\r\n /**\r\n * Checks if the stream is closed\r\n * @member PullAudioOutputStreamImpl.prototype.isClosed\r\n * @property\r\n * @public\r\n */\r\n public get isClosed(): boolean {\r\n return this.privStream.isClosed;\r\n }\r\n\r\n /**\r\n * Gets the id of the stream\r\n * @member PullAudioOutputStreamImpl.prototype.id\r\n * @property\r\n * @public\r\n */\r\n public id(): string {\r\n return this.privId;\r\n }\r\n\r\n /**\r\n * Reads audio data from the internal buffer.\r\n * @member PullAudioOutputStreamImpl.prototype.read\r\n * @function\r\n * @public\r\n * @param {ArrayBuffer} dataBuffer - An ArrayBuffer to store the read data.\r\n * @returns {Promise<number>} - Audio buffer length has been read.\r\n */\r\n public async read(dataBuffer: ArrayBuffer): Promise<number> {\r\n const intView: Int8Array = new Int8Array(dataBuffer);\r\n let totalBytes: number = 0;\r\n\r\n if (this.privLastChunkView !== undefined) {\r\n if (this.privLastChunkView.length > dataBuffer.byteLength) {\r\n intView.set(this.privLastChunkView.slice(0, dataBuffer.byteLength));\r\n this.privLastChunkView = this.privLastChunkView.slice(dataBuffer.byteLength);\r\n return Promise.resolve(dataBuffer.byteLength);\r\n }\r\n intView.set(this.privLastChunkView);\r\n totalBytes = this.privLastChunkView.length;\r\n this.privLastChunkView = undefined;\r\n }\r\n\r\n // Until we have the minimum number of bytes to send in a transmission, keep asking for more.\r\n while (totalBytes < dataBuffer.byteLength && !this.privStream.isReadEnded) {\r\n const chunk: IStreamChunk<ArrayBuffer> = await this.privStream.read();\r\n if (chunk !== undefined && !chunk.isEnd) {\r\n let tmpBuffer: ArrayBuffer;\r\n if (chunk.buffer.byteLength > dataBuffer.byteLength - totalBytes) {\r\n tmpBuffer = chunk.buffer.slice(0, dataBuffer.byteLength - totalBytes);\r\n this.privLastChunkView = new Int8Array(chunk.buffer.slice(dataBuffer.byteLength - totalBytes));\r\n } else {\r\n tmpBuffer = chunk.buffer;\r\n }\r\n intView.set(new Int8Array(tmpBuffer), totalBytes);\r\n totalBytes += tmpBuffer.byteLength;\r\n } else {\r\n this.privStream.readEnded();\r\n }\r\n }\r\n return totalBytes;\r\n }\r\n\r\n /**\r\n * Writes the audio data specified by making an internal copy of the data.\r\n * @member PullAudioOutputStreamImpl.prototype.write\r\n * @function\r\n * @public\r\n * @param {ArrayBuffer} dataBuffer - The audio buffer of which this function will make a copy.\r\n */\r\n public write(dataBuffer: ArrayBuffer): void {\r\n Contracts.throwIfNullOrUndefined(this.privStream, \"must set format before writing\");\r\n this.privStream.writeStreamChunk({\r\n buffer: dataBuffer,\r\n isEnd: false,\r\n timeReceived: Date.now()\r\n });\r\n }\r\n\r\n /**\r\n * Closes the stream.\r\n * @member PullAudioOutputStreamImpl.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public close(): void {\r\n this.privStream.close();\r\n }\r\n}\r\n\r\n/*\r\n * Represents audio output stream used for custom audio output configurations.\r\n * @class PushAudioOutputStream\r\n */\r\nexport abstract class PushAudioOutputStream extends AudioOutputStream {\r\n /**\r\n * Creates and initializes and instance.\r\n * @constructor\r\n */\r\n protected constructor() {\r\n super();\r\n }\r\n\r\n /**\r\n * Creates a PushAudioOutputStream that delegates to the specified callback interface for\r\n * write() and close() methods.\r\n * @member PushAudioOutputStream.create\r\n * @function\r\n * @public\r\n * @param {PushAudioOutputStreamCallback} callback - The custom audio output object,\r\n * derived from PushAudioOutputStreamCallback\r\n * @returns {PushAudioOutputStream} The push audio output stream being created.\r\n */\r\n public static create(callback: PushAudioOutputStreamCallback): PushAudioOutputStream {\r\n return new PushAudioOutputStreamImpl(callback);\r\n }\r\n\r\n /**\r\n * Explicitly frees any external resource attached to the object\r\n * @member PushAudioOutputStream.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public abstract close(): void;\r\n\r\n}\r\n\r\n/**\r\n * Represents audio output stream used for custom audio output configurations.\r\n * @private\r\n * @class PushAudioOutputStreamImpl\r\n */\r\nexport class PushAudioOutputStreamImpl extends PushAudioOutputStream implements IAudioDestination {\r\n private readonly privId: string;\r\n private privCallback: PushAudioOutputStreamCallback;\r\n\r\n /**\r\n * Creates a PushAudioOutputStream that delegates to the specified callback interface for\r\n * read() and close() methods.\r\n * @constructor\r\n * @param {PushAudioOutputStreamCallback} callback - The custom audio output object,\r\n * derived from PushAudioOutputStreamCallback\r\n */\r\n public constructor(callback: PushAudioOutputStreamCallback) {\r\n super();\r\n this.privId = createNoDashGuid();\r\n this.privCallback = callback;\r\n }\r\n\r\n // eslint-disable-next-line @typescript-eslint/no-empty-function\r\n public set format(format: AudioStreamFormat) { }\r\n\r\n public write(buffer: ArrayBuffer): void {\r\n if (!!this.privCallback.write) {\r\n this.privCallback.write(buffer);\r\n }\r\n }\r\n\r\n public close(): void {\r\n if (!!this.privCallback.close) {\r\n this.privCallback.close();\r\n }\r\n }\r\n\r\n public id(): string {\r\n return this.privId;\r\n }\r\n}\r\n"]}