microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 10.8 kB
Source Map (JSON)
{"version":3,"sources":["src/sdk/Audio/AudioStreamFormat.ts"],"names":[],"mappings":"AAIA,oBAAY,cAAc;IACtB,GAAG,IAAI;IACP,KAAK,IAAA;IACL,KAAK,IAAA;IACL,GAAG,IAAA;IACH,SAAS,IAAA;IACT,QAAQ,IAAA;IACR,SAAS,IAAA;IACT,IAAI,IAAA;IACJ,IAAI,IAAA;IACJ,IAAI,KAAA;IACJ,MAAM,KAAA;IACN,IAAI,KAAA;CACP;AAED;;;GAGG;AACH,8BAAsB,iBAAiB;IACnC;;;;;;;OAOG;WACW,qBAAqB,IAAI,iBAAiB;IAIxD;;;;;;;;;;;OAWG;WACW,aAAa,CAAC,gBAAgB,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,cAAc,GAAG,iBAAiB;IAIzI;;;;;;;;;;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;AACH,qBAAa,qBAAsB,SAAQ,iBAAiB;IACxD,SAAS,CAAC,UAAU,EAAE,WAAW,CAAC;IAElC;;;;;;;OAOG;gBACgB,aAAa,GAAE,MAAc,EAAE,aAAa,GAAE,MAAW,EAAE,QAAQ,GAAE,MAAU,EAAE,MAAM,GAAE,cAAmC;IAyD/I;;;;;;OAMG;WACW,qBAAqB,IAAI,qBAAqB;IAI5D;;;;;;OAMG;WAEW,eAAe,CAAC,UAAU,CAAC,EAAE,MAAM,GAAG,YAAY;IAmBhE;;;;;OAKG;IACI,KAAK,IAAI,IAAI;IAIpB;;;;;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,CAAC,IAAI,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,IAAI;CAKzE","file":"AudioStreamFormat.d.ts","sourcesContent":["// Copyright (c) Microsoft Corporation. All rights reserved.\r\n// Licensed under the MIT license.\r\n\r\n// eslint-disable-next-line max-classes-per-file\r\nexport enum AudioFormatTag {\r\n PCM = 1,\r\n MuLaw,\r\n Siren,\r\n MP3,\r\n SILKSkype,\r\n OGG_OPUS,\r\n WEBM_OPUS,\r\n ALaw,\r\n FLAC,\r\n OPUS,\r\n AMR_WB,\r\n G722,\r\n}\r\n\r\n/**\r\n * Represents audio stream format used for custom audio input configurations.\r\n * @class AudioStreamFormat\r\n */\r\nexport abstract class AudioStreamFormat {\r\n /**\r\n * Creates an audio stream format object representing the default audio stream\r\n * format (16KHz 16bit mono PCM).\r\n * @member AudioStreamFormat.getDefaultInputFormat\r\n * @function\r\n * @public\r\n * @returns {AudioStreamFormat} The audio stream format being created.\r\n */\r\n public static getDefaultInputFormat(): AudioStreamFormat {\r\n return AudioStreamFormatImpl.getDefaultInputFormat();\r\n }\r\n\r\n /**\r\n * Creates an audio stream format object with the specified format characteristics.\r\n * @member AudioStreamFormat.getWaveFormat\r\n * @function\r\n * @public\r\n * @param {number} samplesPerSecond - Sample rate, in samples per second (Hertz).\r\n * @param {number} bitsPerSample - Bits per sample, typically 16.\r\n * @param {number} channels - Number of channels in the waveform-audio data. Monaural data\r\n * uses one channel and stereo data uses two channels.\r\n * @param {AudioFormatTag} format - Audio format (PCM, alaw or mulaw).\r\n * @returns {AudioStreamFormat} The audio stream format being created.\r\n */\r\n public static getWaveFormat(samplesPerSecond: number, bitsPerSample: number, channels: number, format: AudioFormatTag): AudioStreamFormat {\r\n return new AudioStreamFormatImpl(samplesPerSecond, bitsPerSample, channels, format);\r\n }\r\n\r\n /**\r\n * Creates an audio stream format object with the specified pcm waveformat characteristics.\r\n * @member AudioStreamFormat.getWaveFormatPCM\r\n * @function\r\n * @public\r\n * @param {number} samplesPerSecond - Sample rate, in samples per second (Hertz).\r\n * @param {number} bitsPerSample - Bits per sample, typically 16.\r\n * @param {number} channels - Number of channels in the waveform-audio data. Monaural data\r\n * uses one channel and stereo data uses two channels.\r\n * @returns {AudioStreamFormat} The audio stream format being created.\r\n */\r\n public static getWaveFormatPCM(samplesPerSecond: number, bitsPerSample: number, channels: number): AudioStreamFormat {\r\n return new AudioStreamFormatImpl(samplesPerSecond, bitsPerSample, channels);\r\n }\r\n\r\n /**\r\n * Explicitly frees any external resource attached to the object\r\n * @member AudioStreamFormat.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public abstract close(): void;\r\n}\r\n\r\n/**\r\n * @private\r\n * @class AudioStreamFormatImpl\r\n */\r\nexport class AudioStreamFormatImpl extends AudioStreamFormat {\r\n protected privHeader: ArrayBuffer;\r\n\r\n /**\r\n * Creates an instance with the given values.\r\n * @constructor\r\n * @param {number} samplesPerSec - Samples per second.\r\n * @param {number} bitsPerSample - Bits per sample.\r\n * @param {number} channels - Number of channels.\r\n * @param {AudioFormatTag} format - Audio format (PCM, alaw or mulaw).\r\n */\r\n public constructor(samplesPerSec: number = 16000, bitsPerSample: number = 16, channels: number = 1, format: AudioFormatTag = AudioFormatTag.PCM) {\r\n super();\r\n\r\n let isWavFormat: boolean = true;\r\n /* 1 for PCM; 6 for alaw; 7 for mulaw */\r\n switch (format) {\r\n case AudioFormatTag.PCM:\r\n this.formatTag = 1;\r\n break;\r\n case AudioFormatTag.ALaw:\r\n this.formatTag = 6;\r\n break;\r\n case AudioFormatTag.MuLaw:\r\n this.formatTag = 7;\r\n break;\r\n default:\r\n isWavFormat = false;\r\n }\r\n this.bitsPerSample = bitsPerSample;\r\n this.samplesPerSec = samplesPerSec;\r\n this.channels = channels;\r\n this.avgBytesPerSec = this.samplesPerSec * this.channels * (this.bitsPerSample / 8);\r\n this.blockAlign = this.channels * Math.max(this.bitsPerSample, 8);\r\n\r\n if (isWavFormat) {\r\n this.privHeader = new ArrayBuffer(44);\r\n\r\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DataView\r\n const view = new DataView(this.privHeader);\r\n\r\n /* RIFF identifier */\r\n this.setString(view, 0, \"RIFF\");\r\n /* file length */\r\n view.setUint32(4, 0, true);\r\n /* RIFF type & Format */\r\n this.setString(view, 8, \"WAVEfmt \");\r\n /* format chunk length */\r\n view.setUint32(16, 16, true);\r\n /* audio format */\r\n view.setUint16(20, this.formatTag, true);\r\n /* channel count */\r\n view.setUint16(22, this.channels, true);\r\n /* sample rate */\r\n view.setUint32(24, this.samplesPerSec, true);\r\n /* byte rate (sample rate * block align) */\r\n view.setUint32(28, this.avgBytesPerSec, true);\r\n /* block align (channel count * bytes per sample) */\r\n view.setUint16(32, this.channels * (this.bitsPerSample / 8), true);\r\n /* bits per sample */\r\n view.setUint16(34, this.bitsPerSample, true);\r\n /* data chunk identifier */\r\n this.setString(view, 36, \"data\");\r\n /* data chunk length */\r\n view.setUint32(40, 0, true);\r\n }\r\n }\r\n\r\n /**\r\n * Retrieves the default input format.\r\n * @member AudioStreamFormatImpl.getDefaultInputFormat\r\n * @function\r\n * @public\r\n * @returns {AudioStreamFormatImpl} The default input format.\r\n */\r\n public static getDefaultInputFormat(): AudioStreamFormatImpl {\r\n return new AudioStreamFormatImpl();\r\n }\r\n\r\n /**\r\n * Creates an audio context appropriate to current browser\r\n * @member AudioStreamFormatImpl.getAudioContext\r\n * @function\r\n * @public\r\n * @returns {AudioContext} An audio context instance\r\n */\r\n /* eslint-disable */\r\n public static getAudioContext(sampleRate?: number): AudioContext {\r\n // Workaround for Speech SDK bug in Safari.\r\n const AudioContext = (window as any).AudioContext // our preferred impl\r\n || (window as any).webkitAudioContext // fallback, mostly when on Safari\r\n || false; // could not find.\r\n\r\n // https://developer.mozilla.org/en-US/docs/Web/API/AudioContext\r\n if (!!AudioContext) {\r\n if (sampleRate !== undefined && navigator.mediaDevices.getSupportedConstraints().sampleRate) {\r\n return new AudioContext({ sampleRate });\r\n } else {\r\n return new AudioContext();\r\n }\r\n } else {\r\n throw new Error(\"Browser does not support Web Audio API (AudioContext is not available).\");\r\n }\r\n }\r\n /* eslint-enable */\r\n\r\n /**\r\n * Closes the configuration object.\r\n * @member AudioStreamFormatImpl.prototype.close\r\n * @function\r\n * @public\r\n */\r\n public close(): void {\r\n return;\r\n }\r\n\r\n /**\r\n * The format of the audio, valid values: 1 (PCM)\r\n * @member AudioStreamFormatImpl.prototype.formatTag\r\n * @function\r\n * @public\r\n */\r\n public formatTag: number;\r\n\r\n /**\r\n * The number of channels, valid values: 1 (Mono).\r\n * @member AudioStreamFormatImpl.prototype.channels\r\n * @function\r\n * @public\r\n */\r\n public channels: number;\r\n\r\n /**\r\n * The sample rate, valid values: 16000.\r\n * @member AudioStreamFormatImpl.prototype.samplesPerSec\r\n * @function\r\n * @public\r\n */\r\n public samplesPerSec: number;\r\n\r\n /**\r\n * The bits per sample, valid values: 16\r\n * @member AudioStreamFormatImpl.prototype.b\r\n * @function\r\n * @public\r\n */\r\n public bitsPerSample: number;\r\n\r\n /**\r\n * Average bytes per second, usually calculated as nSamplesPerSec * nChannels * ceil(wBitsPerSample, 8).\r\n * @member AudioStreamFormatImpl.prototype.avgBytesPerSec\r\n * @function\r\n * @public\r\n */\r\n public avgBytesPerSec: number;\r\n\r\n /**\r\n * The size of a single frame, valid values: nChannels * ceil(wBitsPerSample, 8).\r\n * @member AudioStreamFormatImpl.prototype.blockAlign\r\n * @function\r\n * @public\r\n */\r\n public blockAlign: number;\r\n\r\n public get header(): ArrayBuffer {\r\n return this.privHeader;\r\n }\r\n\r\n protected setString(view: DataView, offset: number, str: string): void {\r\n for (let i = 0; i < str.length; i++) {\r\n view.setUint8(offset + i, str.charCodeAt(i));\r\n }\r\n }\r\n}\r\n"]}