#include <config.h>
+#include <assert.h>
#include <SDL.h>
+
#include "stream_sound_source.hpp"
#include "sound_manager.hpp"
#include "sound_file.hpp"
+#include "msg.hpp"
StreamSoundSource::StreamSoundSource()
- : file(0), fade_state(NoFading)
+ : file(0), fade_state(NoFading), looping(false)
{
alGenBuffers(STREAMFRAGMENTS, buffers);
SoundManager::check_al_error("Couldn't allocate audio buffers: ");
ALint queued;
alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
for(size_t i = 0; i < STREAMFRAGMENTS - queued; ++i) {
- fillBufferAndQueue(buffers[i]);
+ if(fillBufferAndQueue(buffers[i]) == false)
+ break;
}
}
alSourceUnqueueBuffers(source, 1, &buffer);
SoundManager::check_al_error("Couldn't unqueu audio buffer: ");
- fillBufferAndQueue(buffer);
+ if(fillBufferAndQueue(buffer) == false)
+ break;
}
if(!playing()) {
- if(processed == 0)
+ if(processed == 0 || !looping)
return;
// we might have to restart the source if we had a buffer underrun
- std::cerr << "Restarting audio source because of buffer underrun.\n";
+ msg_info("Restarting audio source because of buffer underrun");
play();
}
-#ifdef DEBUG
- ALint queued;
- alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
- assert(queued == (ALint) STREAMFRAGMENTS);
-#endif
-
if(fade_state == FadingOn) {
Uint32 ticks = SDL_GetTicks();
float time = (ticks - fade_start_ticks) / 1000.0;
this->fade_start_ticks = SDL_GetTicks();
}
-void
+bool
StreamSoundSource::fillBufferAndQueue(ALuint buffer)
{
// fill buffer
do {
bytesread += file->read(bufferdata + bytesread,
STREAMFRAGMENTSIZE - bytesread);
+ // end of sound file
if(bytesread < STREAMFRAGMENTSIZE) {
- file->reset();
+ if(looping)
+ file->reset();
+ else
+ break;
}
} while(bytesread < STREAMFRAGMENTSIZE);
-
- ALenum format = SoundManager::get_sample_format(file);
- alBufferData(buffer, format, bufferdata, STREAMFRAGMENTSIZE, file->rate);
- delete[] bufferdata;
- SoundManager::check_al_error("Couldn't refill audio buffer: ");
-
- alSourceQueueBuffers(source, 1, &buffer);
- SoundManager::check_al_error("Couldn't queue audio buffer: ");
+
+ if(bytesread > 0) {
+ ALenum format = SoundManager::get_sample_format(file);
+ alBufferData(buffer, format, bufferdata, bytesread, file->rate);
+ delete[] bufferdata;
+ SoundManager::check_al_error("Couldn't refill audio buffer: ");
+
+ alSourceQueueBuffers(source, 1, &buffer);
+ SoundManager::check_al_error("Couldn't queue audio buffer: ");
+ }
+
+ // return false if there aren't more buffers to fill
+ return bytesread >= STREAMFRAGMENTSIZE;
}
+