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