Fade out and pause music on death and resume on restart of level, fixes #1064
[supertux.git] / src / audio / sound_manager.hpp
1 //  SuperTux
2 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17 #ifndef HEADER_SUPERTUX_AUDIO_SOUND_MANAGER_HPP
18 #define HEADER_SUPERTUX_AUDIO_SOUND_MANAGER_HPP
19
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24
25 #include <al.h>
26 #include <alc.h>
27
28 #include "math/vector.hpp"
29 #include "util/currenton.hpp"
30
31 class SoundFile;
32 class SoundSource;
33 class StreamSoundSource;
34 class OpenALSoundSource;
35
36 class SoundManager : public Currenton<SoundManager>
37 {
38 public:
39   SoundManager();
40   virtual ~SoundManager();
41
42   void enable_sound(bool sound_enabled);
43   /**
44    * Creates a new sound source object which plays the specified soundfile.
45    * You are responsible for deleting the sound source later (this will stop the
46    * sound).
47    * This function never throws exceptions, but might return a DummySoundSource
48    */
49   std::unique_ptr<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(std::unique_ptr<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 pause_music(float fadetime = 0);
68   void resume_music(float fadetime = 0);
69   void stop_music(float fadetime = 0);
70
71   bool is_music_enabled() { return music_enabled; }
72   bool is_sound_enabled() { return sound_enabled; }
73
74   bool is_audio_enabled() {
75     return device != 0 && context != 0;
76   }
77
78   void update();
79
80   /*
81    * Tell soundmanager to call update() for stream_sound_source.
82    */
83   void register_for_update( StreamSoundSource* sss );
84   /*
85    * Unsubscribe from updates for stream_sound_source.
86    */
87   void remove_from_update( StreamSoundSource* sss );
88
89 private:
90   friend class OpenALSoundSource;
91   friend class StreamSoundSource;
92
93   /** creates a new sound source, might throw exceptions, never returns NULL */
94   std::unique_ptr<OpenALSoundSource> intern_create_sound_source(const std::string& filename);
95   static ALuint load_file_into_buffer(SoundFile& file);
96   static ALenum get_sample_format(const SoundFile& file);
97
98   void print_openal_version();
99   void check_alc_error(const char* message);
100   static void check_al_error(const char* message);
101
102   ALCdevice* device;
103   ALCcontext* context;
104   bool sound_enabled;
105
106   typedef std::map<std::string, ALuint> SoundBuffers;
107   SoundBuffers buffers;
108   typedef std::vector<std::unique_ptr<OpenALSoundSource> > SoundSources;
109   SoundSources sources;
110
111   typedef std::vector<StreamSoundSource*> StreamSoundSources;
112   StreamSoundSources update_list;
113
114   std::unique_ptr<StreamSoundSource> music_source;
115
116   bool music_enabled;
117   std::string current_music;
118
119 private:
120   SoundManager(const SoundManager&);
121   SoundManager& operator=(const SoundManager&);
122 };
123
124 #endif
125
126 /* EOF */