Does Playing Sound blocks drawing?

I notived that when playing sound the drawing funktion gets blocked/delayed.
Is the any way to play sound/music in the bachround, not blocking any drawing?
Thank you fro your help.

Are the any plans to add CoreMID support or any MIDI support for Codea?

@Doc this usually happens when the sound is played for the first time. Subsequent plays should not cause a performance slowdown.

One way to “cache” the sound is to play it back for the first time with zero volume. So:

sound( name, seed, 0 )

Then subsequent calls to sound( name, seed ) should not hurt your performance.

Of course, if you omit the seed value (and use sound( name )), the sound will be random each time it is played. This will force Codea to generate a new sound each time the sound is played.

Edit: That said, what device are you using? The sound doesn’t normally cause delays. For instance, if you look at the Lua Jump example project the sound is random for each jump, but performance seems okay.

If you are using the advanced sound picker, the sounds can get long enough to cause a delay. As @Simeon says, caching it first is the way around this.

As a thought, would it be possible to save the sound buffer as part of the project data? Then we could load it up again faster rather than playing it again.

Thank you for your help. I’m generating notes/sounds within the program.
So it’s difficult to precalculate them before playing. What kind of notes
are played depend on touch user input, parameters and some algorithm I use.
Looks like thats more difficult in Codea than I thought. Any advice and hints welcome.
At least I need to have access to the frequency and level part of the sound and chnage that. Other parameters of the sound can be the same.

I’ve had a look at ABCMusicPlayer and there the graphics run smooth while playing
the music. I have not figured out yet how that is done.

Are the any plans to improve the sound/music part of Codea?

I think the libpd project very interesting. It’s available for iOS and even Android and
it might be not to difficult to add to Codea. Opens a whole new
world for writing sound/music applications.

Hi @Doc, with ABCMusicPlayer I totally pre cache all the sounds, which essentially moves the drawing delays to the start rather than during the animation of whatever it is.

You could adjust volume on the fly, as sound() takes a final argument between 0 and 1 for the volume. But the rest I think you will need to pre cache.

What kind of sound/music are you doing?

Didn’t notice you precached ALL sounds. If I want to use your precached method I woudl have to generate any possible sound that I might use or generate in my program.
That means around 88 different key’s for each (up to 4) instruments and up to 8 different note length’s. A lot stuff, but maybe could be done. I don’t knwo if I will run into any memory problems using that method.

How does sound() know that I have used a specific setup before and it is cached?
A specific sound is not bound to a variable that can be adressed and used later.

The music will be generated by some complex algorithms I’ve developed
for the motion graphics I did for a movie (
I now want to apply the same algorithm to music to play with the graphics.

Hi @Doc, yes, because I have the notes to play ahead of time, I deduplicate them and play them all as quickly as possible. The sound() buffer can be adjusted to any arbitrary size, but I don’t think 2816 sounds is feasible to generate and precache. Codea remembers the audio generated for each sound from when you run the program, and will discard the oldest sound if you reach the limits of the sound buffer.

Really what I have implemented is an ugly hack… :slight_smile: I don’t think generating sounds from parameters in real time without frame delays is currently possible, I’m afraid. There was some discussion about whether to add the capability to adjust a sound buffer in real time… but I’m not aware of any imminent plans for audio developments.

You did and excellent job with ABCMusic. I learned a lot about sound in codea by
looking on your code and yor discussions in the forum.
I hope Codea will add more sound/music stuff soon. Codea is such a great tool.
I enjoy being able to write programs for my iPad.

Thanks! I’m still incrementally improving it, but it’s far from perfect.