fix cr/lfs and remove trailing whitespaces...
[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 class OpenALSoundSource;
34
35 class SoundManager
36 {
37 public:
38   SoundManager();
39   virtual ~SoundManager();
40
41   void enable_sound(bool sound_enabled);
42   /**
43    * Creates a new sound source object which plays the specified soundfile.
44    * You are responsible for deleting the sound source later (this will stop the
45    * sound).
46    * This function might throw exceptions. It returns 0 if no audio device is
47    * available.
48    */
49   SoundSource* create_sound_source(const std::string& filename);
50   /**
51    * Convenience function to simply play a sound at a given position.
52    */
53   void play(const std::string& name, const Vector& pos = Vector(-1, -1));
54   /**
55    * Adds the source to the list of managed sources (= the source gets deleted
56    * when it finished playing)
57    */
58   void manage_source(SoundSource* source);
59   /// preloads a sound, so that you don't get a lag later when playing it
60   void preload(const std::string& name);
61
62   void set_listener_position(const Vector& position);
63   void set_listener_velocity(const Vector& velocity);
64
65   void enable_music(bool music_enabled);
66   void play_music(const std::string& filename, bool fade = false);
67   void stop_music(float fadetime = 0);
68
69   bool is_music_enabled() { return music_enabled; }
70   bool is_sound_enabled() { return sound_enabled; }
71
72   bool is_audio_enabled() {
73       return (device == 0 || context == 0 ? false : true);
74   }
75
76   void update();
77
78   /*
79    * Tell soundmanager to call update() for stream_sound_source.
80    */
81   void register_for_update( StreamSoundSource* sss );
82   /*
83    * Unsubscribe from updates for stream_sound_source.
84    */
85   void remove_from_update( StreamSoundSource* sss );
86
87 private:
88   friend class OpenALSoundSource;
89   friend class StreamSoundSource;
90
91   static ALuint load_file_into_buffer(SoundFile* file);
92   static ALenum get_sample_format(SoundFile* file);
93
94   void print_openal_version();
95   void check_alc_error(const char* message);
96   static void check_al_error(const char* message);
97
98   ALCdevice* device;
99   ALCcontext* context;
100   bool sound_enabled;
101
102   typedef std::map<std::string, ALuint> SoundBuffers;
103   SoundBuffers buffers;
104   typedef std::vector<OpenALSoundSource*> SoundSources;
105   SoundSources sources;
106
107   typedef std::vector<StreamSoundSource*> StreamSoundSources;
108   StreamSoundSources update_list;
109
110   StreamSoundSource* music_source;
111
112   bool music_enabled;
113   std::string current_music;
114 };
115
116 extern SoundManager* sound_manager;
117
118 #endif