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AGvB;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.\n// Licensed under the MIT license.\n\n/* eslint-disable max-classes-per-file */\n\nimport {\n    createNoDashGuid,\n    IAudioDestination,\n    IStreamChunk,\n    Stream,\n} from \"../../common/Exports.js\";\nimport { Contracts } from \"../Contracts.js\";\nimport {\n    AudioStreamFormat,\n    PushAudioOutputStreamCallback\n} from \"../Exports.js\";\nimport { AudioOutputFormatImpl } from \"./AudioOutputFormat.js\";\n\n/**\n * Represents audio output stream used for custom audio output configurations.\n * @class AudioOutputStream\n */\nexport abstract class AudioOutputStream {\n\n    /**\n     * Creates and initializes an instance.\n     * @constructor\n     */\n    protected constructor() {\n        return;\n    }\n\n    /**\n     * Sets the format of the AudioOutputStream\n     * Note: the format is set by the synthesizer before writing. Do not set it before passing it to AudioConfig\n     * @member AudioOutputStream.prototype.format\n     */\n    public abstract set format(format: AudioStreamFormat);\n\n    /**\n     * Creates a memory backed PullAudioOutputStream with the specified audio format.\n     * @member AudioOutputStream.createPullStream\n     * @function\n     * @public\n     * @returns {PullAudioOutputStream} The audio output stream being created.\n     */\n    public static createPullStream(): PullAudioOutputStream {\n        return PullAudioOutputStream.create();\n    }\n\n    /**\n     * Explicitly frees any external resource attached to the object\n     * @member AudioOutputStream.prototype.close\n     * @function\n     * @public\n     */\n    public abstract close(): void;\n}\n\n/**\n * Represents memory backed push audio output stream used for custom audio output configurations.\n * @class PullAudioOutputStream\n */\nexport abstract class PullAudioOutputStream extends AudioOutputStream {\n\n    /**\n     * Creates a memory backed PullAudioOutputStream with the specified audio format.\n     * @member PullAudioOutputStream.create\n     * @function\n     * @public\n     * @returns {PullAudioOutputStream} The push audio output stream being created.\n     */\n    public static create(): PullAudioOutputStream {\n        return new PullAudioOutputStreamImpl();\n    }\n\n    /**\n     * Reads audio data from the internal buffer.\n     * @member PullAudioOutputStream.prototype.read\n     * @function\n     * @public\n     * @param {ArrayBuffer} dataBuffer - An ArrayBuffer to store the read data.\n     * @returns {Promise<number>} Audio buffer length has been read.\n     */\n    public abstract read(dataBuffer: ArrayBuffer): Promise<number>;\n\n    /**\n     * Closes the stream.\n     * @member PullAudioOutputStream.prototype.close\n     * @function\n     * @public\n     */\n    public abstract close(): void;\n}\n\n/**\n * Represents memory backed push audio output stream used for custom audio output configurations.\n * @private\n * @class PullAudioOutputStreamImpl\n */\nexport class PullAudioOutputStreamImpl extends PullAudioOutputStream implements IAudioDestination {\n    private privFormat: AudioOutputFormatImpl;\n    private privId: string;\n    private privStream: Stream<ArrayBuffer>;\n    private privLastChunkView: Int8Array;\n\n    /**\n     * Creates and initializes an instance with the given values.\n     * @constructor\n     */\n    public constructor() {\n        super();\n        this.privId = createNoDashGuid();\n        this.privStream = new Stream<ArrayBuffer>();\n    }\n\n    /**\n     * Sets the format information to the stream. For internal use only.\n     * @param {AudioStreamFormat} format - the format to be set.\n     */\n    public set format(format: AudioStreamFormat) {\n        if (format === undefined || format === null) {\n            this.privFormat = AudioOutputFormatImpl.getDefaultOutputFormat();\n        }\n        this.privFormat = format as AudioOutputFormatImpl;\n    }\n\n    /**\n     * Format information for the audio\n     */\n    public get format(): AudioStreamFormat {\n        return this.privFormat;\n    }\n\n    /**\n     * Checks if the stream is closed\n     * @member PullAudioOutputStreamImpl.prototype.isClosed\n     * @property\n     * @public\n     */\n    public get isClosed(): boolean {\n        return this.privStream.isClosed;\n    }\n\n    /**\n     * Gets the id of the stream\n     * @member PullAudioOutputStreamImpl.prototype.id\n     * @property\n     * @public\n     */\n    public id(): string {\n        return this.privId;\n    }\n\n    /**\n     * Reads audio data from the internal buffer.\n     * @member PullAudioOutputStreamImpl.prototype.read\n     * @function\n     * @public\n     * @param {ArrayBuffer} dataBuffer - An ArrayBuffer to store the read data.\n     * @returns {Promise<number>} - Audio buffer length has been read.\n     */\n    public async read(dataBuffer: ArrayBuffer): Promise<number> {\n        const intView: Int8Array = new Int8Array(dataBuffer);\n        let totalBytes: number = 0;\n\n        if (this.privLastChunkView !== undefined) {\n            if (this.privLastChunkView.length > dataBuffer.byteLength) {\n                intView.set(this.privLastChunkView.slice(0, dataBuffer.byteLength));\n                this.privLastChunkView = this.privLastChunkView.slice(dataBuffer.byteLength);\n                return Promise.resolve(dataBuffer.byteLength);\n            }\n            intView.set(this.privLastChunkView);\n            totalBytes = this.privLastChunkView.length;\n            this.privLastChunkView = undefined;\n        }\n\n        // Until we have the minimum number of bytes to send in a transmission, keep asking for more.\n        while (totalBytes < dataBuffer.byteLength && !this.privStream.isReadEnded) {\n            const chunk: IStreamChunk<ArrayBuffer> = await this.privStream.read();\n            if (chunk !== undefined && !chunk.isEnd) {\n                let tmpBuffer: ArrayBuffer;\n                if (chunk.buffer.byteLength > dataBuffer.byteLength - totalBytes) {\n                    tmpBuffer = chunk.buffer.slice(0, dataBuffer.byteLength - totalBytes);\n                    this.privLastChunkView = new Int8Array(chunk.buffer.slice(dataBuffer.byteLength - totalBytes));\n                } else {\n                    tmpBuffer = chunk.buffer;\n                }\n                intView.set(new Int8Array(tmpBuffer), totalBytes);\n                totalBytes += tmpBuffer.byteLength;\n            } else {\n                this.privStream.readEnded();\n            }\n        }\n        return totalBytes;\n    }\n\n    /**\n     * Writes the audio data specified by making an internal copy of the data.\n     * @member PullAudioOutputStreamImpl.prototype.write\n     * @function\n     * @public\n     * @param {ArrayBuffer} dataBuffer - The audio buffer of which this function will make a copy.\n     */\n    public write(dataBuffer: ArrayBuffer): void {\n        Contracts.throwIfNullOrUndefined(this.privStream, \"must set format before writing\");\n        this.privStream.writeStreamChunk({\n            buffer: dataBuffer,\n            isEnd: false,\n            timeReceived: Date.now()\n        });\n    }\n\n    /**\n     * Closes the stream.\n     * @member PullAudioOutputStreamImpl.prototype.close\n     * @function\n     * @public\n     */\n    public close(): void {\n        this.privStream.close();\n    }\n}\n\n/*\n * Represents audio output stream used for custom audio output configurations.\n * @class PushAudioOutputStream\n */\nexport abstract class PushAudioOutputStream extends AudioOutputStream {\n    /**\n     * Creates and initializes and instance.\n     * @constructor\n     */\n    protected constructor() {\n        super();\n    }\n\n    /**\n     * Creates a PushAudioOutputStream that delegates to the specified callback interface for\n     * write() and close() methods.\n     * @member PushAudioOutputStream.create\n     * @function\n     * @public\n     * @param {PushAudioOutputStreamCallback} callback - The custom audio output object,\n     * derived from PushAudioOutputStreamCallback\n     * @returns {PushAudioOutputStream} The push audio output stream being created.\n     */\n    public static create(callback: PushAudioOutputStreamCallback): PushAudioOutputStream {\n        return new PushAudioOutputStreamImpl(callback);\n    }\n\n    /**\n     * Explicitly frees any external resource attached to the object\n     * @member PushAudioOutputStream.prototype.close\n     * @function\n     * @public\n     */\n    public abstract close(): void;\n\n}\n\n/**\n * Represents audio output stream used for custom audio output configurations.\n * @private\n * @class PushAudioOutputStreamImpl\n */\nexport class PushAudioOutputStreamImpl extends PushAudioOutputStream implements IAudioDestination {\n    private readonly privId: string;\n    private privCallback: PushAudioOutputStreamCallback;\n\n    /**\n     * Creates a PushAudioOutputStream that delegates to the specified callback interface for\n     * read() and close() methods.\n     * @constructor\n     * @param {PushAudioOutputStreamCallback} callback - The custom audio output object,\n     * derived from PushAudioOutputStreamCallback\n     */\n    public constructor(callback: PushAudioOutputStreamCallback) {\n        super();\n        this.privId = createNoDashGuid();\n        this.privCallback = callback;\n    }\n\n    // eslint-disable-next-line @typescript-eslint/no-empty-function\n    public set format(format: AudioStreamFormat) { }\n\n    public write(buffer: ArrayBuffer): void {\n        if (!!this.privCallback.write) {\n            this.privCallback.write(buffer);\n        }\n    }\n\n    public close(): void {\n        if (!!this.privCallback.close) {\n            this.privCallback.close();\n        }\n    }\n\n    public id(): string {\n        return this.privId;\n    }\n}\n"]}