-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
129 lines (101 loc) · 3.42 KB
/
script.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
let audioContext = null;
let hissGainRange;
let oscGainRange;
let hissGenNode;
let gainNode;
let soundSource;
let hissGainParam;
async function createHissProcessor() {
if (!audioContext) {
try {
audioContext = new AudioContext();
} catch(e) {
console.log("** Error: Unable to create audio context");
return null;
}
}
let processorNode;
try {
processorNode = new AudioWorkletNode(audioContext, "hiss-generator");
} catch(e) {
try {
console.log("adding...")
await audioContext.audioWorklet.addModule("hiss-generator.js");
processorNode = new AudioWorkletNode(audioContext, "hiss-generator");
} catch(e) {
console.log(`** Error: Unable to create worklet node: ${e}`);
return null;
}
}
await audioContext.resume();
return processorNode;
}
async function audioDemoStart() {
hissGenNode = await createHissProcessor();
if (!hissGenNode) {
console.log("** Error: unable to create hiss processor");
return;
}
soundSource = new OscillatorNode(audioContext);
gainNode = audioContext.createGain();
// Configure the oscillator node
soundSource.type = "square";
soundSource.frequency.setValueAtTime(440, audioContext.currentTime); // (A4)
// Configure the gain for the oscillator
gainNode.gain.setValueAtTime(oscGainRange.value, audioContext.currentTime);
// Connect and start
soundSource.connect(gainNode).connect(hissGenNode).connect(audioContext.destination);
soundSource.start();
// Get access to the worklet's gain parameter
hissGainParam = hissGenNode.parameters.get("gain");
hissGainParam.setValueAtTime(hissGainRange.value, audioContext.currentTime);
hissGainRange.disabled = false;
oscGainRange.disabled = false;
document.getElementById("startDemo").disabled = true;
document.getElementById("endDemo").disabled = false;
}
window.addEventListener("load", event => {
//document.getElementById("toggle").addEventListener("click", toggleSound);
document.getElementById("startDemo").addEventListener("click", audioDemoStart);
document.getElementById("endDemo").addEventListener("click", audioDemoEnd);
hissGainRange = document.getElementById("hiss-gain");
oscGainRange = document.getElementById("osc-gain");
hissGainRange.oninput = updateHissGain;
oscGainRange.oninput = updateOscGain;
hissGainRange.disabled = true;
oscGainRange.disabled = true;
});
async function audioDemoEnd(event){
hissGainRange.disabled = true;
oscGainRange.disabled = true;
hissGenNode.port.close();
hissGenNode.disconnect();
gainNode.disconnect();
soundSource.disconnect();
await audioContext.close();
audioContext = null;
document.getElementById("startDemo").disabled = false;
document.getElementById("endDemo").disabled = true;
}
/*async function toggleSound(event) {
if (!audioContext) {
audioDemoStart();
hissGainRange.disabled = false;
oscGainRange.disabled = false;
} else {
hissGainRange.disabled = true;
oscGainRange.disabled = true;
hissGenNode.port.close();
hissGenNode.disconnect();
gainNode.disconnect();
soundSource.disconnect();
await audioContext.close();
audioContext = null;
}
}*/
function updateHissGain(event) {
hissGainParam.setValueAtTime(event.target.value, audioContext.currentTime);
}
function updateOscGain(event) {
gainNode.gain.setValueAtTime(event.target.value, audioContext.currentTime);
}