Question

In openSL when I create a simpleBufferQueue, why is that it plays buffers only one after another ? How is this scenario helpful ? A more practical usage is when I can create it with 5 or more buffers and it can play them mixed/simultaneously as soon as I enque a buffer !

As of now, the only way to play sounds together is by creating multiple simple buffer queue AudioPlayers.

Please help and correct if I am reading anything wrong in the openSL documentation !

Was it helpful?

Solution

OpenSL ES is designed to be an especially useful audio API. Any advanced audio workstation apps, synthesizer apps, etc. are possibly going to have a lot of effects and internal mixing done by the programmers. Especially for portability of design and code, all that's needed is somewhere to put the output.

With OpenSL, if you want to play sounds simultaneously, you should mix them yourself and load buffers with the mixed sound. The easiest way to do this is to add them up sample-by-sample. Assuming each sound can achieve maximum headroom, divide by the number of input sounds. (This division can make the volume a bit low, but it takes some effort to get around it, assuming clipping is unacceptable.)

To mix 2 sounds, when you create a sample of the output (i.e., the data to be enqueued), you do this:

out[i] = (snd1[i] + snd2[i])/2;

(You may want to optimize the math, watch out for data types, and might use different indices.)

In general:

out[i] = (snd1[i] + ... + sndN[i])/N;

OTHER TIPS

you have to create multiple buffer queues or mix the audio by your self. You can check this library which replace the the soundpool one and uses opensl : https://code.google.com/p/opensl-soundpool/

also this question is also answered here : Play several sound effects simultaneously using OpenSL ES Android

PS: if you blend two sounds manually never divide (normalize) by the number of channels because it's not right: for example two violins play louder than one, right? . It's up to the sound designer to avoid saturation, not the programmer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top