-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio-controller.js
More file actions
160 lines (132 loc) · 4.9 KB
/
Copy pathaudio-controller.js
File metadata and controls
160 lines (132 loc) · 4.9 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
class AudioController {
constructor() {
this.visualizer = null;
this.analyser = null;
this.audioContext = null;
this.audioWorkletNode = null;
this.isPlaying = false;
}
msToSamples(ms) {
return Math.floor((ms * this.audioContext.sampleRate) / 1000);
}
samplesToMs(samples) {
return Math.round((samples / this.audioContext.sampleRate) * 1000);
}
updateDuration(durationMs) {
this.audioWorkletNode.port.postMessage({
type: "setDurationSamples",
samples: this.msToSamples(durationMs),
});
}
playSound(isTempered, playRoot = true, playThird = true) {
if (this.isPlaying) return;
this.isPlaying = true;
this.audioWorkletNode.port.postMessage({
type: "setActiveBuffers",
playRoot: playRoot,
playThird: playThird
});
// Set which third type via the parameter
this.audioWorkletNode.parameters.get("thirdType").value = isTempered ? 0 : 1;
this.audioWorkletNode.parameters.get("playing").value = 1;
}
demonstrateSound(isTempered) {
const buttonId = isTempered ? "temperedPlayButton" : "purePlayButton";
document.getElementById(buttonId).textContent = "playing...";
// Store current duration
const slider = document.getElementById("durationSlider");
const originalDuration = slider.value;
// Set to maximum duration for demo
slider.value = slider.max;
this.updateDuration(slider.max);
// Play the sound
this.playSound(isTempered);
// Add handler to restore duration when playback completes
const originalHandler = this.audioWorkletNode.port.onmessage;
this.audioWorkletNode.port.onmessage = (event) => {
if (event.data.type === "playbackComplete" && event.data.shouldStop) {
// Restore original duration
slider.value = originalDuration;
this.updateDuration(originalDuration);
// Restore original message handler
this.audioWorkletNode.port.onmessage = originalHandler;
// Call original handler for normal cleanup
originalHandler(event);
}
};
}
async init() {
this.audioContext = new AudioContext();
try {
await this.audioContext.audioWorklet.addModule("simple-processor.js");
// Load all three buffers
const [rootBuffer, temperedBuffer, pureBuffer] = await Promise.all(
["Rhodes_C_128hz_A.wav", "Rhodes_E_Equal_A.wav", "Rhodes_E_Just_A.wav"].map(
async (url) => {
const response = await fetch(url);
return Object.setPrototypeOf(
await this.audioContext.decodeAudioData(
await response.arrayBuffer()
),
OperableAudioBuffer.prototype
);
}
)
);
this.audioWorkletNode = new AudioWorkletNode(
this.audioContext,
"simple-processor",
{
numberOfOutputs: 2, // One for audio, one for visualization
outputChannelCount: [2, 2] // Stereo for each output
}
);
this.audioWorkletNode.connect(this.audioContext.destination);
/// analyser
this.analyser = this.audioContext.createAnalyser();
this.visualizer = new AudioAnalyzer(this.audioContext, this.audioWorkletNode);
///
this.audioWorkletNode.port.onmessage = (event) => {
if (event.data.type === "playbackComplete" && event.data.shouldStop) {
this.audioWorkletNode.parameters.get("playing").value = 0;
document.getElementById("playButton").textContent = "play sound";
document.getElementById("temperedPlayButton").textContent = "play tempered";
document.getElementById("purePlayButton").textContent = "play pure";
document.querySelector(".choice-buttons").style.visibility =
"visible";
this.isPlaying = false;
}
};
// Add all three buffers
this.audioWorkletNode.port.postMessage({
type: "addBuffer",
id: "root",
audio: rootBuffer.toArray(),
});
this.audioWorkletNode.port.postMessage({
type: "addBuffer",
id: "tempered",
audio: temperedBuffer.toArray(),
});
this.audioWorkletNode.port.postMessage({
type: "addBuffer",
id: "pure",
audio: pureBuffer.toArray(),
});
const slider = document.getElementById("durationSlider");
const originalDurationMs = this.samplesToMs(rootBuffer.length);
document.getElementById("currentDuration").textContent =
originalDurationMs;
slider.max = Math.min(1500, originalDurationMs);
slider.value = originalDurationMs;
document.getElementById("temperedPlayButton").onclick = () =>
this.demonstrateSound(true);
document.getElementById("purePlayButton").onclick = () =>
this.demonstrateSound(false);
return true;
} catch (err) {
console.error("Error initializing audio worklet:", err);
return false;
}
}
}