fix openal stalling at exit
[supertux.git] / src / audio / sound_manager.hpp
1 #ifndef __SOUND_MANAGER_H__
2 #define __SOUND_MANAGER_H__
3
4 #include "math/vector.hpp"
5 #include <string>
6 #include <vector>
7 #include <map>
8
9 #include <AL/alc.h>
10 #include <AL/al.h>
11
12 typedef void* SoundHandle;
13
14 class SoundFile;
15 class SoundSource;
16 class StreamSoundSource;
17
18 class SoundManager
19 {
20 public:
21   SoundManager();
22   virtual ~SoundManager();
23
24   void enable_sound(bool sound_enabled);
25   /**
26    * Creates a new sound source object which plays the specified soundfile.
27    * You are responsible for deleting the sound source later (this will stop the
28    * sound).
29    * This function might throw exceptions. It returns 0 if no audio device is
30    * available.
31    */
32   SoundSource* create_sound_source(const std::string& filename);
33   /**
34    * Convenience function to simply play a sound at a given position.
35    */
36   void play(const std::string& name, const Vector& pos = Vector(-1, -1));
37
38   void set_listener_position(Vector position);
39   void set_listener_velocity(Vector velocity);
40
41   void enable_music(bool music_enabled);
42   void play_music(const std::string& filename, bool fade = true);
43
44   void update();
45
46 private:
47   friend class SoundSource;
48   friend class StreamSoundSource;
49
50   static ALuint load_file_into_buffer(const std::string& filename);
51   static ALenum get_sample_format(SoundFile* file);
52
53   void print_openal_version();
54   void check_alc_error(const char* message);
55   static void check_al_error(const char* message);
56
57   ALCdevice* device;
58   ALCcontext* context;
59   bool sound_enabled;
60
61   typedef std::map<std::string, ALuint> SoundBuffers;
62   SoundBuffers buffers;
63   typedef std::vector<SoundSource*> SoundSources;
64   SoundSources sources;
65
66   StreamSoundSource* music_source;
67   StreamSoundSource* next_music_source;
68
69   bool music_enabled;
70   std::string current_music;
71 };
72
73 extern SoundManager* sound_manager;
74
75 #endif
76