Skip to content

Commit 22baeca

Browse files
committed
Realtime parsing of notes
1 parent 1755a6d commit 22baeca

File tree

1 file changed

+157
-86
lines changed

1 file changed

+157
-86
lines changed

noteify/public/js/high.js

+157-86
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
var dancer = new Dancer();
22
var array = 0;
33
var note;
4-
var notes = [];
4+
var notes = [{scientific: function(){return "R";}, "duration": 1}];
55
var index = 0;
66
var prev2 = "";
77
var scientific = "";
88
var pause_count = 0;
99
var last_time = 0;
10-
var MIN_LENGTH = 10;
10+
var last_note = 0;
11+
var last_ampl = 0.000001;
12+
var MIN_LENGTH = 8;
1113
var VOLUME_THRESHOLD = .01;
12-
var SAMPLING_RATE = 22050; //change this to your computer's sampling rate
13-
// var SAMPLING_RATE = 24000; //change this to your computer's sampling rate
14+
// var SAMPLING_RATE = 22050; //change this to your computer's sampling rate
15+
var SAMPLING_RATE = 24000; //change this to your computer's sampling rate
16+
17+
var LOW_NOTE = "C4", HIGH_NOTE = "C6";
1418

1519

1620
// These are the final results
@@ -19,9 +23,9 @@ var durations = [];
1923

