4 #include "stream_sound_source.hpp"
5 #include "sound_manager.hpp"
6 #include "sound_file.hpp"
8 StreamSoundSource::StreamSoundSource(SoundFile* file)
11 alGenBuffers(STREAMFRAGMENTS, buffers);
12 SoundManager::check_al_error("Couldn't allocate audio buffers: ");
13 format = SoundManager::get_sample_format(file);
15 for(size_t i = 0; i < STREAMFRAGMENTS; ++i) {
16 fillBufferAndQueue(buffers[i]);
19 alDeleteBuffers(STREAMFRAGMENTS, buffers);
23 StreamSoundSource::~StreamSoundSource()
25 alDeleteBuffers(STREAMFRAGMENTS, buffers);
26 SoundManager::check_al_error("Couldn't delete audio buffers: ");
30 StreamSoundSource::update()
36 alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
37 while(processed > 0) {
41 alSourceUnqueueBuffers(source, 1, &buffer);
42 SoundManager::check_al_error("Couldn't unqueu audio buffer: ");
44 fillBufferAndQueue(buffer);
47 // we might have to restart the source if we had a buffer underrun
49 std::cerr << "Restarting audio source because of buffer underrun.\n";
51 SoundManager::check_al_error("Couldn't restart audio source: ");
54 if(fade_state == FadingOn) {
55 Uint32 ticks = SDL_GetTicks();
56 float time = (ticks - fade_start_ticks) / 1000.0;
57 if(time >= fade_time) {
59 fade_state = NoFading;
61 set_gain(time / fade_time);
63 } else if(fade_state == FadingOff) {
64 Uint32 ticks = SDL_GetTicks();
65 float time = (ticks - fade_start_ticks) / 1000.0;
66 if(time >= fade_time) {
68 fade_state = NoFading;
70 set_gain( (fade_time-time) / fade_time);
76 StreamSoundSource::setFading(FadeState state, float fade_time)
78 this->fade_state = state;
79 this->fade_time = fade_time;
80 this->fade_start_ticks = SDL_GetTicks();
84 StreamSoundSource::fillBufferAndQueue(ALuint buffer)
87 char* bufferdata = new char[STREAMFRAGMENTSIZE];
90 bytesread += file->read(bufferdata + bytesread,
91 STREAMFRAGMENTSIZE - bytesread);
92 if(bytesread < STREAMFRAGMENTSIZE) {
95 } while(bytesread < STREAMFRAGMENTSIZE);
97 alBufferData(buffer, format, bufferdata, STREAMFRAGMENTSIZE, file->rate);
99 SoundManager::check_al_error("Couldn't refill audio buffer: ");
101 alSourceQueueBuffers(source, 1, &buffer);
102 SoundManager::check_al_error("Couldn't queue audio buffer: ");