Massive copyright update. I'm sorry if I'm crediting Matze for something he didn...
[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
20 #ifndef __SOUND_MANAGER_H__
21 #define __SOUND_MANAGER_H__
22
23 #include <string>
24 #include <vector>
25 #include <map>
26
27 #include <AL/alc.h>
28 #include <AL/al.h>
29 #include "math/vector.hpp"
30
31 typedef void* SoundHandle;
32
33 class SoundFile;
34 class SoundSource;
35 class StreamSoundSource;
36
37 class SoundManager
38 {
39 public:
40   SoundManager();
41   virtual ~SoundManager();
42
43   void enable_sound(bool sound_enabled);
44   /**
45    * Creates a new sound source object which plays the specified soundfile.
46    * You are responsible for deleting the sound source later (this will stop the
47    * sound).
48    * This function might throw exceptions. It returns 0 if no audio device is
49    * available.
50    */
51   SoundSource* create_sound_source(const std::string& filename);
52   /**
53    * Convenience function to simply play a sound at a given position.
54    */
55   void play(const std::string& name, const Vector& pos = Vector(-1, -1));
56
57   void set_listener_position(const Vector& position);
58   void set_listener_velocity(const Vector& velocity);
59
60   void enable_music(bool music_enabled);
61   void play_music(const std::string& filename, bool fade = false);
62   void stop_music(float fadetime = 0);
63   
64   bool is_sound_enabled() { return sound_enabled; }
65
66   void update();
67
68 private:
69   friend class SoundSource;
70   friend class StreamSoundSource;
71
72   static ALuint load_file_into_buffer(SoundFile* file);
73   static ALenum get_sample_format(SoundFile* file);
74
75   void print_openal_version();
76   void check_alc_error(const char* message);
77   static void check_al_error(const char* message);
78
79   ALCdevice* device;
80   ALCcontext* context;
81   bool sound_enabled;
82
83   typedef std::map<std::string, ALuint> SoundBuffers;
84   SoundBuffers buffers;
85   typedef std::vector<SoundSource*> SoundSources;
86   SoundSources sources;
87
88   StreamSoundSource* music_source;
89
90   bool music_enabled;
91   std::string current_music;
92 };
93
94 extern SoundManager* sound_manager;
95
96 #endif
97