-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathdoppler.js
130 lines (107 loc) · 4.02 KB
/
doppler.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
window.doppler = (function() {
var AuContext = (window.AudioContext ||
window.webkitAudioContext ||
window.mozAudioContext ||
window.oAudioContext ||
window.msAudioContext);
var ctx = new AuContext();
var osc = ctx.createOscillator();
// This is just preliminary, we'll actually do a quick scan
// (as suggested in the paper) to optimize this.
var freq = 20000;
// See paper for this particular choice of frequencies
var relevantFreqWindow = 33;
var getBandwidth = function(analyser, freqs) {
var primaryTone = freqToIndex(analyser, freq);
var primaryVolume = freqs[primaryTone];
// This ratio is totally empirical (aka trial-and-error).
var maxVolumeRatio = 0.001;
var leftBandwidth = 0;
do {
leftBandwidth++;
var volume = freqs[primaryTone-leftBandwidth];
var normalizedVolume = volume / primaryVolume;
} while (normalizedVolume > maxVolumeRatio && leftBandwidth < relevantFreqWindow);
var rightBandwidth = 0;
do {
rightBandwidth++;
var volume = freqs[primaryTone+rightBandwidth];
var normalizedVolume = volume / primaryVolume;
} while (normalizedVolume > maxVolumeRatio && rightBandwidth < relevantFreqWindow);
return { left: leftBandwidth, right: rightBandwidth };
};
var freqToIndex = function(analyser, freq) {
var nyquist = ctx.sampleRate / 2;
return Math.round( freq/nyquist * analyser.fftSize/2 );
};
var indexToFreq = function(analyser, index) {
var nyquist = ctx.sampleRate / 2;
return nyquist/(analyser.fftSize/2) * index;
};
var optimizeFrequency = function(osc, analyser, freqSweepStart, freqSweepEnd) {
var oldFreq = osc.frequency.value;
var audioData = new Uint8Array(analyser.frequencyBinCount);
var maxAmp = 0;
var maxAmpIndex = 0;
var from = freqToIndex(analyser, freqSweepStart);
var to = freqToIndex(analyser, freqSweepEnd);
for (var i = from; i < to; i++) {
osc.frequency.value = indexToFreq(analyser, i);
analyser.getByteFrequencyData(audioData);
if (audioData[i] > maxAmp) {
maxAmp = audioData[i];
maxAmpIndex = i;
}
}
// Sometimes the above procedure seems to fail, not sure why.
// If that happends, just use the old value.
if (maxAmpIndex == 0) {
return oldFreq;
}
else {
return indexToFreq(analyser, maxAmpIndex);
}
};
var readMicInterval = 0;
var readMic = function(analyser, userCallback) {
var audioData = new Uint8Array(analyser.frequencyBinCount);
analyser.getByteFrequencyData(audioData);
var band = getBandwidth(analyser, audioData);
userCallback(band);
readMicInterval = setTimeout(readMic, 1, analyser, userCallback);
};
var handleMic = function(stream, callback, userCallback) {
// Mic
var mic = ctx.createMediaStreamSource(stream);
var analyser = ctx.createAnalyser();
analyser.smoothingTimeConstant = 0.5;
analyser.fftSize = 2048;
mic.connect(analyser);
// Doppler tone
osc.frequency.value = freq;
osc.type = osc.SINE;
osc.start(0);
osc.connect(ctx.destination);
// There seems to be some initial "warm-up" period
// where all frequencies are significantly louder.
// A quick timeout will hopefully decrease that bias effect.
setTimeout(function() {
// Optimize doppler tone
freq = optimizeFrequency(osc, analyser, 19000, 22000);
osc.frequency.value = freq;
clearInterval(readMicInterval);
callback(analyser, userCallback);
});
};
return {
init: function(callback) {
navigator.getUserMedia_ = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
navigator.getUserMedia_({ audio: { optional: [{ echoCancellation: false }] } }, function(stream) {
handleMic(stream, readMic, callback);
}, function() { console.log('Error!') });
},
stop: function () {
clearInterval(readMicInterval);
}
}
})(window, document);