Skip to content

Instantly share code, notes, and snippets.

@Schokokex
Created December 15, 2020 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schokokex/daad2cb8a64a03e8616fb20cf792a390 to your computer and use it in GitHub Desktop.
Save Schokokex/daad2cb8a64a03e8616fb20cf792a390 to your computer and use it in GitHub Desktop.
acoustic speedtest
var audioCtx = new (window.AudioContext || window.webkitAudioContext)();
function playNote(frequency, duration, callback) {
duration = duration / 1000;
// create Oscillator node
var oscillator = audioCtx.createOscillator();
oscillator.type = 'square';
oscillator.frequency.value = frequency; // value in hertz
oscillator.connect(audioCtx.destination);
oscillator.onended = callback;
oscillator.start(0);
oscillator.stop(audioCtx.currentTime + duration);
}
function calculateAverage(array){
var total = 0;
var count = 0;
array.forEach(function(item, index){
total += item;
count++;
});
return total / count;
}
//JUST AN EXAMPLE, PLEASE USE YOUR OWN PICTURE!
var imageAddr = "https://raw.githubusercontent.com/Schokokex/speedtest/main/Bild_2020-11-08_002410.png";
var downloadSize = 236832; //bytes
function ShowProgressMessage(msg) {
if (console) {
if (typeof msg == "string") {
console.log(msg);
} else {
for (var i = 0; i < msg.length; i++) {
console.log(msg[i]);
}
}
}
var oProgress = document.getElementById("progress");
if (oProgress) {
var actualHTML = (typeof msg == "string") ? msg : msg.join("<br />");
oProgress.innerHTML = actualHTML;
}
}
function InitiateSpeedDetection() {
ShowProgressMessage("Loading the image, please wait...");
window.setTimeout(MeasureConnectionSpeed, 1);
};
if (window.addEventListener) {
window.addEventListener('load', InitiateSpeedDetection, false);
} else if (window.attachEvent) {
window.attachEvent('onload', InitiateSpeedDetection);
}
var lasthund = Array(100).fill(10000000);
function MeasureConnectionSpeed() {
var startTime, endTime;
var download = new Image();
download.onload = function () {
endTime = (new Date()).getTime();
showResults();
}
download.onerror = function (err, msg) {
ShowProgressMessage("Invalid image, or error downloading");
}
startTime = (new Date()).getTime();
var cacheBuster = "?nnn=" + startTime;
download.src = imageAddr + cacheBuster;
function showResults() {
var duration = (endTime - startTime) / 1000;
var bitsLoaded = downloadSize * 8;
var speedBps = (bitsLoaded / duration).toFixed(2);
var speedKbps = (speedBps / 1024).toFixed(2);
var speedMbps = (speedKbps / 1024).toFixed(2);
ShowProgressMessage([
"Your connection speed is:",
speedBps + " bps",
speedKbps + " kbps",
speedMbps + " Mbps"
]);
lasthund.push(Number(speedKbps));
lasthund.shift();
var frequency = speedKbps * 1000 / calculateAverage(lasthund) ;
playNote(frequency, 100);
}
}
var shouldrun = true;
function plsstop(){
shouldrun = false;
setTimeout(()=>{shouldrun = true}, 200);
}
function test() {
if (shouldrun){
MeasureConnectionSpeed();
setTimeout(test, 100);
}
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment