mirror of
https://github.com/danog/system-bus-radio.git
synced 2024-12-02 16:37:48 +01:00
d935c28249
Functionality is now taken care of by Web Workers. The advantages of this is that the page will no longer freeze/lock up when the script is working. This also means that the page status can now update in realtime. (Before, it only updated after the script finished running.) Because it possible for buttons to be clicked during operation, it is now possible to stop script mid-execution. Song recursion should also be possible now but, has not been implemented.
60 lines
1.3 KiB
JavaScript
60 lines
1.3 KiB
JavaScript
//Credits to https://github.com/fulldecent/system-bus-radio
|
|
//As well as Jordan Harband for the nodejs simd library
|
|
//Tested to be working on Chrome at 1560khz
|
|
|
|
var window = self; // Create "window" object
|
|
|
|
function now() {
|
|
return window.performance.now() * 1000000;
|
|
}
|
|
|
|
var NSEC_PER_SEC = 1000000000;
|
|
var register = 3.1415;
|
|
|
|
function square_am_signal(time, freq) {
|
|
postMessage("\nPlaying / " + time + " seconds / " + freq + "Hz");
|
|
var period = NSEC_PER_SEC / freq;
|
|
var start = now();
|
|
var end = now() + time * NSEC_PER_SEC;
|
|
while (now() < end) {
|
|
var mid = start + period / 2;
|
|
var reset = start + period;
|
|
while (now() < mid) {
|
|
for (var i = 0; i < 100; i++) {
|
|
register = 1 - Math.log(register) / 1.7193;
|
|
}
|
|
}
|
|
while (now() < reset) {}
|
|
start = reset;
|
|
}
|
|
}
|
|
|
|
function play(song) {
|
|
song = song.split("\n");
|
|
var length = song.length;
|
|
var line, time, freq;
|
|
for (var i = 0; i < length; i++) {
|
|
line = song[i].split(" ");
|
|
if (+line[1] == 0) {
|
|
pause(line[0]);
|
|
}
|
|
else {
|
|
freq = +line[1];
|
|
time = (+line[0])*.001;
|
|
square_am_signal(time, freq);
|
|
}
|
|
}
|
|
|
|
close(); // Close Web Worker
|
|
}
|
|
|
|
function pause(time) {
|
|
postMessage("\nPaused / " + time*.001 + " seconds");
|
|
var dt = new Date();
|
|
while ((new Date()) - dt <= time) { /* Do nothing */ }
|
|
}
|
|
|
|
onmessage = function(event) {
|
|
var data = event.data;
|
|
play(data);
|
|
}; |