Last active
June 9, 2017 10:31
-
-
Save daformat/86df1b00514d5aa5d4e746adf736f6e1 to your computer and use it in GitHub Desktop.
Beep according to the realtime visitor count on Google Analytics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is more a simple and naive proof of concept than anything | |
it's based on [this answer](https://stackoverflow.com/a/41077092/1358317) | |
on stackoverflow. | |
*/ | |
(function() { | |
// Create the audio context | |
var audioCtx = new(window.AudioContext || window.webkitAudioContext)(); | |
function beep(options, callback) { | |
options = options || {}; | |
var volume = options.volume || 1; | |
var frequency = options.frequency || 196; | |
var type = options.type || 'sine'; | |
var duration = options.duration || 500; | |
var delay = options.delay || 0; | |
var harsh = options.harsh || false; | |
setTimeout(function() { | |
var oscillator = audioCtx.createOscillator(); | |
var gainNode = audioCtx.createGain(); | |
oscillator.connect(gainNode); | |
gainNode.connect(audioCtx.destination); | |
oscillator.frequency.value = frequency; | |
oscillator.type = type; | |
if (callback){ | |
oscillator.onended = callback; | |
} | |
if (harsh) { | |
gainNode.gain.value = volume; | |
} else { | |
gainNode.gain.value = 0.00001; | |
gainNode.gain.setTargetAtTime(volume, audioCtx.currentTime, duration / 1000); | |
gainNode.gain.setTargetAtTime(0.00001, audioCtx.currentTime + (duration / 4) / 1000, duration / 4 / 1000); | |
} | |
oscillator.start(); | |
setTimeout( | |
function() { | |
oscillator.stop(); | |
}, | |
duration | |
); | |
}, (delay ? delay : 0)); | |
} | |
function handleTick() { | |
// 196 Hz is the frequency for a G3 | |
// see https://www.seventhstring.com/resources/notefrequencies.html | |
var baseFreq = 196; | |
var beepDelay = 750; | |
var nbVisitors = document.getElementById('ID-overviewCounterValue').innerText; | |
// Log the current visitor count if not 0 and beep accordingly | |
if( nbVisitors != 0) { | |
console.log( | |
new Date().toLocaleString() + "\t", | |
nbVisitors + ' visitor' + (nbVisitors > 1 ? 's' : '') | |
); | |
for(var i = 0; i < nbVisitors; i++) { | |
// 4 / 3 is the ratio for a perfect fourth, so it'll always sound nice | |
// see http://www.sengpielaudio.com/Root-Ratios.gif | |
beep({ | |
frequency: baseFreq, | |
delay: i * beepDelay | |
}); | |
beep({ | |
frequency: baseFreq * 4 / 3, | |
delay: i * beepDelay + 150 | |
}); | |
beep({ | |
frequency: baseFreq * 4 / 3 * 4 / 3, | |
delay: i * beepDelay + 150 * 2 | |
}); | |
} | |
} | |
} | |
// Call the handler immediatly, before setting an interval | |
handleTick(); | |
// Change the interval to whatever you like | |
// Google analytics stops counting a user as active after 5 minutes | |
window.setInterval(handleTick, 5 * 60 * 1000); | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Notes frequencies:
source
Interval ratios
source