Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…-radio into rocketinventor-master

* 'master' of https://github.com/rocketinventor/system-bus-radio:
  Added comments to source code
  Implemented Web Workers
  Preperations for web using a worker
  moved var definition
  created var for window log
  fixed description meta
  Added a delay function and fixed time being *1000.
  improved link styling
  removed red outline around text box
  removed more unused CSS
  removed unused and unrelated  CSS classes
  Improved formatting
  • Loading branch information
fulldecent committed Dec 1, 2016
2 parents b47a9ec + 958e5d4 commit efc865d
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 40 deletions.
3 changes: 2 additions & 1 deletion In Javascript/airgap.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
</div>
<div class="content">
</br>
<input type="button" value="Play Song" onclick="start()"></br></br>
<input type="button" value="Play Song" onclick="start()">
<input type="button" value="Stop Song" onclick="end()"></br></br>
<textarea id="logs" style="width:70%;min-height:100px">Tested with Chrome at 1560Khz</textarea>
<p style="font-size:14px">Feel free to edit the code below or copy and paste any <em>valid</em> code.<br>Column one is time in <i>milliseconds</i>, and column two is <i>frequency</i>.</p>
<textarea id="tones" style="width:70%;min-height:200px">
Expand Down
55 changes: 16 additions & 39 deletions In Javascript/airgap.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,25 @@
//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 player; // Define "player" var to make my code linter happy

function now() {
return window.performance.now() * 1000000;
}
function start() { // Start Web Worker & send song data to player
var logs = document.getElementById('logs'); // Define log element

var NSEC_PER_SEC = 1000000000;
var register = 3.1415;
var logs = document.getElementById('logs');
// Create Web Worker if it doesn't already exist
if (window.Worker && typeof(player) == "undefined") {
var player = new Worker("worker.js");
window.player = player; // Make variable Global
player.onmessage = function(event) {
var data = event.data;
window.logs.value += data;
};

function square_am_signal(time, freq) {
window.logs.value += "\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;
// Send song data to player
var song = document.getElementById("tones").innerHTML;
player.postMessage(song);
}
}

function start() {
var song = document.getElementById("tones").value.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);
}
}
function end() { // Stops the Web Worker
player.terminate();
}

function pause(time) {
Expand Down
60 changes: 60 additions & 0 deletions In Javascript/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//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) { // This funcion generates the radio waves
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) { // Parse song data, and call on required scripts to run it
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) { // A useless function to run when there is no noise to play
postMessage("\nPaused / " + time*.001 + " seconds");
var dt = new Date();
while ((new Date()) - dt <= time) { /* Do nothing */ }
}

onmessage = function(event) { // Recieve song data from main page
var data = event.data;
play(data);
};

0 comments on commit efc865d

Please sign in to comment.