Fixed missing header in stream_sound_source.cpp
[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     fillBufferAndQueue(buffers[i]);
34   }
35 }
36
37 void
38 StreamSoundSource::update()
39 {
40   ALint processed = 0;
41   alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
42   for(ALint i = 0; i < processed; ++i) {
43     ALuint buffer;
44     alSourceUnqueueBuffers(source, 1, &buffer);
45     SoundManager::check_al_error("Couldn't unqueu audio buffer: ");
46
47     fillBufferAndQueue(buffer);
48   }
49
50   if(!playing()) {
51     if(processed == 0)
52       return;
53     
54     // we might have to restart the source if we had a buffer underrun  
55     std::cerr << "Restarting audio source because of buffer underrun.\n";
56     play();
57   }
58
59 #ifdef DEBUG
60   ALint queued;
61   alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
62   assert(queued == (ALint) STREAMFRAGMENTS);
63 #endif
64
65   if(fade_state == FadingOn) {
66     Uint32 ticks = SDL_GetTicks();
67     float time = (ticks - fade_start_ticks) / 1000.0;
68     if(time >= fade_time) {
69       set_gain(1.0);
70       fade_state = NoFading;
71     } else {
72       set_gain(time / fade_time);
73     }
74   } else if(fade_state == FadingOff) {
75     Uint32 ticks = SDL_GetTicks();
76     float time = (ticks - fade_start_ticks) / 1000.0;
77     if(time >= fade_time) {
78       stop();
79       fade_state = NoFading;
80     } else {
81       set_gain( (fade_time-time) / fade_time);
82     }
83   }
84 }
85
86 void
87 StreamSoundSource::set_fading(FadeState state, float fade_time)
88 {
89   this->fade_state = state;
90   this->fade_time = fade_time;
91   this->fade_start_ticks = SDL_GetTicks();
92 }
93
94 void
95 StreamSoundSource::fillBufferAndQueue(ALuint buffer)
96 {
97   // fill buffer
98   char* bufferdata = new char[STREAMFRAGMENTSIZE];
99   size_t bytesread = 0;
100   do {
101     bytesread += file->read(bufferdata + bytesread,
102         STREAMFRAGMENTSIZE - bytesread);
103     if(bytesread < STREAMFRAGMENTSIZE) {
104       file->reset();
105     }
106   } while(bytesread < STREAMFRAGMENTSIZE);
107   
108   ALenum format = SoundManager::get_sample_format(file);
109   alBufferData(buffer, format, bufferdata, STREAMFRAGMENTSIZE, file->rate);
110   delete[] bufferdata;
111   SoundManager::check_al_error("Couldn't refill audio buffer: ");
112
113   alSourceQueueBuffers(source, 1, &buffer);
114   SoundManager::check_al_error("Couldn't queue audio buffer: ");
115 }