Reputation: 368
I'm new to OpenAL. I managed to get a soundmanager code that wraps OpenAL for iPhone, so I can load sounds and play them.
But I really need to know how long each sound file is in seconds because I need to call an event as soon as the sound as finished.
I've noticed that there is a way to calculate the length of a sound when populating the buffers(?). Can someone help me with this? Thanks in advance.
Upvotes: 2
Views: 3297
Reputation: 61
I have the same problem and came up with the following solution. The first function is optional, but allows to compensate for the elapsed time. I'm then firing an NSTimer with the resulting time interval.
Have fun! Dirk
static NSTimeInterval OPElapsedPlaybackTimeForSource(ALuint sourceID) {
float result = 0.0;
alGetSourcef(sourceID, AL_SEC_OFFSET, &result);
return result;
}
static NSTimeInterval OPDurationFromSourceId(ALuint sourceID) {
ALint bufferID, bufferSize, frequency, bitsPerSample, channels;
alGetSourcei(sourceID, AL_BUFFER, &bufferID);
alGetBufferi(bufferID, AL_SIZE, &bufferSize);
alGetBufferi(bufferID, AL_FREQUENCY, &frequency);
alGetBufferi(bufferID, AL_CHANNELS, &channels);
alGetBufferi(bufferID, AL_BITS, &bitsPerSample);
NSTimeInterval result = ((double)bufferSize)/(frequency*channels*(bitsPerSample/8));
NSLog(@"duration in seconds %lf", result);
return result;
}
Upvotes: 2
Reputation: 56
float result;
alGetSourcef(sourceID, AL_SEC_OFFSET, &result);
return result;
You can use this snippet to get the current playback time of the sound.
Upvotes: 4
Reputation: 2106
ALint bufferID, bufferSize;
alGetSourcei(sourceID, AL_BUFFER, &bufferID);
alGetBufferi(bufferID, AL_SIZE, &bufferSize);
NSLog(@"time in seconds %f", (1.0*bufferSize)/(44100*2*2)); //44100 * 2 chanel * 2byte (16bit)
Upvotes: 1
Reputation: 70743
If you are populating known size buffers with raw PCM audio samples of a known format, then:
duration = numberOfSampleFrames / sampleRate;
where, typically, the number of sample frames is the number_of_bytes/2 for mono 16-bit samples, or the number_of_bytes/4 for stereo, etc.
Upvotes: 2