4 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "stream_sound_source.hpp"
26 #include "sound_manager.hpp"
27 #include "sound_file.hpp"
30 StreamSoundSource::StreamSoundSource()
31 : file(0), fade_state(NoFading), looping(false)
33 alGenBuffers(STREAMFRAGMENTS, buffers);
34 SoundManager::check_al_error("Couldn't allocate audio buffers: ");
37 StreamSoundSource::~StreamSoundSource()
41 alDeleteBuffers(STREAMFRAGMENTS, buffers);
42 SoundManager::check_al_error("Couldn't delete audio buffers: ");
46 StreamSoundSource::set_sound_file(SoundFile* newfile)
52 alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
53 for(size_t i = 0; i < STREAMFRAGMENTS - queued; ++i) {
54 if(fillBufferAndQueue(buffers[i]) == false)
60 StreamSoundSource::update()
63 alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
64 for(ALint i = 0; i < processed; ++i) {
66 alSourceUnqueueBuffers(source, 1, &buffer);
67 SoundManager::check_al_error("Couldn't unqueu audio buffer: ");
69 if(fillBufferAndQueue(buffer) == false)
74 if(processed == 0 || !looping)
77 // we might have to restart the source if we had a buffer underrun
78 log_info << "Restarting audio source because of buffer underrun" << std::endl;
82 if(fade_state == FadingOn) {
83 Uint32 ticks = SDL_GetTicks();
84 float time = (ticks - fade_start_ticks) / 1000.0;
85 if(time >= fade_time) {
87 fade_state = NoFading;
89 set_gain(time / fade_time);
91 } else if(fade_state == FadingOff) {
92 Uint32 ticks = SDL_GetTicks();
93 float time = (ticks - fade_start_ticks) / 1000.0;
94 if(time >= fade_time) {
96 fade_state = NoFading;
98 set_gain( (fade_time-time) / fade_time);
104 StreamSoundSource::set_fading(FadeState state, float fade_time)
106 this->fade_state = state;
107 this->fade_time = fade_time;
108 this->fade_start_ticks = SDL_GetTicks();
112 StreamSoundSource::fillBufferAndQueue(ALuint buffer)
115 char* bufferdata = new char[STREAMFRAGMENTSIZE];
116 size_t bytesread = 0;
118 bytesread += file->read(bufferdata + bytesread,
119 STREAMFRAGMENTSIZE - bytesread);
121 if(bytesread < STREAMFRAGMENTSIZE) {
127 } while(bytesread < STREAMFRAGMENTSIZE);
130 ALenum format = SoundManager::get_sample_format(file);
131 alBufferData(buffer, format, bufferdata, bytesread, file->rate);
133 SoundManager::check_al_error("Couldn't refill audio buffer: ");
135 alSourceQueueBuffers(source, 1, &buffer);
136 SoundManager::check_al_error("Couldn't queue audio buffer: ");
139 // return false if there aren't more buffers to fill
140 return bytesread >= STREAMFRAGMENTSIZE;