-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
289 lines (211 loc) · 7.71 KB
/
index.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
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
<!doctype html>
<html>
<head>
<title>instrument detection</title>
<meta charset="utf-8" />
<link href='./css/style.css' rel='stylesheet'>
</head>
<body>
<select id="audioSource"></select>
<div id="detector" class="vague">
<div class="pitch"><span id="pitch">--</span> Hz</div>
<div class="note"><span id="note">--</span></div>
<canvas id="output" width=300 height=42></canvas>
<div id="detune"><input id="detune_amt"></div>
</div>
<script src="js/tune.js"></script>
<hr>
<h2>create training data</h2>
<div>
<select id="instrument">
<option value="bassoon">bassoon</option>
<option value="cello">cello</option>
<option value="clarinet">clarinet</option>
<option value="euphonium">euphonium</option>
<option value="flute">flute</option>
<option value="horn">horn</option>
<option value="marimba">marimba</option>
<option value="oboe">oboe</option>
<option value="recorder">recorder</option>
<option value="saxophone">saxophone</option>
<option value="trombone">trombone</option>
<option value="trumpet">trumpet </option>
<option value="tuba">tuba </option>
<option value="violin">violin </option>
</select>
<label for="instrument">instrument</label>
</div>
<input type="file" onchange="readFile(this.files);">
<br>
<button id="analyze">analyze</button>
<script>
</script>
<hr>
<button id="saxophone">saxophone</button>
<button id="flute">flute</button>
<button id="trumpet">trumpet</button>
<script>
// audio file buffer source
var source
var playing;
function readFile(files) {
var fileReader = new FileReader();
fileReader.readAsArrayBuffer(files[0]);
fileReader.onload = function(e) {
playAudioFile(e.target.result);
//console.log(("Filename: '" + files[0].name + "'"), ( "(" + ((Math.floor(files[0].size/1024/1024*100))/100) + " MB)" ));
}
}
function playAudioFile(file) {
audioContext.decodeAudioData(file, function(buffer) {
source = audioContext.createBufferSource();
source.buffer = buffer;
source.loop = false;
source.connect(audioContext.destination);
source.connect(analyser)
source.connect(analyser1)
});
}
////////////////////////////////////////
var theData = {
harmonics : [],
decibels : []
}
var harmonics = [];
var tempData = []
// create tuner
var tuner = new Tune({
temperament: "equal",
fundamental: 440
});
// set the temperament
//tuner.setTemperament("meantone");
//tuner.tune(44);
//tuner.setWavelength(0.777);
// listen for dropdown change
let dropdown = document.querySelector('#temperament');
if (dropdown) dropdown.addEventListener('change', function(event) {
console.log(event.target.value);
tuner.setTemperament(event.target.value);
});
var ac;
function updatePitch( time ) {
// Float32Array should be the same length as the frequencyBinCount
decibelArray = new Float32Array(analyser1.frequencyBinCount);
analyser1.getFloatFrequencyData(decibelArray);
// console.log(decibelArray)
var cycles = new Array;
analyser.getFloatTimeDomainData( buf );
ac = autoCorrelate( buf, audioContext.sampleRate );
if (ac == -1) {
detectorElem.className = "vague";
pitchElem.innerText = "--";
noteElem.innerText = "-";
detuneElem.className = "";
detuneAmount.innerText = "--";
} else {
detectorElem.className = "confident";
frequency = ac;
pitchElem.innerText = Math.round( frequency ) ;
var note = Tune.noteFromPitch( frequency );
Tune.getNoteName(Tune.ftom(frequency))
noteElem.innerHTML = Tune.getNoteName(Tune.ftom(frequency) * -1);
// current fundamental frequency (rounded)
var freq = Math.round(frequency)
console.log(getHarmonics(freq))
console.log(theData.decibels)
var detune = tuner.tune(frequency);
if (detune == 0 ) {
detuneElem.className = "";
detuneAmount.innerHTML = "--";
} else {
detuneAmount.value = format(detune);
}
}
if (!window.requestAnimationFrame){ window.requestAnimationFrame = window.webkitRequestAnimationFrame;}
rafID = window.requestAnimationFrame( updatePitch );
}
// add (+) if positive, and round to 4 decimals
function format(e){ return (e<0?"":"+") + (e).toFixed(0); }
// get harmonics of fundamental
function getHarmonics(e){
theData = {
harmonics : [],
decibels : [],
normal : []
}
// calculate harmonics, then get the decibel values for each harmonic
var harm = new Promise((resolve, reject) => {
var x = new Promise((resolve, reject) => {
theData.harmonics = [Tune.harmonic(e, 1), Tune.harmonic(e, 2), Tune.harmonic(e, 3), Tune.harmonic(e, 4), Tune.harmonic(e, 5), Tune.harmonic(e, 6), Tune.harmonic(e, 7), Tune.harmonic(e, 8), Tune.harmonic(e, 9), Tune.harmonic(e, 10), Tune.harmonic(e, 11), Tune.harmonic(e, 12), Tune.harmonic(e, 13), Tune.harmonic(e, 14), Tune.harmonic(e, 15), Tune.harmonic(e, 16), Tune.harmonic(e, 17), Tune.harmonic(e, 18), Tune.harmonic(e, 19), Tune.harmonic(e, 20), Tune.harmonic(e, 21), Tune.harmonic(e, 22), Tune.harmonic(e, 23), Tune.harmonic(e, 24), Tune.harmonic(e, 25), Tune.harmonic(e, 26), Tune.harmonic(e, 27), Tune.harmonic(e, 28), Tune.harmonic(e, 29), Tune.harmonic(e, 30)]
resolve();
});
var y = new Promise((resolve, reject) => {
theData.harmonics.forEach(calculateHarmonics);
resolve();
});
var z = new Promise((resolve, reject) => {
// normalize...?
//calculateHarmonics.forEach(normalize)
resolve();
} );
x.then(() => { y.then(() => { z.then(() => { resolve() })})});
});
harm
return theData.harmonics;
}
function calculateHarmonics(item, index) {
theData.decibels.push(decibelArray[item])
// document.getElementById("demo").innerHTML += index + ":" + item + "<br>";
}
// push data to json
// add listener to all buttons
document.querySelector('#analyze').addEventListener("click", function(e) {
playing = true
tempData = [];
var timeout
// wait 1 second
//setTimeout(function timer() {
console.log("starting...")
source.start(0);
source.onended = onEnded;
function onEnded() {
console.log('stoppped')
//clearTimeout(timeout);
saveData();
// source.disconnect(analyser)
//document.getElementById("startButton").disabled = false;
playing = false
}
// push 2000 sets of data, at 25ms interval
for (let i = 1; i < 1000000; i++) {
if(!playing){//saveData();
i = 1000000
clearTimeout(timeout);
break;
} else {
timeout = setTimeout(function timer() {
if(theData.decibels[0] && ac !== -1 ){ tempData.push(theData.decibels) }
console.log("training... step " + (i+ 1));
}, i * 25);
}
}
//}, 1000);
function saveData(){
console.log("training done")
var inst = document.getElementById("instrument");
exportJSON(tempData, inst.value + '.json');
}
});
// normalize values in range of 0 to 1
function normalize(val, max) { return (val - 0) / (max - 0); }
// export json
function exportJSON(e, filename) {
let link = document.createElement('a');
link.setAttribute('href', 'data:application/json;charset=utf-8,'+ encodeURIComponent(JSON.stringify(e, undefined, 2)));
link.setAttribute('download', filename);
link.click();
}
</script>
</body>
</html>