fade out console
[supertux.git] / src / audio / stream_sound_source.cpp
1 #include <config.h>
2 #include <assert.h>
3
4 #include <SDL.h>
5
6 #include "stream_sound_source.hpp"
7 #include "sound_manager.hpp"
8 #include "sound_file.hpp"
9 #include "log.hpp"
10
11 StreamSoundSource::StreamSoundSource()
12   : file(0), fade_state(NoFading), looping(false)
13 {
14   alGenBuffers(STREAMFRAGMENTS, buffers);
15   SoundManager::check_al_error("Couldn't allocate audio buffers: ");
16 }
17
18 StreamSoundSource::~StreamSoundSource()
19 {
20   delete file;
21   stop();
22   alDeleteBuffers(STREAMFRAGMENTS, buffers);
23   SoundManager::check_al_error("Couldn't delete audio buffers: ");
24 }
25
26 void
27 StreamSoundSource::set_sound_file(SoundFile* newfile)
28 {
29   delete file;
30   file = newfile;
31
32   ALint queued;
33   alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
34   for(size_t i = 0; i < STREAMFRAGMENTS - queued; ++i) {
35     if(fillBufferAndQueue(buffers[i]) == false)
36       break;
37   }
38 }
39
40 void
41 StreamSoundSource::update()
42 {
43   ALint processed = 0;
44   alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
45   for(ALint i = 0; i < processed; ++i) {
46     ALuint buffer;
47     alSourceUnqueueBuffers(source, 1, &buffer);
48     SoundManager::check_al_error("Couldn't unqueu audio buffer: ");
49
50     if(fillBufferAndQueue(buffer) == false)
51       break;
52   }
53
54   if(!playing()) {
55     if(processed == 0 || !looping)
56       return;
57     
58     // we might have to restart the source if we had a buffer underrun  
59     log_info << "Restarting audio source because of buffer underrun" << std::endl;
60     play();
61   }
62
63   if(fade_state == FadingOn) {
64     Uint32 ticks = SDL_GetTicks();
65     float time = (ticks - fade_start_ticks) / 1000.0;
66     if(time >= fade_time) {
67       set_gain(1.0);
68       fade_state = NoFading;
69     } else {
70       set_gain(time / fade_time);
71     }
72   } else if(fade_state == FadingOff) {
73     Uint32 ticks = SDL_GetTicks();
74     float time = (ticks - fade_start_ticks) / 1000.0;
75     if(time >= fade_time) {
76       stop();
77       fade_state = NoFading;
78     } else {
79       set_gain( (fade_time-time) / fade_time);
80     }
81   }
82 }
83
84 void
85 StreamSoundSource::set_fading(FadeState state, float fade_time)
86 {
87   this->fade_state = state;
88   this->fade_time = fade_time;
89   this->fade_start_ticks = SDL_GetTicks();
90 }
91
92 bool
93 StreamSoundSource::fillBufferAndQueue(ALuint buffer)
94 {
95   // fill buffer
96   char* bufferdata = new char[STREAMFRAGMENTSIZE];
97   size_t bytesread = 0;
98   do {
99     bytesread += file->read(bufferdata + bytesread,
100         STREAMFRAGMENTSIZE - bytesread);
101     // end of sound file
102     if(bytesread < STREAMFRAGMENTSIZE) {
103       if(looping)
104         file->reset();
105       else
106         break;
107     }
108   } while(bytesread < STREAMFRAGMENTSIZE);
109
110   if(bytesread > 0) {
111     ALenum format = SoundManager::get_sample_format(file);
112     alBufferData(buffer, format, bufferdata, bytesread, file->rate);
113     delete[] bufferdata;
114     SoundManager::check_al_error("Couldn't refill audio buffer: ");
115
116     alSourceQueueBuffers(source, 1, &buffer);
117     SoundManager::check_al_error("Couldn't queue audio buffer: ");
118   }
119
120   // return false if there aren't more buffers to fill
121   return bytesread >= STREAMFRAGMENTSIZE;
122 }
123