-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex.recorder.html
192 lines (172 loc) · 5.89 KB
/
ex.recorder.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf8">
<title>Recorder</title>
<style>
button {
color: white;
padding: 10px 20px;
font-size: large;
margin-bottom: 10px;
background: #96729E;
cursor: pointer;
border-radius: 4px;
border: 1px solid rgba(0, 0, 0, .15);
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25), 0 1px 2px rgba(0, 0, 0, 0.25);
}
button:hover, [role="button"]:hover {
filter: brightness(1.2);
color: white;
}
</style>
</head>
<body>
<button onclick="play()">▶ play</button>
<button onclick="stop()">STOP!!!!</button>
<div id="recordings"></div>
<script>
const audioContext = new AudioContext();
function load(files) {
return new Promise((resolve, reject) => {
const buffers = new Map;
files.forEach(f => {
fetch(f)
.then(response => response.arrayBuffer())
.then(arrayBuffer => audioContext.decodeAudioData(arrayBuffer))
.then(audioBuffer => {
buffers.set(f, audioBuffer);
if (buffers.size === files.length) {
resolve(buffers);
}
})
.catch(e => console.log('uff'));
});
});
}
const notes = {
D1: {rate: 1/4, voices: 4},
D2: {rate: 1/2, voices: 4},
A2: {rate: 3/4, voices: 2},
D3: {rate: 1, voices: 2},
A3: {rate: 3/2, voices: 2},
D4: {rate: 2, voices: 2},
A4: {rate: 3, voices: 2},
D5: {rate: 4, voices: 2},
A5: {rate: 6, voices: 2},
D6: {rate: 8, voices: 2},
Fs: {rate: 10, voices: 6},
};
const C3 = 130.81;
const c3d150 = 150 / C3; // 1.1467013225;
const SAMPLE = 'Roland-SC-88-Cello-C3-glued-01.wav';
const VERB = 'IMreverbs/Large Wide Echo Hall.wav';
const sources = [];
let to = null;
function randRate200to400() {
const freq = rand(200, 400);
return freq / C3;
}
function rand(min, max) {
return Math.random() * (max - min) + min;
}
const volume = audioContext.createGain(); // automation
const panner = audioContext.createStereoPanner(); // stereo
const master = audioContext.createGain(); // master volume
master.gain.value = 0.2;
const verbGain = audioContext.createGain(); // how much reverb
const verbLowPass = audioContext.createBiquadFilter();
verbLowPass.type = 'lowpass';
verbLowPass.frequency.value = 3000;
const verbHighPass = audioContext.createBiquadFilter();
verbHighPass.type = 'highpass';
verbHighPass.frequency.value = 300;
let recorder;
function play() {
//stop();
load([SAMPLE, VERB]).then(buffers => {
const mediaStream = audioContext.createMediaStreamDestination();
recorder = new MediaRecorder(mediaStream.stream);
const chunks = [];
recorder.ondataavailable = evt => chunks.push(evt.data);
recorder.onstop = evt => {
const blob = new Blob(chunks, {type: 'audio/ogg'});
const audio = document.createElement('audio');
audio.controls = true;
audio.src = URL.createObjectURL(blob);
const p = document.createElement('p');
p.appendChild(audio);
recordings.appendChild(p);
};
recorder.start();
const reverb = audioContext.createConvolver();
reverb.buffer = buffers.get(VERB);
verbGain.gain.setValueAtTime(0.7, 0);
verbGain.gain.setTargetAtTime(0.1, audioContext.currentTime + 6, 4);
//setInterval(() => console.log(audioContext.currentTime, verbGain.gain.value), 1000);
// parallel verb
volume
.connect(verbGain)
.connect(verbHighPass)
.connect(verbLowPass)
.connect(reverb)
.connect(panner);
// stereo panning
volume.connect(panner);
panner.connect(master);
master.connect(audioContext.destination);
// plug for recording
master.connect(mediaStream);
// schedule panning
panner.pan.setValueAtTime(0, 0);
panner.pan.setTargetAtTime(-1, audioContext.currentTime, 1);
panner.pan.setTargetAtTime(0.5, audioContext.currentTime + 4, 2);
panner.pan.setTargetAtTime(0, audioContext.currentTime + 8, 2);
// schedule volume automation
volume.gain.setValueAtTime(0, audioContext.currentTime);
volume.gain.setTargetAtTime(0.1, audioContext.currentTime, 1);
volume.gain.setTargetAtTime(0.5, audioContext.currentTime + 10, 4);
volume.gain.setTargetAtTime(1.0, audioContext.currentTime + 14, 2);
volume.gain.setTargetAtTime(0, audioContext.currentTime + 20, 0.5);
for (let note in notes) {
for (let i = 0; i < notes[note].voices; i++) {
const source = audioContext.createBufferSource();
source.buffer = buffers.get(SAMPLE);
source.loop = true;
const initial = randRate200to400();
source.playbackRate.setValueAtTime(initial, audioContext.currentTime);
for (let i = 1; i < 9; i++) {
source.playbackRate.setTargetAtTime(initial + rand(-0.5, 0.5), audioContext.currentTime + rand(i - 0.5, i + 0.5), 2);
}
// FF
const finalRandom = initial + rand(-0.5, 0.5);
source.playbackRate.setTargetAtTime(finalRandom, audioContext.currentTime + 9, 2);
source.playbackRate.setValueAtTime(finalRandom, audioContext.currentTime + 10); // FF
to = setTimeout(() => {
source.playbackRate.exponentialRampToValueAtTime(c3d150 * notes[note].rate, audioContext.currentTime + 4);
if (note === 'Fs' && i > 1) { // 2 "correct" and 4 detuned
source.detune.value = rand(-33, 33); // 100 cents = semitone
}
}, 1000 * 10); // 10 seconds
source.connect(volume);
source.start();
sources.push(source);
}
}
});
}
const releaseTime = 0.1;
function stop() {
if (recorder && recorder.state !== 'inactive') {
recorder.stop();
}
volume.gain.cancelScheduledValues(audioContext.currentTime);
volume.gain.linearRampToValueAtTime(0, audioContext.currentTime + releaseTime);
for (let i = 0; i < sources.length; i++) {
sources[i] && sources[i].stop(audioContext.currentTime + 1);
delete sources[i];
}
}
</script>
</body>
</html>