@audio/effect-bitcrusher 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bitcrusher.js +34 -0
  2. package/package.json +38 -0
package/bitcrusher.js ADDED
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Bitcrusher — sample-rate reduction + bit-depth quantization.
3
+ */
4
+
5
+ let {round, pow} = Math
6
+
7
+ export default function bitcrusher (data, params) {
8
+ let bits = params.bits ?? 8 // target bit depth (1–24)
9
+ let rate = params.rate ?? 0.25 // sample rate ratio: 1 = full, 0.25 = quarter rate
10
+ let fs = params.fs || 44100
11
+
12
+ let step = 1 / rate // hold length in samples
13
+ let levels = pow(2, bits - 1) // quantization levels per polarity
14
+
15
+ if (params._held == null) {
16
+ params._held = 0
17
+ params._phase = 0
18
+ }
19
+ let held = params._held, phase = params._phase
20
+
21
+ for (let i = 0, l = data.length; i < l; i++) {
22
+ phase += 1
23
+ if (phase >= step) {
24
+ phase -= step
25
+ held = round(data[i] * levels) / levels
26
+ }
27
+ data[i] = held
28
+ }
29
+
30
+ params._held = held
31
+ params._phase = phase
32
+
33
+ return data
34
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@audio/effect-bitcrusher",
3
+ "version": "1.0.0",
4
+ "description": "Bitcrusher — sample-rate reduction + bit-depth quantization",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "bitcrusher.js",
8
+ "exports": {
9
+ ".": "./bitcrusher.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "bitcrusher.js"
14
+ ],
15
+ "keywords": [
16
+ "audio",
17
+ "dsp",
18
+ "effect",
19
+ "bitcrusher"
20
+ ],
21
+ "license": "MIT",
22
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
23
+ "repository": {
24
+ "type": "git",
25
+ "url": "git+https://github.com/audiojs/effect.git",
26
+ "directory": "packages/effect-bitcrusher"
27
+ },
28
+ "homepage": "https://github.com/audiojs/effect/tree/main/packages/effect-bitcrusher",
29
+ "bugs": {
30
+ "url": "https://github.com/audiojs/effect/issues"
31
+ },
32
+ "engines": {
33
+ "node": ">=18"
34
+ },
35
+ "publishConfig": {
36
+ "access": "public"
37
+ }
38
+ }