-
Notifications
You must be signed in to change notification settings - Fork 1
/
camerons_lament.js
320 lines (247 loc) · 6.97 KB
/
camerons_lament.js
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
var freq_error;
var result = "";
var potential_start_array = [];
var checksum = [];
var payload_length;
var payload_length_array = [];
var last_char;
var state;
var State = {
UNKNOWN: 1,
LENGTH_CHARS: 2,
CONTENT_CHARS: 3,
CHECKSUM_CHARS: 4
};
var state = State.UNKNOWN;
var data;
/* check we have web audio api */
function get_context(){
try {
//AudioContext()is mozilla
context = AudioContext();
}
catch (e) {
console.log("Browser does not support Web Audio API as AudioContext(), trying another");
}
try{
if(!context){
context = new AudioContext();
console.log("context ok");
console.log(context);
}
}
catch (e) {
console.log("Browser does not support Web Audio API as new AudioContext(), trying another");
}
try{
if(!context){
context = new webkitAudioContext();
}
}
catch (e) {
console.log("Browser does not support Web Audio API as webkitAudioContext(), trying another");
}
try{
if(!context){
context = window.audioContext;
}
}
catch (e) {
console.error("Browser does not support Web Audio API: window.audioContext - all failed");
return false;
}
}
/* get microphone input */
function get_microphone_input() {
navigator.getMedia = ( navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia ||
navigator.msGetUserMedia);
if(navigator.getMedia){
navigator.getMedia({audio: true, video: false}, function(stream) {
var analyser = context.createAnalyser();
var microphone = context.createMediaStreamSource(stream);
microphone.connect(analyser);
process(analyser);
}, function(e){alert(e);});
}else{
alert("no user audio");
}
}
/* process audio */
function process(analyser){
var raw_freqs = new Uint8Array(analyser.frequencyBinCount);
the_interval = setInterval(function(){
analyser.getByteFrequencyData(raw_freqs);
var amp, freq;
decode(raw_freqs, freq, amp);
},10);
}
/* process the data to get freq and amp */
function decode(raw_freqs, freq, amp) {
var max = -Infinity;
var min = Infinity;
var index = -1;
for (var i = 0; i < raw_freqs.length; i++) {
if (raw_freqs[i] > max) {
max = raw_freqs[i];
index = i;
}
if (raw_freqs[i] < max) {
min = raw_freqs[i];
}
}
amp = max - min;
var nyquist = context.sampleRate/2;
freq = nyquist/raw_freqs.length * index;
//document.getElementById("amp").innerHTML = "Amplitude: "+ amp;
document.getElementById("freq").innerHTML = "Frequency: "+freq;
process_character(freq);
}
/* work out what to do with the character */
function process_character(freq){
var char = freq_to_char(freq);
if(char && char!=last_char){
result = result + "" + char;
document.getElementById("result").innerHTML = char;
var match = test_for_lament();
if(match){
document.getElementById("message").innerHTML = "RIGHT! "+match;
result = "";
setTimeout(function(){ document.getElementById("message").innerHTML = ""; }, 3000);
}
}
if(char){
last_char = char;
}
if(result.length>100){
result = "";
}
}
function test_for_lament(){
var str = result;
console.log("str "+str);
if(str.indexOf("G3C4G3D#")!=-1){
console.log("match!");
return "G3C4G3D#";
}else if(str.indexOf("C4F4F#")!=-1){
console.log("match2!");
return "C4F4F#";
}else {
console.log("no match!");
return null;
}
}
/* finish up */
function finish_up(msg,result_payload){
// we are finished - stop and clear everything
console.log("\n\nENDING\nRESULT: ");
console.log(result_payload)
document.getElementById("result").innerHTML = result_payload.join("")+" : "+msg;
result = [];
payload_length_array = [];
payload_length = 0;
potential_start_array = [];
// if(the_interval){
// clearInterval(the_interval);
// }
state = State.UNKNOWN;
// stop_listening();
}
/* convert freqs to chars */
function freq_to_char(freq) {
var argh = 5.00;
for (var i in freqs){
if (( freq > freqs[i]-freq_error) && (freq < freqs[i]+freq_error )){
var match = freqs[i];
var result = keys[i];
return result;
}
}
}
/* stop listening and processing */
function stop_listening(){
console.log("STOPPING\n\n\n\n\n\n\n\n");
if(the_interval){
clearInterval(the_interval);
}else{
console.error("No interval to clear");
}
}
/* play array of notes */
function play_array(arr, len){
console.log("arr");
console.log(arr);
var count = 0;
var freq = arr[count];
console.log("playing "+freq);
play_note(freq);
var tt = setTimeout(function(){
if(freq){
stop_playing_note(freq);
}
count = count+1;
if(arr[count]){
freq = arr[count].toString();
if(!freq){
console.log(" key not found for "+j+" ");
}else{
console.log("playing "+freq);
play_note(freq);
}
}else{
clearTimeout(tt);
}
},len);
/*
var interval = setInterval(function(){
if(freq){
stop_playing_note(freq);
}
count = count+1;
if(arr[count]){
freq = arr[count].toString();
if(!freq){
console.log(" key not found for "+j+" ");
}else{
console.log("playing "+freq);
play_note(freq);
}
}else{
clearInterval(interval);
}
},len);
*/
}
/* create an oscillator for a given frequency */
function create_oscillator(freq) {
var source = context.createOscillator();
source.frequency.value = freq;
source.connect(context.destination);
//safari problem
source.noteOn ? source.noteOn(0) : source.start(0);
return source;
}
/* play a note */
function play_note(freq){
tones[freq.toString()] = create_oscillator(freq);
}
function stop_playing_note(freq){
//safari again
try{
var note = tones[freq.toString()];
note.stop(0);
}catch(e){
console.log(e);
note.noteOff(0);
}
delete tones[freq.toString()];
}
function stop_listening(){
console.log("STOPPING\n\n\n\n\n\n\n\n");
if(the_interval){
clearInterval(the_interval);
}else{
console.error("No interval to clear");
}
}