Merged changes from branches/supertux-milestone2-grumbel/ to trunk/supertux/
[supertux.git] / src / audio / stream_sound_source.cpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
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.
8 //
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.
13 //
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/>.
16
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"
22
23 StreamSoundSource::StreamSoundSource() :
24   file(0), 
25   fade_state(NoFading), 
26   fade_start_time(),
27   fade_time(),
28   looping(false)
29 {
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 );
34 }
35
36 StreamSoundSource::~StreamSoundSource()
37 {
38   //don't update me any longer
39   sound_manager->remove_from_update( this );
40   delete file;
41   stop();
42   alDeleteBuffers(STREAMFRAGMENTS, buffers);
43   SoundManager::check_al_error("Couldn't delete audio buffers: ");
44 }
45
46 void
47 StreamSoundSource::set_sound_file(SoundFile* newfile)
48 {
49   delete file;
50   file = newfile;
51
52   ALint queued;
53   alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
54   for(size_t i = 0; i < STREAMFRAGMENTS - queued; ++i) {
55     if(fillBufferAndQueue(buffers[i]) == false)
56       break;
57   }
58 }
59
60 void
61 StreamSoundSource::update()
62 {
63   ALint processed = 0;
64   alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
65   for(ALint i = 0; i < processed; ++i) {
66     ALuint buffer;
67     alSourceUnqueueBuffers(source, 1, &buffer);
68     SoundManager::check_al_error("Couldn't unqueue audio buffer: ");
69
70     if(fillBufferAndQueue(buffer) == false)
71       break;
72   }
73
74   if(!playing()) {
75     if(processed == 0 || !looping)
76       return;
77
78     // we might have to restart the source if we had a buffer underrun
79     log_info << "Restarting audio source because of buffer underrun" << std::endl;
80     play();
81   }
82
83   if(fade_state == FadingOn) {
84     float time = real_time - fade_start_time;
85     if(time >= fade_time) {
86       set_gain(1.0);
87       fade_state = NoFading;
88     } else {
89       set_gain(time / fade_time);
90     }
91   } else if(fade_state == FadingOff) {
92     float time = real_time - fade_start_time;
93     if(time >= fade_time) {
94       stop();
95       fade_state = NoFading;
96     } else {
97       set_gain( (fade_time-time) / fade_time);
98     }
99   }
100 }
101
102 void
103 StreamSoundSource::set_fading(FadeState state, float fade_time)
104 {
105   this->fade_state = state;
106   this->fade_time = fade_time;
107   this->fade_start_time = real_time;
108 }
109
110 bool
111 StreamSoundSource::fillBufferAndQueue(ALuint buffer)
112 {
113   // fill buffer
114   char* bufferdata = new char[STREAMFRAGMENTSIZE];
115   size_t bytesread = 0;
116   do {
117     bytesread += file->read(bufferdata + bytesread,
118                             STREAMFRAGMENTSIZE - bytesread);
119     // end of sound file
120     if(bytesread < STREAMFRAGMENTSIZE) {
121       if(looping)
122         file->reset();
123       else
124         break;
125     }
126   } while(bytesread < STREAMFRAGMENTSIZE);
127
128   if(bytesread > 0) {
129     ALenum format = SoundManager::get_sample_format(file);
130     alBufferData(buffer, format, bufferdata, bytesread, file->rate);
131     SoundManager::check_al_error("Couldn't refill audio buffer: ");
132
133     alSourceQueueBuffers(source, 1, &buffer);
134     SoundManager::check_al_error("Couldn't queue audio buffer: ");
135   }
136   delete[] bufferdata;
137
138   // return false if there aren't more buffers to fill
139   return bytesread >= STREAMFRAGMENTSIZE;
140 }
141
142 /* EOF */