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 final : public Currenton<SoundManager> 37 { 38 friend class OpenALSoundSource; 39 friend class StreamSoundSource; 40 41 private: 42 static ALuint load_file_into_buffer(SoundFile& file); 43 static ALenum get_sample_format(const SoundFile& file); 44 45 static void print_openal_version(); 46 static void check_al_error(const char* message); 47 48 public: 49 SoundManager(); 50 ~SoundManager() override; 51 52 void enable_sound(bool sound_enabled); 53 54 /** Creates a new sound source object which plays the specified 55 soundfile. You are responsible for deleting the sound source 56 later (this will stop the sound). 57 This function never throws exceptions, but might return a DummySoundSource */ 58 std::unique_ptr<SoundSource> create_sound_source(const std::string& filename); 59 60 /** Convenience functions to simply play a sound at a given position. */ 61 void play(const std::string& name, const Vector& pos = Vector(-1, -1), 62 const float gain = 0.5f); play(const std::string & name,const float gain)63 void play(const std::string& name, const float gain) 64 { 65 play(name, Vector(-1, -1), gain); 66 } 67 68 69 /** Adds the source to the list of managed sources (= the source gets deleted 70 when it finished playing) */ 71 void manage_source(std::unique_ptr<SoundSource> source); 72 73 /** preloads a sound, so that you don't get a lag later when playing it */ 74 void preload(const std::string& name); 75 76 void set_listener_position(const Vector& position); 77 void set_listener_velocity(const Vector& velocity); 78 void set_listener_orientation(const Vector& at, const Vector& up); 79 80 void enable_music(bool music_enabled); 81 void play_music(const std::string& filename, float fadetime); 82 void play_music(const std::string& filename, bool fade = false); 83 void pause_music(float fadetime = 0); 84 void resume_music(float fadetime = 0); 85 void stop_music(float fadetime = 0); 86 void set_music_volume(int volume); 87 88 void pause_sounds(); 89 void resume_sounds(); 90 void stop_sounds(); 91 void set_sound_volume(int volume); 92 is_music_enabled() const93 bool is_music_enabled() const { return m_music_enabled; } is_sound_enabled() const94 bool is_sound_enabled() const { return m_sound_enabled; } 95 is_audio_enabled() const96 bool is_audio_enabled() const { return m_device != nullptr && m_context != nullptr; } get_current_music() const97 std::string get_current_music() const { return m_current_music; } 98 void update(); 99 100 /** Tell soundmanager to call update() for stream_sound_source. */ 101 void register_for_update(StreamSoundSource* sss); 102 103 /** Unsubscribe from updates for stream_sound_source. */ 104 void remove_from_update(StreamSoundSource* sss); 105 106 private: 107 /** creates a new sound source, might throw exceptions, never returns nullptr */ 108 std::unique_ptr<OpenALSoundSource> intern_create_sound_source(const std::string& filename); 109 110 void check_alc_error(const char* message) const; 111 112 private: 113 ALCdevice* m_device; 114 ALCcontext* m_context; 115 bool m_sound_enabled; 116 int m_sound_volume; 117 118 std::map<std::string, ALuint> m_buffers; 119 std::vector<std::unique_ptr<OpenALSoundSource> > m_sources; 120 121 std::vector<StreamSoundSource*> m_update_list; 122 123 std::unique_ptr<StreamSoundSource> m_music_source; 124 125 bool m_music_enabled; 126 int m_music_volume; 127 std::string m_current_music; 128 129 private: 130 SoundManager(const SoundManager&) = delete; 131 SoundManager& operator=(const SoundManager&) = delete; 132 }; 133 134 #endif 135 136 /* EOF */ 137