Added Source:getDuration (thanks Boolsheet!)

For streaming sources, the duration may not always be sample-accurate and may return -1 if it cannot be determined at all.
This commit is contained in:
Alex Szpakowski
2015-11-07 01:49:37 -04:00
parent 1b5a0f49fa
commit 0558fcdbd4
28 changed files with 321 additions and 90 deletions
+34
View File
@@ -65,6 +65,7 @@ Ensure the Source is not multi-channel before calling this function.")
};
StaticDataBuffer::StaticDataBuffer(ALenum format, const ALvoid *data, ALsizei size, ALsizei freq)
: size(size)
{
alGenBuffers(1, &buffer);
alBufferData(buffer, format, data, size, freq);
@@ -95,6 +96,7 @@ Source::Source(Pool *pool, love::sound::SoundData *soundData)
, offsetSeconds(0)
, sampleRate(soundData->getSampleRate())
, channels(soundData->getChannels())
, bitDepth(soundData->getBitDepth())
, decoder(nullptr)
, toLoop(0)
{
@@ -134,6 +136,7 @@ Source::Source(Pool *pool, love::sound::Decoder *decoder)
, offsetSeconds(0)
, sampleRate(decoder->getSampleRate())
, channels(decoder->getChannels())
, bitDepth(decoder->getBitDepth())
, decoder(decoder)
, toLoop(0)
{
@@ -169,6 +172,7 @@ Source::Source(const Source &s)
, offsetSeconds(0)
, sampleRate(s.sampleRate)
, channels(s.channels)
, bitDepth(s.bitDepth)
, decoder(nullptr)
, toLoop(0)
{
@@ -441,6 +445,36 @@ float Source::tell(Source::Unit unit)
return pool->tell(this, &unit);
}
double Source::getDurationAtomic(void *vunit)
{
Unit unit = *(Unit *) vunit;
if (type == TYPE_STREAM)
{
double seconds = decoder->getDuration();
if (unit == UNIT_SECONDS)
return seconds;
else
return seconds * decoder->getSampleRate();
}
else
{
ALsizei size = staticBuffer->getSize();
ALsizei samples = (size / channels) / (bitDepth / 8);
if (unit == UNIT_SAMPLES)
return (double) samples;
else
return (double) samples / (double) sampleRate;
}
}
double Source::getDuration(Unit unit)
{
return pool->getDuration(this, &unit);
}
void Source::setPosition(float *v)
{
if (channels > 1)