@hahnpro/ms-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 10.3 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.\n// Licensed under the MIT license.\n\n// eslint-disable-next-line max-classes-per-file\nexport enum AudioFormatTag {\n PCM = 1,\n MuLaw,\n Siren,\n MP3,\n SILKSkype,\n OGG_OPUS,\n WEBM_OPUS,\n ALaw,\n FLAC,\n OPUS,\n AMR_WB,\n G722,\n}\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 format characteristics.\n * @member AudioStreamFormat.getWaveFormat\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 * @param {AudioFormatTag} format - Audio format (PCM, alaw or mulaw).\n * @returns {AudioStreamFormat} The audio stream format being created.\n */\n public static getWaveFormat(samplesPerSecond: number, bitsPerSample: number, channels: number, format: AudioFormatTag): AudioStreamFormat {\n return new AudioStreamFormatImpl(samplesPerSecond, bitsPerSample, channels, format);\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 */\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 * @param {AudioFormatTag} format - Audio format (PCM, alaw or mulaw).\n */\n public constructor(samplesPerSec: number = 16000, bitsPerSample: number = 16, channels: number = 1, format: AudioFormatTag = AudioFormatTag.PCM) {\n super();\n\n let isWavFormat: boolean = true;\n /* 1 for PCM; 6 for alaw; 7 for mulaw */\n switch (format) {\n case AudioFormatTag.PCM:\n this.formatTag = 1;\n break;\n case AudioFormatTag.ALaw:\n this.formatTag = 6;\n break;\n case AudioFormatTag.MuLaw:\n this.formatTag = 7;\n break;\n default:\n isWavFormat = false;\n }\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 if (isWavFormat) {\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 /* audio format */\n view.setUint16(20, this.formatTag, 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 /**\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 * Creates an audio context appropriate to current browser\n * @member AudioStreamFormatImpl.getAudioContext\n * @function\n * @public\n * @returns {AudioContext} An audio context instance\n */\n /* eslint-disable */\n public static getAudioContext(sampleRate?: number): AudioContext {\n // Workaround for Speech SDK bug in Safari.\n const AudioContext = (window as any).AudioContext // our preferred impl\n || (window as any).webkitAudioContext // fallback, mostly when on Safari\n || false; // could not find.\n\n // https://developer.mozilla.org/en-US/docs/Web/API/AudioContext\n if (!!AudioContext) {\n if (sampleRate !== undefined && navigator.mediaDevices.getSupportedConstraints().sampleRate) {\n return new AudioContext({ sampleRate });\n } else {\n return new AudioContext();\n }\n } else {\n throw new Error(\"Browser does not support Web Audio API (AudioContext is not available).\");\n }\n }\n /* eslint-enable */\n\n /**\n * Closes the configuration object.\n * @member AudioStreamFormatImpl.prototype.close\n * @function\n * @public\n */\n public close(): void {\n return;\n }\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"]}