开发者

OpenAL buffer update in real-time

I'm working in iOS and have a simple OpenAL project running.

The difference to most openAL projects i've seen is that im not loading in a sound file. Instead I load an array of raw data into the alBufferData. Using a couple of equations I can load in data to produce white noise, sine and pulse waves. And all is working well.

My problem is tha开发者_开发百科t I need a way to modify this data whilst the sound is playing in real-time.

Is there a way to modify this data without having to create a new buffer (i tried the approach of creating a new buffer with new data and then use it instead but its nowhere near quick enough).

Any help or suggestions of other ways to accomplish this would be much appreciated.

Thanks


I haven't done it on iOS, but with openAL on the PC what you would do is chain a few buffers together. Each buffer would have a small time period's worth of data. Periodically, check to see if the playing buffer is done, and if so, add it to a free list for reuse When you want to change the sound, write the new waveform into a free buffer and add it to the chain. You select the buffer size to balance latency and required update rate - smaller buffers allow faster response to changes, but need to be generated more often.

This page suggests that a half second update rate is doable. Whether you can go faster depends on the complexity of your calculations as well as on the overhead of the OS.


Changing the data during playback is not supported in OpenAL.

However, you can still try it and see if you get acceptable defaults (though you'll be racing against the OpenAL playback mechanism, and any lag-outs in your app could throw it off, so do this at your own risk).

There's an Apple extension version of ALBufferData that tells OpenAL to use the data you give it directly, rather than making its own local copy. You set it up like so:

typedef ALvoid AL_APIENTRY (*alBufferDataStaticProcPtr) (const ALint bid,
                                                         ALenum format,
                                                         const ALvoid* data,
                                                         ALsizei size,
                                                         ALsizei freq);

static alBufferDataStaticProcPtr alBufferDataStatic = NULL;

alBufferDataStatic = (alBufferDataStaticProcPtr) alcGetProcAddress(NULL, (const ALCchar*) "alBufferDataStatic");

Call alBufferDataStatic() it like you would call alBufferData():

alBufferDataStatic(bufferId, format, data, size, frequency);

Since it's now using your sound data buffer rather than its own, you could conceivably modify that data and it won't be the wiser (provided you're not modifying things too close to where it's currently playing from in the buffer).

However, this approach is risky, since it depends on timing you're not fully in control of. To be 100% safe you'll need to use Audio Units.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