Created
January 17, 2010 18:34
-
-
Save also/279495 to your computer and use it in GitHub Desktop.
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
// Calling Sound.play() after calling SoundChannel.stop() will begin playing | |
// with an empty buffer. | |
package { | |
import flash.display.Sprite; | |
import flash.events.SampleDataEvent; | |
import flash.external.ExternalInterface; | |
import flash.media.Sound; | |
import flash.media.SoundChannel; | |
import flash.utils.setTimeout; | |
public class OutputSound extends Sprite { | |
private const FRAMES_PER_EVENT:int = 8192; | |
private const REPS:int = 5; | |
private const DURATION:int = 1000; | |
private var sound:Sound; | |
private var channel:SoundChannel; | |
private var i:int = 0; | |
private var frames:int = 0; | |
public function OutputSound():void { | |
sound = new Sound(); | |
sound.addEventListener(SampleDataEvent.SAMPLE_DATA, handleSampleData); | |
start(); | |
} | |
private function start():void { | |
log('start'); | |
frames = 0; | |
channel = sound.play(); | |
setTimeout(stop, DURATION); | |
} | |
private function stop():void { | |
log(' frames: ' + frames); | |
log(' played: ' + (channel.position * 44.1)); | |
channel.stop(); | |
if (++i < REPS) { | |
setTimeout(start, 500); | |
} | |
} | |
private function handleSampleData(e:SampleDataEvent):void { | |
var n:int = i < REPS - 1 ? FRAMES_PER_EVENT : 1024; | |
for (var c:int = 0; c < n; c++) { | |
e.data.writeFloat(1); | |
e.data.writeFloat(1); | |
} | |
frames += n; | |
} | |
private function log(o:Object):void { | |
ExternalInterface.call('console.log', o); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment