Socket
Socket
Sign inDemoInstall

bitdepth

Package Overview
Dependencies
Maintainers
1
Versions
28
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bitdepth - npm Package Compare versions

Comparing version 7.0.2 to 8.0.0

externs/bitdepth.js

15

CHANGELOG.md
# CHANGELOG
## 8.0.0 - 2019-12-31
- New package structure:
* No more default exports
* dist file is "./dist/bitdepth.js", a UMD served as "main"
* ES6 source is "./index.js", served as "module"
- bitDepth() renamed to changeBitDepth. Used it like this:
```javascript
const changeBitDepth = require('bitdepth').changeBitDepth;
```
## v7.0.3 (2018-07-13)
- Fix: Trancate floating point samples on overflow or underflow.
## v7.0.2 (2018-07-08)

@@ -34,2 +47,2 @@ - UMD dist transpiled to ES5.

## 3.1.1
- Fix: always writing to output array
- Fix: always writing to output array

179

dist/bitdepth.js

@@ -1,172 +0,7 @@

/*
* bitdepth: Change the resolution of samples to and from any bit depth.
* https://github.com/rochars/bitdepth
*
* 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 The bitdepth() function and private helper functions.
*/
/** @module bitdepth */
/** @private */
const f64f32_ = new Float32Array(1);
/**
* 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.
*/
function bitDepth(input, original, target, output) {
validateBitDepth_(original);
validateBitDepth_(target);
/** @type {!Function} */
let toFunction = getBitDepthFunction_(original, target);
/** @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,
};
/** @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;
}
}
// change the resolution of the samples
for (let i=0; i<len; i++) {
output[i] = toFunction(input[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;
}
}
}
/**
* Change the bit depth from int to int.
* @param {number} sample The sample.
* @param {!Object<string, number>} args Data about the original and target bit depths.
* @return {number}
* @private
*/
function intToInt_(sample, args) {
if (sample > 0) {
sample = parseInt((sample / args.oldMax) * args.newMax, 10);
} else {
sample = parseInt((sample / args.oldMin) * args.newMin, 10);
}
return sample;
}
/**
* Change the bit depth from float to int.
* @param {number} sample The sample.
* @param {!Object<string, number>} args Data about the original and target bit depths.
* @return {number}
* @private
*/
function floatToInt_(sample, args) {
return parseInt(
sample > 0 ? sample * args.newMax : sample * args.newMin, 10);
}
/**
* Change the bit depth from int to float.
* @param {number} sample The sample.
* @param {!Object<string, number>} args Data about the original and target bit depths.
* @return {number}
* @private
*/
function intToFloat_(sample, args) {
return sample > 0 ? sample / args.oldMax : sample / args.oldMin;
}
/**
* 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}
* @private
*/
function getBitDepthFunction_(original, target) {
/** @type {!Function} */
let func = function(x) {return x;};
if (original != target) {
if (["32f", "64"].includes(original)) {
if (["32f", "64"].includes(target)) {
func = floatToFloat_;
} else {
func = floatToInt_;
}
} else {
if (["32f", "64"].includes(target)) {
func = intToFloat_;
} else {
func = intToInt_;
}
}
}
return func;
}
/**
* Validate the bit depth.
* @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.
* @private
*/
function validateBitDepth_(bitDepth) {
if ((bitDepth != "32f" && bitDepth != "64") &&
(parseInt(bitDepth, 10) < "8" || parseInt(bitDepth, 10) > "53")) {
throw new Error("Invalid bit depth.");
}
}
export default bitDepth;
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})});

@@ -1,2 +0,2 @@

// Type definitions for bitdepth 7.0
// Type definitions for bitdepth 8.0
// Project: https://github.com/rochars/bitdepth

@@ -6,2 +6,15 @@ // Definitions by: Rafael S. Rocha <https://github.com/rochars>

export default function bitDepth(input: ArrayLike<number>, original: string, target: string, output: ArrayLike<number>): void;
/**
* 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.
*/
export function changeBitDepth(
input: ArrayLike<number>,
original: string,
target: string,
output: ArrayLike<number>): void;
/*
* bitdepth: Change the resolution of samples to and from any bit depth.
* https://github.com/rochars/bitdepth
*
* Copyright (c) 2017-2018 Rafael da Silva Rocha.

@@ -29,3 +26,4 @@ *

/**
* @fileoverview The bitdepth() function and private helper functions.
* @fileoverview The changeBitdepth() function.
* @see https://github.com/rochars/bitdepth
*/

