Skip to content

Instantly share code, notes, and snippets.

@sethladd
Created February 2, 2012 05:39
Show Gist options
  • Save sethladd/1721740 to your computer and use it in GitHub Desktop.
Save sethladd/1721740 to your computer and use it in GitHub Desktop.
Web Audio API and Dart
#import('dart:dom', prefix:'dom');
#import('dart:html');
main() {
dom.AudioContext audioContext = new dom.AudioContext();
dom.AudioBufferSourceNode source = audioContext.createBufferSource();
dom.AudioGainNode gainNode = audioContext.createGainNode();
source.connect(gainNode, 0, 0);
gainNode.connect(audioContext.destination, 0, 0);
dom.XMLHttpRequest xhr = new dom.XMLHttpRequest();
xhr.open("GET", "techno.mp3", true);
xhr.responseType = "arraybuffer";
xhr.addEventListener('load', (e) {
// asynchronous decoding
audioContext.decodeAudioData(xhr.response, function(buffer) {
source.buffer = buffer;
var button = document.query("#play");
button.disabled = false;
button.on.click.add((e) {
source.noteOn(0);
});
}, function() {
print('Error decoding MP3 file');
});
});
xhr.send();
document.query("#volume").on.change.add((e) {
var volume = Math.parseInt(e.target.value);
var max = Math.parseInt(e.target.max);
var fraction = volume / max;
gainNode.gain.value = fraction * fraction;
});
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
<button id="play" disabled>Play</button>
<label for="volume">Volume: <input type="range" min="0" max="100" id="volume" value="100"></label>
<script type="text/javascript" src="app.dart.js"></script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment