With the new beta 2 version of Flash Player 10 the Sound API has changed a bit.

Most of the changes is very straight forward and only renaming of some methods, events and properties:

SamplesCallbackEvent is now SampleDataEvent
samplesCallback is now sampleData or Event.SAMPLE_DATA
Instead of accessing Sound.samplesCallbackData to write to the output buffer one now use the SampleDataEvent.data.

Tinic Uro has updated his examples if you like to look at some example code with the new API.

So basically very straight forward changes but there was one thing that I managed to waste a bit of time on. This is what the documentation say about the buffer size for the SampleDataEvent:

Provide between 2048 and 8192 samples in a SampleDataEvent object. For best performance, provide as many samples as possible. The fewer samples you provide, the more likely it is that clicks and pops will occur during playback. This behaviour can differ on various platforms and can occur in various situations – for example, when resizing the browser. You might write code that works on one platform when you provide only 2048 samples, but that same code might not work as well when run on a different platform. If you require the lowest latency possible, consider making the amount of data user-selectable.

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.

All my code was using a buffer size of less than 2048 and hence did not work any more.
I’m not sure why the available range has been made smaller since it was nice to be able to lower the latency by having a 512 samples buffer.
I guess it is a common problem that Flash developers are a bit irresponsible with the users CPU power and I can understand that they want to avoid the reputation of being a CPU hog, but IMHO it’s a bit like if they would have decided that the maximum frame rate should be limited to 20FPS.

Related posts:

  1. Sound generation in FlashPlayer 10
  2. Simple 3-band EQ with Flash Player 10
  3. Random thoughts on Vectors and samplesCallbackData
  4. Audio mixing on AIR for android
  5. Tweaking the samplesCallbackEvent loop

4 Responses to “Flash Player 10 Sound API changes, SampleData and SampleDataEvent”

Comments (4)
  1. Thanks for this useful information, by the way:

    I want to generate a chord using a couple of sin waves which is simple. But, can I generate a WAV/AIFF file too?… would that be too much ?

    I’d love to redirecting the stream to a file (instead of being played).

    Thanks in advance,
    Paul

  2. Hi Paul,

    I guess you might have found a solution by now, but audio generated in the flash player will be 32bit floating-point PCM data.
    That is a bit heavy to send directly to server if the duration is not very short, so ideally you have to convert it to 16 bit fixed-point first. If quality is a big concern one should also apply dithering at that stage to reduce quantization error.
    When you have the PCM data in the desired bitdepth you just add headers to make it into a wav.

  3. Dear sir:

    May I ask you a question about PCM data?

    I want to play the PCM data from server, and the server constinously push PCM data to me, and I put those data into ByteArray, then assign Sound.souce = ByteArray.

    But one question, the PCM data from server,that is 8000 rate, 16 bit size, one channel, it is not the correct format flash player needs, do you know how I can solve this proble. Thanks in advance.

  4. Unfortunately the Sound object does not have any other way of assigning data to it than the Sound.load method. And Sound.load expects either an mp3 or swf.

    What you need to do is to create a virtual swf in memory and load that using Loader.loadBytes.
    Here is an article explaining the process http://dotbrighton.org/?p=9

    If you want to stream the data I guess you could use the above method to create swf’s each time a small chunk of data has been received. Then you use Sound.extract to get the 32 bit PCM data from those virtual swf’s and send that to the output buffer on the SampleDataEvent. Not a very neat solution, but if you cannot do anything about the source format it’s the only option I can think of.

Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

© 2011 BlixtSystems Suffusion theme by Sayontan Sinha

Switch to our mobile site