On May 20, 9:17 am, Joe Berkovitz <
joseph.berkov...@gmail.com> wrote:
> 1. Wrap your MP3 in a SoundSource and extract a Sample for the range
> to be looped, then use that Sample as the basis for looping in a
> QueuePerformance.
>
> 2. Create a modified version of SoundSource that only looks at a
> subrange of the underlying Sound object, applying some offset to the
> passed-in starting frame and applying some maximum to the end frame.
Thanks very much for the helpful response, Joe. I've solved it, with
your help.
I first tried your option 1, but returning a dynamic Sample based on a
frame range was taking too much time for particularly long segments of
audio. I think this was happening because it was running
sound.extract() for the whole range of audio in advance, instead of
doing it on an as-needed basis.
I ended up implementing your option 2, subclassing SoundSource. I've
pasted my code below and grant permission to you to include it in the
StandingWave2 codebase if you feel like it. Also feel free to come up
with a better name for the class. :-)
This has been working well for me, with one exception: whenever I want
to loop a very short section of a sound, it only plays once. (To
reproduce, create a QueuePerformance with a single SoundSource or
SoundSourceSlice that's around a second or less in length. Then play
it, and it'll only play once instead of being looped infinitely.)
I think this is happening because SampleDataEvent gets fewer than 2048
samples, which causes it to assume it's reached the end of the file.
Per the SampleData Event docs (
http://livedocs.adobe.com/flex/3/
langref/flash/events/SampleDataEvent.html) -- "If you provide fewer
than 2048 samples, Flash Player plays the remaining samples and then
stops the sound as if the end of a sound file was reached, generating
a SoundComplete event."
I tried modifying my SoundSourceSlice class's getSampleRange() method
to check the length of the slice and return a Sample that just
duplicates the data several times, to get past the 2048 limit, but
that messed with the position and duration and I couldn't get it to
work -- plus it feels like that fix should happen at a lower level.
Would this be considered a bug in StandingWave2, or should I be
working around it in some other way?
Adrian
-------------------------------------
package {
import flash.media.Sound;
import com.noteflight.standingwave2.elements.IAudioSource;
import com.noteflight.standingwave2.elements.Sample;
import com.noteflight.standingwave2.sources.SoundSource;
public class SoundSourceSlice extends SoundSource {
private var start_frame:Number;
private var end_frame:Number;
public function SoundSourceSlice(sound:Sound, start_frame:Number,
end_frame:Number) {
super(sound);
this._position = start_frame;
this.start_frame = start_frame;
this.end_frame = end_frame;
this.duration = (end_frame - start_frame) / 44100.0;
}
override public function resetPosition():void {
this._position = this.start_frame;
}
override public function getSampleRange(fromOffset:Number,
toOffset:Number):Sample {
return super.getSampleRange(fromOffset + this.start_frame,
toOffset + this.start_frame);
}
override public function clone():IAudioSource {
return new SoundSourceSlice(this.sound, this.start_frame,
this.end_frame);