cleanup and fixes to openal code
[supertux.git] / src / audio / stream_sound_source.cpp
1 #include <config.h>
2
3 #include <SDL.h>
4 #include "stream_sound_source.hpp"
5 #include "sound_manager.hpp"
6 #include "sound_file.hpp"
7
8 StreamSoundSource::StreamSoundSource()
9   : file(0), fade_state(NoFading)
10 {
11   alGenBuffers(STREAMFRAGMENTS, buffers);
12   SoundManager::check_al_error("Couldn't allocate audio buffers: ");
13 }
14
15 StreamSoundSource::~StreamSoundSource()
16 {
17   delete file;
18   alDeleteBuffers(STREAMFRAGMENTS, buffers);
19   SoundManager::check_al_error("Couldn't delete audio buffers: ");
20 }
21
22 void
23 StreamSoundSource::set_sound_file(SoundFile* newfile)
24 {
25   delete file;
26   file = newfile;
27
28   ALint queued;
29   alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
30   for(size_t i = 0; i < STREAMFRAGMENTS - queued; ++i) {
31     fillBufferAndQueue(buffers[i]);
32   }
33 }
34
35 void
36 StreamSoundSource::update()
37 {
38   ALint processed = 0;
39   alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
40   for(ALint i = 0; i < processed; ++i) {
41     ALuint buffer;
42     alSourceUnqueueBuffers(source, 1, &buffer);
43     SoundManager::check_al_error("Couldn't unqueu audio buffer: ");
44
45     fillBufferAndQueue(buffer);
46   }
47
48   if(!playing()) {
49     if(processed == 0)
50       return;
51     
52     // we might have to restart the source if we had a buffer underrun  
53     std::cerr << "Restarting audio source because of buffer underrun.\n";
54     play();
55   }
56
57 #ifdef DEBUG
58   ALint queued;
59   alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
60   assert(queued == (ALint) STREAMFRAGMENTS);
61 #endif
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 void
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     if(bytesread < STREAMFRAGMENTSIZE) {
102       file->reset();
103     }
104   } while(bytesread < STREAMFRAGMENTSIZE);
105   
106   ALenum format = SoundManager::get_sample_format(file);
107   alBufferData(buffer, format, bufferdata, STREAMFRAGMENTSIZE, file->rate);
108   delete[] bufferdata;
109   SoundManager::check_al_error("Couldn't refill audio buffer: ");
110
111   alSourceQueueBuffers(source, 1, &buffer);
112   SoundManager::check_al_error("Couldn't queue audio buffer: ");
113 }