Skip to content

Instantly share code, notes, and snippets.

@chadaustin
Created September 17, 2014 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chadaustin/508e41e2d18dc3d4eae8 to your computer and use it in GitHub Desktop.
Save chadaustin/508e41e2d18dc3d4eae8 to your computer and use it in GitHub Desktop.
Simple Audio API Test
(function() {
var context = new AudioContext();
// 32000 Hz, 1 second, 200 Hz tone
// 32000 / 200 = exact 160 cycles
function playSineWave() {
var buffer = context.createBuffer(1, 32000, 32000);
var data = buffer.getChannelData(0);
for (var i = 0; i < data.length; ++i) {
data[i] = Math.sin(i / 160 * 2 * Math.PI);
}
for (var i = 0; i < 5; ++i) {
var source = context.createBufferSource();
source.buffer = buffer;
source.connect(context.destination);
source.start(i);
}
}
playSineWave();
})();
@mr21
Copy link

mr21 commented Oct 4, 2017

You should use context.currentTime, in this example...
3 years late... i know.

@VigibotDev
Copy link

You can work with faster integer and use var arrayFloat = Float32Array.from(array, n => n / 32768); to convert in float:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment