├── MIT-LICENSE ├── README.md ├── doppler.js └── example.html /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (C) 2015 by Daniel Rapp 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 | THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Motion sensing using the doppler effect 2 | [This is an implementation](https://danielrapp.github.io/doppler/) of the [SoundWave paper](http://research.microsoft.com/en-us/um/redmond/groups/cue/publications/guptasoundwavechi2012.pdf) 3 | on the web. It enables you to detect motion using only the microphone and speakers! 4 | 5 | ## How to use it 6 | Just run it like this 7 | ```javascript 8 | doppler.init(function(bandwidth) { 9 | console.log(bandwidth.left - bandwidth.right); 10 | }); 11 | ``` 12 | See more in [example.html](example.html). (Note that doppler uses `navigator.getUserMedia`, which can't be run on the local filesystem, so you'll have to start a server to run this. E.g. with `python -m SimpleHTTPServer`.) Read more about the theory of how this works [on the github-pages site](https://danielrapp.github.io/doppler/). 13 | 14 | ## What to contribute? 15 | What to contribute? 16 | Here's what is most needed: 17 | 18 | ### Multiple sinusoids 19 | Add support for using multiple sinusoids, and combining the data (could be as simple as taking the average), to improve robustness. 20 | 21 | ### Experimental robustness improvement 22 | Up for a challenge? It'd be great to implement the various tricks [described on HN](https://news.ycombinator.com/item?id=9180380) on improving the robustness/accuracy for this (using tricks from radar tech). 23 | 24 | ### Moving the hand too quickly 25 | In the [SoundWave paper](http://research.microsoft.com/en-us/um/redmond/groups/cue/publications/guptasoundwavechi2012.pdf) they talk about a phenomenon that occurs when you move your hand too quickly. (See Figure 2d.) A new bulge is formed. I didn't implement the method they described for reducing this, but it should be pretty easy. What I'm doing at the moment to calculate the bandwidth (see `getBandwidth`), is just iteratively step to the right and left until I've hit a frequency with amplitude `0.001` (see `maxVolumeRatio`) of the doppler tone (see the global variable `freq`). What should be done instead (as suggested by the paper) is 26 | 27 | > perform a second scan, looking beyond the stopping point 28 | > of the first scan. If a second peak with at least 30% of the 29 | > primary tone’s energy is found, the first scan is repeated to 30 | > find amplitude drops calculated from the second peak. 31 | 32 | This improvement can/should all occur in the `getBandwidth` function. 33 | 34 | ## Firefox? 35 | Unfortunately this doesn't work on Firefox since it doesn't seem to support the `echoCancellation: false` parameter to navigator.getUserMedia. This means there's no way to turn off it filtering out the sounds which are coming from the computer itself (which is precisely what we want to measure). 36 | 37 | ## Derivatives 38 | * The awesome [Jasper Lu](https://github.com/jasper-lu) implemented a version of this [to android](https://github.com/jasper-lu/doppler-android). [Go check it out](https://github.com/jasper-lu/doppler-android)! 39 | * The wonderful [Harrison Green](https://github.com/hgarrereyn) wrote a [chrome extension](https://chrome.google.com/webstore/detail/audioscroll-extension/nknlpaccngmmdfjcbjkccfmoimehdeli?hl=en-US&gl=US) with this! 40 | * [Stan James](https://github.com/wanderingstan/handybird) created a wonderful flappy birds implementation with it. 41 | -------------------------------------------------------------------------------- /doppler.js: -------------------------------------------------------------------------------- 1 | window.doppler = (function() { 2 | var AuContext = (window.AudioContext || 3 | window.webkitAudioContext || 4 | window.mozAudioContext || 5 | window.oAudioContext || 6 | window.msAudioContext); 7 | 8 | var ctx = new AuContext(); 9 | var osc = ctx.createOscillator(); 10 | // This is just preliminary, we'll actually do a quick scan 11 | // (as suggested in the paper) to optimize this. 12 | var freq = 20000; 13 | 14 | // See paper for this particular choice of frequencies 15 | var relevantFreqWindow = 33; 16 | 17 | var getBandwidth = function(analyser, freqs) { 18 | var primaryTone = freqToIndex(analyser, freq); 19 | var primaryVolume = freqs[primaryTone]; 20 | // This ratio is totally empirical (aka trial-and-error). 21 | var maxVolumeRatio = 0.001; 22 | 23 | var leftBandwidth = 0; 24 | do { 25 | leftBandwidth++; 26 | var volume = freqs[primaryTone-leftBandwidth]; 27 | var normalizedVolume = volume / primaryVolume; 28 | } while (normalizedVolume > maxVolumeRatio && leftBandwidth < relevantFreqWindow); 29 | 30 | var rightBandwidth = 0; 31 | do { 32 | rightBandwidth++; 33 | var volume = freqs[primaryTone+rightBandwidth]; 34 | var normalizedVolume = volume / primaryVolume; 35 | } while (normalizedVolume > maxVolumeRatio && rightBandwidth < relevantFreqWindow); 36 | 37 | return { left: leftBandwidth, right: rightBandwidth }; 38 | }; 39 | 40 | var freqToIndex = function(analyser, freq) { 41 | var nyquist = ctx.sampleRate / 2; 42 | return Math.round( freq/nyquist * analyser.fftSize/2 ); 43 | }; 44 | 45 | var indexToFreq = function(analyser, index) { 46 | var nyquist = ctx.sampleRate / 2; 47 | return nyquist/(analyser.fftSize/2) * index; 48 | }; 49 | 50 | var optimizeFrequency = function(osc, analyser, freqSweepStart, freqSweepEnd) { 51 | var oldFreq = osc.frequency.value; 52 | 53 | var audioData = new Uint8Array(analyser.frequencyBinCount); 54 | var maxAmp = 0; 55 | var maxAmpIndex = 0; 56 | 57 | var from = freqToIndex(analyser, freqSweepStart); 58 | var to = freqToIndex(analyser, freqSweepEnd); 59 | for (var i = from; i < to; i++) { 60 | osc.frequency.value = indexToFreq(analyser, i); 61 | analyser.getByteFrequencyData(audioData); 62 | 63 | if (audioData[i] > maxAmp) { 64 | maxAmp = audioData[i]; 65 | maxAmpIndex = i; 66 | } 67 | } 68 | // Sometimes the above procedure seems to fail, not sure why. 69 | // If that happends, just use the old value. 70 | if (maxAmpIndex == 0) { 71 | return oldFreq; 72 | } 73 | else { 74 | return indexToFreq(analyser, maxAmpIndex); 75 | } 76 | }; 77 | 78 | var readMicInterval = 0; 79 | var readMic = function(analyser, userCallback) { 80 | var audioData = new Uint8Array(analyser.frequencyBinCount); 81 | analyser.getByteFrequencyData(audioData); 82 | 83 | var band = getBandwidth(analyser, audioData); 84 | userCallback(band); 85 | 86 | readMicInterval = setTimeout(readMic, 1, analyser, userCallback); 87 | }; 88 | 89 | var handleMic = function(stream, callback, userCallback) { 90 | // Mic 91 | var mic = ctx.createMediaStreamSource(stream); 92 | var analyser = ctx.createAnalyser(); 93 | 94 | analyser.smoothingTimeConstant = 0.5; 95 | analyser.fftSize = 2048; 96 | 97 | mic.connect(analyser); 98 | 99 | // Doppler tone 100 | osc.frequency.value = freq; 101 | osc.type = osc.SINE; 102 | osc.start(0); 103 | osc.connect(ctx.destination); 104 | 105 | // There seems to be some initial "warm-up" period 106 | // where all frequencies are significantly louder. 107 | // A quick timeout will hopefully decrease that bias effect. 108 | setTimeout(function() { 109 | // Optimize doppler tone 110 | freq = optimizeFrequency(osc, analyser, 19000, 22000); 111 | osc.frequency.value = freq; 112 | 113 | clearInterval(readMicInterval); 114 | callback(analyser, userCallback); 115 | }); 116 | }; 117 | 118 | return { 119 | init: function(callback) { 120 | navigator.getUserMedia_ = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia); 121 | navigator.getUserMedia_({ audio: { optional: [{ echoCancellation: false }] } }, function(stream) { 122 | handleMic(stream, readMic, callback); 123 | }, function() { console.log('Error!') }); 124 | }, 125 | stop: function () { 126 | clearInterval(readMicInterval); 127 | } 128 | } 129 | })(window, document); 130 | 131 | -------------------------------------------------------------------------------- /example.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14 | 15 | 16 |
17 | 18 | 33 | 34 | 35 | --------------------------------------------------------------------------------