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