UNPKG

@voice-ping/cognitive-services-speech

Version:

VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft

1 lines 6.88 kB
{"version":3,"sources":["src/sdk/Audio/AudioStreamFormat.ts"],"names":[],"mappings":"AAGA;;;GAGG;AACH,8BAAsB,iBAAiB;IACnC;;;;;;;OAOG;WACW,qBAAqB,IAAI,iBAAiB;IAIxD;;;;;;;;;;OAUG;WACW,gBAAgB,CAAC,gBAAgB,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,iBAAiB;IAIpH;;;;;OAKG;aACa,KAAK,IAAI,IAAI;CAChC;AAED;;;GAGG;AAEH,qBAAa,qBAAsB,SAAQ,iBAAiB;IACxD,SAAS,CAAC,UAAU,EAAE,WAAW,CAAC;IAElC;;;;;;OAMG;gBACgB,aAAa,GAAE,MAAc,EAAE,aAAa,GAAE,MAAW,EAAE,QAAQ,GAAE,MAAU;IAwClG;;;;;;OAMG;WACW,qBAAqB,IAAI,qBAAqB;IAI5D;;;;;OAKG;IACI,KAAK,IAAI,IAAI;IAEpB;;;;;OAKG;IACI,SAAS,EAAE,MAAM,CAAC;IAEzB;;;;;OAKG;IACI,QAAQ,EAAE,MAAM,CAAC;IAExB;;;;;OAKG;IACI,aAAa,EAAE,MAAM,CAAC;IAE7B;;;;;OAKG;IACI,aAAa,EAAE,MAAM,CAAC;IAE7B;;;;;OAKG;IACI,cAAc,EAAE,MAAM,CAAC;IAE9B;;;;;OAKG;IACI,UAAU,EAAE,MAAM,CAAC;IAE1B,IAAW,MAAM,IAAI,WAAW,CAE/B;IAED,SAAS,CAAC,SAAS,wDAIlB;CACJ","file":"AudioStreamFormat.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\n// Licensed under the MIT license.\n\n/**\n * Represents audio stream format used for custom audio input configurations.\n * @class AudioStreamFormat\n */\nexport abstract class AudioStreamFormat {\n /**\n * Creates an audio stream format object representing the default audio stream\n * format (16KHz 16bit mono PCM).\n * @member AudioStreamFormat.getDefaultInputFormat\n * @function\n * @public\n * @returns {AudioStreamFormat} The audio stream format being created.\n */\n public static getDefaultInputFormat(): AudioStreamFormat {\n return AudioStreamFormatImpl.getDefaultInputFormat();\n }\n\n /**\n * Creates an audio stream format object with the specified pcm waveformat characteristics.\n * @member AudioStreamFormat.getWaveFormatPCM\n * @function\n * @public\n * @param {number} samplesPerSecond - Sample rate, in samples per second (Hertz).\n * @param {number} bitsPerSample - Bits per sample, typically 16.\n * @param {number} channels - Number of channels in the waveform-audio data. Monaural data\n * uses one channel and stereo data uses two channels.\n * @returns {AudioStreamFormat} The audio stream format being created.\n */\n public static getWaveFormatPCM(samplesPerSecond: number, bitsPerSample: number, channels: number): AudioStreamFormat {\n return new AudioStreamFormatImpl(samplesPerSecond, bitsPerSample, channels);\n }\n\n /**\n * Explicitly frees any external resource attached to the object\n * @member AudioStreamFormat.prototype.close\n * @function\n * @public\n */\n public abstract close(): void;\n}\n\n/**\n * @private\n * @class AudioStreamFormatImpl\n */\n// tslint:disable-next-line:max-classes-per-file\nexport class AudioStreamFormatImpl extends AudioStreamFormat {\n protected privHeader: ArrayBuffer;\n\n /**\n * Creates an instance with the given values.\n * @constructor\n * @param {number} samplesPerSec - Samples per second.\n * @param {number} bitsPerSample - Bits per sample.\n * @param {number} channels - Number of channels.\n */\n public constructor(samplesPerSec: number = 16000, bitsPerSample: number = 16, channels: number = 1) {\n super();\n this.formatTag = 1;\n this.bitsPerSample = bitsPerSample;\n this.samplesPerSec = samplesPerSec;\n this.channels = channels;\n this.avgBytesPerSec = this.samplesPerSec * this.channels * (this.bitsPerSample / 8);\n this.blockAlign = this.channels * Math.max(this.bitsPerSample, 8);\n\n this.privHeader = new ArrayBuffer(44);\n\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView\n const view = new DataView(this.privHeader);\n\n /* RIFF identifier */\n this.setString(view, 0, \"RIFF\");\n /* file length */\n view.setUint32(4, 0, true);\n /* RIFF type & Format */\n this.setString(view, 8, \"WAVEfmt \");\n /* format chunk length */\n view.setUint32(16, 16, true);\n /* sample format (raw) */\n view.setUint16(20, 1, true);\n /* channel count */\n view.setUint16(22, this.channels, true);\n /* sample rate */\n view.setUint32(24, this.samplesPerSec, true);\n /* byte rate (sample rate * block align) */\n view.setUint32(28, this.avgBytesPerSec, true);\n /* block align (channel count * bytes per sample) */\n view.setUint16(32, this.channels * (this.bitsPerSample / 8), true);\n /* bits per sample */\n view.setUint16(34, this.bitsPerSample, true);\n /* data chunk identifier */\n this.setString(view, 36, \"data\");\n /* data chunk length */\n view.setUint32(40, 0, true);\n }\n\n /**\n * Retrieves the default input format.\n * @member AudioStreamFormatImpl.getDefaultInputFormat\n * @function\n * @public\n * @returns {AudioStreamFormatImpl} The default input format.\n */\n public static getDefaultInputFormat(): AudioStreamFormatImpl {\n return new AudioStreamFormatImpl();\n }\n\n /**\n * Closes the configuration object.\n * @member AudioStreamFormatImpl.prototype.close\n * @function\n * @public\n */\n public close(): void { return; }\n\n /**\n * The format of the audio, valid values: 1 (PCM)\n * @member AudioStreamFormatImpl.prototype.formatTag\n * @function\n * @public\n */\n public formatTag: number;\n\n /**\n * The number of channels, valid values: 1 (Mono).\n * @member AudioStreamFormatImpl.prototype.channels\n * @function\n * @public\n */\n public channels: number;\n\n /**\n * The sample rate, valid values: 16000.\n * @member AudioStreamFormatImpl.prototype.samplesPerSec\n * @function\n * @public\n */\n public samplesPerSec: number;\n\n /**\n * The bits per sample, valid values: 16\n * @member AudioStreamFormatImpl.prototype.b\n * @function\n * @public\n */\n public bitsPerSample: number;\n\n /**\n * Average bytes per second, usually calculated as nSamplesPerSec * nChannels * ceil(wBitsPerSample, 8).\n * @member AudioStreamFormatImpl.prototype.avgBytesPerSec\n * @function\n * @public\n */\n public avgBytesPerSec: number;\n\n /**\n * The size of a single frame, valid values: nChannels * ceil(wBitsPerSample, 8).\n * @member AudioStreamFormatImpl.prototype.blockAlign\n * @function\n * @public\n */\n public blockAlign: number;\n\n public get header(): ArrayBuffer {\n return this.privHeader;\n }\n\n protected setString = (view: DataView, offset: number, str: string): void => {\n for (let i = 0; i < str.length; i++) {\n view.setUint8(offset + i, str.charCodeAt(i));\n }\n }\n}\n"]}