Made sure the state of the Source is applied to the AL source when it's played. Also increased the max number if sources allowed at the same time.

This commit is contained in:
rude
2010-02-02 21:22:57 +01:00
parent 662406efbe
commit e7a6323635
4 changed files with 50 additions and 20 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ namespace openal
while(!instance->finish) while(!instance->finish)
{ {
instance->pool->update(); instance->pool->update();
SDL_Delay(10); SDL_Delay(5);
} }
return 0; return 0;
+1 -1
View File
@@ -57,7 +57,7 @@ namespace openal
private: private:
// Number of OpenAL sources. // Number of OpenAL sources.
static const int NUM_SOURCES = 16; static const int NUM_SOURCES = 64;
// OpenAL sources // OpenAL sources
ALuint sources[NUM_SOURCES]; ALuint sources[NUM_SOURCES];
+43 -18
View File
@@ -32,8 +32,6 @@ namespace audio
namespace openal namespace openal
{ {
static int acc = 0;
Source::Source(Pool * pool, love::sound::SoundData * soundData) Source::Source(Pool * pool, love::sound::SoundData * soundData)
: love::audio::Source(Source::TYPE_STATIC), pool(pool), pitch(1.0f), : love::audio::Source(Source::TYPE_STATIC), pool(pool), pitch(1.0f),
volume(1.0f), looping(false), decoder(0), valid(false) volume(1.0f), looping(false), decoder(0), valid(false)
@@ -41,6 +39,12 @@ namespace openal
alGenBuffers(1, buffers); alGenBuffers(1, buffers);
ALenum fmt = getFormat(soundData->getChannels(), soundData->getBits()); ALenum fmt = getFormat(soundData->getChannels(), soundData->getBits());
alBufferData(buffers[0], fmt, soundData->getData(), soundData->getSize(), soundData->getSampleRate()); alBufferData(buffers[0], fmt, soundData->getData(), soundData->getSize(), soundData->getSampleRate());
static float z[3] = {0, 0, 0};
setFloatv(position, z);
setFloatv(velocity, z);
setFloatv(direction, z);
} }
Source::Source(Pool * pool, love::sound::Decoder * decoder) Source::Source(Pool * pool, love::sound::Decoder * decoder)
@@ -49,6 +53,12 @@ namespace openal
{ {
decoder->retain(); decoder->retain();
alGenBuffers(MAX_BUFFERS, buffers); alGenBuffers(MAX_BUFFERS, buffers);
static float z[3] = {0, 0, 0};
setFloatv(position, z);
setFloatv(velocity, z);
setFloatv(direction, z);
} }
Source::~Source() Source::~Source()
@@ -65,6 +75,9 @@ namespace openal
void Source::play() void Source::play()
{ {
valid = pool->play(this, source); valid = pool->play(this, source);
if(valid)
reset(source);
} }
void Source::stop() void Source::stop()
@@ -179,40 +192,55 @@ namespace openal
{ {
if(valid) if(valid)
alSourcefv(source, AL_POSITION, v); alSourcefv(source, AL_POSITION, v);
setFloatv(position, v);
} }
void Source::getPosition(float * v) const void Source::getPosition(float * v) const
{ {
if(valid) if(valid)
alGetSourcefv(source, AL_POSITION, v); alGetSourcefv(source, AL_POSITION, v);
else
setFloatv(v, position);
} }
void Source::setVelocity(float * v) void Source::setVelocity(float * v)
{ {
if(valid) if(valid)
alSourcefv(source, AL_VELOCITY, v); alSourcefv(source, AL_VELOCITY, v);
setFloatv(velocity, v);
} }
void Source::getVelocity(float * v) const void Source::getVelocity(float * v) const
{ {
if(valid) if(valid)
alGetSourcefv(source, AL_VELOCITY, v); alGetSourcefv(source, AL_VELOCITY, v);
else
setFloatv(v, velocity);
} }
void Source::setDirection(float * v) void Source::setDirection(float * v)
{ {
if(valid) if(valid)
alSourcefv(source, AL_DIRECTION, v); alSourcefv(source, AL_DIRECTION, v);
else
setFloatv(direction, v);
} }
void Source::getDirection(float * v) const void Source::getDirection(float * v) const
{ {
if(valid) if(valid)
alGetSourcefv(source, AL_DIRECTION, v); alGetSourcefv(source, AL_DIRECTION, v);
else
setFloatv(v, direction);
} }
void Source::setLooping(bool looping) void Source::setLooping(bool looping)
{ {
if(valid && type == TYPE_STATIC)
alSourcei(source, AL_LOOPING, looping ? AL_TRUE : AL_FALSE);
this->looping = looping; this->looping = looping;
} }
@@ -226,7 +254,6 @@ namespace openal
if(type == TYPE_STATIC) if(type == TYPE_STATIC)
{ {
alSourcei(source, AL_BUFFER, buffers[0]); alSourcei(source, AL_BUFFER, buffers[0]);
alSourcei(source, AL_LOOPING, isLooping() ? AL_TRUE : AL_FALSE);
} }
else if(type == TYPE_STREAM) else if(type == TYPE_STREAM)
{ {
@@ -274,7 +301,7 @@ namespace openal
} }
} }
reset(source); alSourcei(source, AL_BUFFER, AL_NONE);
} }
rewindAtomic(); rewindAtomic();
valid = false; valid = false;
@@ -310,7 +337,18 @@ namespace openal
void Source::reset(ALenum source) void Source::reset(ALenum source)
{ {
alSourcei(source, AL_BUFFER, AL_NONE); alSourcefv(source, AL_POSITION, position);
alSourcefv(source, AL_VELOCITY, velocity);
alSourcefv(source, AL_DIRECTION, direction);
alSourcef(source, AL_PITCH, pitch);
alSourcef(source, AL_GAIN, volume);
}
void Source::setFloatv(float * dst, const float * src) const
{
dst[0] = src[0];
dst[1] = src[1];
dst[2] = src[2];
} }
ALenum Source::getFormat(int channels, int bits) const ALenum Source::getFormat(int channels, int bits) const
@@ -329,29 +367,16 @@ namespace openal
int Source::streamAtomic(ALuint buffer, love::sound::Decoder * d) int Source::streamAtomic(ALuint buffer, love::sound::Decoder * d)
{ {
//if(isLooping() && d->isFinished())
// d->rewind();
// Get more sound data. // Get more sound data.
int decoded = d->decode(); int decoded = d->decode();
int fmt = getFormat(d->getChannels(), d->getBits()); int fmt = getFormat(d->getChannels(), d->getBits());
//printf("Decoded %i ( %i )", decoded, d->getSize());
if(decoded > 0 && fmt != 0) if(decoded > 0 && fmt != 0)
{
alBufferData(buffer, fmt, d->getBuffer(), decoded, d->getSampleRate()); alBufferData(buffer, fmt, d->getBuffer(), decoded, d->getSampleRate());
acc++;
//printf(" [%i] ", acc);
}
if(decoded < d->getSize() && isLooping()) if(decoded < d->getSize() && isLooping())
{
printf(" Rewinding at %i.\n", acc);
d->rewind(); d->rewind();
acc = 0;
}
return decoded; return decoded;
} }
+5
View File
@@ -58,6 +58,9 @@ namespace openal
float pitch; float pitch;
float volume; float volume;
float position[3];
float velocity[3];
float direction[3];
bool looping; bool looping;
love::sound::Decoder * decoder; love::sound::Decoder * decoder;
@@ -99,6 +102,8 @@ namespace openal
void reset(ALenum source); void reset(ALenum source);
void setFloatv(float * dst, const float * src) const;
/** /**
* Gets the OpenAL format identifier based on number of * Gets the OpenAL format identifier based on number of
* channels and bits. * channels and bits.