-
Notifications
You must be signed in to change notification settings - Fork 0
/
osci.js
216 lines (199 loc) · 8.46 KB
/
osci.js
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @typedef {import("./types").FaustDspDistribution} FaustDspDistribution
* @typedef {import("./faustwasm").FaustAudioWorkletNode} FaustAudioWorkletNode
* @typedef {import("./faustwasm").FaustDspMeta} FaustDspMeta
* @typedef {import("./faustwasm").FaustUIDescriptor} FaustUIDescriptor
* @typedef {import("./faustwasm").FaustUIGroup} FaustUIGroup
* @typedef {import("./faustwasm").FaustUIItem} FaustUIItem
*/
/** @type {HTMLSpanElement} */
const $spanAudioInput = document.getElementById("audio-input");
/** @type {HTMLSpanElement} */
const $spanMidiInput = document.getElementById("midi-input");
/** @type {HTMLSelectElement} */
const $selectAudioInput = document.getElementById("select-audio-input");
/** @type {HTMLSelectElement} */
const $selectMidiInput = document.getElementById("select-midi-input");
/** @type {HTMLSelectElement} */
const $buttonDsp = document.getElementById("button-dsp");
/** @type {HTMLDivElement} */
const $divFaustUI = document.getElementById("div-faust-ui");
/** @type {typeof AudioContext} */
const AudioCtx = window.AudioContext || window.webkitAudioContext;
const audioContext = new AudioCtx({ latencyHint: 0.00001 });
audioContext.destination.channelInterpretation = "discrete";
audioContext.suspend();
/**
* @param {FaustAudioWorkletNode} faustNode
*/
const buildAudioDeviceMenu = async (faustNode) => {
/** @type {MediaStreamAudioSourceNode} */
let inputStreamNode;
const handleDeviceChange = async () => {
const devicesInfo = await navigator.mediaDevices.enumerateDevices();
$selectAudioInput.innerHTML = '';
devicesInfo.forEach((deviceInfo, i) => {
const { kind, deviceId, label } = deviceInfo;
if (kind === "audioinput") {
const option = new Option(label || `microphone ${i + 1}`, deviceId);
$selectAudioInput.add(option);
}
});
}
await handleDeviceChange();
navigator.mediaDevices.addEventListener("devicechange", handleDeviceChange)
$selectAudioInput.onchange = async () => {
const id = $selectAudioInput.value;
const constraints = {
audio: {
echoCancellation: false,
mozNoiseSuppression: false,
mozAutoGainControl: false,
deviceId: id ? { exact: id } : undefined,
},
};
const stream = await navigator.mediaDevices.getUserMedia(constraints);
if (inputStreamNode) inputStreamNode.disconnect();
inputStreamNode = audioContext.createMediaStreamSource(stream);
inputStreamNode.connect(faustNode);
};
const defaultConstraints = {
audio: {
echoCancellation: false,
mozNoiseSuppression: false,
mozAutoGainControl: false
}
};
const defaultStream = await navigator.mediaDevices.getUserMedia(defaultConstraints);
if (defaultStream) {
inputStreamNode = audioContext.createMediaStreamSource(defaultStream);
inputStreamNode.connect(faustNode);
}
};
/**
* @param {FaustAudioWorkletNode} faustNode
*/
const buildMidiDeviceMenu = async (faustNode) => {
const midiAccess = await navigator.requestMIDIAccess();
/** @type {WebMidi.MIDIInput} */
let currentInput;
/**
* @param {WebMidi.MIDIMessageEvent} e
*/
const handleMidiMessage = e => faustNode.midiMessage(e.data);
const handleStateChange = () => {
const { inputs } = midiAccess;
if ($selectMidiInput.options.length === inputs.size + 1) return;
if (currentInput) currentInput.removeEventListener("midimessage", handleMidiMessage);
$selectMidiInput.innerHTML = '<option value="-1" disabled selected>Select...</option>';
inputs.forEach((midiInput) => {
const { name, id } = midiInput;
const option = new Option(name, id);
$selectMidiInput.add(option);
});
};
handleStateChange();
midiAccess.addEventListener("statechange", handleStateChange);
$selectMidiInput.onchange = () => {
if (currentInput) currentInput.removeEventListener("midimessage", handleMidiMessage);
const id = $selectMidiInput.value;
currentInput = midiAccess.inputs.get(id);
currentInput.addEventListener("midimessage", handleMidiMessage);
};
};
$buttonDsp.disabled = true;
$buttonDsp.onclick = () => {
if (audioContext.state === "running") {
$buttonDsp.textContent = "Suspended";
audioContext.suspend();
} else if (audioContext.state === "suspended") {
$buttonDsp.textContent = "Running";
audioContext.resume();
}
}
/**
* Creates a Faust audio node for use in the Web Audio API.
*
* @param {AudioContext} audioContext - The Web Audio API AudioContext to which the Faust audio node will be connected.
* @param {string} dspName - The name of the DSP to be loaded.
* @param {number} voices - The number of voices to be used for polyphonic DSPs.
* @returns {Object} - An object containing the Faust audio node and the DSP metadata.
*/
const createFaustNode = async (audioContext, dspName = "template", voices = 0) => {
// Import necessary Faust modules and data
const { FaustMonoDspGenerator, FaustPolyDspGenerator } = await import("./faustwasm/index.js");
// Load DSP metadata from JSON
/** @type {FaustDspMeta} */
const dspMeta = await (await fetch(`./${dspName}.json`)).json();
// Compile the DSP module from WebAssembly binary data
const dspModule = await WebAssembly.compileStreaming(await fetch(`./${dspName}.wasm`));
// Create an object representing Faust DSP with metadata and module
/** @type {FaustDspDistribution} */
const faustDsp = { dspMeta, dspModule };
/** @type {FaustAudioWorkletNode} */
let faustNode;
// Create either a polyphonic or monophonic Faust audio node based on the number of voices
if (voices > 0) {
// Try to load optional mixer and effect modules
try {
faustDsp.mixerModule = await WebAssembly.compileStreaming(await fetch("./mixerModule.wasm"));
faustDsp.effectMeta = await (await fetch(`./${dspName}_effect.json`)).json();
faustDsp.effectModule = await WebAssembly.compileStreaming(await fetch(`./${dspName}_effect.wasm`));
} catch (e) { }
const generator = new FaustPolyDspGenerator();
faustNode = await generator.createNode(
audioContext,
voices,
"FaustPolyDSP",
{ module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta) },
faustDsp.mixerModule,
faustDsp.effectModule ? { module: faustDsp.effectModule, json: JSON.stringify(faustDsp.effectMeta) } : undefined
);
} else {
const generator = new FaustMonoDspGenerator();
faustNode = await generator.createNode(
audioContext,
"FaustMonoDSP",
{ module: faustDsp.dspModule, json: JSON.stringify(faustDsp.dspMeta) }
);
}
// Return an object with the Faust audio node and the DSP metadata
return { faustNode, dspMeta };
}
/**
* @param {FaustAudioWorkletNode} faustNode
*/
const createFaustUI = async (faustNode) => {
const { FaustUI } = await import("./faust-ui/index.js");
const $container = document.createElement("div");
$container.style.margin = "0";
$container.style.position = "absolute";
$container.style.overflow = "auto";
$container.style.display = "flex";
$container.style.flexDirection = "column";
$container.style.width = "100%";
$container.style.height = "100%";
$divFaustUI.appendChild($container);
const faustUI = new FaustUI({
ui: faustNode.getUI(),
root: $container,
listenWindowMessage: false,
listenWindowResize: true,
});
faustUI.paramChangeByUI = (path, value) => faustNode.setParamValue(path, value);
faustNode.setOutputParamHandler((path, value) => faustUI.paramChangeByDSP(path, value));
$container.style.minWidth = `${faustUI.minWidth}px`;
$container.style.minHeight = `${faustUI.minHeight}px`;
faustUI.resize();
};
(async () => {
const { faustNode, dspMeta: { name } } = await createFaustNode(audioContext, "osci");
await createFaustUI(faustNode);
faustNode.connect(audioContext.destination);
if (faustNode.numberOfInputs) await buildAudioDeviceMenu(faustNode);
else $spanAudioInput.hidden = true;
if (navigator.requestMIDIAccess) await buildMidiDeviceMenu(faustNode);
else $spanMidiInput.hidden = true;
$buttonDsp.disabled = false;
document.title = name;
})();