Created
October 14, 2011 16:33
-
-
Save weyert/1287611 to your computer and use it in GitHub Desktop.
Madness!
This file contains hidden or 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
package sounds | |
{ | |
import flash.events.Event; | |
import flash.events.EventDispatcher; | |
import flash.events.SampleDataEvent; | |
import flash.media.Sound; | |
import flash.utils.ByteArray; | |
/** | |
* RandomlyPlaylist | |
* | |
* @author sprog | |
*/ | |
public class RandomlyPlaylist extends EventDispatcher | |
{ | |
private var _resources: Vector.<Sound>; | |
private var _resourcesList: Array = []; | |
private var _outputSound: Sound; | |
public var enabled: Boolean = true; | |
private const MAGIC_DELAY:Number = 2257.0; // LAME 3.98.2 + flash.media.Sound Delay | |
private const bufferSize: int = 4096; // Stable playback | |
private const samplesTotal: int = 0; // original amount of sample before encoding (change it to your loop) | |
private var soundIndex: uint = 0; | |
private var samplesPosition:int; | |
private var _soundInfos: Vector.<SoundInfoVO>; | |
/** | |
* | |
*/ | |
public function RandomlyPlaylist() | |
{ | |
super(); | |
_resources = new Vector.<Sound>(); | |
_soundInfos = new Vector.<SoundInfoVO>(); | |
_outputSound = new Sound(); | |
} | |
public function play(): void | |
{ | |
trace("RandomlyPlaylist.stop()"); | |
this.enabled = true; | |
_outputSound.addEventListener(SampleDataEvent.SAMPLE_DATA, onSoundSampleData ); | |
_outputSound.play(); | |
} | |
public function stop(): void | |
{ | |
trace("RandomlyPlaylist.stop()"); | |
this.enabled = false; | |
} | |
protected function onSoundSampleData(event:SampleDataEvent):void | |
{ | |
if( enabled ) | |
{ | |
extract( event.data, bufferSize ); | |
} | |
else | |
{ | |
silent( event.data, bufferSize ); | |
} | |
} | |
private function extract( target: ByteArray, length:int ):void | |
{ | |
// check if we are finished with playing the current sound object | |
var currentSoundObject: Sound = this.getCurrentSoundObject(); | |
var currentSoundInfo: SoundInfoVO = this.getCurrentSoundInfo(); | |
trace("RandomlyPlaylist.extract(): sound index #" + soundIndex + " --> " + currentSoundInfo.id ); | |
while( 0 < length ) | |
{ | |
if( samplesPosition + length > currentSoundInfo.totalSamples ) | |
{ | |
var read: int = currentSoundInfo.totalSamples - samplesPosition; | |
currentSoundObject.extract( target, read, samplesPosition + MAGIC_DELAY ); | |
samplesPosition += read; | |
length -= read; | |
} | |
else | |
{ | |
currentSoundObject.extract( target, length, samplesPosition + MAGIC_DELAY ); | |
samplesPosition += length; | |
length = 0; | |
} | |
if( samplesPosition == currentSoundInfo.totalSamples ) // END OF LOOP > WRAP | |
{ | |
samplesPosition = 0; | |
// get next sound object | |
trace("FINISHED PLAYING SOUND"); | |
this.soundIndex++; | |
if ( soundIndex >= this._resources.length ) | |
{ | |
this.soundIndex = 0; | |
} | |
} | |
} | |
} | |
private function getCurrentSoundInfo():SoundInfoVO | |
{ | |
// TODO Auto Generated method stub | |
return _soundInfos[ soundIndex ]; | |
} | |
private function getCurrentSoundObject(): Sound | |
{ | |
var sound: Sound = this._resources[ soundIndex ]; | |
return sound; | |
} | |
private function silent( target:ByteArray, length:int ):void | |
{ | |
target.position = 0; | |
while( length-- ) | |
{ | |
target.writeFloat( 0.0 ); | |
target.writeFloat( 0.0 ); | |
} | |
} | |
/** | |
* | |
* @param data | |
* | |
*/ | |
public function addResource(data: Class, info: SoundInfoVO=null): void | |
{ | |
trace("RandomlyPlaylist.addResource()"); | |
if ( info == null ) | |
{ | |
throw new Error('You need to define the sound info instance'); | |
} | |
_resourcesList.push( data ); | |
_soundInfos.push( info ); | |
} | |
/** | |
* | |
* @param event | |
*/ | |
private function onSoundResourceLoaded(event:Event):void | |
{ | |
var soundBlobResource: EmbeddedSoundBlob = event.target as EmbeddedSoundBlob; | |
trace("RandomlyPlaylist.onSoundResourceLoaded(): soundBlobResource=" + soundBlobResource); | |
soundBlobResource.removeEventListener( Event.COMPLETE, onSoundResourceLoaded); | |
_resources.push( soundBlobResource.sound ); | |
nextSoundItem(); | |
} | |
/** | |
* | |
*/ | |
public function loadResources(): void | |
{ | |
trace("RandomlyPlaylist.loadResources()"); | |
nextSoundItem(); | |
} | |
private function nextSoundItem():void | |
{ | |
trace("RandomlyPlaylist.nextSoundItem()"); | |
if ( _resourcesList.length == 0 ) | |
{ | |
trace("RandomlyPlaylist.nextSoundItem(): COMPLETED!"); | |
dispatchEvent( new Event( Event.COMPLETE ) ); | |
return; | |
} | |
var dataResource: Class = _resourcesList.shift(); | |
var soundBlob: EmbeddedSoundBlob = new EmbeddedSoundBlob( new dataResource() ); | |
soundBlob.addEventListener( Event.COMPLETE, onSoundResourceLoaded); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment