Fade out and pause music on death and resume on restart of level, fixes #1064
[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(),
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   SoundManager::current()->register_for_update( this );
34 }
35
36 StreamSoundSource::~StreamSoundSource()
37 {
38   //don't update me any longer
39   SoundManager::current()->remove_from_update( this );
40   file.reset();
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(std::unique_ptr<SoundFile> newfile)
48 {
49   file = std::move(newfile);
50
51   ALint queued;
52   alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
53   for(size_t i = 0; i < STREAMFRAGMENTS - queued; ++i) {
54     if(fillBufferAndQueue(buffers[i]) == false)
55       break;
56   }
57 }
58
59 void
60 StreamSoundSource::update()
61 {
62   ALint processed = 0;
63   alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
64   for(ALint i = 0; i < processed; ++i) {
65     ALuint buffer;
66     alSourceUnqueueBuffers(source, 1, &buffer);
67     SoundManager::check_al_error("Couldn't unqueue audio buffer: ");
68
69     if(fillBufferAndQueue(buffer) == false)
70       break;
71   }
72
73   if(!playing()) {
74     if(processed == 0 || !looping)
75       return;
76
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;
79     play();
80   }
81
82   if(fade_state == FadingOn || fade_state == FadingResume) {
83     float time = real_time - fade_start_time;
84     if(time >= fade_time) {
85       set_gain(1.0);
86       fade_state = NoFading;
87     } else {
88       set_gain(time / fade_time);
89     }
90   } else if(fade_state == FadingOff || fade_state == FadingPause) {
91     float time = real_time - fade_start_time;
92     if(time >= fade_time) {
93       if(fade_state == FadingOff)
94         stop();
95       else
96         pause();
97       fade_state = NoFading;
98     } else {
99       set_gain( (fade_time-time) / fade_time);
100     }
101   }
102 }
103
104 void
105 StreamSoundSource::set_fading(FadeState state, float fade_time_)
106 {
107   this->fade_state = state;
108   this->fade_time = fade_time_;
109   this->fade_start_time = real_time;
110 }
111
112 bool
113 StreamSoundSource::fillBufferAndQueue(ALuint buffer)
114 {
115   // fill buffer
116   std::unique_ptr<char[]> bufferdata(new char[STREAMFRAGMENTSIZE]);
117   size_t bytesread = 0;
118   do {
119     bytesread += file->read(bufferdata.get() + bytesread,
120                             STREAMFRAGMENTSIZE - bytesread);
121     // end of sound file
122     if(bytesread < STREAMFRAGMENTSIZE) {
123       if(looping)
124         file->reset();
125       else
126         break;
127     }
128   } while(bytesread < STREAMFRAGMENTSIZE);
129
130   if(bytesread > 0) {
131     ALenum format = SoundManager::get_sample_format(*file);
132     alBufferData(buffer, format, bufferdata.get(), bytesread, file->rate);
133     SoundManager::check_al_error("Couldn't refill audio buffer: ");
134
135     alSourceQueueBuffers(source, 1, &buffer);
136     SoundManager::check_al_error("Couldn't queue audio buffer: ");
137   }
138
139   // return false if there aren't more buffers to fill
140   return bytesread >= STREAMFRAGMENTSIZE;
141 }
142
143 /* EOF */