1
1
var dancer = new Dancer ( ) ;
2
2
var array = 0 ;
3
3
var note ;
4
- var notes = [ ] ;
4
+ var notes = [ { scientific : function ( ) { return "R" ; } , "duration" : 1 } ] ;
5
5
var index = 0 ;
6
6
var prev2 = "" ;
7
7
var scientific = "" ;
8
8
var pause_count = 0 ;
9
9
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 ;
11
13
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" ;
14
18
15
19
16
20
// These are the final results
@@ -19,9 +23,9 @@ var durations = [];
19
23
20
24
// This starts the playing/parsing
21
25
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
25
29
dancer . play ( ) ;
26
30
27
31
dancer . after ( 0 , function ( ) {
@@ -33,69 +37,138 @@ function getNotes(url, callback) {
33
37
last_time = dancer . getTime ( ) ;
34
38
35
39
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" ; } } ;
52
47
} 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 } ;
63
59
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 ;
71
65
}
72
- callback ( notesarr , durations ) ;
66
+
67
+ var temp = parseNote ( notes [ index ] ) ;
68
+ callback ( temp [ 0 ] , temp [ 1 ] ) ;
73
69
}
70
+ } else {
71
+ // increment current note
72
+ notes [ index ] . duration ++ ;
74
73
}
75
- } ) ;
74
+ last_note = note . scientific ( ) ;
75
+ last_ampl = ampl ;
76
76
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 ;
88
80
}
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' ;
94
141
}
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 ] ;
96
147
}
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
+ // }
97
171
function fuzzy_index ( array ) {
98
- var gap = false ;
99
172
var max = 0 ;
100
173
var a = 0 ;
101
174
for ( var i = 0 ; i < array . length ; i ++ ) {
@@ -105,38 +178,36 @@ function filter(array){
105
178
}
106
179
}
107
180
if ( array [ a ] < VOLUME_THRESHOLD ) {
108
- gap = true ;
181
+ return [ 0 , 0 ] ;
109
182
}
110
183
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 ] ] ;
116
185
}
117
186
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
+ // }
136
207
137
- // Converts A5 to a/4
208
+ // Converts A5 to a/5
138
209
function process ( note ) {
139
210
note = note . toLowerCase ( ) ;
140
- return note . slice ( 0 , - 1 ) + '/' + ( parseInt ( note . slice ( - 1 ) ) - 1 ) ;
211
+ return note . slice ( 0 , - 1 ) + '/' + ( parseInt ( note . slice ( - 1 ) ) ) ;
141
212
}
142
213
}
0 commit comments