Supertux can now run even if we were unable to open an audio device
[supertux.git] / src / audio / sound_manager.hpp
1 //  $Id$
2 //
3 //  SuperTux
4 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 #ifndef __SOUND_MANAGER_H__
20 #define __SOUND_MANAGER_H__
21
22 #include <string>
23 #include <vector>
24 #include <map>
25
26 #include <AL/alc.h>
27 #include <AL/al.h>
28 #include "math/vector.hpp"
29
30 class SoundFile;
31 class SoundSource;
32 class StreamSoundSource;
33
34 class SoundManager
35 {
36 public:
37   SoundManager();
38   virtual ~SoundManager();
39
40   void enable_sound(bool sound_enabled);
41   /**
42    * Creates a new sound source object which plays the specified soundfile.
43    * You are responsible for deleting the sound source later (this will stop the
44    * sound).
45    * This function might throw exceptions. It returns 0 if no audio device is
46    * available.
47    */
48   SoundSource* create_sound_source(const std::string& filename);
49   /**
50    * Convenience function to simply play a sound at a given position.
51    */
52   void play(const std::string& name, const Vector& pos = Vector(-1, -1));
53
54   void set_listener_position(const Vector& position);
55   void set_listener_velocity(const Vector& velocity);
56
57   void enable_music(bool music_enabled);
58   void play_music(const std::string& filename, bool fade = false);
59   void stop_music(float fadetime = 0);
60
61   bool is_music_enabled() { return music_enabled; } 
62   bool is_sound_enabled() { return sound_enabled; }
63
64   bool is_audio_enabled() {
65       return (device == 0 || context == 0 ? false : true);
66   }
67
68   void update();
69
70 private:
71   friend class SoundSource;
72   friend class StreamSoundSource;
73
74   static ALuint load_file_into_buffer(SoundFile* file);
75   static ALenum get_sample_format(SoundFile* file);
76
77   void print_openal_version();
78   void check_alc_error(const char* message);
79   static void check_al_error(const char* message);
80
81   ALCdevice* device;
82   ALCcontext* context;
83   bool sound_enabled;
84
85   typedef std::map<std::string, ALuint> SoundBuffers;
86   SoundBuffers buffers;
87   typedef std::vector<SoundSource*> SoundSources;
88   SoundSources sources;
89
90   StreamSoundSource* music_source;
91
92   bool music_enabled;
93   std::string current_music;
94 };
95
96 extern SoundManager* sound_manager;
97
98 #endif
99