@voice-ping/cognitive-services-speech
Version:
VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft
1 lines • 2.85 kB
Source Map (JSON)
{"version":3,"sources":["src/sdk/Audio/AudioFileWriter.ts"],"names":[],"mappings":";AAGA,OAAO,KAAK,EAAE,MAAM,IAAI,CAAC;AACzB,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AAEzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AAG/C,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;IAIxC,IAAW,MAAM,CAAC,MAAM,EAAE,iBAAiB,EAU1C;IAEM,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI;IAOhC,KAAK,IAAI,IAAI;IAkBb,EAAE,eAER;CACJ","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\";\nimport { Contracts } from \"../Contracts\";\nimport { AudioStreamFormat } from \"../Exports\";\nimport { AudioOutputFormatImpl } from \"./AudioOutputFormat\";\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 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\", () => {\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"]}