/// Whether the stream doesn't return any more data.
bool data_end;
+
+ /// The loop count of the source.
+ int loopcount;
};
struct AUD_OpenALBufferedFactory
length = m_buffersize;
sound->reader->read(length, buffer);
+ // looping necessary?
+ if(length == 0 && sound->loopcount)
+ {
+ if(sound->loopcount > 0)
+ sound->loopcount--;
+
+ sound->reader->seek(0);
+
+ length = m_buffersize;
+ sound->reader->read(length, buffer);
+ }
+
// read nothing?
if(length == 0)
{
sound->current = -1;
sound->isBuffered = true;
sound->data_end = true;
+ sound->loopcount = 0;
alcSuspendContext(m_context);
sound->current = 0;
sound->isBuffered = false;
sound->data_end = false;
+ sound->loopcount = 0;
valid &= getFormat(sound->format, specs.specs);
return result;
}
+int AUD_OpenALDevice::getLoopCount(AUD_Handle* handle)
+{
+ lock();
+ int result = 0;
+ if(isValid(handle))
+ result = ((AUD_OpenALHandle*)handle)->loopcount;
+ unlock();
+ return result;
+}
+
+bool AUD_OpenALDevice::setLoopCount(AUD_Handle* handle, int count)
+{
+ lock();
+ bool result = isValid(handle);
+ if(result)
+ ((AUD_OpenALHandle*)handle)->loopcount = count;
+ unlock();
+ return result;
+}
+
/* AUD_XXX Temorary disabled
bool AUD_OpenALDevice::bufferFactory(void *value)
virtual bool setVolume(AUD_Handle* handle, float volume);
virtual float getPitch(AUD_Handle* handle);
virtual bool setPitch(AUD_Handle* handle, float pitch);
+ virtual int getLoopCount(AUD_Handle* handle);
+ virtual bool setLoopCount(AUD_Handle* handle, int count);
virtual AUD_Vector3 getListenerLocation() const;
virtual void setListenerLocation(const AUD_Vector3& location);
try
{
- // AUD_XXX will come soon; return Py_BuildValue("f", device->device->getPitch(self->handle));
- AUD_THROW(AUD_ERROR_FACTORY);
+ return Py_BuildValue("i", device->device->getLoopCount(self->handle));
}
catch(AUD_Exception&)
{
try
{
- /* AUD_XXX Doesn't work atm, will come back
- AUD_Message message;
- message.loopcount = loops;
- message.type = AUD_MSG_LOOP;
- if(device->device->sendMessage(self->handle, message))
- {
+ if(device->device->setLoopCount(self->handle, loops))
return 0;
- }*/
}
catch(AUD_Exception&)
{
float x, y, z;
if(!PyArg_Parse(args, "(fff)", &x, &y, &z))
- return NULL;
+ return -1;
Device* dev = (Device*)self->device;
float x, y, z;
if(!PyArg_Parse(args, "(fff)", &x, &y, &z))
- return NULL;
+ return -1;
Device* dev = (Device*)self->device;
float w, x, y, z;
if(!PyArg_Parse(args, "(ffff)", &w, &x, &y, &z))
- return NULL;
+ return -1;
Device* dev = (Device*)self->device;
float x, y, z;
if(!PyArg_Parse(args, "(fff)", &x, &y, &z))
- return NULL;
+ return -1;
try
{
float x, y, z;
if(!PyArg_Parse(args, "(fff)", &x, &y, &z))
- return NULL;
+ return -1;
try
{
float w, x, y, z;
if(!PyArg_Parse(args, "(ffff)", &w, &x, &y, &z))
- return NULL;
+ return -1;
try
{
}
}
-int AUD_setLoop(AUD_Channel* handle, int loops, float time)
+int AUD_setLoop(AUD_Channel* handle, int loops)
{
if(handle)
{
- /* AUD_XXX Doesn't work atm, will come back
-
- AUD_Message message;
- message.type = AUD_MSG_LOOP;
- message.loopcount = loops;
- message.time = time;
-
try
{
- return AUD_device->sendMessage(handle, message);
+ return AUD_device->setLoopCount(handle, loops);
}
catch(AUD_Exception&)
{
- }*/
+ }
}
return false;
}
* Sets a remaining loop count of a looping sound that currently plays.
* \param handle The playback handle.
* \param loops The count of remaining loops, -1 for infinity.
- * \param time The time after which playback should stop, -1 for infinity.
* \return Whether the handle is valid.
*/
-extern int AUD_setLoop(AUD_Channel* handle, int loops, float time);
+extern int AUD_setLoop(AUD_Channel* handle, int loops);
/**
* Rectifies a sound.
* - false if the handle is invalid.
*/
virtual bool setPitch(AUD_Handle* handle, float pitch)=0;
+
+ /**
+ * Retrieves the loop count of a playing sound.
+ * A negative value indicates infinity.
+ * \return The remaining loop count.
+ */
+ virtual int getLoopCount(AUD_Handle* handle)=0;
+
+ /**
+ * Sets the loop count of a playing sound.
+ * A negative value indicates infinity.
+ * \param handle The sound handle.
+ * \param count The new loop count.
+ * \return
+ * - true if the handle is valid.
+ * - false if the handle is invalid.
+ */
+ virtual bool setLoopCount(AUD_Handle* handle, int count)=0;
};
#endif //AUD_IDevice
{
return false;
}
+
+int AUD_NULLDevice::getLoopCount(AUD_Handle* handle)
+{
+ return 0;
+}
+
+bool AUD_NULLDevice::setLoopCount(AUD_Handle* handle, int count)
+{
+ return false;
+}
virtual bool setVolume(AUD_Handle* handle, float volume);
virtual float getPitch(AUD_Handle* handle);
virtual bool setPitch(AUD_Handle* handle, float pitch);
+ virtual int getLoopCount(AUD_Handle* handle);
+ virtual bool setLoopCount(AUD_Handle* handle, int count);
};
#endif //AUD_NULLDEVICE
/// The volume of the source.
float volume;
+
+ /// The loop count of the source.
+ int loopcount;
};
typedef std::list<AUD_SoftwareHandle*>::iterator AUD_HandleIterator;
void AUD_SoftwareDevice::create()
{
- m_playingSounds = new std::list<AUD_SoftwareHandle*>();
- m_pausedSounds = new std::list<AUD_SoftwareHandle*>();
m_playback = false;
m_volume = 1.0f;
m_mixer = new AUD_DefaultMixer(m_specs);
delete m_mixer;
+ AUD_SoftwareHandle* handle;
+
// delete all playing sounds
- while(m_playingSounds->begin() != m_playingSounds->end())
+ while(!m_playingSounds.empty())
{
- delete (*(m_playingSounds->begin()))->reader;
- delete *(m_playingSounds->begin());
- m_playingSounds->erase(m_playingSounds->begin());
+ handle = m_playingSounds.front();
+ m_playingSounds.pop_front();
+ delete handle->reader;
+ delete handle;
}
- delete m_playingSounds;
// delete all paused sounds
- while(m_pausedSounds->begin() != m_pausedSounds->end())
+ while(!m_pausedSounds.empty())
{
- delete (*(m_pausedSounds->begin()))->reader;
- delete *(m_pausedSounds->begin());
- m_pausedSounds->erase(m_pausedSounds->begin());
+ handle = m_pausedSounds.front();
+ m_pausedSounds.pop_front();
+ delete handle->reader;
+ delete handle;
}
- delete m_pausedSounds;
pthread_mutex_destroy(&m_mutex);
}
{
AUD_SoftwareHandle* sound;
int len;
+ int pos;
sample_t* buf;
std::list<AUD_SoftwareHandle*> stopSounds;
+ std::list<AUD_Buffer*> tempBufs;
+ AUD_Buffer* tempbuf;
+ int samplesize = AUD_SAMPLE_SIZE(m_specs);
// for all sounds
- AUD_HandleIterator it = m_playingSounds->begin();
- while(it != m_playingSounds->end())
+ AUD_HandleIterator it = m_playingSounds.begin();
+ while(it != m_playingSounds.end())
{
sound = *it;
// increment the iterator to make sure it's valid,
++it;
// get the buffer from the source
+ pos = 0;
len = length;
sound->reader->read(len, buf);
- m_mixer->add(buf, 0, len, sound->volume);
+ // in case of looping
+ while(pos + len < length && sound->loopcount)
+ {
+ tempbuf = new AUD_Buffer(len * samplesize);
+ memcpy(tempbuf->getBuffer(), buf, len * samplesize);
+ tempBufs.push_back(tempbuf);
+ m_mixer->add(tempbuf->getBuffer(), pos, len, sound->volume);
+
+ pos += len;
+
+ if(sound->loopcount > 0)
+ sound->loopcount--;
+
+ sound->reader->seek(0);
+
+ len = length - pos;
+ sound->reader->read(len, buf);
+
+ // prevent endless loop
+ if(!len)
+ break;
+ }
+
+ m_mixer->add(buf, pos, len, sound->volume);
+ pos += len;
// in case the end of the sound is reached
- if(len < length)
+ if(pos < length)
{
if(sound->keep)
pause(sound);
// superpose
m_mixer->superpose(buffer, length, m_volume);
+ // cleanup
while(!stopSounds.empty())
{
sound = stopSounds.front();
stopSounds.pop_front();
stop(sound);
}
+
+ while(!tempBufs.empty())
+ {
+ tempbuf = tempBufs.front();
+ tempBufs.pop_front();
+ delete tempbuf;
+ }
}
unlock();
bool AUD_SoftwareDevice::isValid(AUD_Handle* handle)
{
- for(AUD_HandleIterator i = m_playingSounds->begin();
- i != m_playingSounds->end(); i++)
+ for(AUD_HandleIterator i = m_playingSounds.begin();
+ i != m_playingSounds.end(); i++)
if(*i == handle)
return true;
- for(AUD_HandleIterator i = m_pausedSounds->begin();
- i != m_pausedSounds->end(); i++)
+ for(AUD_HandleIterator i = m_pausedSounds.begin();
+ i != m_pausedSounds.end(); i++)
if(*i == handle)
return true;
return false;
if(reader == NULL)
return NULL;
- AUD_Specs rs = reader->getSpecs();
-
// play sound
AUD_SoftwareHandle* sound = new AUD_SoftwareHandle;
sound->keep = keep;
sound->reader = reader;
sound->volume = 1.0f;
+ sound->loopcount = 0;
lock();
- m_playingSounds->push_back(sound);
+ m_playingSounds.push_back(sound);
if(!m_playback)
playing(m_playback = true);
lock();
// only songs that are played can be paused
- for(AUD_HandleIterator i = m_playingSounds->begin();
- i != m_playingSounds->end(); i++)
+ for(AUD_HandleIterator i = m_playingSounds.begin();
+ i != m_playingSounds.end(); i++)
{
if(*i == handle)
{
- m_pausedSounds->push_back(*i);
- m_playingSounds->erase(i);
- if(m_playingSounds->empty())
+ m_pausedSounds.push_back(*i);
+ m_playingSounds.erase(i);
+ if(m_playingSounds.empty())
playing(m_playback = false);
result = true;
break;
lock();
// only songs that are paused can be resumed
- for(AUD_HandleIterator i = m_pausedSounds->begin();
- i != m_pausedSounds->end(); i++)
+ for(AUD_HandleIterator i = m_pausedSounds.begin();
+ i != m_pausedSounds.end(); i++)
{
if(*i == handle)
{
- m_playingSounds->push_back(*i);
- m_pausedSounds->erase(i);
+ m_playingSounds.push_back(*i);
+ m_pausedSounds.erase(i);
if(!m_playback)
playing(m_playback = true);
result = true;
lock();
- for(AUD_HandleIterator i = m_playingSounds->begin();
- i != m_playingSounds->end(); i++)
+ for(AUD_HandleIterator i = m_playingSounds.begin();
+ i != m_playingSounds.end(); i++)
{
if(*i == handle)
{
delete (*i)->reader;
delete *i;
- m_playingSounds->erase(i);
- if(m_playingSounds->empty())
+ m_playingSounds.erase(i);
+ if(m_playingSounds.empty())
playing(m_playback = false);
result = true;
break;
}
if(!result)
{
- for(AUD_HandleIterator i = m_pausedSounds->begin();
- i != m_pausedSounds->end(); i++)
+ for(AUD_HandleIterator i = m_pausedSounds.begin();
+ i != m_pausedSounds.end(); i++)
{
if(*i == handle)
{
delete (*i)->reader;
delete *i;
- m_pausedSounds->erase(i);
+ m_pausedSounds.erase(i);
result = true;
break;
}
lock();
- for(AUD_HandleIterator i = m_playingSounds->begin();
- i != m_playingSounds->end(); i++)
+ for(AUD_HandleIterator i = m_playingSounds.begin();
+ i != m_playingSounds.end(); i++)
{
if(*i == handle)
{
}
if(status == AUD_STATUS_INVALID)
{
- for(AUD_HandleIterator i = m_pausedSounds->begin();
- i != m_pausedSounds->end(); i++)
+ for(AUD_HandleIterator i = m_pausedSounds.begin();
+ i != m_pausedSounds.end(); i++)
{
if(*i == handle)
{
{
return false;
}
+
+int AUD_SoftwareDevice::getLoopCount(AUD_Handle* handle)
+{
+ lock();
+ int result = 0;
+ if(isValid(handle))
+ result = ((AUD_SoftwareHandle*)handle)->loopcount;
+ unlock();
+ return result;
+}
+
+bool AUD_SoftwareDevice::setLoopCount(AUD_Handle* handle, int count)
+{
+ lock();
+ bool result = isValid(handle);
+ if(result)
+ ((AUD_SoftwareHandle*)handle)->loopcount = count;
+ unlock();
+ return result;
+}
#include "AUD_IDevice.h"
struct AUD_SoftwareHandle;
class AUD_Mixer;
+class AUD_Buffer;
#include <list>
#include <pthread.h>
/**
* The list of sounds that are currently playing.
*/
- std::list<AUD_SoftwareHandle*>* m_playingSounds;
+ std::list<AUD_SoftwareHandle*> m_playingSounds;
/**
* The list of sounds that are currently paused.
*/
- std::list<AUD_SoftwareHandle*>* m_pausedSounds;
+ std::list<AUD_SoftwareHandle*> m_pausedSounds;
/**
* Whether there is currently playback.
virtual bool setVolume(AUD_Handle* handle, float volume);
virtual float getPitch(AUD_Handle* handle);
virtual bool setPitch(AUD_Handle* handle, float pitch);
+ virtual int getLoopCount(AUD_Handle* handle);
+ virtual bool setLoopCount(AUD_Handle* handle, int count);
};
#endif //AUD_SOFTWAREDEVICE
void sound_start_play_scene(struct Scene *scene)
{
- AUD_Sound* sound;
- sound = AUD_loopSound(scene->sound_scene);
- scene->sound_scene_handle = AUD_play(sound, 1);
- AUD_unload(sound);
+ scene->sound_scene_handle = AUD_play(scene->sound_scene, 1);
+ AUD_setLoop(scene->sound_scene_handle, -1);
}
void sound_play_scene(struct Scene *scene)
if(status == AUD_STATUS_INVALID)
sound_start_play_scene(scene);
- AUD_setLoop(scene->sound_scene_handle, -1, -1);
-
if(status != AUD_STATUS_PLAYING)
{
AUD_seek(scene->sound_scene_handle, CFRA / FPS);
if(scene->audio.flag & AUDIO_SCRUB && !CTX_wm_screen(C)->animtimer)
{
- AUD_setLoop(scene->sound_scene_handle, -1, 1 / FPS);
+ // AUD_XXX TODO: fix scrubbing, it currently doesn't stop playing
if(scene->audio.flag & AUDIO_SYNC)
AUD_seekSequencer(scene->sound_scene_handle, CFRA / FPS);
else
// this is the sound that will be played and not deleted afterwards
AUD_Sound* sound = m_sound;
- // this sounds are for temporary stacked sounds, will be deleted if not NULL
+ // this sound is for temporary stacked sounds, will be deleted if not NULL
AUD_Sound* sound2 = NULL;
- AUD_Sound* sound3 = NULL;
+
+ bool loop = false;
switch (m_type)
{
case KX_SOUNDACT_LOOPBIDIRECTIONAL:
case KX_SOUNDACT_LOOPBIDIRECTIONAL_STOP:
- // create a ping pong sound on sound2 stacked on the orignal sound
- sound2 = AUD_pingpongSound(sound);
- // create a loop sound on sound3 stacked on the pingpong sound and let that one play (save it to sound)
- sound = sound3 = AUD_loopSound(sound2);
- break;
+ sound = sound2 = AUD_pingpongSound(sound);
+ // fall through
case KX_SOUNDACT_LOOPEND:
case KX_SOUNDACT_LOOPSTOP:
- // create a loop sound on sound2 stacked on the pingpong sound and let that one play (save it to sound)
- sound = sound2 = AUD_loopSound(sound);
+ loop = true;
break;
case KX_SOUNDACT_PLAYSTOP:
case KX_SOUNDACT_PLAYEND:
else
m_handle = AUD_play(sound, 0);
+ if(loop)
+ AUD_setLoop(m_handle, -1);
AUD_setSoundPitch(m_handle, m_pitch);
AUD_setSoundVolume(m_handle, m_volume);
m_isplaying = true;
- // now we unload the pingpong and loop sounds, as we don't need them anymore
- // the started sound will continue playing like it was created, don't worry!
- if(sound3)
- AUD_unload(sound3);
if(sound2)
AUD_unload(sound2);
}
case KX_SOUNDACT_LOOPBIDIRECTIONAL:
{
// stop the looping so that the sound stops when it finished
- AUD_setLoop(m_handle, 0, -1);
+ AUD_setLoop(m_handle, 0);
break;
}
default: