-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdissonance.js
More file actions
145 lines (115 loc) · 3.71 KB
/
Copy pathdissonance.js
File metadata and controls
145 lines (115 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const POINTS = 1000;
function score(sine1, sine2) {
const f1 = sine1.frequency;
const l1 = sine1.amplitude / Math.sqrt(2);
const f2 = sine2.frequency;
const l2 = sine2.amplitude / Math.sqrt(2);
const x = 0.24;
const s1 = 0.0207;
const s2 = 18.96;
const fmin = Math.min(f1, f2);
const fmax = Math.max(f1, f2);
const s = x / (s1 * fmin + s2);
const p = s * (fmax - fmin);
const b1 = 3.51;
const b2 = 5.75;
const l12 = Math.min(l1, l2);
return l12 * (Math.exp(-b1 * p) - Math.exp(-b2 * p));
}
class Sine {
constructor(frequency, amplitude) {
this.frequency = frequency;
this.amplitude = amplitude;
}
toWave() {
return new Wave(this.frequency, { 1: this.amplitude });
}
}
class Wave {
constructor(frequency, harmonics) {
this.frequency = frequency;
this.harmonics = harmonics;
}
sine(harmonic) {
const frequency = this.frequency * harmonic;
const amplitude = this.harmonics[harmonic] || 0.0;
return new Sine(frequency, amplitude);
}
}
function dissonance(wave1, wave2) {
if (wave1 instanceof Sine && wave2 instanceof Sine) {
return score(wave1, wave2);
}
if (wave1 instanceof Sine) {
wave1 = wave1.toWave();
}
if (wave2 instanceof Sine) {
wave2 = wave2.toWave();
}
let d = 0;
for (const h1 in wave1.harmonics) {
for (const h2 in wave2.harmonics) {
d += score(wave1.sine(h1), wave2.sine(h2));
}
}
return d;
}
function getDissonanceGraph(series, baseFrequency = BASE_FREQUENCY, points = POINTS) {
const wave = new Wave(baseFrequency, series);
const ratios = new Array(points).fill(0).map((_, i) =>
Math.pow(2, (2.0 * i) / (points - 1))
);
const graphFunction = (ratio) => {
const wave2 = new Wave(baseFrequency * ratio, series);
return dissonance(wave, wave2);
};
const values = ratios.map(graphFunction);
const maxValue = Math.max(...values);
const normalizer = maxValue !== 0 ? 1 / maxValue : 1.0;
const normalizedValues = values.map(v => v * normalizer);
return {
ratios: ratios,
values: normalizedValues,
function: graphFunction,
normalizer: normalizer
};
}
function toneError(harmonic, edo) {
const f = Math.log2(harmonic) % 1;
const x = f * edo;
return Math.pow(x - Math.round(x), 2);
}
function scaleError(edo, series) {
const maxError = 0.25 * Object.values(series).reduce((a, b) => a + b, 0);
let totalError = 0.0;
for (const [harmonic, amplitude] of Object.entries(series)) {
const error = toneError(harmonic, edo);
totalError += error * amplitude;
}
return Math.sqrt(totalError / maxError);
}
function getLog2Scale(scale) {
let log2Scale = Array.from(new Set(scale.map(x => ((Math.log2(x) % 1) + 1) % 1)));
log2Scale.sort((a, b) => a - b);
if (log2Scale[log2Scale.length - 1] < 1.0 - 1e-8) log2Scale.push(1.0);
return log2Scale;
}
function distanceToneError(harmonic, log2Scale) {
const x = Math.log2(harmonic) % 1;
let minDiff = Infinity;
for (let i = 0; i < log2Scale.length; i++) {
let diff = x - log2Scale[i];
minDiff = Math.min(minDiff, diff * diff);
}
return minDiff * log2Scale.length * log2Scale.length;
}
function distanceScaleError(scale, series) {
const maxError = Object.values(series).reduce((a, b) => a + b, 0);
const log2Scale = getLog2Scale(scale);
let totalError = 0.0;
for (const [harmonic, amplitude] of Object.entries(series)) {
const error = distanceToneError(harmonic, log2Scale);
totalError += amplitude * error;
}
return Math.sqrt(totalError / maxError);
}