Skip to content

Commit

Permalink
Crunchin' that audio.
Browse files Browse the repository at this point in the history
  • Loading branch information
secretrobotron committed May 21, 2011
1 parent 6c477cb commit 9467fae
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 67 deletions.
3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
var gl;
var canvas_w, canvas_h, aspect;
var canvas, scene;
var audioData;

document.addEventListener('DOMContentLoaded', function (e) {
canvas = document.createElement('canvas');
Expand Down Expand Up @@ -166,7 +167,7 @@
var fft;
var audioBuffer;
var signal = new Float32Array(4096);
var audioEngine = new AudioEngine(function (data) {
var audioEngine = new AudioEngine(audioData, function (data) {
fft.forward(data);
audioBuffer = data;
audioEngine.audioBuffer = data;
Expand Down
25 changes: 23 additions & 2 deletions js/audio.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function AudioEngine ( updateFunction ) {
function AudioEngine ( audioData, updateFunction ) {

var modPlayer;
var playing = false;
Expand Down Expand Up @@ -42,6 +42,21 @@ function AudioEngine ( updateFunction ) {

}; //writeAudio

function loadEmbedded() {
var t = atob(audioData);
var ff = [];
var mx = t.length;
var scc= String.fromCharCode;
for (var z = 0; z < mx; z++) {
ff[z] = scc(t.charCodeAt(z) & 255);
}
var binString = ff.join("");

var modFile = new ModFile(binString);
modPlayer = new ModPlayer(modFile, 44100);
play();
}; //loadEmbedded

function loadRemote(path) {
var fetch = new XMLHttpRequest();
fetch.open('GET', path);
Expand Down Expand Up @@ -73,7 +88,13 @@ function AudioEngine ( updateFunction ) {
var writeInterval = Math.floor(1000 * bufferSize / sampleRate);
setInterval(writeAudio, writeInterval);
} //if
loadRemote(modFileName);

if (audioData) {
loadEmbedded();
}
else {
loadRemote(modFileName);
} //if
};

}; //ModPlayer
7 changes: 7 additions & 0 deletions make.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
import base64

in_file = open('index.html', 'r')
contents = in_file.read()
Expand Down Expand Up @@ -40,6 +41,12 @@
for swap in swaps:
contents = contents.replace(swap, 'shader_fragment: "'+shader_src.group(1)+'"')

audio_file = open('mod.mod', 'rb')
audio_data = audio_file.read()
audio_str = base64.encodestring(audio_data)
audio_file.close()
contents = contents.replace('audioData;', 'audioData = "'+(str(audio_str)[2:-1].replace("\\n",''))+'";')

out_file = open('super.html', 'w')
out_file.write(contents)
out_file.close()
Expand Down
Loading

0 comments on commit 9467fae

Please sign in to comment.