Skip to content

Instantly share code, notes, and snippets.

@daformat
Last active June 9, 2017 10:31
Show Gist options
  • Save daformat/86df1b00514d5aa5d4e746adf736f6e1 to your computer and use it in GitHub Desktop.
Save daformat/86df1b00514d5aa5d4e746adf736f6e1 to your computer and use it in GitHub Desktop.
Beep according to the realtime visitor count on Google Analytics
/*
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);
})()
@daformat
Copy link
Author

daformat commented Jun 8, 2017

Notes frequencies:

C C# D Eb E F F# G G# A Bb B
0 16.35 17.32 18.35 19.45 20.60 21.83 23.12 24.50 25.96 27.50 29.14 30.87
1 32.70 34.65 36.71 38.89 41.20 43.65 46.25 49.00 51.91 55.00 58.27 61.74
2 65.41 69.30 73.42 77.78 82.41 87.31 92.50 98.00 103.8 110.0 116.5 123.5
3 130.8 138.6 146.8 155.6 164.8 174.6 185.0 196.0 207.7 220.0 233.1 246.9
4 261.6 277.2 293.7 311.1 329.6 349.2 370.0 392.0 415.3 440.0 466.2 493.9
5 523.3 554.4 587.3 622.3 659.3 698.5 740.0 784.0 830.6 880.0 932.3 987.8
6 1047 1109 1175 1245 1319 1397 1480 1568 1661 1760 1865 1976
7 2093 2217 2349 2489 2637 2794 2960 3136 3322 3520 3729 3951
8 4186 4435 4699 4978 5274 5588 5920 6272 6645 7040 7459 7902

source

Interval ratios

interval-ratios

source

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment