sethares-dissonance 0.0.1

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 NewTonality
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # sethares-dissonance
2
+
3
+ To install dependencies:
4
+
5
+ ```bash
6
+ bun install
7
+ ```
8
+
9
+ To run:
10
+
11
+ ```bash
12
+ bun run index.ts
13
+ ```
14
+
15
+ This project was created using `bun init` in bun v1.2.15. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.
@@ -0,0 +1,83 @@
1
+ import { getSetharesDissonance, transpose, type Spectrum } from "../lib/utils";
2
+
3
+ type DissonanceCurveOptions = {
4
+ context: Spectrum;
5
+ compliment: Spectrum;
6
+ precision?: number;
7
+ rangeMin?: number;
8
+ rangeMax?: number;
9
+ };
10
+
11
+ export class DissonanceCurve {
12
+ private _data: Map<number, number> = new Map();
13
+ private step: number;
14
+
15
+ public readonly rangeMin: NonNullable<DissonanceCurveOptions["rangeMin"]>;
16
+ public readonly rangeMax: NonNullable<DissonanceCurveOptions["rangeMax"]>;
17
+ public readonly precision: NonNullable<DissonanceCurveOptions["precision"]>;
18
+ public readonly context: DissonanceCurveOptions["context"];
19
+ public readonly compliment: DissonanceCurveOptions["compliment"];
20
+ public readonly maxDissonance: number = 0;
21
+ public readonly change: number = 0;
22
+
23
+ constructor(opts: DissonanceCurveOptions) {
24
+ this.context = opts.context;
25
+ this.compliment = opts.compliment;
26
+
27
+ this.rangeMin = opts.rangeMin ?? 0;
28
+ this.rangeMax = opts.rangeMax ?? 1200;
29
+ this.precision = opts.precision ?? 1;
30
+
31
+ if (this.rangeMin > this.rangeMax)
32
+ throw Error("rangeMin should be less or equal to rangeMax");
33
+ if (this.precision <= 0)
34
+ throw Error("precision should be greater than zero");
35
+
36
+ this.step =
37
+ (this.rangeMax - this.rangeMin) / (this.rangeMax * this.precision);
38
+
39
+ for (let cent = this.rangeMin; cent <= this.rangeMax; cent += this.step) {
40
+ const dissonance = getSetharesDissonance(
41
+ this.context,
42
+ transpose(this.compliment, cent)
43
+ );
44
+
45
+ if (dissonance > this.maxDissonance) this.maxDissonance = dissonance;
46
+
47
+ this._data.set(cent, dissonance);
48
+ }
49
+ }
50
+
51
+ public get points() {
52
+ return Array.from(this._data.entries()).sort((a, b) => a[0] - b[0]);
53
+ }
54
+
55
+ private getRowString(row: Array<number | string>) {
56
+ if (row.length === 0) return "";
57
+
58
+ let result = `${row[0]}`;
59
+
60
+ for (let i = 1; i < row.length; i += 1) {
61
+ result += `\t${row[i]}`;
62
+ }
63
+
64
+ return result;
65
+ }
66
+
67
+ public toFileString() {
68
+ if (this._data.size === 0) return "";
69
+
70
+ const headerRow = this.getRowString([
71
+ "Interval (cents)",
72
+ "Sensory dissonance",
73
+ ]);
74
+
75
+ let result = headerRow + "\n";
76
+
77
+ for (const point of this.points) {
78
+ result += this.getRowString(point);
79
+ }
80
+
81
+ return result;
82
+ }
83
+ }
@@ -0,0 +1 @@
1
+ export * from "./DissonanceCurve"
package/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./classes"
2
+ export * from "./lib"
package/lib/index.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./utils"
package/lib/utils.ts ADDED
@@ -0,0 +1,122 @@
1
+ export type Spectrum = {
2
+ freq: number;
3
+ loudness: number;
4
+ }[];
5
+
6
+ /** The formula to calculate loudness from amplitude proposed by Sethares in the appendix "How to Draw Dissonance Curves" */
7
+ export function getSetharesLoudness(amplitude: number): number {
8
+ const P_e = amplitude / Math.SQRT2;
9
+ const P_ref = 20;
10
+
11
+ const SPL = 20 * Math.log10(P_e / P_ref);
12
+
13
+ return 2 ** (SPL / 10) / 16;
14
+ }
15
+
16
+ /** Fitting parameters proposed by Sethares in the appendix "How to Draw Dissonance Curves" */
17
+ export const SETHARES_DISSONANCE_PARAMS = {
18
+ s1: 0.021,
19
+ s2: 19,
20
+ b1: 3.5,
21
+ b2: 5.75,
22
+ x_star: 0.24,
23
+ };
24
+
25
+ /** The formula to calculate sensory dissoannce proposed by Sethares in the appendix "How to Draw Dissonance Curves" */
26
+ export function getPlompLeveltDissonance(
27
+ freq1: number,
28
+ freq2: number,
29
+ loudness1: number,
30
+ loudness2: number,
31
+ params = SETHARES_DISSONANCE_PARAMS
32
+ ): number {
33
+ if (freq1 === freq2) return 0;
34
+
35
+ const minLoudness = Math.min(loudness1, loudness2);
36
+ if (minLoudness <= 0) return 0;
37
+
38
+ const minFrequency = Math.min(freq1, freq2);
39
+ const frequencyDifference = Math.abs(freq1 - freq2);
40
+
41
+ if (minFrequency <= 0) return 0;
42
+
43
+ const s = params.x_star / (params.s1 * minFrequency + params.s2);
44
+
45
+ return (
46
+ minLoudness *
47
+ (Math.exp(-1 * params.b1 * s * frequencyDifference) -
48
+ Math.exp(-1 * params.b2 * s * frequencyDifference))
49
+ );
50
+ }
51
+
52
+ export function getIntrinsicDissonance(
53
+ spectrum: Spectrum,
54
+ params = SETHARES_DISSONANCE_PARAMS
55
+ ) {
56
+ let dissonance = 0;
57
+
58
+ for (let i = 0; i < spectrum.length; i++) {
59
+ for (let j = i + 1; j < spectrum.length; j++) {
60
+ const partial1 = spectrum[i]!;
61
+ const partial2 = spectrum[j]!;
62
+
63
+ dissonance += getPlompLeveltDissonance(
64
+ partial1.freq,
65
+ partial2.freq,
66
+ partial1.loudness,
67
+ partial2.loudness,
68
+ params
69
+ );
70
+ }
71
+ }
72
+
73
+ return dissonance;
74
+ }
75
+
76
+ export function getSetharesDissonance(
77
+ spectrum1: Spectrum,
78
+ spectrum2: Spectrum,
79
+ params = SETHARES_DISSONANCE_PARAMS
80
+ ) {
81
+ let dissonance =
82
+ getIntrinsicDissonance(spectrum1, params) +
83
+ getIntrinsicDissonance(spectrum2, params);
84
+
85
+ for (let i = 0; i < spectrum1.length; i++) {
86
+ for (let j = i + 1; j < spectrum2.length; j++) {
87
+ const partial1 = spectrum1[i]!;
88
+ const partial2 = spectrum2[j]!;
89
+
90
+ dissonance += getPlompLeveltDissonance(
91
+ partial1.freq,
92
+ partial2.freq,
93
+ partial1.loudness,
94
+ partial2.loudness,
95
+ params
96
+ );
97
+ }
98
+ }
99
+
100
+ return dissonance;
101
+ }
102
+
103
+ export function ratioToCents(ratio: number): number {
104
+ return ratio > 0 ? 1200 * Math.log2(ratio) : 0;
105
+ }
106
+
107
+ export function centsToRatio(cents: number): number {
108
+ return 2 ** (cents / 1200);
109
+ }
110
+
111
+ export function transpose(spectrum: Spectrum, cents: number) {
112
+ const result: Spectrum = [];
113
+
114
+ for (const partial of spectrum) {
115
+ result.push({
116
+ freq: partial.freq * centsToRatio(cents),
117
+ loudness: partial.loudness,
118
+ });
119
+ }
120
+
121
+ return result;
122
+ }
package/package.json ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "name": "sethares-dissonance",
3
+ "version": "0.0.1",
4
+ "module": "index.ts",
5
+ "type": "module",
6
+ "devDependencies": {
7
+ "@types/bun": "latest"
8
+ },
9
+ "peerDependencies": {
10
+ "typescript": "^5"
11
+ }
12
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "compilerOptions": {
3
+ // Environment setup & latest features
4
+ "lib": ["ESNext"],
5
+ "target": "ESNext",
6
+ "module": "Preserve",
7
+ "moduleDetection": "force",
8
+ "jsx": "react-jsx",
9
+ "allowJs": true,
10
+
11
+ // Bundler mode
12
+ "moduleResolution": "bundler",
13
+ "allowImportingTsExtensions": true,
14
+ "verbatimModuleSyntax": true,
15
+ "noEmit": true,
16
+
17
+ // Best practices
18
+ "strict": true,
19
+ "skipLibCheck": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+ "noUncheckedIndexedAccess": true,
22
+ "noImplicitOverride": true,
23
+
24
+ // Some stricter flags (disabled by default)
25
+ "noUnusedLocals": false,
26
+ "noUnusedParameters": false,
27
+ "noPropertyAccessFromIndexSignature": false
28
+ }
29
+ }