@@ -39,3 +37,3 @@

/**
* Change the bit depth of samples. The input array.
* Change the bit depth of samples.
* @param {!TypedArray} input The samples.

@@ -48,3 +46,3 @@ * @param {string} original The original bit depth of the data.

*/
export default function bitDepth(input, original, target, output) {
export function changeBitDepth(input, original, target, output) {
validateBitDepth_(original);

@@ -69,2 +67,5 @@ validateBitDepth_(target);

}
if (original == "32f" || original == "64") {
truncateSamples(input);
}
// change the resolution of the samples

@@ -175,1 +176,17 @@ for (let i=0; i<len; i++) {

}
/**
* Truncate float samples on overflow.
* @private
*/
function truncateSamples(samples) {
/** @type {number} */
let len = samples.length;
for (let i=0; i<len; i++) {
if (samples[i] > 1) {
samples[i] = 1;
} else if (samples[i] < -1) {
samples[i] = -1;
}
}
}
{
"name": "bitdepth",
"version": "7.0.2",
"description": "Change the resolution of samples.",
"version": "8.0.0",
"description": "Change the bit depth of samples.",
"homepage": "https://github.com/rochars/bitdepth",
"author": "Rafael S. Rocha <rocha.rafaelsilva@gmail.com>",
"author": "Rafael da Silva Rocha <rocha.rafaelsilva@gmail.com>",
"license": "MIT",
"main": "./dist/bitdepth.cjs.js",
"main": "./dist/bitdepth.js",
"module": "./index.js",
"es2015": "./dist/bitdepth.js",
"browser": "./dist/bitdepth.umd.js",
"jsdelivr": "./dist/bitdepth.min.js",
"unpkg": "./dist/bitdepth.min.js",
"types": "./index.d.ts",

@@ -23,9 +19,5 @@ "engines": {

"8-bit",
"11-bit",
"12-bit",
"20-bit",
"16-bit",
"24-bit",
"32-bit",
"48-bit",
"64-bit",

@@ -43,14 +35,23 @@ "integer",

"directories": {
"dist": "./dist"
"dist": "dist",
"externs": "externs"
},
"files": [
"dist",
"externs",
"index.js",
"index.d.ts",
"LICENSE",
"AUTHORS.md",
"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 test/src",
"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",

@@ -60,18 +61,15 @@ "coverage": "nyc report --reporter=lcov > coverage.lcov && codecov"

"devDependencies": {
"browser-env": "^3.2.5",
"chai": "^4.1.2",
"codecov": "^3.0.2",
"docdash": "^0.4.0",
"esm": "^3.0.51",
"google-closure-compiler-js": "^20180610.0.0",
"jsdoc": "^3.5.5",
"jshint": "^2.9.5",
"mocha": "^5.2.0",
"@ampproject/rollup-plugin-closure-compiler": "^0.13.0",
"codecov": "^3.6.1",
"docdash": "^1.1.1",
"esm": "^3.2.25",
"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": {}
}
# bitdepth
Copyright (c) 2017-2018 Rafael da Silva Rocha.
Copyright (c) 2017-2019 Rafael da Silva Rocha.
https://github.com/rochars/bitdepth
[![NPM version](https://img.shields.io/npm/v/bitdepth.svg?style=for-the-badge)](https://www.npmjs.com/package/bitdepth) [![Docs](https://img.shields.io/badge/docs-online-blue.svg?style=for-the-badge)](https://rochars.github.io/bitdepth/index.html) [![Tests](https://img.shields.io/badge/tests-online-blue.svg?style=for-the-badge)](https://rawgit.com/rochars/bitdepth/master/test/browser.html)
[![NPM version](https://img.shields.io/npm/v/bitdepth.svg?style=for-the-badge)](https://www.npmjs.com/package/bitdepth) [![Docs](https://img.shields.io/badge/docs-online-blue.svg?style=for-the-badge)](https://rochars.github.io/bitdepth/index.html) [![Tests](https://img.shields.io/badge/tests-online-blue.svg?style=for-the-badge)](https://rawgit.com/rochars/bitdepth/master/test/dist/browser.html)
[![Codecov](https://img.shields.io/codecov/c/github/rochars/bitdepth.svg?style=flat-square)](https://codecov.io/gh/rochars/bitdepth) [![Unix Build](https://img.shields.io/travis/rochars/bitdepth.svg?style=flat-square)](https://travis-ci.org/rochars/bitdepth) [![Windows Build](https://img.shields.io/appveyor/ci/rochars/bitdepth.svg?style=flat-square&logo=appveyor)](https://ci.appveyor.com/project/rochars/bitdepth) [![Scrutinizer](https://img.shields.io/scrutinizer/g/rochars/bitdepth.svg?style=flat-square&logo=scrutinizer)](https://scrutinizer-ci.com/g/rochars/bitdepth/)
Change the resolution of samples. Supported bit depths (to and from):
- "8": 8-bit int (unsigned)
- Anything between "9" and "53" (integers, signed)
- "32f": 32-bit float
- "64": 64-bit float
**bitdepth** is a module to change the bit depth of samples.

@@ -23,15 +19,13 @@ ## Install

```javascript
const bitDepth = require("bitdepth");
const changeBitDepth = require("bitdepth").changeBitDepth;
```
### ES module
import bitDepth from **./dist/bitdepth.js**:
```javascript
import bitDepth from './dist/bitdepth.js';
```
## Browser
Use the compiled file in the */dist* folder:
Use the **bitdepth.js** file in the */dist* folder:
```html
<script src="./dist/bitdepth.min.js"></script>
<script src="bitdepth.js"></script>
<script>
var changeBitDepth = bitdepth.changeBitDepth;
changeBitDepth(originalArray, "32f", "64", outputArray);
</script>
```

@@ -49,10 +43,2 @@

Or as a ES6 module in modern browsers from [jspm](https://jspm.io):
```html
<script type="module">
import bitDepth from 'https://dev.jspm.io/bitdepth';
// ...
</script>
```
## API

@@ -69,40 +55,7 @@ ```javascript

*/
function bitDepth(input, original, target, output) {}
export function changeBitDepth(input, original, target, output) {}
```
## Distribution
This library is a ES module also distributed as a CommonJS module, UMD module and a compiled script for browsers. It works out of the box in Node when installed with ```npm install bitdepth```. It includes a TypeScript definition file.
If you use the [Closure Compiler](https://github.com/google/closure-compiler), this package includes a externs file: **./externs.js**.
### If you are using this lib in a browser:
You may load both **./dist/bitdepth.umd.js** and **./dist/bitdepth.min.js** in the browser with ```<script>``` tags. Ideally you should use **bitdepth.min.js**. You can load it via the https://unpkg.com and https://www.jsdelivr.com/ CDNs:
[unpkg](https://unpkg.com/bitdepth):
```html
<script src="https://unpkg.com/bitdepth"></script>
```
[jsDelivr](https://cdn.jsdelivr.net/npm/bitdepth):
```html
<script src="https://cdn.jsdelivr.net/npm/bitdepth"></script>
```
### If you are using this lib as a dependency:
- The **CommonJS** dist is **./dist/bitdepth.cjs.js**. It is the dist file used by Node. It is served in the "main" field of package.json and is the source you are running when you **npm install bitdepth**. It is not compiled or minified.
- The **UMD** module is **./dist/bitdepth.umd.js**. It is transpiled to ES5 and compatible with Node, AMD and browsers. It is served in the "browser" field of package.json.
- The **browser-only** dist is **./dist/bitdepth.min.js**. It is transpiled to ES5 and compiled. It is used in the "unpkg" and "jsdelivr" fields of package.json.
- The **ES6 dist** is **./dist/bitdepth.js**, served as "es2015" in package.json. It is not compiled/minified.
- **./index.js** is served as "module" in package.json. This should be the entry point for bundlers.
If your module bundler is using "browser" as the entry point **your dist should work the same** but will be a larger file.
## LICENSE
Copyright (c) 2017-2018 Rafael da Silva Rocha.
Copyright (c) 2017-2019 Rafael da Silva Rocha.

@@ -109,0 +62,0 @@ Permission is hereby granted, free of charge, to any person obtaining

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc