2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
17 #include "audio/sound_file.hpp"
18 #include "audio/sound_manager.hpp"
19 #include "audio/stream_sound_source.hpp"
20 #include "supertux/timer.hpp"
21 #include "util/log.hpp"
23 StreamSoundSource::StreamSoundSource() :
30 alGenBuffers(STREAMFRAGMENTS, buffers);
31 SoundManager::check_al_error("Couldn't allocate audio buffers: ");
32 //add me to update list
33 sound_manager->register_for_update( this );
36 StreamSoundSource::~StreamSoundSource()
38 //don't update me any longer
39 sound_manager->remove_from_update( this );
42 alDeleteBuffers(STREAMFRAGMENTS, buffers);
43 SoundManager::check_al_error("Couldn't delete audio buffers: ");
47 StreamSoundSource::set_sound_file(std::unique_ptr<SoundFile> newfile)
49 file = std::move(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 unqueue 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 float time = real_time - fade_start_time;
84 if(time >= fade_time) {
86 fade_state = NoFading;
88 set_gain(time / fade_time);
90 } else if(fade_state == FadingOff) {
91 float time = real_time - fade_start_time;
92 if(time >= fade_time) {
94 fade_state = NoFading;
96 set_gain( (fade_time-time) / fade_time);
102 StreamSoundSource::set_fading(FadeState state, float fade_time_)
104 this->fade_state = state;
105 this->fade_time = fade_time_;
106 this->fade_start_time = real_time;
110 StreamSoundSource::fillBufferAndQueue(ALuint buffer)
113 std::unique_ptr<char[]> bufferdata(new char[STREAMFRAGMENTSIZE]);
114 size_t bytesread = 0;
116 bytesread += file->read(bufferdata.get() + bytesread,
117 STREAMFRAGMENTSIZE - bytesread);
119 if(bytesread < STREAMFRAGMENTSIZE) {
125 } while(bytesread < STREAMFRAGMENTSIZE);
128 ALenum format = SoundManager::get_sample_format(*file);
129 alBufferData(buffer, format, bufferdata.get(), bytesread, file->rate);
130 SoundManager::check_al_error("Couldn't refill audio buffer: ");
132 alSourceQueueBuffers(source, 1, &buffer);
133 SoundManager::check_al_error("Couldn't queue audio buffer: ");
136 // return false if there aren't more buffers to fill
137 return bytesread >= STREAMFRAGMENTSIZE;