sethares-dissonance 4.0.0 → 5.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.
|
@@ -74,18 +74,20 @@ export declare class DissonanceCurve {
|
|
|
74
74
|
*/
|
|
75
75
|
get(ratio: FractionInput): DissonanceCurvePoint | undefined;
|
|
76
76
|
/**
|
|
77
|
-
* Normalize dissonance values in place to
|
|
77
|
+
* Normalize dissonance values in place to a target range.
|
|
78
78
|
*
|
|
79
79
|
* **Destructive:** This method mutates the curve permanently. All dissonance values
|
|
80
|
-
* are
|
|
81
|
-
* recovered. Use {@link recalculate} to rebuild the curve with raw values.
|
|
80
|
+
* are scaled from [0, maxDissonance] to [min, max], and the original raw values
|
|
81
|
+
* cannot be recovered. Use {@link recalculate} to rebuild the curve with raw values.
|
|
82
82
|
*
|
|
83
83
|
* After normalization, `points`, `get`, `plot`, `plotCents`, `toJSON`, and
|
|
84
|
-
* `toString` will all return normalized dissonance values
|
|
84
|
+
* `toString` will all return normalized dissonance values in the [min, max] range.
|
|
85
85
|
*
|
|
86
|
+
* @param min - Target minimum value (default: 0)
|
|
87
|
+
* @param max - Target maximum value (default: 1)
|
|
86
88
|
* @returns `this` for method chaining
|
|
87
89
|
*/
|
|
88
|
-
normalize(): this;
|
|
90
|
+
normalize(min?: number, max?: number): this;
|
|
89
91
|
/**
|
|
90
92
|
* Get plot points with intervals as ratio values (decimal numbers).
|
|
91
93
|
* @returns Array of [ratio, dissonance] tuples suitable for plotting
|
|
@@ -118,27 +118,32 @@ export class DissonanceCurve {
|
|
|
118
118
|
return this._data.get(new Fraction(ratio).toFraction());
|
|
119
119
|
}
|
|
120
120
|
/**
|
|
121
|
-
* Normalize dissonance values in place to
|
|
121
|
+
* Normalize dissonance values in place to a target range.
|
|
122
122
|
*
|
|
123
123
|
* **Destructive:** This method mutates the curve permanently. All dissonance values
|
|
124
|
-
* are
|
|
125
|
-
* recovered. Use {@link recalculate} to rebuild the curve with raw values.
|
|
124
|
+
* are scaled from [0, maxDissonance] to [min, max], and the original raw values
|
|
125
|
+
* cannot be recovered. Use {@link recalculate} to rebuild the curve with raw values.
|
|
126
126
|
*
|
|
127
127
|
* After normalization, `points`, `get`, `plot`, `plotCents`, `toJSON`, and
|
|
128
|
-
* `toString` will all return normalized dissonance values
|
|
128
|
+
* `toString` will all return normalized dissonance values in the [min, max] range.
|
|
129
129
|
*
|
|
130
|
+
* @param min - Target minimum value (default: 0)
|
|
131
|
+
* @param max - Target maximum value (default: 1)
|
|
130
132
|
* @returns `this` for method chaining
|
|
131
133
|
*/
|
|
132
|
-
normalize() {
|
|
134
|
+
normalize(min = 0, max = 1) {
|
|
135
|
+
if (max <= min)
|
|
136
|
+
throw new Error('max must be greater than min');
|
|
133
137
|
if (this.maxDissonance === 0)
|
|
134
138
|
return this;
|
|
139
|
+
const range = max - min;
|
|
135
140
|
for (const [key, point] of this._data) {
|
|
136
141
|
this._data.set(key, {
|
|
137
142
|
interval: point.interval,
|
|
138
|
-
dissonance: point.dissonance / this.maxDissonance,
|
|
143
|
+
dissonance: (point.dissonance / this.maxDissonance) * range + min,
|
|
139
144
|
});
|
|
140
145
|
}
|
|
141
|
-
this.maxDissonance =
|
|
146
|
+
this.maxDissonance = max;
|
|
142
147
|
return this;
|
|
143
148
|
}
|
|
144
149
|
/**
|
package/dist/lib/const.d.ts
CHANGED
|
@@ -7,6 +7,8 @@ export declare const SETHARES_DISSONANCE_PARAMS: {
|
|
|
7
7
|
readonly b2: 5.75;
|
|
8
8
|
readonly x_star: 0.24;
|
|
9
9
|
readonly magnitude: 1;
|
|
10
|
+
/** At 0: no decay (constant magnitude). Higher values = faster decay with minFrequency. */
|
|
11
|
+
readonly magnitudeFrequencyDecay: 0;
|
|
10
12
|
};
|
|
11
13
|
export declare const DEFAULT_FIRST_ORDER_DISSONANCE_PARAMS: {
|
|
12
14
|
s1: 0.021;
|
|
@@ -15,9 +17,11 @@ export declare const DEFAULT_FIRST_ORDER_DISSONANCE_PARAMS: {
|
|
|
15
17
|
b2: 5.75;
|
|
16
18
|
x_star: 0.24;
|
|
17
19
|
magnitude: 1;
|
|
20
|
+
magnitudeFrequencyDecay: 0;
|
|
18
21
|
};
|
|
19
22
|
export declare const DEFAULT_SECOND_ORDER_DISSONANCE_PARAMS: {
|
|
20
23
|
magnitude: number;
|
|
24
|
+
magnitudeFrequencyDecay: number;
|
|
21
25
|
s1: 0.021;
|
|
22
26
|
s2: 19;
|
|
23
27
|
b1: 3.5;
|
|
@@ -26,10 +30,11 @@ export declare const DEFAULT_SECOND_ORDER_DISSONANCE_PARAMS: {
|
|
|
26
30
|
};
|
|
27
31
|
export declare const DEFAULT_THIRD_ORDER_DISSONANCE_PARAMS: {
|
|
28
32
|
magnitude: number;
|
|
33
|
+
magnitudeFrequencyDecay: number;
|
|
29
34
|
s1: 0.021;
|
|
30
35
|
s2: 19;
|
|
31
36
|
b1: 3.5;
|
|
32
37
|
b2: 5.75;
|
|
33
38
|
x_star: 0.24;
|
|
34
39
|
};
|
|
35
|
-
export declare const DEFAULT_PHANTOM_HARMONICS_NUMBER =
|
|
40
|
+
export declare const DEFAULT_PHANTOM_HARMONICS_NUMBER = 3;
|
package/dist/lib/const.js
CHANGED
|
@@ -7,8 +7,10 @@ export const SETHARES_DISSONANCE_PARAMS = {
|
|
|
7
7
|
b2: 5.75,
|
|
8
8
|
x_star: 0.24,
|
|
9
9
|
magnitude: 1,
|
|
10
|
+
/** At 0: no decay (constant magnitude). Higher values = faster decay with minFrequency. */
|
|
11
|
+
magnitudeFrequencyDecay: 0,
|
|
10
12
|
};
|
|
11
13
|
export const DEFAULT_FIRST_ORDER_DISSONANCE_PARAMS = { ...SETHARES_DISSONANCE_PARAMS };
|
|
12
|
-
export const DEFAULT_SECOND_ORDER_DISSONANCE_PARAMS = { ...SETHARES_DISSONANCE_PARAMS, magnitude: 0.
|
|
13
|
-
export const DEFAULT_THIRD_ORDER_DISSONANCE_PARAMS = { ...SETHARES_DISSONANCE_PARAMS, magnitude: 0.1 };
|
|
14
|
-
export const DEFAULT_PHANTOM_HARMONICS_NUMBER =
|
|
14
|
+
export const DEFAULT_SECOND_ORDER_DISSONANCE_PARAMS = { ...SETHARES_DISSONANCE_PARAMS, magnitude: 0.75, magnitudeFrequencyDecay: 0.75 };
|
|
15
|
+
export const DEFAULT_THIRD_ORDER_DISSONANCE_PARAMS = { ...SETHARES_DISSONANCE_PARAMS, magnitude: 0.5, magnitudeFrequencyDecay: 1.75 };
|
|
16
|
+
export const DEFAULT_PHANTOM_HARMONICS_NUMBER = 3;
|
package/dist/lib/utils.js
CHANGED
|
@@ -12,6 +12,7 @@ export function getPlompLeveltDissonance(h1, h2, params) {
|
|
|
12
12
|
const s2 = params?.s2 ?? SETHARES_DISSONANCE_PARAMS.s2;
|
|
13
13
|
const b1 = params?.b1 ?? SETHARES_DISSONANCE_PARAMS.b1;
|
|
14
14
|
const b2 = params?.b2 ?? SETHARES_DISSONANCE_PARAMS.b2;
|
|
15
|
+
const magnitudeFrequencyDecay = params?.magnitudeFrequencyDecay ?? SETHARES_DISSONANCE_PARAMS.magnitudeFrequencyDecay;
|
|
15
16
|
if (h1.frequency.equals(h2.frequency))
|
|
16
17
|
return 0;
|
|
17
18
|
const minLoudness = Math.min("loudness" in h1 ? h1.loudness : getLoudness(h1.amplitude), "loudness" in h2 ? h2.loudness : getLoudness(h2.amplitude));
|
|
@@ -20,9 +21,11 @@ export function getPlompLeveltDissonance(h1, h2, params) {
|
|
|
20
21
|
const minFrequency = Math.min(h1.frequencyNum, h2.frequencyNum);
|
|
21
22
|
if (minFrequency <= 0)
|
|
22
23
|
return 0;
|
|
24
|
+
const magnitudeDecayFactor = Math.exp(-magnitudeFrequencyDecay * (minFrequency / 1000));
|
|
23
25
|
const frequencyDifference = Math.abs(h1.frequencyNum - h2.frequencyNum);
|
|
24
26
|
const s = x_star / (s1 * minFrequency + s2);
|
|
25
27
|
return (magnitude *
|
|
28
|
+
magnitudeDecayFactor *
|
|
26
29
|
minLoudness *
|
|
27
30
|
(Math.exp(-1 * b1 * s * frequencyDifference) -
|
|
28
31
|
Math.exp(-1 * b2 * s * frequencyDifference)));
|