With OpenAL, is setting AL_PITCH and AL_GAIN expensive?
Asked Answered
L

1

5

Whenever I play a sound effect I use:

alSourcei(sources[index], AL_BUFFER, 0);
alSourcei(sources[index], AL_BUFFER, bufferID);
alSourcef(sources[index], AL_PITCH, 1.0);
alSourcef(sources[index], AL_GAIN, 1.0);

would it be a significant optimization to remove setting the AL_PITCH and the AL_GAIN every time a sound effect is played? Obviously since they're 1.0 every time I could just set it once when I initialize the sound. What values do AL_PITCH and AL_GAIN default to if they aren't set?

Lynxeyed answered 7/3, 2012 at 1:41 Comment(0)
H
8

Only an "in general" type of answer is possible as far as the optimization bit goes since an implementation is not required to work in some particular way as long as it works within the conditions laid down by the OpenAL specification. It is nevertheless likely that all implementations more or less work similarly.

In general, alSourcei/alSourcef involves at least calling a function like GetContextSuspended, which involves an access to thread-local storage and entering/leaving a critical section, as well as a switch statement (it also means a jump through a function pointer equivalent to a possibly not cached address in a possibly out-of-core page, and likely wasting one TLB cache entry).

alSourcei further needs to do a thread-safe increment of a reference count, and allocate/append a new list node to the source's buffer list, which means something on the order of magnitude of calling malloc at least once.

Setting AL_GAIN and AL_PITCH per se is almost a free operation. It sets a value and marks the source as being updated, so the context mixer thread knows something has changed when mixing the next time slice. In the worst case, if the parameters are illegal, alSourcef needs to set the last error code.

Insofar, removing the calls to alSourcef will of course avoid some unnecessary calls, and since you say that there is no chance the values could be anything else but 1.0, there is actually no reason to ever touch them at all, since that is the default value as per the specification.
But... if you expect a noticeable speed-up from removing these calls, you will likely be disappointed (unless there's several hundred thousands of them per second).

Hervey answered 7/3, 2012 at 9:2 Comment(2)
thank you. this is for game sound effects. as you can see whenever any random sound effect needs to play i clear the source buffer and then set the source buffer. is it more common to setup separate sets of sources for each specific sound effect so that i don't have to clear and reset the buffer contents?Lynxeyed
That depends on a lot of factors. Most people will want many sources, but again more buffers than sources. In general, you need one source for every sound that needs to play concurrently. A single source can be fed by a million buffers, but can only play one at a time. A source that does not play does not (should not) consume any CPU resources, but will still consume memory. Thus, having a few extra does not hurt, having one for every sound probably does. Enabling a source is an atomic queue operation, and queueing a buffer is an atomic queue operation, so they are similarly expensive.Hervey

© 2022 - 2024 — McMap. All rights reserved.