Comparing version 4.1.2 to 5.0.0
# CHANGELOG | ||
## 5.0.0 - 2019-12-31 | ||
- New package structure: | ||
* dist file is "./dist/imaadpcm.js", a UMD served as "main" | ||
* ES6 source is "./index.js", served as "module" | ||
## v4.1.2 (2019-09-12) | ||
@@ -4,0 +9,0 @@ - Fix: output array in correct size |
@@ -1,325 +0,4 @@ | ||
/* | ||
* imaadpcm: IMA ADPCM codec in JavaScript. | ||
* Derived from https://github.com/acida/pyima | ||
* Copyright (c) 2016 acida. MIT License. | ||
* Copyright (c) 2018-2019 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 API and private methods. | ||
*/ | ||
/** @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 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 {!Int16Array} samples A array of samples. | ||
* @return {!Uint8Array} | ||
*/ | ||
function encode(samples) { | ||
/** @type {!Uint8Array} */ | ||
let adpcmSamples = new Uint8Array((samples.length)); | ||
/** @type {!Array<number>} */ | ||
let block = []; | ||
/** @type {number} */ | ||
let fileIndex = 0; | ||
/** @type {number} */ | ||
let blockCount = 0; | ||
for (let i=0; i<samples.length; i++) { | ||
if ((i % 505 == 0 && i != 0)) { | ||
adpcmSamples.set(encodeBlock(block), fileIndex); | ||
fileIndex += 256; | ||
block = []; | ||
blockCount++; | ||
} | ||
block.push(samples[i]); | ||
} | ||
let samplesLength = samples.length / 2; | ||
if (samplesLength % 2) { | ||
samplesLength++; | ||
} | ||
return adpcmSamples.slice(0, samplesLength + 512 + blockCount * 4); | ||
} | ||
/** | ||
* Decode IMA ADPCM samples into 16-bit PCM samples. | ||
* @param {!Uint8Array} adpcmSamples A array of ADPCM samples. | ||
* @param {number} blockAlign The block size. | ||
* @return {!Int16Array} | ||
*/ | ||
function decode(adpcmSamples, blockAlign=256) { | ||
/** @type {!Int16Array} */ | ||
let samples = new Int16Array(adpcmSamples.length * 2); | ||
/** @type {!Array<number>} */ | ||
let block = []; | ||
/** @type {number} */ | ||
let fileIndex = 0; | ||
for (let i=0; i<adpcmSamples.length; i++) { | ||
if (i % blockAlign == 0 && i != 0) { | ||
let decoded = decodeBlock(block); | ||
samples.set(decoded, fileIndex); | ||
fileIndex += decoded.length; | ||
block = []; | ||
} | ||
block.push(adpcmSamples[i]); | ||
} | ||
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); | ||
} | ||
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_, | ||
decoderPredicted_ | ||
]; | ||
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; | ||
} | ||
/** | ||
* 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; | ||
} | ||
/** @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; | ||
} | ||
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_; | ||
} | ||
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; | ||
} | ||
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; | ||
} | ||
export { encode, decode, encodeBlock, decodeBlock }; | ||
(function(g,n){"object"===typeof exports&&"undefined"!==typeof module?n(exports):"function"===typeof define&&define.amd?define(["exports"],n):(g=g||self,n(g.imaadpcm={}))})(this,function(g){function n(b){var a=b[0];q(a);var c=[];c.push(a&255);c.push(a>>8&255);c.push(m);c.push(0);for(a=3;a<b.length;a+=2){var d=q(b[a]),f=q(b[a+1]);c.push(f<<4|d)}return c}function t(b){var a=b[1]<<8|b[0];h=32768<a?a-65536:a;k=b[2];p=r[k];a=[h,h];for(var c=4;c<b.length;c++){var d=b[c],f=d>>4;a.push(u(f<<4^d));a.push(u(f))}return a} | ||
function q(b){var a=b-l;0<=a?b=0:(b=8,a=-a);var c=r[m],d=c>>3;a>c&&(b|=4,a-=c,d+=c);c>>=1;a>c&&(b|=2,a-=c,d+=c);c>>=1;a>c&&(b|=1,d+=c);a=b;l=a&8?l-d:l+d;-32768>l?l=-32768:32767<l&&(l=32767);m+=v[a&7];0>m?m=0:88<m&&(m=88);return b}function u(b){var a=0;b&4&&(a+=p);b&2&&(a+=p>>1);b&1&&(a+=p>>2);a+=p>>3;b&8&&(a=-a);h+=a;32767<h?h=32767:-32767>h&&(h=-32767);k+=v[b];0>k?k=0:88<k&&(k=88);p=r[k];return h}var v=[-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,p=7;g.decode=function(b,a){a=void 0===a?256:a;for(var c=new Int16Array(2*b.length),d=[],f=0,e=0;e<b.length;e++)0==e%a&&0!=e&&(d=t(d),c.set(d,f),f+=d.length,d=[]), | ||
d.push(b[e]);return c};g.decodeBlock=t;g.encode=function(b){for(var a=new Uint8Array(b.length),c=[],d=0,f=0,e=0;e<b.length;e++)0==e%505&&0!=e&&(a.set(n(c),d),d+=256,c=[],f++),c.push(b[e]);b=b.length/2;b%2&&b++;return a.slice(0,b+512+4*f)};g.encodeBlock=n;Object.defineProperty(g,"__esModule",{value:!0})}); |
@@ -1,2 +0,2 @@ | ||
// Type definitions for imaadpcm 4.0 | ||
// Type definitions for imaadpcm 5.0 | ||
// Project: https://github.com/rochars/imaadpcm | ||
@@ -6,8 +6,29 @@ // Definitions by: Rafael S. Rocha <https://github.com/rochars> | ||
/** | ||
* Encode 16-bit PCM samples into 4-bit IMA ADPCM samples. | ||
* @param {!Int16Array} samples A array of samples. | ||
* @return {!Uint8Array} | ||
*/ | ||
export function encode(samples: Int16Array): Uint8Array; | ||
/** | ||
* Decode IMA ADPCM samples into 16-bit PCM samples. | ||
* @param {!Uint8Array} adpcmSamples A array of ADPCM samples. | ||
* @param {number} blockAlign The block size. | ||
* @return {!Int16Array} | ||
*/ | ||
export function decode(samples: Uint8Array, blockAlign?: number): Int16Array; | ||
/** | ||
* 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>} | ||
*/ | ||
export function encodeBlock(block: Array<number>): Array<number>; | ||
/** | ||
* Decode a block of ADPCM samples into 16-bit PCM samples. | ||
* @param {!Array<number>} block A adpcm sample block. | ||
* @return {!Array<number>} | ||
*/ | ||
export function decodeBlock(block: Array<number>): Array<number>; |
13
index.js
/* | ||
* imaadpcm: IMA ADPCM codec in JavaScript. | ||
* Derived from https://github.com/acida/pyima | ||
* Copyright (c) 2018-2019 Rafael da Silva Rocha. | ||
* Copyright (c) 2016 acida. MIT License. | ||
* Copyright (c) 2018-2019 Rafael da Silva Rocha. | ||
* | ||
@@ -94,3 +93,3 @@ * Permission is hereby granted, free of charge, to any person obtaining | ||
*/ | ||
export function encode(samples) { | ||
function encode(samples) { | ||
/** @type {!Uint8Array} */ | ||
@@ -126,3 +125,3 @@ let adpcmSamples = new Uint8Array((samples.length)); | ||
*/ | ||
export function decode(adpcmSamples, blockAlign=256) { | ||
function decode(adpcmSamples, blockAlign=256) { | ||
/** @type {!Int16Array} */ | ||
@@ -151,3 +150,3 @@ let samples = new Int16Array(adpcmSamples.length * 2); | ||
*/ | ||
export function encodeBlock(block) { | ||
function encodeBlock(block) { | ||
/** @type {!Array<number>} */ | ||
@@ -170,3 +169,3 @@ let adpcmSamples = blockHead_(block[0]); | ||
*/ | ||
export function decodeBlock(block) { | ||
function decodeBlock(block) { | ||
decoderPredicted_ = sign_((block[1] << 8) | block[0]); | ||
@@ -333,1 +332,3 @@ decoderIndex_ = block[2]; | ||
} | ||
export { encode, decode, encodeBlock, decodeBlock }; |
{ | ||
"name": "imaadpcm", | ||
"version": "4.1.2", | ||
"version": "5.0.0", | ||
"description": "IMA ADPCM codec in JavaScript.", | ||
@@ -8,8 +8,4 @@ "homepage": "https://github.com/rochars/imaadpcm", | ||
"license": "MIT", | ||
"main": "./dist/imaadpcm.cjs.js", | ||
"main": "./dist/imaadpcm.js", | ||
"module": "./index.js", | ||
"es2015": "./dist/imaadpcm.js", | ||
"browser": "./dist/imaadpcm.umd.js", | ||
"jsdelivr": "./dist/imaadpcm.min.js", | ||
"unpkg": "./dist/imaadpcm.min.js", | ||
"types": "./index.d.ts", | ||
@@ -37,14 +33,22 @@ "engines": { | ||
"directories": { | ||
"dist": "./dist" | ||
"dist": "dist", | ||
"externs": "externs" | ||
}, | ||
"files": [ | ||
"dist", | ||
"externs", | ||
"index.js", | ||
"index.d.ts", | ||
"LICENSE", | ||
"CHANGELOG.md", | ||
"README.md" | ||
], | ||
"scripts": { | ||
"lint": "jshint index.js && jshint 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 && npm run test-dist && npm run test", | ||
"doc": "./node_modules/.bin/jsdoc index.js -d docs -r README.md -t node_modules/docdash", | ||
"lint": "jshint index.js externs", | ||
"test": "nyc ./node_modules/mocha/bin/_mocha test/src --recursive -R dot", | ||
"test-umd": "node ./node_modules/mocha/bin/_mocha test/src --umd --recursive -R dot", | ||
"test-tsc": "tsc ./test/dist/TypeScript/main.ts && node -r esm ./test/dist/TypeScript/main.js", | ||
"test-dist": "npm run test-umd && npm run test-tsc", | ||
"pack": "npm run test && rollup -c && npm run test-dist", | ||
"doc": "./node_modules/.bin/jsdoc -c .jsdocrc index.js -d docs -r README.md -t node_modules/docdash", | ||
"build": "npm run lint && npm run pack && npm run doc", | ||
@@ -54,18 +58,15 @@ "coverage": "nyc report --reporter=lcov > coverage.lcov && codecov" | ||
"devDependencies": { | ||
"browser-env": "^3.2.6", | ||
"chai": "^4.1.2", | ||
"codecov": "^3.0.2", | ||
"docdash": "^0.4.0", | ||
"@ampproject/rollup-plugin-closure-compiler": "^0.13.0", | ||
"codecov": "^3.6.1", | ||
"docdash": "^1.1.1", | ||
"esm": "^3.2.25", | ||
"google-closure-compiler-js": "^20180610.0.0", | ||
"jsdoc": "^3.5.5", | ||
"jshint": "^2.9.5", | ||
"mocha": "^5.2.0", | ||
"jsdoc": "^3.6.3", | ||
"jshint": "^2.10.3", | ||
"mocha": "^6.2.2", | ||
"mocha-lcov-reporter": "^1.3.0", | ||
"nyc": "^12.0.2", | ||
"rollup": "^0.61.2", | ||
"rollup-plugin-closure-compiler-js": "^1.0.6", | ||
"rollup-plugin-commonjs": "^9.1.3", | ||
"rollup-plugin-node-resolve": "^3.3.0" | ||
} | ||
"nyc": "^14.1.1", | ||
"rollup": "^1.27.14", | ||
"typescript": "^3.7.4" | ||
}, | ||
"dependencies": {} | ||
} |
# imaadpcm | ||
Copyright (c) 2018-2019 Rafael da Silva Rocha. | ||
https://github.com/rochars/imaadpcm | ||
Copyright (c) 2016 acida. MIT License. | ||
https://github.com/rochars/imaadpcm | ||
https://github.com/acida/pyima | ||
@@ -85,9 +87,8 @@ [![NPM version](https://img.shields.io/npm/v/imaadpcm.svg?style=for-the-badge)](https://www.npmjs.com/package/imaadpcm) [![Docs](https://img.shields.io/badge/docs-online-blue.svg?style=for-the-badge)](https://rochars.github.io/imaadpcm/index.html) | ||
## References | ||
http://www.cs.columbia.edu/~hgs/audio/dvi/ | ||
https://github.com/acida/pyima | ||
https://github.com/acida/pyima | ||
http://www.cs.columbia.edu/~hgs/audio/dvi/ | ||
## LICENSE | ||
Derived from https://github.com/acida/pyima | ||
Copyright (c) 2016 acida. MIT License. | ||
Copyright (c) 2018-2019 Rafael da Silva Rocha. | ||
Copyright (c) 2018-2019 Rafael da Silva Rocha. | ||
Copyright (c) 2016 acida. MIT License. | ||
@@ -94,0 +95,0 @@ Permission is hereby granted, free of charge, to any person obtaining |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
11
112
22051
8
408
1