1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 | 1x 6x 6x 21x 2x 2x 1x 1x 16x 7x 9x 22x 20x 12x 20x 18x 12x 8x 8x 6x 10x 9x 6x 8x 10x 6x 8x 8x 6x 6x 6x 6x 6x 6x 12x 12x 24x 24x 6x 8x 8x 6x 12x 11x 6x 10x 12x 6x 6x 10x 10x 10x 10x 10x 10x 8x 7x 6x 6x 12x 12x 24x 24x 12x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | // audiobuffer-arraybuffer-serializer Version 0.0.36. Copyright 2018 Taito Suzuki <suzuito3@gmail.com>. 'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function generateAudioBuffer(conf) { Eif (typeof AudioBuffer === 'undefined') return new require('audio-buffer')(conf); return new AudioBuffer(conf); } function isInstanceOfAudioBuffer(v) { return (v.constructor.name === 'AudioBuffer'); } class InvalidBufferLengthError extends Error { constructor(expected, real) { super(`Expected buffer length is '${expected}' but real is '${real}'`); } } class InvalidAudioBufferLengthError extends Error { constructor(expected, real) { super(`Expected audio buffer length is '${expected}' but real is '${real}'`); } } class InvalidSampleRateError extends Error { constructor(expected, real) { super(`Expected audio buffer sampleRate is '${expected}' but real is '${real}'`); } } class InvalidNumberOfChannelsError extends Error { constructor(expected, real) { super(`Expected audio buffer numberOfChannels is '${expected}' but real is '${real}'`); } } class InvalidDurationError extends Error {} class Base { constructor(conf) { if (conf.littleEndian) this.littleEndian = conf.littleEndian; else this.littleEndian = true; } generateDestinationBuffer(src) { throw new Error(`This method must be implemented: generateDestinationBuffer`); } validate(src, dst) { throw new Error(`This method must be implemented: generateDestinationBuffer`); } each(src, dst) { throw new Error(`This method must be implemented: generateDestinationBuffer`); } checkArgumentExecute(src, dst) { throw new Error(`This method must be implemented: generateDestinationBuffer`); } execute(src, dst) { this.checkSrcExecute(src); if (dst === undefined) { dst = this.generateDestinationBuffer(src); } this.checkDstExecute(dst); this.validate(src, dst); return this.each(src, dst); } } /** Configuration for Encoder/Decoder * @property {boolean} littleEndian - Specify whether use littleEndian or not. */ class Configuration { constructor(littleEndian) { this.littleEndian = littleEndian; } } /** Serialize {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer AudioBuffer} to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer ArrayBuffer}. */ class Encoder extends Base { /** * @param {Configuration} conf Configuration object. */ constructor(conf) { if (!conf) conf = {}; super(conf); } generateDestinationBuffer(src) { return generateDestinationBufferOnEncoder(src); } checkSrcExecute(src) { if (isInstanceOfAudioBuffer(src) === false) { throw new TypeError(`'src' must be instance of AudioBuffer`); } } checkDstExecute(dst) { if (dst instanceof ArrayBuffer === false) { throw new TypeError(`'dst' must be instance of ArrayBuffer`); } } each(src, dst) { return encode(src, dst, this.littleEndian); } validate(src, dst) { validateOnEncoder(src, dst); } /** * Serialize AudioBuffer to ArrayBuffer. * * @param {AudioBuffer} src * @param {ArrayBuffer} [dst] If not specified, generate ArrayBuffer object in this method. * @return {ArrayBuffer} * @throws {InvalidBufferLengthError} If dst.byteLength does not match buffer length to store src. */ execute(src, dst) { return super.execute(src, dst); } } function generateDestinationBufferOnEncoder(src) { return new ArrayBuffer(16 + (src.length * src.numberOfChannels * 4)); } function validateOnEncoder(src, dst) { const lengthArrayBuffer = 16 /** 4 * 4 **/ + (4 * src.length * src.numberOfChannels); // console.log(lengthArrayBuffer); if (lengthArrayBuffer !== dst.byteLength) { throw new InvalidBufferLengthError(lengthArrayBuffer, dst.byteLength); } } function encode(src, dst, littleEndian) { let dv = new DataView(dst); dv.setFloat32( 0, src.sampleRate, littleEndian); dv.setFloat32( 4, src.duration, littleEndian); dv.setUint32 ( 8, src.length, littleEndian); dv.setUint32 (12, src.numberOfChannels, littleEndian); for (let c = 0; c < src.numberOfChannels; c++) { const f64 = src.getChannelData(c); for (let i = 0; i < f64.length; i++) { let j = 16 + (c * src.length * 4) + (i * 4); dv.setFloat32(j, f64[i], littleEndian); } } return dst; } /** Deserialize {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer ArrayBuffer} to {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer AudioBuffer}. */ class Decoder extends Base { /** * @param {Configuration} conf Configuration object. */ constructor(conf) { if (!conf) conf = {}; super(conf); } generateDestinationBuffer(src) { return generateDestinationBufferOnDecoder(src, this.littleEndian); } checkSrcExecute(src) { if (src instanceof ArrayBuffer === false) { throw new TypeError(`'src' must be instance of ArrayBuffer`); } } checkDstExecute(dst) { if (isInstanceOfAudioBuffer(dst) === false) { throw new TypeError(`'dst' must be instance of AudioBuffer`); } } each(src, dst) { return decode(src, dst, this.littleEndian); } validate(src, dst) { validateOnDecoder(src, dst, this.littleEndian); } /** * Deserialize ArrayBuffer to AudioBuffer. * * @param {ArrayBuffer} src * @param {AudioBuffer} [dst] If not specified, generate AudioBuffer object in this method. * @return {AudioBuffer} * @throws {InvalidAudioBufferLengthError} If the length extracted from 'src' argument does not match dst.length. * @throws {InvalidSampleRateError} If the sampleRate extracted from 'src' argument does not match dst.sampleRate. * @throws {InvalidNumberOfChannelsError} If the numberOfChannels extracted from 'src' argument does not match dst.numberOfChannels. */ execute(src, dst) { return super.execute(src, dst); } } function generateDestinationBufferOnDecoder(src, littleEndian) { let dv = new DataView(src); return generateAudioBuffer({ sampleRate: dv.getFloat32(0, littleEndian), length: dv.getUint32(8, littleEndian), numberOfChannels: dv.getUint32(12, littleEndian), }) } function validateOnDecoder(src, dst, littleEndian) { const dv = new DataView(src); const sampleRate = dv.getFloat32(0, littleEndian); const duration = dv.getFloat32(4, littleEndian); const length = dv.getUint32(8, littleEndian); const numberOfChannels = dv.getUint32(12, littleEndian); if (length !== dst.length) { throw new InvalidAudioBufferLengthError(length, dst.length); } if (sampleRate !== dst.sampleRate) { throw new InvalidSampleRateError(sampleRate, dst.sampleRate); } if (numberOfChannels !== dst.numberOfChannels) { throw new InvalidNumberOfChannelsError(numberOfChannels, dst.numberOfChannels); } // if (duration !== dst.duration) { throw new InvalidDurationError(); } } function decode(src, dst, littleEndian) { const dv = new DataView(src); for (let c = 0; c < dst.numberOfChannels; c++) { let f32 = new Float32Array(dst.length); for (let i = 0; i < f32.length; i++) { let j = 16 + (c * dst.length * 4) + (i * 4); f32[i] = dv.getFloat32(j, littleEndian); } dst.copyToChannel(f32, c); } return dst; } class ObjectKeyNotFoundError extends Error { constructor(name, key) { super(`'${name}' must have '${key}' property`); } } /** Create an instance of ArrayBuffer from AudioBuffer object. * @function createArrayBuffer * @param {AudioBuffer} audioBuffer * @return {ArrayBuffer} */ /** Create an instance of ArrayBuffer from specified configuration. * @function createArrayBuffer * @param {Object} conf Configuration object for initialization of ArrayBuffer. * @param {String} conf.length AudioBuffer.length property. {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer see} * @param {String} conf.numberOfChannels AudioBuffer.numberOfChannels property. {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer see} * @param {String} conf.sampleRate AudioBuffer.sampleRate property. {@link https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer see} * @return {ArrayBuffer} */ function createArrayBuffer() { if (typeof arguments[0] === 'object') { let o = arguments[0]; if ('length' in o === false) throw new ObjectKeyNotFoundError('object', 'length'); if ('numberOfChannels' in o === false) throw new ObjectKeyNotFoundError('object', 'numberOfChannels'); if ('sampleRate' in o === false) throw new ObjectKeyNotFoundError('object', 'sampleRate'); return generateDestinationBufferOnEncoder(o); } throw new TypeError(`'arguments[0]' must be object`); } exports.InvalidBufferLengthError = InvalidBufferLengthError; exports.InvalidAudioBufferLengthError = InvalidAudioBufferLengthError; exports.InvalidSampleRateError = InvalidSampleRateError; exports.InvalidNumberOfChannelsError = InvalidNumberOfChannelsError; exports.InvalidDurationError = InvalidDurationError; exports.Configuration = Configuration; exports.Encoder = Encoder; exports.Decoder = Decoder; exports.ObjectKeyNotFoundError = ObjectKeyNotFoundError; exports.createArrayBuffer = createArrayBuffer; |