2024
// This starts the playing/parsing
2125
function getNotes(url, callback) {
22-
dancer.load({ src: '/data/?file=' + url });
23-
dancer.setVolume(0);
24-
// dancer.load({ src: url }); //TODO remove
26+
// dancer.load({ src: '/data/?file=' + url });
27+
// dancer.setVolume(0);
28+
dancer.load({ src: url }); //TODO remove
2529
dancer.play();
2630

2731
dancer.after(0, function(){
@@ -33,69 +37,138 @@ function getNotes(url, callback) {
3337
last_time = dancer.getTime();
3438

3539
var merged = false;
36-
var freq = fuzzy_index(array) * SAMPLING_RATE / 512;
37-
//console.log(freq);
38-
if(!isNaN(freq) && freq > 0){
39-
pause_count = 0;
40-
note = teoria.note.fromFrequency(freq);
41-
if(scientific == note.note.scientific()){
42-
notes[index]["duration"] += 1;
43-
console.log(scientific);
44-
} else {
45-
index++;
46-
scientific = note.note.scientific();
47-
notes[index] = {};
48-
notes[index]["note"] = scientific;
49-
notes[index]["duration"] = 1;
50-
console.log(note.note.scientific());
51-
}
40+
var temp = fuzzy_index(array);
41+
var freq = temp[0] * SAMPLING_RATE / 512, ampl = temp[1];
42+
43+
////
44+
var note;
45+
if(freq == 0 || isNaN(freq)){
46+
note = {scientific: function(){return "R";}};
5247
} else{
53-
if(freq == 0){
54-
console.log("gap");
55-
index++;
56-
notes[index] = {};
57-
notes[index]["note"] = "tongue";
58-
notes[index]["duration"] = 1;
59-
scientific = 0;
60-
} else {
61-
// console.log(" ");
62-
}
48+
note = teoria.note.fromFrequency(freq).note;
49+
}
50+
51+
console.log(note.scientific());
52+
53+
if(note.scientific() != last_note || // if new note
54+
(ampl - last_ampl) / last_ampl > 3){ // or 3 times louder
55+
56+
// create new note
57+
index++;
58+
notes[index] = {"note": note, "duration" : 1};
6359

64-
if(pause_count > 30){
65-
dancer.pause();
66-
// console.log("FILTERED, BITCHEEEEES!!!!");
67-
filter(notes);
68-
convert(notes)
69-
for(var i = 0; i < notesarr.length; i++){
70-
console.log(notesarr[i][0] + " " + durations[i]);
60+
// maybe save current note
61+
// debugger;
62+
if(index > 1 && notes[index-1].duration >= MIN_LENGTH && ((QUARTER > 0) || last_note != 0)){
63+
if(QUARTER < 0){
64+
QUARTER = notes[index].duration;
7165
}
72-
callback(notesarr, durations);
66+
67+
var temp = parseNote(notes[index]);
68+
callback(temp[0], temp[1]);
7369
}
70+
} else{
71+
// increment current note
72+
notes[index].duration++;
7473
}
75-
});
74+
last_note = note.scientific();
75+
last_ampl = ampl;
7676

77-
function filter(array){
78-
for(var i = 1; i < array.length; i++){
79-
//console.log(array[i]["duration"]);
80-
if(array[i]["duration"] < MIN_LENGTH && array[i]["note"] != "tongue"){
81-
array.splice(i,1);
82-
if(i > 1 && array[i]["note"] == array[i-1]["note"]){ //merge
83-
array[i]["duration"] += array[i-1]["duration"];
84-
array.splice(i-1,1);
85-
}
86-
i--;
87-
}
77+
if(pause_count > 10 || dancer.getTime() > 30){
78+
dancer.pause();
79+
return;
8880
}
89-
for(var i = 1; i < array.length; i++){
90-
if(array[i]["note"] == "tongue"){
91-
array.splice(i,1);
92-
i--;
93-
}
81+
82+
83+
////
84+
//console.log(freq);
85+
// if(!isNaN(freq) && freq > 0){
86+
// pause_count = 0;
87+
// note = teoria.note.fromFrequency(freq);
88+
// if(scientific == note.note.scientific()){
89+
// notes[index]["duration"] += 1;
90+
// console.log(scientific);
91+
// } else {
92+
// index++;
93+
// scientific = note.note.scientific() + "\t~" + result[1];
94+
// notes[index] = {};
95+
// notes[index]["note"] = scientific;
96+
// notes[index]["duration"] = 1;
97+
// console.log(note.note.scientific());
98+
99+
// if(notes[index-1].duration >= MIN_LENGTH){
100+
// document.writeln(notes[index-1].note + "--" + notes[index-1].duration + "\n");
101+
// }
102+
// }
103+
// } else{
104+
// if(freq == 0){
105+
// console.log("gap");
106+
// index++;
107+
// notes[index] = {};
108+
// notes[index]["note"] = "tongue";
109+
// notes[index]["duration"] = 1;
110+
// scientific = 0;
111+
112+
// if(notes[index-1].duration >= MIN_LENGTH){
113+
// document.writeln(notes[index-1].note + "--" + notes[index-1].duration + "\n");
114+
// }
115+
// }
116+
117+
// if(pause_count > 30 || dancer.getTime() > 10){
118+
// dancer.pause();
119+
// // console.log("FILTERED, BITCHEEEEES!!!!");
120+
// filter(notes);
121+
// convert(notes)
122+
// for(var i = 0; i < notesarr.length; i++){
123+
// console.log(notesarr[i][0] + " " + durations[i]);
124+
// }
125+
// callback(notesarr, durations);
126+
// }
127+
// }
128+
});
129+
130+
function parseNote(note){
131+
//figure out length
132+
var length;
133+
if(note.duration < QUARTER * 0.5){
134+
length = '8';
135+
} else if(note.duration < QUARTER * 1.5){
136+
length = 'q';
137+
} else if(note.duration < QUARTER * 3){
138+
length = 'h';
139+
} else{ //if(note.duration < QUARTER * 5){
140+
length = 'w';
94141
}
95-
//return array;
142+
143+
if(note.note.scientific() == "R"){
144+
return ["c/5", length + "r"];
145+
} else{
146+
return [process(note.note.scientific()), length];
96147
}
148+
}
149+
150+
// function filter(array){
151+
152+
// for(var i = 1; i < array.length; i++){
153+
// //console.log(array[i]["duration"]);
154+
// if(array[i]["duration"] < MIN_LENGTH && array[i]["note"] != "tongue"){
155+
// array.splice(i,1);
156+
// if(i > 1 && array[i]["note"] == array[i-1]["note"]){ //merge
157+
// array[i]["duration"] += array[i-1]["duration"];
158+
// array.splice(i-1,1);
159+
// }
160+
// i--;
161+
// }
162+
// }
163+
// for(var i = 1; i < array.length; i++){
164+
// if(array[i]["note"] == "tongue"){
165+
// array.splice(i,1);
166+
// i--;
167+
// }
168+
// }
169+
// //return array;
170+
// }
97171
function fuzzy_index(array){
98-
var gap = false;
99172
var max = 0;
100173
var a = 0;
101174
for(var i = 0; i < array.length; i++){
@@ -105,38 +178,36 @@ function filter(array){
105178
}
106179
}
107180
if(array[a] < VOLUME_THRESHOLD){
108-
gap = true;
181+
return[0, 0];
109182
}
110183
var b = a + (array[a+1] > array[a-1] ? 1 : -1);
111-
112-
if(gap){
113-
return 0;
114-
}
115-
return (array[a] * a + array[b] * b) / (array[a] + array[b]);
184+
return [(array[a] * a + array[b] * b) / (array[a] + array[b]), array[a] + array[b]];
116185
}
117186

118-
var NOTE_LENGTH = 35;
119-
120-
function convert(array){
121-
// hackily sets quarter note length to the second item.
122-
NOTE_LENGTH = notes[2].duration;
123-
for(var i = 1; i < array.length; i++){
124-
var note = notes[i];
125-
console.log(note);
126-
notesarr[i-1] = [process(note.note)];
127-
if(note.duration < NOTE_LENGTH * 1.5){
128-
durations[i-1] = 'q';
129-
} else if(note.duration < NOTE_LENGTH * 3){
130-
durations[i-1] = 'h';
131-
} else if(note.duration < NOTE_LENGTH * 5){
132-
durations[i-1] = 'w';
133-
}
134-
}
135-
}
187+
var QUARTER = -1;
188+
189+
// function convert(array){
190+
// // hackily sets quarter note length to the second item.
191+
// // QUARTER = notes[2].duration;
192+
// for(var i = 1; i < array.length; i++){
193+
// var note = notes[i];
194+
// console.log(note);
195+
// notesarr[i-1] = [process(note.note)];
196+
// if(note.duration < QUARTER * 0.5){
197+
// durations[i-1] = '8';
198+
// } else if(note.duration < QUARTER * 1.5){
199+
// durations[i-1] = 'q';
200+
// } else if(note.duration < QUARTER * 3){
201+
// durations[i-1] = 'h';
202+
// } else if(note.duration < QUARTER * 5){
203+
// durations[i-1] = 'w';
204+
// }
205+
// }
206+
// }
136207

137-
// Converts A5 to a/4
208+
// Converts A5 to a/5
138209
function process(note){
139210
note = note.toLowerCase();
140-
return note.slice(0, -1) + '/' + (parseInt(note.slice(-1)) - 1);
211+
return note.slice(0, -1) + '/' + (parseInt(note.slice(-1)));
141212
}
142213
}

0 commit comments

Comments
 (0)