Comparing version 8.0.0 to 9.0.0
# CHANGELOG | ||
## 9.0.0 - 2020-01-16 | ||
- *output* param now is required to be a typed array. Previous versions | ||
would work with regulars arrays too, but for now on regular arrays wont | ||
work anymore. | ||
- changeBitDepth() params update: | ||
```javascript | ||
/** | ||
* Change the bit depth of PCM samples. | ||
* @param {!Array|!TypedArray} samples The original samples. | ||
* @param {string} bithDepth The original bit depth. | ||
* @param {!TypedArray} newSamples The output array. | ||
* @param {string} targetBitDepth The target bit depth. | ||
* @throws {Error} If original or target bit depths are not valid. | ||
*/ | ||
changeBitDepth(samples, bithDepth, newSamples, targetBitDepth); | ||
``` | ||
### Other changes | ||
- *originalSamples* type defined as {!Array|!TypedArray} to ease integration | ||
- Remove unecessary conversions | ||
## 8.0.0 - 2019-12-31 | ||
@@ -4,0 +27,0 @@ - New package structure: |
@@ -1,7 +0,7 @@ | ||
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,c,d){a!=Array.prototype&&a!=Object.prototype&&(a[c]=d.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this); | ||
$jscomp.polyfill=function(a,c,d,f){if(c){d=$jscomp.global;a=a.split(".");for(f=0;f<a.length-1;f++){var g=a[f];g in d||(d[g]={});d=d[g]}a=a[a.length-1];f=d[a];c=c(f);c!=f&&null!=c&&$jscomp.defineProperty(d,a,{configurable:!0,writable:!0,value:c})}};$jscomp.polyfill("Object.is",function(a){return a?a:function(a,d){return a===d?0!==a||1/a===1/d:a!==a&&d!==d}},"es6","es3"); | ||
$jscomp.polyfill("Array.prototype.includes",function(a){return a?a:function(a,d){var c=this;c instanceof String&&(c=String(c));var g=c.length,h=d||0;for(0>h&&(h=Math.max(h+g,0));h<g;h++){var k=c[h];if(k===a||Object.is(k,a))return!0}return!1}},"es7","es3"); | ||
$jscomp.checkStringArgs=function(a,c,d){if(null==a)throw new TypeError("The 'this' value for String.prototype."+d+" must not be null or undefined");if(c instanceof RegExp)throw new TypeError("First argument to String.prototype."+d+" must not be a regular expression");return a+""}; | ||
(function(a,c){"object"===typeof exports&&"undefined"!==typeof module?c(exports):"function"===typeof define&&define.amd?define(["exports"],c):(a=a||self,c(a.bitdepth={}))})(this,function(a){function c(a,b){return a=0<a?parseInt(a/b.oldMax*b.newMax,10):parseInt(a/b.oldMin*b.newMin,10)}function d(a,b){return parseInt(0<a?a*b.newMax:a*b.newMin,10)}function f(a,b){return 0<a?a/b.oldMax:a/b.oldMin}function g(a){n[0]=a;return n[0]}function h(a,b){var m=function(a){return a};a!=b&&(m=["32f","64"].includes(a)? | ||
["32f","64"].includes(b)?g:d:["32f","64"].includes(b)?f:c);return m}function k(a){if("32f"!=a&&"64"!=a&&("8">parseInt(a,10)||"53"<parseInt(a,10)))throw Error("Invalid bit depth.");}var n=new Float32Array(1);a.changeBitDepth=function(a,b,c,d){k(b);k(c);var f=h(b,c),g={oldMin:Math.pow(2,parseInt(b,10))/2,newMin:Math.pow(2,parseInt(c,10))/2,oldMax:Math.pow(2,parseInt(b,10))/2-1,newMax:Math.pow(2,parseInt(c,10))/2-1},l=a.length;if("8"==b)for(var e=0;e<l;e++)d[e]=a[e]-=128;if("32f"==b||"64"==b)for(b=a.length, | ||
e=0;e<b;e++)1<a[e]?a[e]=1:-1>a[e]&&(a[e]=-1);for(b=0;b<l;b++)d[b]=f(a[b],g);if("8"==c)for(a=0;a<l;a++)d[a]=d[a]+=128};Object.defineProperty(a,"__esModule",{value:!0})}); | ||
var $jscomp=$jscomp||{};$jscomp.scope={};$jscomp.ASSUME_ES5=!1;$jscomp.ASSUME_NO_NATIVE_MAP=!1;$jscomp.ASSUME_NO_NATIVE_SET=!1;$jscomp.SIMPLE_FROUND_POLYFILL=!1;$jscomp.defineProperty=$jscomp.ASSUME_ES5||"function"==typeof Object.defineProperties?Object.defineProperty:function(a,b,d){a!=Array.prototype&&a!=Object.prototype&&(a[b]=d.value)};$jscomp.getGlobal=function(a){return"undefined"!=typeof window&&window===a?a:"undefined"!=typeof global&&null!=global?global:a};$jscomp.global=$jscomp.getGlobal(this); | ||
$jscomp.polyfill=function(a,b,d,e){if(b){d=$jscomp.global;a=a.split(".");for(e=0;e<a.length-1;e++){var g=a[e];g in d||(d[g]={});d=d[g]}a=a[a.length-1];e=d[a];b=b(e);b!=e&&null!=b&&$jscomp.defineProperty(d,a,{configurable:!0,writable:!0,value:b})}};$jscomp.polyfill("Object.is",function(a){return a?a:function(a,d){return a===d?0!==a||1/a===1/d:a!==a&&d!==d}},"es6","es3"); | ||
$jscomp.polyfill("Array.prototype.includes",function(a){return a?a:function(a,d){var b=this;b instanceof String&&(b=String(b));var g=b.length,f=d||0;for(0>f&&(f=Math.max(f+g,0));f<g;f++){var k=b[f];if(k===a||Object.is(k,a))return!0}return!1}},"es7","es3"); | ||
$jscomp.checkStringArgs=function(a,b,d){if(null==a)throw new TypeError("The 'this' value for String.prototype."+d+" must not be null or undefined");if(b instanceof RegExp)throw new TypeError("First argument to String.prototype."+d+" must not be a regular expression");return a+""}; | ||
(function(a,b){"object"===typeof exports&&"undefined"!==typeof module?b(exports):"function"===typeof define&&define.amd?define(["exports"],b):(a=a||self,b(a.bitdepth={}))})(this,function(a){function b(a,c){return a=0<a?parseInt(a/c.oldMax*c.newMax,10):parseInt(a/c.oldMin*c.newMin,10)}function d(a,c){return parseInt(0<a?a*c.newMax:a*c.newMin,10)}function e(a,c){return 0<a?a/c.oldMax:a/c.oldMin}function g(a,c){var l=function(a){return a};a!=c&&(l=["32f","64"].includes(a)?d:["32f","64"].includes(c)? | ||
e:b);return l}function f(a){if("32f"!=a&&"64"!=a&&("8">parseInt(a,10)||"53"<parseInt(a,10)))throw Error("Invalid bit depth.");}function k(a,c,b){if("8"==a)for(a=b?-128:128,b=0;b<c.length;b++)c[b]=c[b]+=a}a.changeBitDepth=function(a,c,b,d){if(-1<["32f","64"].indexOf(c)&&-1<["32f","64"].indexOf(d))b.set(a);else{f(c);f(d);var e=g(c,d),l={oldMin:Math.pow(2,parseInt(c,10))/2,newMin:Math.pow(2,parseInt(d,10))/2,oldMax:Math.pow(2,parseInt(c,10))/2-1,newMax:Math.pow(2,parseInt(d,10))/2-1};k(c,a,!0);if(-1< | ||
["32f","64"].indexOf(c)){c=a.length;for(var h=0;h<c;h++)1<a[h]?a[h]=1:-1>a[h]&&(a[h]=-1)}for(c=0;c<a.length;c++)b[c]=e(a[c],l);k(d,b,!1)}};Object.defineProperty(a,"__esModule",{value:!0})}); |
@@ -26,3 +26,3 @@ /* | ||
/** | ||
* @fileoverview Externs for bitdepth 8.0 | ||
* @fileoverview Externs for bitdepth 9.0 | ||
* @see https://github.com/rochars/bitdepth | ||
@@ -33,10 +33,9 @@ * @externs | ||
/** | ||
* Change the bit depth of the samples. | ||
* @param {!TypedArray} input The samples. | ||
* @param {string} original The original bit depth of the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {string} target The desired bit depth for the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {!TypedArray} output The output array. | ||
* Change the bit depth of PCM samples. | ||
* @param {!Array|!TypedArray} samples The original samples. | ||
* @param {string} bithDepth The original bit depth. | ||
* @param {!TypedArray} newSamples The output array. | ||
* @param {string} targetBitDepth The target bit depth. | ||
* @throws {Error} If original or target bit depths are not valid. | ||
*/ | ||
function changeBitDepth(input, original, target, output) {} | ||
function changeBitDepth(samples, bithDepth, newSamples, targetBitDepth) {} |
@@ -1,2 +0,2 @@ | ||
// Type definitions for bitdepth 8.0 | ||
// Type definitions for bitdepth 9.0 | ||
// Project: https://github.com/rochars/bitdepth | ||
@@ -7,14 +7,13 @@ // Definitions by: Rafael S. Rocha <https://github.com/rochars> | ||
/** | ||
* Change the bit depth of the samples. | ||
* @param {!TypedArray} input The samples. | ||
* @param {string} original The original bit depth of the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {string} target The desired bit depth for the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {!TypedArray} output The output array. | ||
* Change the bit depth of PCM samples. | ||
* @param {!Array|!TypedArray} samples The original samples. | ||
* @param {string} bithDepth The original bit depth. | ||
* @param {!TypedArray} newSamples The output array. | ||
* @param {string} targetBitDepth The target bit depth. | ||
* @throws {Error} If original or target bit depths are not valid. | ||
*/ | ||
export function changeBitDepth( | ||
input: ArrayLike<number>, | ||
original: string, | ||
target: string, | ||
output: ArrayLike<number>): void; | ||
samples: ArrayLike<number>, | ||
bithDepth: string, | ||
newSamples: ArrayLike<number>, | ||
targetBitDepth: string): void; |
111
index.js
@@ -26,3 +26,3 @@ /* | ||
/** | ||
* @fileoverview The changeBitdepth() function. | ||
* @fileoverview A module to change the bit depth of PCM samples. | ||
* @see https://github.com/rochars/bitdepth | ||
@@ -33,47 +33,41 @@ */ | ||
/** @private */ | ||
const f64f32_ = new Float32Array(1); | ||
/** | ||
* Change the bit depth of samples. | ||
* @param {!TypedArray} input The samples. | ||
* @param {string} original The original bit depth of the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {string} target The desired bit depth for the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {!TypedArray} output The output array. | ||
* Change the bit depth of PCM samples. | ||
* @param {!Array|!TypedArray} samples The original samples. | ||
* @param {string} bithDepth The original bit depth. | ||
* @param {!TypedArray} newSamples The output array. | ||
* @param {string} targetBitDepth The target bit depth. | ||
* @throws {Error} If original or target bit depths are not valid. | ||
*/ | ||
export function changeBitDepth(input, original, target, output) { | ||
validateBitDepth_(original); | ||
validateBitDepth_(target); | ||
export function changeBitDepth(samples, bithDepth, newSamples, targetBitDepth) { | ||
// float to float, just copy the values | ||
if (["32f","64"].indexOf(bithDepth) > -1 && | ||
["32f","64"].indexOf(targetBitDepth) > -1) { | ||
newSamples.set(samples); | ||
return; | ||
} | ||
validateBitDepth_(bithDepth); | ||
validateBitDepth_(targetBitDepth); | ||
/** @type {!Function} */ | ||
let toFunction = getBitDepthFunction_(original, target); | ||
let toFunction = getBitDepthFunction_(bithDepth, targetBitDepth); | ||
/** @type {!Object<string, number>} */ | ||
let options = { | ||
oldMin: Math.pow(2, parseInt(original, 10)) / 2, | ||
newMin: Math.pow(2, parseInt(target, 10)) / 2, | ||
oldMax: (Math.pow(2, parseInt(original, 10)) / 2) - 1, | ||
newMax: (Math.pow(2, parseInt(target, 10)) / 2) - 1, | ||
oldMin: Math.pow(2, parseInt(bithDepth, 10)) / 2, | ||
newMin: Math.pow(2, parseInt(targetBitDepth, 10)) / 2, | ||
oldMax: (Math.pow(2, parseInt(bithDepth, 10)) / 2) - 1, | ||
newMax: (Math.pow(2, parseInt(targetBitDepth, 10)) / 2) - 1, | ||
}; | ||
/** @type {number} */ | ||
const len = input.length; | ||
// sign the samples if original is 8-bit | ||
if (original == "8") { | ||
for (let i=0; i<len; i++) { | ||
output[i] = input[i] -= 128; | ||
} | ||
sign8Bit_(bithDepth, samples, true); | ||
// FP to int need the original samples to be clipped | ||
// at 1 and -1 as files can have samples out of those boundaries | ||
if (["32f","64"].indexOf(bithDepth) > -1) { | ||
truncateSamples_(samples); | ||
} | ||
if (original == "32f" || original == "64") { | ||
truncateSamples(input); | ||
} | ||
// change the resolution of the samples | ||
for (let i=0; i<len; i++) { | ||
output[i] = toFunction(input[i], options); | ||
for (let i = 0; i< samples.length; i++) { | ||
newSamples[i] = toFunction(samples[i], options); | ||
} | ||
// unsign the samples if target is 8-bit | ||
if (target == "8") { | ||
for (let i=0; i<len; i++) { | ||
output[i] = output[i] += 128; | ||
} | ||
} | ||
sign8Bit_(targetBitDepth, newSamples, false); | ||
} | ||
@@ -84,3 +78,3 @@ | ||
* @param {number} sample The sample. | ||
* @param {!Object<string, number>} args Data about the original and target bit depths. | ||
* @param {!Object<string, number>} args Data about the bit depths. | ||
* @return {number} | ||
@@ -101,3 +95,3 @@ * @private | ||
* @param {number} sample The sample. | ||
* @param {!Object<string, number>} args Data about the original and target bit depths. | ||
* @param {!Object<string, number>} args Data about the bit depths. | ||
* @return {number} | ||
@@ -114,3 +108,3 @@ * @private | ||
* @param {number} sample The sample. | ||
* @param {!Object<string, number>} args Data about the original and target bit depths. | ||
* @param {!Object<string, number>} args Data about the bit depths. | ||
* @return {number} | ||
@@ -124,18 +118,5 @@ * @private | ||
/** | ||
* Change the bit depth from float to float. | ||
* @param {number} sample The sample. | ||
* @return {number} | ||
* @private | ||
*/ | ||
function floatToFloat_(sample) { | ||
f64f32_[0] = sample; | ||
return f64f32_[0]; | ||
} | ||
/** | ||
* Return the function to change the bit depth of a sample. | ||
* @param {string} original The original bit depth of the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {string} target The new bit depth of the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @return {!Function} | ||
@@ -149,7 +130,3 @@ * @private | ||
if (["32f", "64"].includes(original)) { | ||
if (["32f", "64"].includes(target)) { | ||
func = floatToFloat_; | ||
} else { | ||
func = floatToInt_; | ||
} | ||
func = floatToInt_; | ||
} else { | ||
@@ -169,4 +146,3 @@ if (["32f", "64"].includes(target)) { | ||
* @param {string} bitDepth The original bit depth. | ||
* Should be one of "8" ... "53", "32f" or "64". | ||
* @throws {Error} If any argument does not meet the criteria. | ||
* @throws {Error} If bit depth is not valid. | ||
* @private | ||
@@ -183,5 +159,6 @@ */ | ||
* Truncate float samples on overflow. | ||
* @param {!Array|!TypedArray} samples The samples. | ||
* @private | ||
*/ | ||
function truncateSamples(samples) { | ||
function truncateSamples_(samples) { | ||
/** @type {number} */ | ||
@@ -197,1 +174,17 @@ let len = samples.length; | ||
} | ||
/** | ||
* Sign samples if they are 8-bit. | ||
* @param {string} bitDepth The bit depth code. | ||
* @param {!Array|!TypedArray} samples The samples. | ||
* @param {boolean} sign True to sign, false to unsign. | ||
* @private | ||
*/ | ||
function sign8Bit_(bitDepth, samples, sign) { | ||
if (bitDepth == "8") { | ||
let factor = sign ? -128 : 128; | ||
for (let i = 0; i < samples.length; i++) { | ||
samples[i] = samples[i] += factor; | ||
} | ||
} | ||
} |
{ | ||
"name": "bitdepth", | ||
"version": "8.0.0", | ||
"description": "Change the bit depth of samples.", | ||
"version": "9.0.0", | ||
"description": "Change the bit depth of PCM samples.", | ||
"homepage": "https://github.com/rochars/bitdepth", | ||
@@ -6,0 +6,0 @@ "author": "Rafael da Silva Rocha <rocha.rafaelsilva@gmail.com>", |
@@ -8,3 +8,3 @@ # bitdepth | ||
**bitdepth** is a module to change the bit depth of samples. | ||
Change the bit depth of PCM samples. | ||
@@ -21,3 +21,10 @@ ## Install | ||
const changeBitDepth = require("bitdepth").changeBitDepth; | ||
// The params, in order: | ||
// - the original sample array | ||
// - the original bit depth | ||
// - the output array for the new samples | ||
// - the target bit depth | ||
changeBitDepth(originalArray, "32f", outputArray, "16"); | ||
``` | ||
**The outputArray must be a typed array**. | ||
@@ -29,4 +36,3 @@ ## Browser | ||
<script> | ||
var changeBitDepth = bitdepth.changeBitDepth; | ||
changeBitDepth(originalArray, "32f", "64", outputArray); | ||
bitdepth.changeBitDepth(originalArray, "32f", outputArray, "16"); | ||
</script> | ||
@@ -45,14 +51,28 @@ ``` | ||
## Bit depth codes | ||
The params informing the original and target bit depths are **strings** representing the bit depth code. Their values can be: | ||
- A string representing any integer from "8" to "53" (integer samples) | ||
- "32f" for 32 bit floating-point | ||
- "64" for 64 bit floating-point | ||
## Range of the samples | ||
This lib is inteded to be used with PCM data. Note that: | ||
- 8-bit samples range from 0 to 255 (unsigned) | ||
- Other integers are all signed | ||
- 32 fp and 64 range from -1 to 1 | ||
## Dithering | ||
This lib does not apply any dither to the samples. | ||
## API | ||
```javascript | ||
/** | ||
* Change the bit depth of samples. The input array. | ||
* @param {!TypedArray} input The samples. | ||
* @param {string} original The original bit depth of the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {string} target The desired bit depth for the data. | ||
* One of "8" ... "53", "32f", "64" | ||
* @param {!TypedArray} output The output array. | ||
* Change the bit depth of PCM samples. | ||
* @param {!Array|!TypedArray} samples The original samples. | ||
* @param {string} bithDepth The original bit depth. | ||
* @param {!TypedArray} newSamples The output array. | ||
* @param {string} targetBitDepth The target bit depth. | ||
* @throws {Error} If original or target bit depths are not valid. | ||
*/ | ||
export function changeBitDepth(input, original, target, output) {} | ||
function changeBitDepth(samples, bithDepth, newSamples, targetBitDepth) {}; | ||
``` | ||
@@ -59,0 +79,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
20157
97
0
235