{"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.\r\n// Licensed under the MIT license.\r\n\r\nimport * as fs from \"fs\";\r\nimport { IAudioDestination } from \"../../common/Exports.js\";\r\nimport { Contracts } from \"../Contracts.js\";\r\nimport { AudioStreamFormat } from \"../Exports.js\";\r\nimport { AudioOutputFormatImpl } from \"./AudioOutputFormat.js\";\r\n\r\nexport class AudioFileWriter implements IAudioDestination {\r\n    private privAudioFormat: AudioOutputFormatImpl;\r\n    private privFd: number;\r\n    private privId: string;\r\n    private privWriteStream: fs.WriteStream;\r\n\r\n    public constructor(filename: fs.PathLike) {\r\n        Contracts.throwIfNullOrUndefined(fs.openSync, \"\\nFile System access not available, please use Push or PullAudioOutputStream\");\r\n        this.privFd = fs.openSync(filename, \"w\");\r\n    }\r\n\r\n    public set format(format: AudioStreamFormat) {\r\n        Contracts.throwIfNotUndefined(this.privAudioFormat, \"format is already set\");\r\n        this.privAudioFormat = format as AudioOutputFormatImpl;\r\n        let headerOffset: number = 0;\r\n        if (this.privAudioFormat.hasHeader) {\r\n            headerOffset = this.privAudioFormat.header.byteLength;\r\n        }\r\n        if (this.privFd !== undefined) {\r\n            this.privWriteStream = fs.createWriteStream(\"\", {fd: this.privFd, start: headerOffset, autoClose: false});\r\n        }\r\n    }\r\n\r\n    public write(buffer: ArrayBuffer): void {\r\n        Contracts.throwIfNullOrUndefined(this.privAudioFormat, \"must set format before writing.\");\r\n        if (this.privWriteStream !== undefined) {\r\n            this.privWriteStream.write(new Uint8Array(buffer.slice(0)));\r\n        }\r\n    }\r\n\r\n    public close(): void {\r\n        if (this.privFd !== undefined) {\r\n            this.privWriteStream.on(\"finish\", (): void => {\r\n                if (this.privAudioFormat.hasHeader) {\r\n                    this.privAudioFormat.updateHeader(this.privWriteStream.bytesWritten);\r\n                    fs.writeSync(this.privFd,\r\n                        new Int8Array(this.privAudioFormat.header),\r\n                        0,\r\n                        this.privAudioFormat.header.byteLength,\r\n                        0);\r\n                }\r\n                fs.closeSync(this.privFd);\r\n                this.privFd = undefined;\r\n            });\r\n            this.privWriteStream.end();\r\n        }\r\n    }\r\n\r\n    public id(): string {\r\n        return this.privId;\r\n    }\r\n}\r\n"]}