Created
February 2, 2012 05:39
-
-
Save sethladd/1721740 to your computer and use it in GitHub Desktop.
Web Audio API and Dart
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
#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; | |
}); | |
} |
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
<!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