@audio/note-scale 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/package.json +33 -0
  2. package/scale.js +37 -0
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@audio/note-scale",
3
+ "version": "1.0.0",
4
+ "description": "Scales and pitch snapping — nearest-degree quantization (tune-snap / tuner substrate)",
5
+ "type": "module",
6
+ "sideEffects": false,
7
+ "main": "scale.js",
8
+ "exports": {
9
+ ".": "./scale.js",
10
+ "./package.json": "./package.json"
11
+ },
12
+ "files": [
13
+ "scale.js"
14
+ ],
15
+ "dependencies": {
16
+ "@audio/note-convert": "^1.0.0"
17
+ },
18
+ "keywords": [
19
+ "audio",
20
+ "music",
21
+ "scale",
22
+ "quantize",
23
+ "snap"
24
+ ],
25
+ "license": "MIT",
26
+ "author": "Dmitry Iv. <dfcreative@gmail.com>",
27
+ "engines": {
28
+ "node": ">=18"
29
+ },
30
+ "publishConfig": {
31
+ "access": "public"
32
+ }
33
+ }
package/scale.js ADDED
@@ -0,0 +1,37 @@
1
+ // Scales and pitch snapping — the tune-snap / tuner substrate.
2
+
3
+ import { hzToMidi, midiToHz } from '@audio/note-convert'
4
+
5
+ export const SCALES = {
6
+ chromatic: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
7
+ major: [0, 2, 4, 5, 7, 9, 11],
8
+ minor: [0, 2, 3, 5, 7, 8, 10],
9
+ 'harmonic-minor': [0, 2, 3, 5, 7, 8, 11],
10
+ 'melodic-minor': [0, 2, 3, 5, 7, 9, 11],
11
+ 'pentatonic-major': [0, 2, 4, 7, 9],
12
+ 'pentatonic-minor': [0, 3, 5, 7, 10],
13
+ blues: [0, 3, 5, 6, 7, 10],
14
+ dorian: [0, 2, 3, 5, 7, 9, 10],
15
+ mixolydian: [0, 2, 4, 5, 7, 9, 10],
16
+ whole: [0, 2, 4, 6, 8, 10],
17
+ }
18
+
19
+ /** snap a (possibly fractional) midi value to the nearest scale degree */
20
+ export function snapMidi (midi, { scale = 'chromatic', root = 0 } = {}) {
21
+ let degrees = typeof scale === 'string' ? SCALES[scale] : scale
22
+ if (!degrees) throw new RangeError(`note: unknown scale "${scale}"`)
23
+ let best = 0, bestDist = Infinity
24
+ for (let oct = Math.floor(midi / 12) - 1; oct <= Math.floor(midi / 12) + 1; oct++) {
25
+ for (let d of degrees) {
26
+ let cand = oct * 12 + root + d
27
+ let dist = Math.abs(cand - midi)
28
+ if (dist < bestDist) { bestDist = dist; best = cand }
29
+ }
30
+ }
31
+ return best
32
+ }
33
+
34
+ /** snap a frequency to the nearest scale note frequency */
35
+ export function snapHz (hz, { scale = 'chromatic', root = 0, a4 = 440 } = {}) {
36
+ return midiToHz(snapMidi(hzToMidi(hz, a4), { scale, root }), a4)
37
+ }