first very unfinished and unpolished version of the intro
[supertux.git] / src / audio / sound_manager.cpp
1 #include "sound_manager.hpp"
2
3 #include <stdexcept>
4 #include <iostream>
5 #include <sstream>
6 #include <memory>
7
8 #include "sound_file.hpp"
9 #include "sound_source.hpp"
10 #include "stream_sound_source.hpp"
11
12 SoundManager* sound_manager = 0;
13
14 SoundManager::SoundManager()
15   : device(0), context(0), sound_enabled(false), music_source(0),
16     music_enabled(true)
17 {
18   try {
19     device = alcOpenDevice(0);
20     if(device == 0) {
21       print_openal_version();
22       throw std::runtime_error("Couldn't open audio device.");
23     }
24
25     int attributes[] = { 0 };
26     context = alcCreateContext(device, attributes);
27     check_alc_error("Couldn't create audio context: ");
28     alcMakeContextCurrent(context);
29     check_alc_error("Couldn't select audio context: ");
30
31     check_al_error("Audio error after init: ");
32     sound_enabled = true;
33   } catch(std::exception& e) {
34     device = 0;
35     context = 0;
36     std::cerr << "Couldn't initialize audio device:" << e.what() << "\n";
37     print_openal_version();
38   }
39 }
40
41 SoundManager::~SoundManager()
42 {
43   delete music_source;
44
45   for(SoundSources::iterator i = sources.begin(); i != sources.end(); ++i) {
46     delete *i;
47   }
48
49   for(SoundBuffers::iterator i = buffers.begin(); i != buffers.end(); ++i) {
50     ALuint buffer = i->second;
51     alDeleteBuffers(1, &buffer);
52   }
53
54   if(context != 0) {
55     alcDestroyContext(context);
56   }
57   if(device != 0) {
58     alcCloseDevice(device);
59   }
60 }
61
62 ALuint
63 SoundManager::load_file_into_buffer(SoundFile* file)
64 {
65   ALenum format = get_sample_format(file);
66   ALuint buffer;
67   alGenBuffers(1, &buffer);
68   check_al_error("Couldn't create audio buffer: ");
69   char* samples = new char[file->size];
70   try {
71     file->read(samples, file->size);
72     alBufferData(buffer, format, samples,
73         static_cast<ALsizei> (file->size),
74         static_cast<ALsizei> (file->rate));
75     check_al_error("Couldn't fill audio buffer: ");
76   } catch(...) {
77     delete[] samples;
78     throw;
79   }
80   delete[] samples;
81
82   return buffer;
83 }
84
85 SoundSource*
86 SoundManager::create_sound_source(const std::string& filename)
87 {
88   if(!sound_enabled)
89     return 0;
90
91   ALuint buffer;
92   
93   // reuse an existing static sound buffer            
94   SoundBuffers::iterator i = buffers.find(filename);
95   if(i != buffers.end()) {
96     buffer = i->second;
97   } else {
98     // Load sound file
99     std::auto_ptr<SoundFile> file (load_sound_file(filename));
100
101     if(file->size < 100000) {
102       buffer = load_file_into_buffer(file.get());
103       buffers.insert(std::make_pair(filename, buffer));
104     } else {
105       StreamSoundSource* source = new StreamSoundSource();
106       source->set_sound_file(file.release());
107       return source;
108     }
109   }
110   
111   SoundSource* source = new SoundSource();
112   alSourcei(source->source, AL_BUFFER, buffer);
113   return source;  
114 }
115
116 void
117 SoundManager::play(const std::string& filename, const Vector& pos)
118 {
119   try {
120     SoundSource* source = create_sound_source(filename);
121     if(source == 0)
122       return;
123     if(pos == Vector(-1, -1)) {
124       alSourcef(source->source, AL_ROLLOFF_FACTOR, 0);
125     } else {
126       source->set_position(pos);
127     }
128     source->play();
129     sources.push_back(source);
130   } catch(std::exception& e) {
131     std::cout << "Couldn't play sound " << filename << ": " << e.what() << "\n";
132   }
133 }
134
135 void
136 SoundManager::enable_sound(bool enable)
137 {
138   if(device == 0)
139     return;
140   sound_enabled = enable;
141 }
142
143 void
144 SoundManager::enable_music(bool enable)
145 {
146   if(device == 0)
147     return;
148   music_enabled = enable;
149   if(music_enabled) {
150     play_music(current_music);
151   } else {
152     if(music_source) {
153       delete music_source;
154       music_source = 0;
155     }
156   }
157 }
158
159 void
160 SoundManager::stop_music(float fadetime)
161 {
162   if(fadetime > 0) {
163     if(music_source
164         && music_source->get_fade_state() != StreamSoundSource::FadingOff)
165       music_source->set_fading(StreamSoundSource::FadingOff, fadetime);
166   } else {
167     delete music_source;
168     music_source = 0;
169   }
170   current_music = "";
171 }
172
173 void
174 SoundManager::play_music(const std::string& filename, bool fade)
175 {
176   if(filename == current_music && music_source != NULL)
177     return;
178   current_music = filename;
179   if(!music_enabled)
180     return;
181
182   if(filename == "") {
183     delete music_source;
184     music_source = 0;
185     return;
186   }
187
188   try {
189     std::auto_ptr<StreamSoundSource> newmusic (new StreamSoundSource());
190     alSourcef(newmusic->source, AL_ROLLOFF_FACTOR, 0);
191     newmusic->set_sound_file(load_sound_file(filename));
192     if(fade)
193       newmusic->set_fading(StreamSoundSource::FadingOn, .5f);
194     newmusic->play();
195
196     delete music_source;
197     music_source = newmusic.release();
198   } catch(std::exception& e) {
199     std::cerr << "Couldn't play music file '" << filename << "': "
200       << e.what() << "\n";
201   }
202 }
203
204 void
205 SoundManager::set_listener_position(const Vector& pos)
206 {
207   static Uint32 lastticks = 0;
208
209   Uint32 current_ticks = SDL_GetTicks();
210   if(current_ticks - lastticks < 300)
211     return;
212   lastticks = current_ticks;                 
213
214   alListener3f(AL_POSITION, pos.x, pos.y, 0);
215 }
216
217 void
218 SoundManager::set_listener_velocity(const Vector& vel)
219 {
220   alListener3f(AL_VELOCITY, vel.x, vel.y, 0);
221 }
222
223 void
224 SoundManager::update()
225 {
226   static Uint32 lastticks = 0;
227
228   Uint32 current_ticks = SDL_GetTicks();
229   if(current_ticks - lastticks < 300)
230     return;
231   lastticks = current_ticks;
232
233   // update and check for finished sound sources
234   for(SoundSources::iterator i = sources.begin(); i != sources.end(); ) {
235     SoundSource* source = *i;
236
237     source->update();
238     
239     if(!source->playing()) {
240       delete source;
241       i = sources.erase(i);
242     } else {
243       ++i;
244     }
245   }
246   // check streaming sounds
247   if(music_source) {
248     music_source->update();
249   }
250   
251   alcProcessContext(context);
252   check_alc_error("Error while processing audio context: ");
253 }
254
255 ALenum
256 SoundManager::get_sample_format(SoundFile* file)
257 {
258   if(file->channels == 2) {
259     if(file->bits_per_sample == 16) {
260       return AL_FORMAT_STEREO16;
261     } else if(file->bits_per_sample == 8) {
262       return AL_FORMAT_STEREO8;
263     } else {
264       throw std::runtime_error("Only 16 and 8 bit samples supported");
265     }
266   } else if(file->channels == 1) {
267     if(file->bits_per_sample == 16) {
268       return AL_FORMAT_MONO16;
269     } else if(file->bits_per_sample == 8) {
270       return AL_FORMAT_MONO8;
271     } else {
272       throw std::runtime_error("Only 16 and 8 bit samples supported");
273     }
274   }
275   
276   throw std::runtime_error("Only 1 and 2 channel samples supported");
277 }
278
279 void
280 SoundManager::print_openal_version()
281 {
282   std::cout << "OpenAL Vendor: " << alGetString(AL_VENDOR) << "\n"
283             << "OpenAL Version: " << alGetString(AL_VERSION) << "\n" 
284             << "OpenAL Renderer: " << alGetString(AL_RENDERER) << "\n"
285             << "OpenAl Extensions: " << alGetString(AL_EXTENSIONS) << "\n";
286 }
287
288 void
289 SoundManager::check_alc_error(const char* message)
290 {
291   int err = alcGetError(device);
292   if(err != ALC_NO_ERROR) {
293     std::stringstream msg;
294     msg << message << alcGetString(device, err);
295     throw std::runtime_error(msg.str());
296   }                
297 }
298
299 void
300 SoundManager::check_al_error(const char* message)
301 {
302   int err = alGetError();
303   if(err != AL_NO_ERROR) {
304     std::stringstream msg;
305     msg << message << alGetString(err);
306     throw std::runtime_error(msg.str());
307   }  
308 }
309