Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
52 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters