Sign in to follow this  
Richy2k

OpenAL. Checking how much of a sound has played.

Recommended Posts

I'm reading up on this, and I can't seen to find a way to do this. I would just time it manuelly, except I would have to then account for pitch shifting dynamically, which would be pretty damn annoying to do. All I want is to be able to ask a source its current position within a buffer, 0 would be at the start, 1 at the end, 0.5 in the middle. Is there already a way to do this, or am I going to have to start hacking at the OpenAL source?

Share this post


Link to post
Share on other sites
You could always split the buffer up into numerous smaller buffers and queue them all, that way you should be able to determine how much has been played by the number of buffers that have been processed.

Share this post


Link to post
Share on other sites
I took a look through all the source attributes in the OpenAL documentation and I didn't see anything to support this either. There are functions for seeking to a part of the file based on time in seconds, number of samples, or number of bytes, but no way to read the position. [sad]


Could you perhaps attach a timer (created yourself, or from an external library) to the source once you set its state to AL_PLAYING, and then you'll be able to read the timer and get an idea of where in the file you are? It might not sync well though, so I don't know.

Share this post


Link to post
Share on other sites
I just read something in the docs that back up what Gorax was suggesting.

link (see section 1.3.5 on the bottom)


Quote:

Splitting large samples over several buffers maintained in a queue has a distinct advantages over approaches that require explicit management of samples and sample indices.



So I suppose if you had a WAV file with 4 distinct portions, you could load those sections each individually into their own buffers, then queue the source with those 4 buffers and loop through it. That way you could change the pitch (or whatever) based on which section is playing. At least that's how I understand it, I haven't really gotten to understanding the source queing mechanism of OpenAL yet...

Share this post


Link to post
Share on other sites
I wrote a function ages ago to figure out how much time is chewed up by some buffer at some frequency and so forth. I forgot the unit of time I was using, but I assume it's in seconds... In any case, here it is:


/* oalGetTrackLength
Purpose:
Retrieves the track length for some amount of data based on frequency and format.
Example:
oalGetTrackLength(1048576,22050,AL_FORMAT_MONO16);
*/

ALfloat oalGetTrackLength(ALuint dataLength,ALuint freq,ALenum format){
ALfloat trackLength = 0.0f;
if (freq != 0.0f){//avoid divide by zero
trackLength = dataLength / (ALfloat)(freq * oalGetSampleSize(format));
}
return trackLength;
}



[EDIT]
I didn't see that sample size bit, so here's that too:


/* oalGetSampleSize
Purpose:
Retrieves the byte-length of the given format's samples.
Example:
ALuint numSamples = dataLength / oalGetSampleSize(AL_FORMAT_MONO16);
*/

ALuint oalGetSampleSize(ALenum format){
//defaults to 1, even if the format is invalid
ALuint sampleSize = 1;
switch (format){
case AL_FORMAT_STEREO16:
sampleSize *= 2;
case AL_FORMAT_MONO16:
case AL_FORMAT_STEREO8:
sampleSize *= 2;
break;
}
return sampleSize;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this