2 // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
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.
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.
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/>.
17 #ifndef HEADER_SUPERTUX_AUDIO_SOUND_MANAGER_HPP
18 #define HEADER_SUPERTUX_AUDIO_SOUND_MANAGER_HPP
28 #include "math/vector.hpp"
32 class StreamSoundSource;
33 class OpenALSoundSource;
39 virtual ~SoundManager();
41 void enable_sound(bool sound_enabled);
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
46 * This function never throws exceptions, but might return a DummySoundSource
48 std::unique_ptr<SoundSource> create_sound_source(const std::string& filename);
50 * Convenience function to simply play a sound at a given position.
52 void play(const std::string& name, const Vector& pos = Vector(-1, -1));
54 * Adds the source to the list of managed sources (= the source gets deleted
55 * when it finished playing)
57 void manage_source(std::unique_ptr<SoundSource> source);
58 /// preloads a sound, so that you don't get a lag later when playing it
59 void preload(const std::string& name);
61 void set_listener_position(const Vector& position);
62 void set_listener_velocity(const Vector& velocity);
64 void enable_music(bool music_enabled);
65 void play_music(const std::string& filename, bool fade = false);
66 void stop_music(float fadetime = 0);
68 bool is_music_enabled() { return music_enabled; }
69 bool is_sound_enabled() { return sound_enabled; }
71 bool is_audio_enabled() {
72 return device != 0 && context != 0;
78 * Tell soundmanager to call update() for stream_sound_source.
80 void register_for_update( StreamSoundSource* sss );
82 * Unsubscribe from updates for stream_sound_source.
84 void remove_from_update( StreamSoundSource* sss );
87 friend class OpenALSoundSource;
88 friend class StreamSoundSource;
90 /** creates a new sound source, might throw exceptions, never returns NULL */
91 std::unique_ptr<OpenALSoundSource> intern_create_sound_source(const std::string& filename);
92 static ALuint load_file_into_buffer(SoundFile& file);
93 static ALenum get_sample_format(const SoundFile& file);
95 void print_openal_version();
96 void check_alc_error(const char* message);
97 static void check_al_error(const char* message);
103 typedef std::map<std::string, ALuint> SoundBuffers;
104 SoundBuffers buffers;
105 typedef std::vector<std::unique_ptr<OpenALSoundSource> > SoundSources;
106 SoundSources sources;
108 typedef std::vector<StreamSoundSource*> StreamSoundSources;
109 StreamSoundSources update_list;
111 std::unique_ptr<StreamSoundSource> music_source;
114 std::string current_music;
117 SoundManager(const SoundManager&);
118 SoundManager& operator=(const SoundManager&);