{"version":3,"sources":["src/sdk/Audio/AudioFileWriter.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAGlD,qBAAa,eAAgB,YAAW,iBAAiB;IACrD,OAAO,CAAC,eAAe,CAAwB;IAC/C,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,eAAe,CAAiB;gBAErB,QAAQ,EAAE,EAAE,CAAC,QAAQ;IAKxC,IAAW,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAU1C;IAEM,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IAOhC,KAAK,IAAI,IAAI;IAkBb,EAAE,IAAI,MAAM;CAGtB","file":"AudioFileWriter.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license.\n\nimport * as fs from \"fs\";\nimport { IAudioDestination } from \"../../common/Exports.js\";\nimport { Contracts } from \"../Contracts.js\";\nimport { AudioStreamFormat } from \"../Exports.js\";\nimport { AudioOutputFormatImpl } from \"./AudioOutputFormat.js\";\n\nexport class AudioFileWriter implements IAudioDestination {\n    private privAudioFormat: AudioOutputFormatImpl;\n    private privFd: number;\n    private privId: string;\n    private privWriteStream: fs.WriteStream;\n\n    public constructor(filename: fs.PathLike) {\n        Contracts.throwIfNullOrUndefined(fs.openSync, \"\\nFile System access not available, please use Push or PullAudioOutputStream\");\n        this.privFd = fs.openSync(filename, \"w\");\n    }\n\n    public set format(format: AudioStreamFormat) {\n        Contracts.throwIfNotUndefined(this.privAudioFormat, \"format is already set\");\n        this.privAudioFormat = format as AudioOutputFormatImpl;\n        let headerOffset: number = 0;\n        if (this.privAudioFormat.hasHeader) {\n            headerOffset = this.privAudioFormat.header.byteLength;\n        }\n        if (this.privFd !== undefined) {\n            this.privWriteStream = fs.createWriteStream(\"\", {fd: this.privFd, start: headerOffset, autoClose: false});\n        }\n    }\n\n    public write(buffer: ArrayBuffer): void {\n        Contracts.throwIfNullOrUndefined(this.privAudioFormat, \"must set format before writing.\");\n        if (this.privWriteStream !== undefined) {\n            this.privWriteStream.write(new Uint8Array(buffer.slice(0)));\n        }\n    }\n\n    public close(): void {\n        if (this.privFd !== undefined) {\n            this.privWriteStream.on(\"finish\", (): void => {\n                if (this.privAudioFormat.hasHeader) {\n                    this.privAudioFormat.updateHeader(this.privWriteStream.bytesWritten);\n                    fs.writeSync(this.privFd,\n                        new Int8Array(this.privAudioFormat.header),\n                        0,\n                        this.privAudioFormat.header.byteLength,\n                        0);\n                }\n                fs.closeSync(this.privFd);\n                this.privFd = undefined;\n            });\n            this.privWriteStream.end();\n        }\n    }\n\n    public id(): string {\n        return this.privId;\n    }\n}\n"]}