-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
289 lines (256 loc) · 11.2 KB
/
Copy pathrenderer.js
File metadata and controls
289 lines (256 loc) · 11.2 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
Most of the below code is utilizing mattdiamond's popular Recordjs plugin found:
https://github.com/mattdiamond/Recorderjs
code will be slightly repurposed for our use
*/
const {
promisify
} = require('util');
var note_detection = require("./note-detection.js");
//webkitURL is deprecated but nevertheless
URL = window.URL || window.webkitURL;
var recordButton = document.getElementById("mic-icon");
var stopButton = document.getElementById("stop-icon");
var retryButton = document.getElementById("retry-icon");
var recording = false;
var detectTempoButton = document.getElementById("detect-tempo-button");
var tempoCountdown = document.getElementById("tempo-countdown");
var tempoInput = document.querySelector("input[name='tempo']");
var detectingTempoContent = document.getElementById("detecting-tempo-div");
var detectKeyCheckbox = document.getElementById("auto-detect-key-signature");
var editButton = document.getElementById("edit-button");
var reRenderButton = document.getElementById("re-render-button");
var taps;
var startTime, endTime;
var detectingTempo = false;
var tempoBPM;
var timer;
var gumStream; //stream from getUserMedia()
var rec; //Recorder.js object
var input; //MediaStreamAudioSourceNode we'll be recording
var AudioContext = window.AudioContext || window.webkitAudioContext; // shim for AudioContext when it's not avb.
var audioContext = new AudioContext; //new audio context to help us record
var audioBuffer; //this variable will contain the audiobuffer post-recording
var measures = [];
var detectKeyFlag = false;
var theBlob;
var theSampleRate;
recordButton.addEventListener("click", startRecording);
stopButton.addEventListener("click", stopRecording);
retryButton.addEventListener("click", retryRecording);
detectTempoButton.addEventListener("click", startDetectTempo);
document.addEventListener("keypress", keyPress);
detectKeyCheckbox.addEventListener("click", toggleDetectKey);
editButton.addEventListener("click", editInputs);
reRenderButton.addEventListener("click", reRender);
function startRecording() {
if (!recording) {
if (!document.getElementById("mic-icon").classList.contains("disabled-button")) {
document.getElementById("mic-icon").classList.add("hidden");
document.getElementById("stop-icon").classList.add("disabled-button");
document.getElementById("stop-icon").classList.remove("hidden");
document.getElementById("metronome-main-content").classList.remove("hidden");
document.getElementById("input-main-content").classList.add("hidden");
recording = true;
/*
Simple constraints object, for more advanced audio features see
<div class="video-container"><blockquote class="wp-embedded-content" data-secret="cVHlrYJoGD"><a href="https://addpipe.com/blog/audio-constraints-getusermedia/">Supported Audio Constraints in getUserMedia()</a></blockquote><iframe class="wp-embedded-content" sandbox="allow-scripts" security="restricted" style="position: absolute; clip: rect(1px, 1px, 1px, 1px);" src="https://addpipe.com/blog/audio-constraints-getusermedia/embed/#?secret=cVHlrYJoGD" data-secret="cVHlrYJoGD" width="600" height="338" title="“Supported Audio Constraints in getUserMedia()” — Pipe Blog" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe></div>
*/
var constraints = {
audio: true,
video: false
}
/*
We're using the standard promise based getUserMedia()
https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia
*/
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
/* assign to gumStream for later use */
gumStream = stream;
/* use the stream */
input = audioContext.createMediaStreamSource(stream);
/*
Create the Recorder object and configure to record mono sound (1 channel)
Recording 2 channels will double the file size
*/
rec = new Recorder(input, {
numChannels: 1
})
//start the recording process
startMetronome();
//rec.record();
}).catch(function (err) {
recordButton.disabled = false;
});
}
} else {
stopRecording();
}
}
function stopRecording() {
if (!document.getElementById("stop-icon").classList.contains("disabled-button")) {
recording = false;
// remove metronome and show progress bar
document.getElementById("metronome-main-content").classList.add("hidden");
document.getElementById("progress-bar-main-content").classList.remove("hidden");
document.getElementById("stop-icon").classList.add("hidden");
rec.stop();
stopMetronome();
//stop microphone access
gumStream.getAudioTracks()[0].stop();
//create the wav blob and pass it on
//rec.exportWAV(createDownloadLink);
rec.exportWAV(createAudioBuffer);
rec.clear();
}
}
function retryRecording() {
// hide sheet music and go back to input screen
document.getElementById("sheet-music-main-content").classList.add("hidden");
document.getElementById("input-main-content").classList.remove("hidden");
// hide the retry button and show the record button
document.getElementById("retry-icon").classList.add("hidden");
document.getElementById("mic-icon").classList.remove("hidden");
// reset the progress bar
progress = [0,0,0,0,0];
document.getElementById("progress-fill").style.width = 1 + "%";
}
function editInputs() {
// hide sheet music and go back to input screen
document.getElementById("sheet-music-main-content").classList.add("hidden");
document.getElementById("input-main-content").classList.remove("hidden");
// hide the retry button and show the re-render button
document.getElementById("retry-icon").classList.add("hidden");
document.getElementById("re-render-button-container").classList.remove("hidden");
// reset the progress bar
progress = [0,0,0,0,0];
document.getElementById("progress-fill").style.width = 1 + "%";
}
function reRender() {
// display progress bar
document.getElementById("progress-bar-main-content").classList.remove("hidden");
document.getElementById("input-main-content").classList.add("hidden");
document.getElementById("re-render-button-container").classList.add("hidden");
setTimeout(function(){
console.log(audioBuffer, time_signature_top_num_input, time_signature_bottom_num_input, tempo_input, key_signature_input, detectKeyFlag);
// re-evaluate audio with the new tempo and inputs
measures = note_detection.get_notes(audioBuffer, time_signature_top_num_input, time_signature_bottom_num_input, tempo_input);
// determine key signature
if (detectKeyFlag) {
key_signature_input = detectKey(measures);
if (key_signature_input == undefined) {
key_signature_input = "C";
}
}
updateProgress("auto-detect-key");
// re-render the sheet music
syncRecognize(theBlob, theSampleRate, measures);
updateProgress("parse-notes-to-render");
}, 100);
}
function createAudioBuffer(blob) {
var readBlob = require('read-blob');
return new Promise(function (resolve, reject) {
readBlob(blob, 'arraybuffer', function (err, arraybuffer) {
if (err) {
reject(err);
} else {
resolve(audioContext.decodeAudioData(arraybuffer, function (buffer) {
audioBuffer = buffer;
// Note-detection
measures = note_detection.get_notes(audioBuffer, time_signature_top_num_input, time_signature_bottom_num_input, tempo_input);
// Key detection
if (detectKeyFlag) {
key_signature_input = detectKey(measures);
if (key_signature_input == undefined) {
key_signature_input = "C";
}
}
updateProgress("auto-detect-key");
theBlob = blob;
theSampleRate = audioBuffer.sampleRate;
// Speech to text
syncRecognize(blob, audioBuffer.sampleRate, measures);
}, function (e) {
"Error decoding data"
}));
}
});
});
}
// Show tempo detection interface
function startDetectTempo() {
detectingTempoContent.classList.remove("hidden");
detectingTempo = true;
taps = 10;
tempoCountdown.innerText = "" + taps + " times";
detectTempoButton.blur();
}
// Tempo detection
function keyPress(e) {
if (detectingTempo) {
if (e.keyCode == 32) {
e.preventDefault();
if (taps == 10) {
startTime = new Date();
}
taps--;
tempoCountdown.innerText = "" + taps + " times";
if (taps == 0) {
detectingTempo = false;
endTime = new Date();
var elapsedTime = endTime.getTime() - startTime.getTime();
tempoBPM = 9 / (elapsedTime / 60000);
console.log(tempoBPM);
tempoInput.value = Math.round(tempoBPM);
detectingTempoContent.classList.add("hidden");
}
}
}
}
// Toggle key detection with checkbox
function toggleDetectKey() {
if (detectKeyFlag) {
detectKeyFlag = false;
} else {
detectKeyFlag = true;
}
}
function startMetronome() {
//create text for countdown and append it to the html
var cd = document.getElementById("countdown");
var countFrom = time_signature_top_num_input;
var count = document.createTextNode(countFrom);
cd.appendChild(count);
//visual + text for metronome
var circ = document.getElementById("circ");
circ.classList.add("circle");
if (circ.childNodes.length > 0) {
circ.removeChild(circ.childNodes[0]);
}
var text = document.createTextNode(tempo_input + " BPM");
circ.appendChild(text);
//start the countdown based on the tempo
timer = window.setInterval(function () { //decrement on the beat?
document.getElementById("countdown").innerHTML = "" + countFrom;
if (countFrom == 0) {
//remove the countdown text
document.getElementById("countdown").innerHTML = "Go!";
//clean
//window.clearInterval(timerBoi);
document.getElementById("stop-icon").classList.remove("disabled-button");
rec.record();
} else {
countFrom = countFrom - 1;
}
document.getElementById("circ").classList.add('shadow');
window.setTimeout(function () {
document.getElementById("circ").classList.remove('shadow');
}, 100)
}, 60000 / tempo_input);
}
function stopMetronome() {
window.clearInterval(timer);
document.getElementById("circ").classList.toggle('paused');
document.getElementById("countdown").innerHTML = "";
}