Comparing version 3.0.0 to 3.1.0
# CHANGELOG | ||
## v3.1.0 (2018-06-25) | ||
- Allow better use of this lib as a dependency: | ||
- package.json refactored with bundlers and ES6 envs in mind | ||
## version 3.0.0 (2018-06-22) | ||
- ES6 module. |
@@ -1,76 +0,5 @@ | ||
(function(e, a) { for(var i in a) e[i] = a[i]; }(exports, /******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
/******/ | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/******/ | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) { | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ } | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
/******/ | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
/******/ | ||
/******/ // Flag the module as loaded | ||
/******/ module.l = true; | ||
/******/ | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
/******/ | ||
/******/ | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
/******/ | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ | ||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { | ||
/******/ configurable: false, | ||
/******/ enumerable: true, | ||
/******/ get: getter | ||
/******/ }); | ||
/******/ } | ||
/******/ }; | ||
/******/ | ||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
/******/ | ||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
/******/ | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
'use strict'; | ||
"use strict"; | ||
Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); | ||
/* harmony export (immutable) */ __webpack_exports__["encode"] = encode; | ||
/* harmony export (immutable) */ __webpack_exports__["decode"] = decode; | ||
/* harmony export (immutable) */ __webpack_exports__["encodeBlock"] = encodeBlock; | ||
/* harmony export (immutable) */ __webpack_exports__["decodeBlock"] = decodeBlock; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/* | ||
@@ -147,7 +76,2 @@ * imaadpcm: IMA ADPCM codec in JavaScript. | ||
*/ | ||
let encoderStep_ = 7; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let decoderPredicted_ = 0; | ||
@@ -395,4 +319,5 @@ /** | ||
/***/ }) | ||
/******/ ]))); | ||
exports.encode = encode; | ||
exports.decode = decode; | ||
exports.encodeBlock = encodeBlock; | ||
exports.decodeBlock = decodeBlock; |
@@ -1,5 +0,7 @@ | ||
window.imaadpcm=function(q){function e(c){if(l[c])return l[c].a;var f=l[c]={m:c,f:!1,a:{}};q[c].call(f.a,f,f.a,e);f.f=!0;return f.a}var l={};e.l=q;e.h=l;e.b=function(c,f){e.c(c)||Object.defineProperty(c,"a",{configurable:!1,enumerable:!0,get:f})};e.i=function(c){var f=c&&c.g?function(){return c["default"]}:function(){return c};e.b(f,f);return f};e.c=function(c){return Object.prototype.hasOwnProperty.call(c,"a")};e.j="";return e(e.o=0)}([function(q,e){function l(b){var a=b[0];r(a);var d=[];d.push(a& | ||
255);d.push(a>>8&255);d.push(m);d.push(0);for(a=3;a<b.length;a+=2){var p=r(b[a]),c=r(b[a+1]);d.push(c<<4|p)}for(;256>d.length;)d.push(0);return d}function c(b){g=f(b[1]<<8|b[0]);h=b[2];n=t[h];for(var a=[g,f(b[3]<<8|b[2])],d=4;d<b.length;d++){var p=b[d],c=p>>4;a.push(u(c<<4^p));a.push(u(c))}return a}function f(b){return 32768<b?b-65536:b}function r(b){var a=b-k;0<=a?b=0:(b=8,a=-a);var d=t[m],c=d>>3;a>d&&(b|=4,a-=d,c+=d);d>>=1;a>d&&(b|=2,a-=d,c+=d);d>>=1;a>d&&(b|=1,c+=d);a=b;k=a&8?k-c:k+c;-32768>k? | ||
k=-32768:32767<k&&(k=32767);m+=v[a&7];0>m?m=0:88<m&&(m=88);return b}function u(b){var a=0;b&4&&(a+=n);b&2&&(a+=n>>1);b&1&&(a+=n>>2);a+=n>>3;b&8&&(a=-a);g+=a;32767<g?g=32767:-32767>g&&(g=-32767);h+=v[b];0>h?h=0:88<h&&(h=88);n=t[h];return g}Object.defineProperty(e,"__esModule",{value:!0});e.encode=function(b){for(var a=[],d=[],c=0;c<b.length;c++)if(d.push(b[c]),0==c%505&&0!=c||c==b.length-1)a=a.concat(l(d)),d=[];return a};e.decode=function(b,a){a=void 0===a?256:a;for(var d=[],e=[],f=0;f<b.length;f++)0== | ||
f%a&&0!=f&&(d=d.concat(c(e)),e=[]),e.push(b[f]);return d};e.encodeBlock=l;e.decodeBlock=c;var v=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],t=[7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818,18500,20350,22385,24623,27086, | ||
29794,32767],k=0,m=0,g=0,h=0,n=7}]); | ||
var p="undefined"!=typeof window&&window===this?this:"undefined"!=typeof global&&null!=global?global:this;function t(){t=function(){};p.Symbol||(p.Symbol=z)}var z=function(){var g=0;return function(a){return"jscomp_symbol_"+(a||"")+g++}}(); | ||
window.imaadpcm=function(g){function a(b){if(d[b])return d[b].a;var c=d[b]={m:b,f:!1,a:{}};g[b].call(c.a,c,c.a,a);c.f=!0;return c.a}var d={};a.l=g;a.g=d;a.d=function(b,c,d){a.c(b,c)||Object.defineProperty(b,c,{enumerable:!0,get:d})};a.r=function(b){t();t();"undefined"!==typeof Symbol&&Symbol.toStringTag&&(t(),Object.defineProperty(b,Symbol.toStringTag,{value:"Module"}));Object.defineProperty(b,"__esModule",{value:!0})};a.j=function(b,c){c&1&&(b=a(b));if(c&8)return b;if(c&4&&"object"===typeof b&&b&& | ||
b.b)return b;var d=Object.create(null);a.r(d);Object.defineProperty(d,"default",{enumerable:!0,value:b});if(c&2&&"string"!=typeof b)for(var g in b)a.d(d,g,function(a){return b[a]}.bind(null,g));return d};a.h=function(b){var c=b&&b.b?function(){return b["default"]}:function(){return b};a.d(c,"a",c);return c};a.c=function(b,a){return Object.prototype.hasOwnProperty.call(b,a)};a.i="";return a(a.o=0)}([function(g,a,d){function b(f){for(var e=[],b=[],a=0;a<f.length;a++)if(b.push(f[a]),0==a%505&&0!=a|| | ||
a==f.length-1)e=e.concat(u(b)),b=[];return e}function c(f,e){e=void 0===e?256:e;for(var b=[],a=[],c=0;c<f.length;c++)0==c%e&&0!=c&&(b=b.concat(v(a)),a=[]),a.push(f[c]);return b}function u(b){var e=b[0];q(e);var a=[];a.push(e&255);a.push(e>>8&255);a.push(m);a.push(0);for(e=3;e<b.length;e+=2){var f=q(b[e]),c=q(b[e+1]);a.push(c<<4|f)}for(;256>a.length;)a.push(0);return a}function v(a){h=w(a[1]<<8|a[0]);k=a[2];n=r[k];for(var b=[h,w(a[3]<<8|a[2])],c=4;c<a.length;c++){var f=a[c],d=f>>4;b.push(x(d<<4^f)); | ||
b.push(x(d))}return b}function w(a){return 32768<a?a-65536:a}function q(a){var b=a-l;0<=b?a=0:(a=8,b=-b);var c=r[m],d=c>>3;b>c&&(a|=4,b-=c,d+=c);c>>=1;b>c&&(a|=2,b-=c,d+=c);c>>=1;b>c&&(a|=1,d+=c);b=a;l=b&8?l-d:l+d;-32768>l?l=-32768:32767<l&&(l=32767);m+=y[b&7];0>m?m=0:88<m&&(m=88);return a}function x(a){var b=0;a&4&&(b+=n);a&2&&(b+=n>>1);a&1&&(b+=n>>2);b+=n>>3;a&8&&(b=-b);h+=b;32767<h?h=32767:-32767>h&&(h=-32767);k+=y[a];0>k?k=0:88<k&&(k=88);n=r[k];return h}d.r(a);d.d(a,"encode",function(){return b}); | ||
d.d(a,"decode",function(){return c});d.d(a,"encodeBlock",function(){return u});d.d(a,"decodeBlock",function(){return v});var y=[-1,-1,-1,-1,2,4,6,8,-1,-1,-1,-1,2,4,6,8],r=[7,8,9,10,11,12,13,14,16,17,19,21,23,25,28,31,34,37,41,45,50,55,60,66,73,80,88,97,107,118,130,143,157,173,190,209,230,253,279,307,337,371,408,449,494,544,598,658,724,796,876,963,1060,1166,1282,1411,1552,1707,1878,2066,2272,2499,2749,3024,3327,3660,4026,4428,4871,5358,5894,6484,7132,7845,8630,9493,10442,11487,12635,13899,15289,16818, | ||
18500,20350,22385,24623,27086,29794,32767],l=0,m=0,h=0,k=0,n=7}]);window.imaadpcm=window.imaadpcm; |
@@ -1,407 +0,327 @@ | ||
(function webpackUniversalModuleDefinition(root, factory) { | ||
if(typeof exports === 'object' && typeof module === 'object') | ||
module.exports = factory(); | ||
else if(typeof define === 'function' && define.amd) | ||
define([], factory); | ||
else if(typeof exports === 'object') | ||
exports["imaadpcm"] = factory(); | ||
else | ||
root["imaadpcm"] = factory(); | ||
})(typeof self !== 'undefined' ? self : this, function() { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
/******/ | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/******/ | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) { | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ } | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ i: moduleId, | ||
/******/ l: false, | ||
/******/ exports: {} | ||
/******/ }; | ||
/******/ | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
/******/ | ||
/******/ // Flag the module as loaded | ||
/******/ module.l = true; | ||
/******/ | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
/******/ | ||
/******/ | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
/******/ | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ | ||
/******/ // define getter function for harmony exports | ||
/******/ __webpack_require__.d = function(exports, name, getter) { | ||
/******/ if(!__webpack_require__.o(exports, name)) { | ||
/******/ Object.defineProperty(exports, name, { | ||
/******/ configurable: false, | ||
/******/ enumerable: true, | ||
/******/ get: getter | ||
/******/ }); | ||
/******/ } | ||
/******/ }; | ||
/******/ | ||
/******/ // getDefaultExport function for compatibility with non-harmony modules | ||
/******/ __webpack_require__.n = function(module) { | ||
/******/ var getter = module && module.__esModule ? | ||
/******/ function getDefault() { return module['default']; } : | ||
/******/ function getModuleExports() { return module; }; | ||
/******/ __webpack_require__.d(getter, 'a', getter); | ||
/******/ return getter; | ||
/******/ }; | ||
/******/ | ||
/******/ // Object.prototype.hasOwnProperty.call | ||
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; | ||
/******/ | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ (function(module, __webpack_exports__, __webpack_require__) { | ||
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(factory((global.imaadpcm = {}))); | ||
}(this, (function (exports) { 'use strict'; | ||
"use strict"; | ||
Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); | ||
/* harmony export (immutable) */ __webpack_exports__["encode"] = encode; | ||
/* harmony export (immutable) */ __webpack_exports__["decode"] = decode; | ||
/* harmony export (immutable) */ __webpack_exports__["encodeBlock"] = encodeBlock; | ||
/* harmony export (immutable) */ __webpack_exports__["decodeBlock"] = decodeBlock; | ||
/* | ||
* imaadpcm: IMA ADPCM codec in JavaScript. | ||
* https://github.com/rochars/imaadpcm | ||
* | ||
* Copyright (c) 2017-2018 Rafael da Silva Rocha. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
/* | ||
* imaadpcm: IMA ADPCM codec in JavaScript. | ||
* https://github.com/rochars/imaadpcm | ||
* | ||
* Copyright (c) 2017-2018 Rafael da Silva Rocha. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
*/ | ||
/** | ||
* @fileoverview imaadpcm public API and private methods. | ||
*/ | ||
/** | ||
* @fileoverview imaadpcm public API and private methods. | ||
*/ | ||
/** @module imaadpcm */ | ||
/** @module imaadpcm */ | ||
/** | ||
* @type {!Array<number>} | ||
* @private | ||
*/ | ||
const INDEX_TABLE = [ | ||
-1, -1, -1, -1, 2, 4, 6, 8, | ||
-1, -1, -1, -1, 2, 4, 6, 8]; | ||
/** | ||
* @type {!Array<number>} | ||
* @private | ||
*/ | ||
const STEP_TABLE = [ | ||
7, 8, 9, 10, 11, 12, 13, 14, | ||
16, 17, 19, 21, 23, 25, 28, 31, | ||
34, 37, 41, 45, 50, 55, 60, 66, | ||
73, 80, 88, 97, 107, 118, 130, 143, | ||
157, 173, 190, 209, 230, 253, 279, 307, | ||
337, 371, 408, 449, 494, 544, 598, 658, | ||
724, 796, 876, 963, 1060, 1166, 1282, 1411, | ||
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, | ||
3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, | ||
7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | ||
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, | ||
32767]; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let encoderPredicted_ = 0; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let encoderIndex_ = 0; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let encoderStep_ = 7; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let decoderPredicted_ = 0; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let decoderIndex_ = 0; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let decoderStep_ = 7; | ||
/** | ||
* @type {!Array<number>} | ||
* @private | ||
*/ | ||
const INDEX_TABLE = [ | ||
-1, -1, -1, -1, 2, 4, 6, 8, | ||
-1, -1, -1, -1, 2, 4, 6, 8]; | ||
/** | ||
* @type {!Array<number>} | ||
* @private | ||
*/ | ||
const STEP_TABLE = [ | ||
7, 8, 9, 10, 11, 12, 13, 14, | ||
16, 17, 19, 21, 23, 25, 28, 31, | ||
34, 37, 41, 45, 50, 55, 60, 66, | ||
73, 80, 88, 97, 107, 118, 130, 143, | ||
157, 173, 190, 209, 230, 253, 279, 307, | ||
337, 371, 408, 449, 494, 544, 598, 658, | ||
724, 796, 876, 963, 1060, 1166, 1282, 1411, | ||
1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, | ||
3327, 3660, 4026, 4428, 4871, 5358, 5894, 6484, | ||
7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899, | ||
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, | ||
32767]; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let encoderPredicted_ = 0; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let encoderIndex_ = 0; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let decoderPredicted_ = 0; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let decoderIndex_ = 0; | ||
/** | ||
* @type {number} | ||
* @private | ||
*/ | ||
let decoderStep_ = 7; | ||
/** | ||
* Encode 16-bit PCM samples into 4-bit IMA ADPCM samples. | ||
* @param {!Array<number>} samples A array of samples. | ||
* @return {!Array<number>} | ||
*/ | ||
function encode(samples) { | ||
/** @type {!Array<number>} */ | ||
let adpcmSamples = []; | ||
/** @type {Array<number>} */ | ||
let block = []; | ||
for (let i=0; i<samples.length; i++) { | ||
block.push(samples[i]); | ||
if ((i % 505 == 0 && i != 0) || i == samples.length - 1) { | ||
adpcmSamples = adpcmSamples.concat(encodeBlock(block)); | ||
block = []; | ||
/** | ||
* Encode 16-bit PCM samples into 4-bit IMA ADPCM samples. | ||
* @param {!Array<number>} samples A array of samples. | ||
* @return {!Array<number>} | ||
*/ | ||
function encode(samples) { | ||
/** @type {!Array<number>} */ | ||
let adpcmSamples = []; | ||
/** @type {Array<number>} */ | ||
let block = []; | ||
for (let i=0; i<samples.length; i++) { | ||
block.push(samples[i]); | ||
if ((i % 505 == 0 && i != 0) || i == samples.length - 1) { | ||
adpcmSamples = adpcmSamples.concat(encodeBlock(block)); | ||
block = []; | ||
} | ||
} | ||
return adpcmSamples; | ||
} | ||
return adpcmSamples; | ||
} | ||
/** | ||
* Decode IMA ADPCM samples into 16-bit PCM samples. | ||
* @param {!Array<number>} adpcmSamples A array of ADPCM samples. | ||
* @param {number} blockAlign The block size. | ||
* @return {!Array<number>} | ||
*/ | ||
function decode(adpcmSamples, blockAlign=256) { | ||
/** @type {!Array<number>} */ | ||
let samples = []; | ||
/** @type {!Array<number>} */ | ||
let block = []; | ||
for (let i=0; i<adpcmSamples.length; i++) { | ||
if (i % blockAlign == 0 && i != 0) { | ||
samples = samples.concat(decodeBlock(block)); | ||
block = []; | ||
/** | ||
* Decode IMA ADPCM samples into 16-bit PCM samples. | ||
* @param {!Array<number>} adpcmSamples A array of ADPCM samples. | ||
* @param {number} blockAlign The block size. | ||
* @return {!Array<number>} | ||
*/ | ||
function decode(adpcmSamples, blockAlign=256) { | ||
/** @type {!Array<number>} */ | ||
let samples = []; | ||
/** @type {!Array<number>} */ | ||
let block = []; | ||
for (let i=0; i<adpcmSamples.length; i++) { | ||
if (i % blockAlign == 0 && i != 0) { | ||
samples = samples.concat(decodeBlock(block)); | ||
block = []; | ||
} | ||
block.push(adpcmSamples[i]); | ||
} | ||
block.push(adpcmSamples[i]); | ||
return samples; | ||
} | ||
return samples; | ||
} | ||
/** | ||
* Encode a block of 505 16-bit samples as 4-bit ADPCM samples. | ||
* @param {!Array<number>} block A sample block of 505 samples. | ||
* @return {!Array<number>} | ||
*/ | ||
function encodeBlock(block) { | ||
/** @type {!Array<number>} */ | ||
let adpcmSamples = blockHead_(block[0]); | ||
for (let i=3; i<block.length; i+=2) { | ||
/** @type {number} */ | ||
let sample2 = encodeSample_(block[i]); | ||
/** @type {number} */ | ||
let sample = encodeSample_(block[i + 1]); | ||
adpcmSamples.push((sample << 4) | sample2); | ||
/** | ||
* Encode a block of 505 16-bit samples as 4-bit ADPCM samples. | ||
* @param {!Array<number>} block A sample block of 505 samples. | ||
* @return {!Array<number>} | ||
*/ | ||
function encodeBlock(block) { | ||
/** @type {!Array<number>} */ | ||
let adpcmSamples = blockHead_(block[0]); | ||
for (let i=3; i<block.length; i+=2) { | ||
/** @type {number} */ | ||
let sample2 = encodeSample_(block[i]); | ||
/** @type {number} */ | ||
let sample = encodeSample_(block[i + 1]); | ||
adpcmSamples.push((sample << 4) | sample2); | ||
} | ||
while (adpcmSamples.length < 256) { | ||
adpcmSamples.push(0); | ||
} | ||
return adpcmSamples; | ||
} | ||
while (adpcmSamples.length < 256) { | ||
adpcmSamples.push(0); | ||
/** | ||
* Decode a block of ADPCM samples into 16-bit PCM samples. | ||
* @param {!Array<number>} block A adpcm sample block. | ||
* @return {!Array<number>} | ||
*/ | ||
function decodeBlock(block) { | ||
decoderPredicted_ = sign_((block[1] << 8) | block[0]); | ||
decoderIndex_ = block[2]; | ||
decoderStep_ = STEP_TABLE[decoderIndex_]; | ||
/** @type {!Array<number>} */ | ||
let result = [ | ||
decoderPredicted_, | ||
sign_((block[3] << 8) | block[2]) | ||
]; | ||
for (let i=4; i<block.length; i++) { | ||
/** @type {number} */ | ||
let original_sample = block[i]; | ||
/** @type {number} */ | ||
let second_sample = original_sample >> 4; | ||
/** @type {number} */ | ||
let first_sample = (second_sample << 4) ^ original_sample; | ||
result.push(decodeSample_(first_sample)); | ||
result.push(decodeSample_(second_sample)); | ||
} | ||
return result; | ||
} | ||
return adpcmSamples; | ||
} | ||
/** | ||
* Decode a block of ADPCM samples into 16-bit PCM samples. | ||
* @param {!Array<number>} block A adpcm sample block. | ||
* @return {!Array<number>} | ||
*/ | ||
function decodeBlock(block) { | ||
decoderPredicted_ = sign_((block[1] << 8) | block[0]); | ||
decoderIndex_ = block[2]; | ||
decoderStep_ = STEP_TABLE[decoderIndex_]; | ||
/** @type {!Array<number>} */ | ||
let result = [ | ||
decoderPredicted_, | ||
sign_((block[3] << 8) | block[2]) | ||
]; | ||
for (let i=4; i<block.length; i++) { | ||
/** | ||
* Sign a 16-bit integer. | ||
* @param {number} num A 16-bit integer. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function sign_(num) { | ||
return num > 32768 ? num - 65536 : num; | ||
} | ||
/** | ||
* Compress a 16-bit PCM sample into a 4-bit ADPCM sample. | ||
* @param {number} sample The sample. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function encodeSample_(sample) { | ||
/** @type {number} */ | ||
let original_sample = block[i]; | ||
let delta = sample - encoderPredicted_; | ||
/** @type {number} */ | ||
let second_sample = original_sample >> 4; | ||
let value = 0; | ||
if (delta >= 0) { | ||
value = 0; | ||
} else { | ||
value = 8; | ||
delta = -delta; | ||
} | ||
/** @type {number} */ | ||
let first_sample = (second_sample << 4) ^ original_sample; | ||
result.push(decodeSample_(first_sample)); | ||
result.push(decodeSample_(second_sample)); | ||
let step = STEP_TABLE[encoderIndex_]; | ||
/** @type {number} */ | ||
let diff = step >> 3; | ||
if (delta > step) { | ||
value |= 4; | ||
delta -= step; | ||
diff += step; | ||
} | ||
step >>= 1; | ||
if (delta > step) { | ||
value |= 2; | ||
delta -= step; | ||
diff += step; | ||
} | ||
step >>= 1; | ||
if (delta > step) { | ||
value |= 1; | ||
diff += step; | ||
} | ||
updateEncoder_(value, diff); | ||
return value; | ||
} | ||
return result; | ||
} | ||
/** | ||
* Sign a 16-bit integer. | ||
* @param {number} num A 16-bit integer. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function sign_(num) { | ||
return num > 32768 ? num - 65536 : num; | ||
} | ||
/** | ||
* Compress a 16-bit PCM sample into a 4-bit ADPCM sample. | ||
* @param {number} sample The sample. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function encodeSample_(sample) { | ||
/** @type {number} */ | ||
let delta = sample - encoderPredicted_; | ||
/** @type {number} */ | ||
let value = 0; | ||
if (delta >= 0) { | ||
value = 0; | ||
} else { | ||
value = 8; | ||
delta = -delta; | ||
/** | ||
* Set the value for encoderPredicted_ and encoderIndex_ | ||
* after each sample is compressed. | ||
* @param {number} value The compressed ADPCM sample | ||
* @param {number} diff The calculated difference | ||
* @private | ||
*/ | ||
function updateEncoder_(value, diff) { | ||
if (value & 8) { | ||
encoderPredicted_ -= diff; | ||
} else { | ||
encoderPredicted_ += diff; | ||
} | ||
if (encoderPredicted_ < -0x8000) { | ||
encoderPredicted_ = -0x8000; | ||
} else if (encoderPredicted_ > 0x7fff) { | ||
encoderPredicted_ = 0x7fff; | ||
} | ||
encoderIndex_ += INDEX_TABLE[value & 7]; | ||
if (encoderIndex_ < 0) { | ||
encoderIndex_ = 0; | ||
} else if (encoderIndex_ > 88) { | ||
encoderIndex_ = 88; | ||
} | ||
} | ||
/** @type {number} */ | ||
let step = STEP_TABLE[encoderIndex_]; | ||
/** @type {number} */ | ||
let diff = step >> 3; | ||
if (delta > step) { | ||
value |= 4; | ||
delta -= step; | ||
diff += step; | ||
} | ||
step >>= 1; | ||
if (delta > step) { | ||
value |= 2; | ||
delta -= step; | ||
diff += step; | ||
} | ||
step >>= 1; | ||
if (delta > step) { | ||
value |= 1; | ||
diff += step; | ||
} | ||
updateEncoder_(value, diff); | ||
return value; | ||
} | ||
/** | ||
* Set the value for encoderPredicted_ and encoderIndex_ | ||
* after each sample is compressed. | ||
* @param {number} value The compressed ADPCM sample | ||
* @param {number} diff The calculated difference | ||
* @private | ||
*/ | ||
function updateEncoder_(value, diff) { | ||
if (value & 8) { | ||
encoderPredicted_ -= diff; | ||
} else { | ||
encoderPredicted_ += diff; | ||
/** | ||
* Decode a 4-bit ADPCM sample into a 16-bit PCM sample. | ||
* @param {number} nibble A 4-bit adpcm sample. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function decodeSample_(nibble) { | ||
/** @type {number} */ | ||
let difference = 0; | ||
if (nibble & 4) { | ||
difference += decoderStep_; | ||
} | ||
if (nibble & 2) { | ||
difference += decoderStep_ >> 1; | ||
} | ||
if (nibble & 1) { | ||
difference += decoderStep_ >> 2; | ||
} | ||
difference += decoderStep_ >> 3; | ||
if (nibble & 8) { | ||
difference = -difference; | ||
} | ||
decoderPredicted_ += difference; | ||
if (decoderPredicted_ > 32767) { | ||
decoderPredicted_ = 32767; | ||
} else if (decoderPredicted_ < -32767) { | ||
decoderPredicted_ = -32767; | ||
} | ||
updateDecoder_(nibble); | ||
return decoderPredicted_; | ||
} | ||
if (encoderPredicted_ < -0x8000) { | ||
encoderPredicted_ = -0x8000; | ||
} else if (encoderPredicted_ > 0x7fff) { | ||
encoderPredicted_ = 0x7fff; | ||
} | ||
encoderIndex_ += INDEX_TABLE[value & 7]; | ||
if (encoderIndex_ < 0) { | ||
encoderIndex_ = 0; | ||
} else if (encoderIndex_ > 88) { | ||
encoderIndex_ = 88; | ||
} | ||
} | ||
/** | ||
* Decode a 4-bit ADPCM sample into a 16-bit PCM sample. | ||
* @param {number} nibble A 4-bit adpcm sample. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function decodeSample_(nibble) { | ||
/** @type {number} */ | ||
let difference = 0; | ||
if (nibble & 4) { | ||
difference += decoderStep_; | ||
/** | ||
* Update the index and step after decoding a sample. | ||
* @param {number} nibble A 4-bit adpcm sample. | ||
* @private | ||
*/ | ||
function updateDecoder_(nibble) { | ||
decoderIndex_ += INDEX_TABLE[nibble]; | ||
if (decoderIndex_ < 0) { | ||
decoderIndex_ = 0; | ||
} else if (decoderIndex_ > 88) { | ||
decoderIndex_ = 88; | ||
} | ||
decoderStep_ = STEP_TABLE[decoderIndex_]; | ||
} | ||
if (nibble & 2) { | ||
difference += decoderStep_ >> 1; | ||
} | ||
if (nibble & 1) { | ||
difference += decoderStep_ >> 2; | ||
} | ||
difference += decoderStep_ >> 3; | ||
if (nibble & 8) { | ||
difference = -difference; | ||
} | ||
decoderPredicted_ += difference; | ||
if (decoderPredicted_ > 32767) { | ||
decoderPredicted_ = 32767; | ||
} else if (decoderPredicted_ < -32767) { | ||
decoderPredicted_ = -32767; | ||
} | ||
updateDecoder_(nibble); | ||
return decoderPredicted_; | ||
} | ||
/** | ||
* Update the index and step after decoding a sample. | ||
* @param {number} nibble A 4-bit adpcm sample. | ||
* @private | ||
*/ | ||
function updateDecoder_(nibble) { | ||
decoderIndex_ += INDEX_TABLE[nibble]; | ||
if (decoderIndex_ < 0) { | ||
decoderIndex_ = 0; | ||
} else if (decoderIndex_ > 88) { | ||
decoderIndex_ = 88; | ||
/** | ||
* Return the head of a ADPCM sample block. | ||
* @param {number} sample The first sample of the block. | ||
* @return {!Array<number>} | ||
* @private | ||
*/ | ||
function blockHead_(sample) { | ||
encodeSample_(sample); | ||
/** @type {!Array<number>} */ | ||
let adpcmSamples = []; | ||
adpcmSamples.push(sample & 0xFF); | ||
adpcmSamples.push((sample >> 8) & 0xFF); | ||
adpcmSamples.push(encoderIndex_); | ||
adpcmSamples.push(0); | ||
return adpcmSamples; | ||
} | ||
decoderStep_ = STEP_TABLE[decoderIndex_]; | ||
} | ||
/** | ||
* Return the head of a ADPCM sample block. | ||
* @param {number} sample The first sample of the block. | ||
* @return {!Array<number>} | ||
* @private | ||
*/ | ||
function blockHead_(sample) { | ||
encodeSample_(sample); | ||
/** @type {!Array<number>} */ | ||
let adpcmSamples = []; | ||
adpcmSamples.push(sample & 0xFF); | ||
adpcmSamples.push((sample >> 8) & 0xFF); | ||
adpcmSamples.push(encoderIndex_); | ||
adpcmSamples.push(0); | ||
return adpcmSamples; | ||
} | ||
exports.encode = encode; | ||
exports.decode = decode; | ||
exports.encodeBlock = encodeBlock; | ||
exports.decodeBlock = decodeBlock; | ||
Object.defineProperty(exports, '__esModule', { value: true }); | ||
/***/ }) | ||
/******/ ]); | ||
}); | ||
}))); |
{ | ||
"name": "imaadpcm", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "IMA ADPCM codec in JavaScript.", | ||
@@ -10,2 +10,3 @@ "homepage": "https://github.com/rochars/imaadpcm", | ||
"module": "./index.js", | ||
"es2015": "./dist/imaadpcm.js", | ||
"browser": "./dist/imaadpcm.umd.js", | ||
@@ -34,10 +35,14 @@ "jsdelivr": "./dist/imaadpcm.min.js", | ||
}, | ||
"directories": { | ||
"dist": "./dist" | ||
}, | ||
"scripts": { | ||
"lint": "jshint index.js && jshint test", | ||
"test": "nyc --require=esm ./node_modules/mocha/bin/_mocha test --recursive -R min", | ||
"test-min": "node ./node_modules/mocha/bin/_mocha test --min --recursive -R min", | ||
"test-cjs": "node ./node_modules/mocha/bin/_mocha test --cjs --recursive -R min", | ||
"test-umd": "node ./node_modules/mocha/bin/_mocha test --umd --recursive -R min", | ||
"test-dist": "npm run test-min && npm run test-cjs && npm run test-umd", | ||
"pack": "webpack && npm run test-dist && npm run test", | ||
"test": "nyc --require=esm ./node_modules/mocha/bin/_mocha test --recursive -R dot", | ||
"test-min": "node ./node_modules/mocha/bin/_mocha test --min --recursive -R dot", | ||
"test-cjs": "node ./node_modules/mocha/bin/_mocha test --cjs --recursive -R dot", | ||
"test-umd": "node ./node_modules/mocha/bin/_mocha test --umd --recursive -R dot", | ||
"test-esm": "nyc ./node_modules/mocha/bin/_mocha test --esm --require=esm --recursive -R dot", | ||
"test-dist": "npm run test-min && npm run test-cjs && npm run test-umd && npm run test-esm", | ||
"pack": "rollup --config && webpack && npm run test-dist && npm run test", | ||
"doc": "./node_modules/.bin/jsdoc index.js -d docs -r README.md -t node_modules/docdash", | ||
@@ -59,4 +64,8 @@ "build": "npm run lint && npm run pack && npm run doc", | ||
"nyc": "^12.0.2", | ||
"webpack": "^3.12.0" | ||
"rollup": "^0.61.2", | ||
"rollup-plugin-commonjs": "^9.1.3", | ||
"rollup-plugin-node-resolve": "^3.3.0", | ||
"webpack": "^4.12.1", | ||
"webpack-cli": "^3.0.8" | ||
} | ||
} |
@@ -8,3 +8,2 @@ # imaadpcm | ||
## About | ||
IMA ADPCM codec in JavaScript. | ||
@@ -17,6 +16,27 @@ | ||
## Browser | ||
## Use | ||
### ES6 | ||
import imaadpcm from **imaadpcm.js**: | ||
```javascript | ||
import * as imaadpcm from 'imaadpcm.js'; | ||
let adpcmSamples = imaadpcm.encode(pcmSamples); | ||
``` | ||
### Node | ||
```javascript | ||
const imaadpcm = require("imaadpcm"); | ||
let adpcmSamples = imaadpcm.encode(pcmSamples); | ||
``` | ||
### Browser | ||
Use the compiled file in the */dist* folder: | ||
```html | ||
<script src="imaadpcm.min.js"></script> | ||
<script> | ||
var adpcmSamples = imaadpcm.encode(pcmSamples); | ||
pcmSamples = imaadpcm.decode(adpcmSamples); | ||
var adpcmBlock = imaadpcm.encodeBlock(pcmBlock); | ||
pcmBlock = imaadpcm.decodeBlock(adpcmBlock); | ||
</script> | ||
``` | ||
@@ -34,4 +54,12 @@ | ||
## Use | ||
Or as a ES6 module in modern browsers from [jspm](https://jspm.io): | ||
```html | ||
<script type="module"> | ||
import imaadpcm from 'https://dev.jspm.io/imaadpcm'; | ||
// ... | ||
</script> | ||
``` | ||
## Examples | ||
### Files: | ||
@@ -72,13 +100,2 @@ ```javascript | ||
### In the browser | ||
```html | ||
<script src="imaadpcm.min.js"></script> | ||
<script> | ||
var adpcmSamples = imaadpcm.encode(pcmSamples); | ||
pcmSamples = imaadpcm.decode(adpcmSamples); | ||
var adpcmBlock = imaadpcm.encodeBlock(pcmBlock); | ||
pcmBlock = imaadpcm.decodeBlock(adpcmBlock); | ||
</script> | ||
``` | ||
## API | ||
@@ -117,22 +134,10 @@ ```javascript | ||
## Distribution | ||
This library is implemented as a ES6 module and also distributed as a CommonJS module, UMD module and a compiled script for browsers. If your system does not pick one automatically for you, you can pick one in the **dist/** folder. | ||
- The CommonJS is the one used by Node. It is served in the "main" field of this library's package.json | ||
- The UMD module is compatible with Node, AMD and browsers. It is served in the "browser" field. | ||
- The compiled dist is browser-only and should be the one served by CDNs. | ||
- The "module" field points to "./index.js" and should be the default entry point. | ||
This library is a ES6 module also distributed as a CommonJS module, UMD and a compiled script for browsers. | ||
If you are using a module bundler to compile a module that depends on this library you might need to specify what is the correct entry point as some bundlers will assume "browser". In general, you should point to "module". | ||
- The **CommonJS** is the one used by Node. It is served in the "main" field of package.json | ||
- The **UMD** module is compatible with Node, AMD and browsers. It is served in the "browser" field. | ||
- The **compiled dist** is browser-only and should be the one served by CDNs. | ||
- The **ES6** dist is **imaadpcm.js**, served as "module" in package.json | ||
### webpack example: | ||
```javascript | ||
module.exports = { | ||
entry: './index.js', | ||
resolve: { | ||
// tells webpack to use 'module' or 'main' | ||
// not 'browser' | ||
mainFields: ['module', 'main'] | ||
}, | ||
... | ||
}; | ||
``` | ||
You may load both **imaadpcm.umd.js** and **imaadpcm.min.js** in the browser with ```<script>``` tags. | ||
@@ -143,5 +148,2 @@ ## References | ||
## Legal | ||
[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Frochars%2Fimaadpcm.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Frochars%2Fimaadpcm?ref=badge_large) | ||
### LICENSE | ||
@@ -148,0 +150,0 @@ Copyright (c) 2018 Rafael da Silva Rocha. |
63672
12
1686
164
16