Minetest 5.10.0-dev
 
Loading...
Searching...
No Matches
sound_manager.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2022 DS
4Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5OpenAL support based on work by:
6Copyright (C) 2011 Sebastian 'Bahamada' Rühl
7Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits <cysoun@gmail.com>
8Copyright (C) 2011 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
9
10This program is free software; you can redistribute it and/or modify
11it under the terms of the GNU Lesser General Public License as published by
12the Free Software Foundation; either version 2.1 of the License, or
13(at your option) any later version.
14
15This program is distributed in the hope that it will be useful,
16but WITHOUT ANY WARRANTY; without even the implied warranty of
17MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18GNU Lesser General Public License for more details.
19
20You should have received a copy of the GNU Lesser General Public License along
21with this program; if not, write to the Free Software Foundation, Inc.,
2251 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23*/
24
25#pragma once
26
27#include "playing_sound.h"
28#include "al_extensions.h"
29#include "sound_constants.h"
31#include "../sound.h"
32#include "threading/thread.h"
33#include "util/container.h" // MutexedQueue
34
35namespace sound {
36
37class SoundManagerSingleton;
38
39/*
40 * The SoundManager thread
41 *
42 * It's not an ISoundManager. It doesn't allocate ids, and doesn't accept id 0.
43 * All sound loading and interaction with OpenAL happens in this thread, and in
44 * SoundManagerSingleton.
45 * Access from other threads happens via ProxySoundManager.
46 *
47 * See sound_constants.h for more details.
48 */
49
50class OpenALSoundManager final : public Thread
51{
52private:
53 std::unique_ptr<SoundFallbackPathProvider> m_fallback_path_provider;
54
55 ALCdevice *const m_device;
56 ALCcontext *const m_context;
57
59
60 // time in seconds until which removeDeadSounds will be called again
62
63 // loaded sounds
64 std::unordered_map<std::string, std::unique_ptr<ISoundDataUnopen>> m_sound_datas_unopen;
65 std::unordered_map<std::string, std::shared_ptr<ISoundDataOpen>> m_sound_datas_open;
66 // sound groups
67 std::unordered_map<std::string, std::vector<std::string>> m_sound_groups;
68
69 // currently playing sounds
70 std::unordered_map<sound_handle_t, std::shared_ptr<PlayingSound>> m_sounds_playing;
71
72 // streamed sounds
73 std::vector<std::weak_ptr<PlayingSound>> m_sounds_streaming_current_bigstep;
74 std::vector<std::weak_ptr<PlayingSound>> m_sounds_streaming_next_bigstep;
75 // time left until current bigstep finishes
77
78 std::vector<std::weak_ptr<PlayingSound>> m_sounds_fading;
79
80 // if true, all sounds will be directly paused after creation
81 bool m_is_paused = false;
82
83 // used for printing warnings only once
84 std::unordered_set<std::string> m_warned_positional_stereo_sounds;
85
86public:
87 // used for communication with ProxySoundManager
90
91private:
92 void stepStreams(f32 dtime);
93 void doFades(f32 dtime);
94
102 std::shared_ptr<ISoundDataOpen> openSingleSound(const std::string &sound_name);
103
111 std::string getLoadedSoundNameFromGroup(const std::string &group_name);
112
117 std::string getOrLoadLoadedSoundNameFromGroup(const std::string &group_name);
118
119 std::shared_ptr<PlayingSound> createPlayingSound(const std::string &sound_name,
120 bool loop, f32 volume, f32 pitch, f32 start_time,
121 const std::optional<std::pair<v3f, v3f>> &pos_vel_opt);
122
123 void playSoundGeneric(sound_handle_t id, const std::string &group_name, bool loop,
124 f32 volume, f32 fade, f32 pitch, bool use_local_fallback, f32 start_time,
125 const std::optional<std::pair<v3f, v3f>> &pos_vel_opt);
126
132 int removeDeadSounds();
133
134public:
136 std::unique_ptr<SoundFallbackPathProvider> fallback_path_provider);
137
138 ~OpenALSoundManager() override;
139
141
142private:
143 /* Similar to ISoundManager */
144
145 void step(f32 dtime);
146 void pauseAll();
147 void resumeAll();
148
149 void updateListener(const v3f &pos_, const v3f &vel_, const v3f &at_, const v3f &up_);
150 void setListenerGain(f32 gain);
151
152 bool loadSoundFile(const std::string &name, const std::string &filepath);
153 bool loadSoundData(const std::string &name, std::string &&filedata);
154 void loadSoundFileNoCheck(const std::string &name, const std::string &filepath);
155 void loadSoundDataNoCheck(const std::string &name, std::string &&filedata);
156 void addSoundToGroup(const std::string &sound_name, const std::string &group_name);
157
158 void playSound(sound_handle_t id, const SoundSpec &spec);
159 void playSoundAt(sound_handle_t id, const SoundSpec &spec, const v3f &pos_,
160 const v3f &vel_);
162 void fadeSound(sound_handle_t soundid, f32 step, f32 target_gain);
163 void updateSoundPosVel(sound_handle_t sound, const v3f &pos_, const v3f &vel_);
164
165protected:
166 /* Thread stuff */
167
168 void *run() override;
169
170private:
172 {
173 m_queue_to_proxy.push_back(std::move(msg));
174 }
175
180};
181
182} // namespace sound
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:41
Definition container.h:132
void push_back(const T &t)
Definition container.h:145
Definition thread.h:57
Definition sound_manager.h:51
void loadSoundDataNoCheck(const std::string &name, std::string &&filedata)
Definition sound_manager.cpp:393
ALCcontext *const m_context
Definition sound_manager.h:56
void playSound(sound_handle_t id, const SoundSpec &spec)
Definition sound_manager.cpp:408
int removeDeadSounds()
Deletes sounds that are dead (=finished).
Definition sound_manager.cpp:252
std::string getOrLoadLoadedSoundNameFromGroup(const std::string &group_name)
Same as getLoadedSoundNameFromGroup, but if sound does not exist, try to load from local files.
Definition sound_manager.cpp:138
std::string getLoadedSoundNameFromGroup(const std::string &group_name)
Gets a random sound name from a group.
Definition sound_manager.cpp:110
std::unordered_map< std::string, std::vector< std::string > > m_sound_groups
Definition sound_manager.h:67
f32 m_stream_timer
Definition sound_manager.h:76
MutexedQueue< SoundManagerMsgToProxy > m_queue_to_proxy
Definition sound_manager.h:89
ALCdevice *const m_device
Definition sound_manager.h:55
void loadSoundFileNoCheck(const std::string &name, const std::string &filepath)
Definition sound_manager.cpp:387
std::vector< std::weak_ptr< PlayingSound > > m_sounds_streaming_next_bigstep
Definition sound_manager.h:74
void setListenerGain(f32 gain)
Definition sound_manager.cpp:351
std::unordered_map< std::string, std::shared_ptr< ISoundDataOpen > > m_sound_datas_open
Definition sound_manager.h:65
void addSoundToGroup(const std::string &sound_name, const std::string &group_name)
Definition sound_manager.cpp:399
void fadeSound(sound_handle_t soundid, f32 step, f32 target_gain)
Definition sound_manager.cpp:432
void stepStreams(f32 dtime)
Definition sound_manager.cpp:37
void reportRemovedSound(sound_handle_t id)
Definition sound_manager.h:176
~OpenALSoundManager() override
Definition sound_manager.cpp:285
const ALExtensions m_exts
Definition sound_manager.h:58
bool m_is_paused
Definition sound_manager.h:81
f32 m_time_until_dead_removal
Definition sound_manager.h:61
void updateSoundPosVel(sound_handle_t sound, const v3f &pos_, const v3f &vel_)
Definition sound_manager.cpp:445
std::unordered_map< sound_handle_t, std::shared_ptr< PlayingSound > > m_sounds_playing
Definition sound_manager.h:70
std::shared_ptr< PlayingSound > createPlayingSound(const std::string &sound_name, bool loop, f32 volume, f32 pitch, f32 start_time, const std::optional< std::pair< v3f, v3f > > &pos_vel_opt)
Definition sound_manager.cpp:154
void step(f32 dtime)
Definition sound_manager.cpp:292
bool loadSoundData(const std::string &name, std::string &&filedata)
Definition sound_manager.cpp:377
void send(SoundManagerMsgToProxy msg)
Definition sound_manager.h:171
void playSoundAt(sound_handle_t id, const SoundSpec &spec, const v3f &pos_, const v3f &vel_)
Definition sound_manager.cpp:414
std::vector< std::weak_ptr< PlayingSound > > m_sounds_streaming_current_bigstep
Definition sound_manager.h:73
void updateListener(const v3f &pos_, const v3f &vel_, const v3f &at_, const v3f &up_)
Definition sound_manager.cpp:336
std::unique_ptr< SoundFallbackPathProvider > m_fallback_path_provider
Definition sound_manager.h:53
void playSoundGeneric(sound_handle_t id, const std::string &group_name, bool loop, f32 volume, f32 fade, f32 pitch, bool use_local_fallback, f32 start_time, const std::optional< std::pair< v3f, v3f > > &pos_vel_opt)
Definition sound_manager.cpp:195
void pauseAll()
Definition sound_manager.cpp:318
void doFades(f32 dtime)
Definition sound_manager.cpp:69
void stopSound(sound_handle_t sound)
Definition sound_manager.cpp:426
std::unordered_map< std::string, std::unique_ptr< ISoundDataUnopen > > m_sound_datas_unopen
Definition sound_manager.h:64
std::shared_ptr< ISoundDataOpen > openSingleSound(const std::string &sound_name)
Gives the open sound for a loaded sound.
Definition sound_manager.cpp:88
void * run() override
Definition sound_manager.cpp:459
std::vector< std::weak_ptr< PlayingSound > > m_sounds_fading
Definition sound_manager.h:78
OpenALSoundManager(SoundManagerSingleton *smg, std::unique_ptr< SoundFallbackPathProvider > fallback_path_provider)
Definition sound_manager.cpp:272
std::unordered_set< std::string > m_warned_positional_stereo_sounds
Definition sound_manager.h:84
bool loadSoundFile(const std::string &name, const std::string &filepath)
Definition sound_manager.cpp:363
void resumeAll()
Definition sound_manager.cpp:327
MutexedQueue< SoundManagerMsgToMgr > m_queue_to_mgr
Definition sound_manager.h:88
Class for the openal device and context.
Definition sound_singleton.h:35
int sound_handle_t
Definition client.h:75
core::vector3df v3f
Definition irr_v3d.h:26
Definition al_extensions.cpp:26
constexpr f32 REMOVE_DEAD_SOUNDS_INTERVAL
Definition sound_constants.h:102
constexpr f32 STREAM_BIGSTEP_TIME
Definition sound_constants.h:108
std::variant< std::monostate, sound_manager_messages_to_proxy::ReportRemovedSound, sound_manager_messages_to_proxy::Stopped > SoundManagerMsgToProxy
Definition sound_manager_messages.h:76
Describes the sound information for playback.
Definition sound.h:35
Struct for AL and ALC extensions.
Definition al_extensions.h:30