Comparing version 1.0.0 to 1.0.1
@@ -8,4 +8,3 @@ /* | ||
*/ | ||
(function(f){function a(b){if(e[b])return e[b].a;var d=e[b]={m:b,f:!1,a:{}};f[b].call(d.a,d,d.a,a);d.f=!0;return d.a}var e={};a.l=f;a.h=e;a.b=function(b,d){a.c(b)||Object.defineProperty(b,"a",{configurable:!1,enumerable:!0,get:d})};a.i=function(b){var d=b&&b.g?function(){return b["default"]}:function(){return b};a.b(d,d);return d};a.c=function(a){return Object.prototype.hasOwnProperty.call(a,"a")};a.j="";return a(a.o=0)})([function(){var f={2:4,4:16,8:256,16:65536,24:16777216,32:4294967296,40:1099511627776, | ||
48:281474976710656};window.toBitDepth=function(a,e,b){if(e!=b){var d="8 16 24 32 32f 64".split(" ");if(-1==d.indexOf(e)||-1==d.indexOf(b))throw Error("Invalid bit depth.");d=a.length;for(var g=parseInt(f[parseInt(e,10)]/2,10),h=parseInt(f[parseInt(b,10)]/2,10),c=0;c<d;c++)"8"==e&&(a[c]-=128),"32f"==b||"64"==b?"32f"!=e&&"64"!=e&&(a[c]=0<a[c]?a[c]/(g-1):a[c]/g):(a[c]="32f"==e||"64"==e?0<a[c]?a[c]*(h-1):a[c]*h:0<a[c]?parseInt(a[c]/(g-1)*h-1,10):parseInt(a[c]/g*h,10),"8"==b&&(a[c]+=128))}};window.BitDepthMaxValues= | ||
f}]); | ||
(function(f){function c(a){if(d[a])return d[a].a;var b=d[a]={m:a,f:!1,a:{}};f[a].call(b.a,b,b.a,c);b.f=!0;return b.a}var d={};c.l=f;c.h=d;c.b=function(a,b){c.c(a)||Object.defineProperty(a,"a",{configurable:!1,enumerable:!0,get:b})};c.i=function(a){var b=a&&a.g?function(){return a["default"]}:function(){return a};c.b(b,b);return b};c.c=function(a){return Object.prototype.hasOwnProperty.call(a,"a")};c.j="";return c(c.o=0)})([function(){var f={8:256,16:65536,24:16777216,32:4294967296};window.toBitDepth= | ||
function(c,d,a){if(d!=a){var b="8 16 24 32 32f 64".split(" ");if(-1==b.indexOf(d)||-1==b.indexOf(a))throw Error("Invalid bit depth.");b=c.length;for(var h=0;h<b;h++){var e=c[h];"8"==d&&(e-=128);if("32f"==a||"64"==a){var g=parseInt(f[d]/2,10);"32f"!=d&&"64"!=d&&(e=0<e?e/(g-1):e/g)}else{g=parseInt(f[d]/2,10);var k=parseInt(f[a]/2,10);e="32f"==d||"64"==d?0<e?e*(k-1):e*k:0<e?parseInt(e/(g-1)*k-1,10):parseInt(e/g*k,10);"8"==a&&(e+=128)}c[h]=e}}};window.BitDepthMaxValues=f}]); |
@@ -79,14 +79,10 @@ /******/ (function(modules) { // webpackBootstrap | ||
/** | ||
* Max number of values for each bit depth. | ||
* Max number of different values for each bit depth. | ||
* @enum {number} | ||
*/ | ||
const BitDepthMaxValues = { | ||
2: 4, | ||
4: 16, | ||
8: 256, | ||
16: 65536, | ||
24: 16777216, | ||
32: 4294967296, | ||
40: 1099511627776, | ||
48: 281474976710656 | ||
32: 4294967296 | ||
}; | ||
@@ -97,3 +93,3 @@ | ||
* The input array is modified in-place. | ||
* @param {!Array<number>} data The data. | ||
* @param {!Array<number>} samples The samples. | ||
* @param {string} originalBitDepth The original bit depth of the data. | ||
@@ -104,3 +100,3 @@ * One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function toBitDepth(data, originalBitDepth, targetBitDepth) { | ||
function toBitDepth(samples, originalBitDepth, targetBitDepth) { | ||
if (originalBitDepth == targetBitDepth) { | ||
@@ -110,24 +106,10 @@ return; | ||
validateBitDepths(originalBitDepth, targetBitDepth); | ||
let len = data.length; | ||
let len = samples.length; | ||
// Get the max values for both original and target bit depth | ||
let oldMaxValue = | ||
parseInt((BitDepthMaxValues[parseInt(originalBitDepth, 10)]) / 2, 10); | ||
let newMaxValue = | ||
parseInt((BitDepthMaxValues[parseInt(targetBitDepth, 10)]) / 2, 10); | ||
// needs dithering if the target bit depth | ||
// is lower than the original bit depth | ||
//if (parseInt(targetBitDepth, 10) < parseInt(originalBitDepth, 10)) { | ||
// TODO: dithering | ||
//} | ||
for (let i=0; i<len; i++) { | ||
for (let i=0; i<len; i++) { | ||
let sample = samples[i]; | ||
// 8-bit samples are unsigned; | ||
// They are signed here before conversion | ||
// (other bit depths are all signed) | ||
if (originalBitDepth == "8") { | ||
data[i] -= 128; | ||
} | ||
sample = sign8Bit(sample, originalBitDepth); | ||
@@ -138,9 +120,3 @@ // If it is a float-to-float or int-to-float conversion then | ||
if (targetBitDepth == "32f" || targetBitDepth == "64") { | ||
if (originalBitDepth != "32f" && originalBitDepth != "64") { | ||
if (data[i] > 0) { | ||
data[i] = data[i] / (oldMaxValue - 1); | ||
} else { | ||
data[i] = data[i] / oldMaxValue; | ||
} | ||
} | ||
sample = toFloat(sample, originalBitDepth); | ||
@@ -150,34 +126,93 @@ // If it is a float-to-int or int-to-int conversion then the | ||
}else { | ||
// If the original samples are float, then they are already | ||
// normalized between -1.0 and 1.0; All that is need is to | ||
// multiply the sample values by the new bit depth max value | ||
if (originalBitDepth == "32f" || originalBitDepth == "64" ) { | ||
if (data[i] > 0) { | ||
data[i] = data[i] * (newMaxValue - 1); | ||
} else { | ||
data[i] = data[i] * newMaxValue; | ||
} | ||
sample = toInt(sample, originalBitDepth, targetBitDepth); | ||
} | ||
samples[i] = sample; | ||
} | ||
} | ||
// If the original samples are integers, then they need to be | ||
// divided by the maximum values of its original bit depth | ||
// (to normalize them between -1.0 and .10) and then multiply | ||
// them by the new bit depth max value | ||
} else { | ||
if (data[i] > 0) { | ||
data[i] = parseInt((data[i] / (oldMaxValue - 1)) * newMaxValue - 1, 10); | ||
} else { | ||
data[i] = parseInt((data[i] / oldMaxValue) * newMaxValue, 10); | ||
} | ||
} | ||
// Make the samples unsigned if the target bit depth is "8" | ||
if (targetBitDepth == "8") { | ||
data[i] += 128; | ||
} | ||
} | ||
/** | ||
* Sign unsigned 8-bit data. | ||
* @param {number} sample The sample. | ||
* @param {string} originalBitDepth The original bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function sign8Bit(sample, originalBitDepth) { | ||
if (originalBitDepth == "8") { | ||
sample -= 128; | ||
} | ||
return sample; | ||
} | ||
/** | ||
* Unsign signed 8-bit data. | ||
* @param {number} sample The sample. | ||
* @param {string} targetBitDepth The target bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function unsign8Bit(sample, targetBitDepth) { | ||
if (targetBitDepth == "8") { | ||
sample += 128; | ||
} | ||
return sample; | ||
} | ||
/** | ||
* Change the bit depth from int to float. | ||
* The input array is modified in-place. | ||
* @param {number} sample The sample. | ||
* @param {string} originalBitDepth The original bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function toFloat(sample, originalBitDepth) { | ||
let oldMaxValue = parseInt((BitDepthMaxValues[originalBitDepth]) / 2, 10); | ||
if (originalBitDepth != "32f" && originalBitDepth != "64") { | ||
if (sample > 0) { | ||
sample = sample / (oldMaxValue - 1); | ||
} else { | ||
sample = sample / oldMaxValue; | ||
} | ||
} | ||
return sample; | ||
} | ||
/** | ||
* Change the bit depth of the data. | ||
* The input array is modified in-place. | ||
* @param {number} sample The sample. | ||
* @param {string} originalBitDepth The original bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
* @param {string} targetBitDepth The new bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function toInt(sample, originalBitDepth, targetBitDepth) { | ||
// If the original samples are float, then they are already | ||
// normalized between -1.0 and 1.0; All that is need is to | ||
// multiply the sample values by the new bit depth max value | ||
let oldMaxValue = parseInt((BitDepthMaxValues[originalBitDepth]) / 2, 10); | ||
let newMaxValue = parseInt((BitDepthMaxValues[targetBitDepth]) / 2, 10); | ||
if (originalBitDepth == "32f" || originalBitDepth == "64" ) { | ||
if (sample > 0) { | ||
sample = sample * (newMaxValue - 1); | ||
} else { | ||
sample = sample * newMaxValue; | ||
} | ||
// If the original samples are integers, then they need to be | ||
// divided by the maximum values of its original bit depth | ||
// (to normalize them between -1.0 and .10) and then multiply | ||
// them by the new bit depth max value | ||
} else { | ||
if (sample > 0) { | ||
sample = | ||
parseInt((sample / (oldMaxValue - 1)) * newMaxValue - 1, 10); | ||
} else { | ||
sample = parseInt((sample / oldMaxValue) * newMaxValue, 10); | ||
} | ||
} | ||
// Make the samples unsigned if the target bit depth is "8" | ||
return unsign8Bit(sample, targetBitDepth); | ||
} | ||
/** | ||
* Validate the bit depth. | ||
@@ -184,0 +219,0 @@ * @param {string} originalBitDepth The original bit depth. |
155
index.js
@@ -10,14 +10,10 @@ /*! | ||
/** | ||
* Max number of values for each bit depth. | ||
* Max number of different values for each bit depth. | ||
* @enum {number} | ||
*/ | ||
const BitDepthMaxValues = { | ||
2: 4, | ||
4: 16, | ||
8: 256, | ||
16: 65536, | ||
24: 16777216, | ||
32: 4294967296, | ||
40: 1099511627776, | ||
48: 281474976710656 | ||
32: 4294967296 | ||
}; | ||
@@ -28,3 +24,3 @@ | ||
* The input array is modified in-place. | ||
* @param {!Array<number>} data The data. | ||
* @param {!Array<number>} samples The samples. | ||
* @param {string} originalBitDepth The original bit depth of the data. | ||
@@ -35,3 +31,3 @@ * One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function toBitDepth(data, originalBitDepth, targetBitDepth) { | ||
function toBitDepth(samples, originalBitDepth, targetBitDepth) { | ||
if (originalBitDepth == targetBitDepth) { | ||
@@ -41,24 +37,10 @@ return; | ||
validateBitDepths(originalBitDepth, targetBitDepth); | ||
let len = data.length; | ||
let len = samples.length; | ||
// Get the max values for both original and target bit depth | ||
let oldMaxValue = | ||
parseInt((BitDepthMaxValues[parseInt(originalBitDepth, 10)]) / 2, 10); | ||
let newMaxValue = | ||
parseInt((BitDepthMaxValues[parseInt(targetBitDepth, 10)]) / 2, 10); | ||
// needs dithering if the target bit depth | ||
// is lower than the original bit depth | ||
//if (parseInt(targetBitDepth, 10) < parseInt(originalBitDepth, 10)) { | ||
// TODO: dithering | ||
//} | ||
for (let i=0; i<len; i++) { | ||
for (let i=0; i<len; i++) { | ||
let sample = samples[i]; | ||
// 8-bit samples are unsigned; | ||
// They are signed here before conversion | ||
// (other bit depths are all signed) | ||
if (originalBitDepth == "8") { | ||
data[i] -= 128; | ||
} | ||
sample = sign8Bit(sample, originalBitDepth); | ||
@@ -69,9 +51,3 @@ // If it is a float-to-float or int-to-float conversion then | ||
if (targetBitDepth == "32f" || targetBitDepth == "64") { | ||
if (originalBitDepth != "32f" && originalBitDepth != "64") { | ||
if (data[i] > 0) { | ||
data[i] = data[i] / (oldMaxValue - 1); | ||
} else { | ||
data[i] = data[i] / oldMaxValue; | ||
} | ||
} | ||
sample = toFloat(sample, originalBitDepth); | ||
@@ -81,34 +57,93 @@ // If it is a float-to-int or int-to-int conversion then the | ||
}else { | ||
// If the original samples are float, then they are already | ||
// normalized between -1.0 and 1.0; All that is need is to | ||
// multiply the sample values by the new bit depth max value | ||
if (originalBitDepth == "32f" || originalBitDepth == "64" ) { | ||
if (data[i] > 0) { | ||
data[i] = data[i] * (newMaxValue - 1); | ||
} else { | ||
data[i] = data[i] * newMaxValue; | ||
} | ||
sample = toInt(sample, originalBitDepth, targetBitDepth); | ||
} | ||
samples[i] = sample; | ||
} | ||
} | ||
// If the original samples are integers, then they need to be | ||
// divided by the maximum values of its original bit depth | ||
// (to normalize them between -1.0 and .10) and then multiply | ||
// them by the new bit depth max value | ||
} else { | ||
if (data[i] > 0) { | ||
data[i] = parseInt((data[i] / (oldMaxValue - 1)) * newMaxValue - 1, 10); | ||
} else { | ||
data[i] = parseInt((data[i] / oldMaxValue) * newMaxValue, 10); | ||
} | ||
} | ||
// Make the samples unsigned if the target bit depth is "8" | ||
if (targetBitDepth == "8") { | ||
data[i] += 128; | ||
} | ||
} | ||
/** | ||
* Sign unsigned 8-bit data. | ||
* @param {number} sample The sample. | ||
* @param {string} originalBitDepth The original bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function sign8Bit(sample, originalBitDepth) { | ||
if (originalBitDepth == "8") { | ||
sample -= 128; | ||
} | ||
return sample; | ||
} | ||
/** | ||
* Unsign signed 8-bit data. | ||
* @param {number} sample The sample. | ||
* @param {string} targetBitDepth The target bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function unsign8Bit(sample, targetBitDepth) { | ||
if (targetBitDepth == "8") { | ||
sample += 128; | ||
} | ||
return sample; | ||
} | ||
/** | ||
* Change the bit depth from int to float. | ||
* The input array is modified in-place. | ||
* @param {number} sample The sample. | ||
* @param {string} originalBitDepth The original bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function toFloat(sample, originalBitDepth) { | ||
let oldMaxValue = parseInt((BitDepthMaxValues[originalBitDepth]) / 2, 10); | ||
if (originalBitDepth != "32f" && originalBitDepth != "64") { | ||
if (sample > 0) { | ||
sample = sample / (oldMaxValue - 1); | ||
} else { | ||
sample = sample / oldMaxValue; | ||
} | ||
} | ||
return sample; | ||
} | ||
/** | ||
* Change the bit depth of the data. | ||
* The input array is modified in-place. | ||
* @param {number} sample The sample. | ||
* @param {string} originalBitDepth The original bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
* @param {string} targetBitDepth The new bit depth of the data. | ||
* One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function toInt(sample, originalBitDepth, targetBitDepth) { | ||
// If the original samples are float, then they are already | ||
// normalized between -1.0 and 1.0; All that is need is to | ||
// multiply the sample values by the new bit depth max value | ||
let oldMaxValue = parseInt((BitDepthMaxValues[originalBitDepth]) / 2, 10); | ||
let newMaxValue = parseInt((BitDepthMaxValues[targetBitDepth]) / 2, 10); | ||
if (originalBitDepth == "32f" || originalBitDepth == "64" ) { | ||
if (sample > 0) { | ||
sample = sample * (newMaxValue - 1); | ||
} else { | ||
sample = sample * newMaxValue; | ||
} | ||
// If the original samples are integers, then they need to be | ||
// divided by the maximum values of its original bit depth | ||
// (to normalize them between -1.0 and .10) and then multiply | ||
// them by the new bit depth max value | ||
} else { | ||
if (sample > 0) { | ||
sample = | ||
parseInt((sample / (oldMaxValue - 1)) * newMaxValue - 1, 10); | ||
} else { | ||
sample = parseInt((sample / oldMaxValue) * newMaxValue, 10); | ||
} | ||
} | ||
// Make the samples unsigned if the target bit depth is "8" | ||
return unsign8Bit(sample, targetBitDepth); | ||
} | ||
/** | ||
* Validate the bit depth. | ||
@@ -115,0 +150,0 @@ * @param {string} originalBitDepth The original bit depth. |
{ | ||
"name": "bitdepth", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Change the bit depth of audio samples to and from 8, 16, 24, 32, 32 IEEE & 64-bit.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/rochars/bitdepth", |
@@ -6,2 +6,4 @@ # bitdepth | ||
[![Build Status](https://travis-ci.org/rochars/bitdepth.svg?branch=master)](https://travis-ci.org/rochars/bitdepth) [![Build status](https://ci.appveyor.com/api/projects/status/rxyv4w8yo5ny97w0?svg=true)](https://ci.appveyor.com/project/rochars/bitdepth) [![codecov](https://codecov.io/gh/rochars/bitdepth/branch/master/graph/badge.svg)](https://codecov.io/gh/rochars/bitdepth) [![NPM version](https://img.shields.io/npm/v/bitdepth.svg?style=flat)](https://www.npmjs.com/package/bitdepth) [![NPM downloads](https://img.shields.io/npm/dm/bitdepth.svg?style=flat)](https://www.npmjs.com/package/bitdepth) | ||
## Install | ||
@@ -33,3 +35,3 @@ ``` | ||
* The input array is modified in-place. | ||
* @param {!Array<number>} data The data. | ||
* @param {!Array<number>} samples The samples. | ||
* @param {string} originalBitDepth The original bit depth of the data. | ||
@@ -40,3 +42,3 @@ * One of "8", "16", "24", "32", "32f", "64" | ||
*/ | ||
function toBitDepth(data, originalBitDepth, targetBitDepth) { | ||
function toBitDepth(samples, originalBitDepth, targetBitDepth) { | ||
``` | ||
@@ -43,0 +45,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
1058697
965
64