Skip to content

Commit

Permalink
cleaned up & added README
Browse files Browse the repository at this point in the history
  • Loading branch information
fritzo committed Jan 18, 2012
1 parent e636e7f commit 4e34ab7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 20 deletions.
52 changes: 52 additions & 0 deletions README.md
@@ -0,0 +1,52 @@
# WavEncoder #

A fast cross-browser riff wave encoder for real-time audio synthesis in HTML5.
Works in Web Workers and in the main window
(in contrast to encoders using window.btoa).

## Examples ##

To play 1 sec of noise in the main window, we can

<script type='text/javascript' src='safety.js'></script>
<script type='text/javascript' src='wavencoder.js'></script>
<script type='text/javascript'>

var samples = [];
for (var t = 0; t < 22050; ++t) {
samples[t] = 2 * Math.random() - 1; // in the interval [-1,1]
}

var datauri = WavEncoder.encode(samples);
var audio = new Audio(datauri);

document.onload(function(){ audio.play(); });

</script>

WavEncoder objects are optimized to create many samples of the same length.
A typical use case is to generate a set of tones, say in a web worker

includeScripts('workersafety.js');
includeScripts('wavencoder.js');

var sampleRateHz = 44100;
var numSamples = 1 * sampleRateHz; // 1 sec
var baseFreq = 2 * Math.PI * 27.5 / sampleRateHz; // A0

var wavEncoder = new WavEncoder(numSamples, {sampleRateHz: sampleRateHz});

var tones = [];
var samples = [];
for (var n = 0; n < 88; ++n) {

var freq = baseFreq * Math.pow(2, n/12);
for (var t = 0; t < numSamples; ++t) {
samples[t] = Math.sin(freq * t);
}

tones[n] = wavEncoder.encode(samples);
}

postMessage(tones);

10 changes: 0 additions & 10 deletions safety.js
Expand Up @@ -13,16 +13,6 @@
var globalEval = eval;
'use strict';

var TodoException = function (message) {
this.message = message || '(unfinished code)';
};
TodoException.prototype.toString = function () {
return 'TODO: ' + this.message;
};
var TODO = function (message) {
throw new TodoException(message);
};

var AssertException = function (message) {
this.message = message || '(unspecified)';
};
Expand Down
10 changes: 0 additions & 10 deletions workersafety.js
Expand Up @@ -13,16 +13,6 @@
var globalEval = eval;
'use strict';

var TodoException = function (message) {
this.message = message || '(unfinished code)';
};
TodoException.prototype.toString = function () {
return 'TODO: ' + this.message;
};
var TODO = function (message) {
throw new TodoException(message);
};

var AssertException = function (message) {
this.message = message || '(unspecified)';
};
Expand Down

0 comments on commit 4e34ab7

Please sign in to comment.