Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
sound_manager.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2022 DS
4// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5// Copyright (C) 2011 Sebastian 'Bahamada' Rühl
6// Copyright (C) 2011 Cyriaque 'Cisoun' Skrapits <cysoun@gmail.com>
7// Copyright (C) 2011 Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
8
9#pragma once
10
11#include "playing_sound.h"
12#include "al_extensions.h"
13#include "sound_constants.h"
15#include "../sound.h"
16#include "threading/thread.h"
17#include "util/container.h" // MutexedQueue
18
19namespace sound {
20
21class SoundManagerSingleton;
22
23/*
24 * The SoundManager thread
25 *
26 * It's not an ISoundManager. It doesn't allocate ids, and doesn't accept id 0.
27 * All sound loading and interaction with OpenAL happens in this thread, and in
28 * SoundManagerSingleton.
29 * Access from other threads happens via ProxySoundManager.
30 *
31 * See sound_constants.h for more details.
32 */
33
34class OpenALSoundManager final : public Thread
35{
36private:
37 std::unique_ptr<SoundFallbackPathProvider> m_fallback_path_provider;
38
39 ALCdevice *const m_device;
40 ALCcontext *const m_context;
41
43
44 // time in seconds until which removeDeadSounds will be called again
46
47 // loaded sounds
48 std::unordered_map<std::string, std::unique_ptr<ISoundDataUnopen>> m_sound_datas_unopen;
49 std::unordered_map<std::string, std::shared_ptr<ISoundDataOpen>> m_sound_datas_open;
50 // sound groups
51 std::unordered_map<std::string, std::vector<std::string>> m_sound_groups;
52
53 // currently playing sounds
54 std::unordered_map<sound_handle_t, std::shared_ptr<PlayingSound>> m_sounds_playing;
55
56 // streamed sounds
57 std::vector<std::weak_ptr<PlayingSound>> m_sounds_streaming_current_bigstep;
58 std::vector<std::weak_ptr<PlayingSound>> m_sounds_streaming_next_bigstep;
59 // time left until current bigstep finishes
61
62 std::vector<std::weak_ptr<PlayingSound>> m_sounds_fading;
63
64 // if true, all sounds will be directly paused after creation
65 bool m_is_paused = false;
66
67 // used for printing warnings only once
68 std::unordered_set<std::string> m_warned_positional_stereo_sounds;
69
70public:
71 // used for communication with ProxySoundManager
74
75private:
76 void stepStreams(f32 dtime);
77 void doFades(f32 dtime);
78
86 std::shared_ptr<ISoundDataOpen> openSingleSound(const std::string &sound_name);
87
95 std::string getLoadedSoundNameFromGroup(const std::string &group_name);
96
101 std::string getOrLoadLoadedSoundNameFromGroup(const std::string &group_name);
102
103 std::shared_ptr<PlayingSound> createPlayingSound(const std::string &sound_name,
104 bool loop, f32 volume, f32 pitch, f32 start_time,
105 const std::optional<std::pair<v3f, v3f>> &pos_vel_opt);
106
107 void playSoundGeneric(sound_handle_t id, const std::string &group_name, bool loop,
108 f32 volume, f32 fade, f32 pitch, bool use_local_fallback, f32 start_time,
109 const std::optional<std::pair<v3f, v3f>> &pos_vel_opt);
110
116 int removeDeadSounds();
117
118public:
120 std::unique_ptr<SoundFallbackPathProvider> fallback_path_provider);
121
122 ~OpenALSoundManager() override;
123
125
126private:
127 /* Similar to ISoundManager */
128
129 void step(f32 dtime);
130 void pauseAll();
131 void resumeAll();
132
133 void updateListener(const v3f &pos_, const v3f &vel_, const v3f &at_, const v3f &up_);
134 void setListenerGain(f32 gain);
135
136 bool loadSoundFile(const std::string &name, const std::string &filepath);
137 bool loadSoundData(const std::string &name, std::string &&filedata);
138 void loadSoundFileNoCheck(const std::string &name, const std::string &filepath);
139 void loadSoundDataNoCheck(const std::string &name, std::string &&filedata);
140 void addSoundToGroup(const std::string &sound_name, const std::string &group_name);
141
142 void playSound(sound_handle_t id, const SoundSpec &spec);
143 void playSoundAt(sound_handle_t id, const SoundSpec &spec, const v3f &pos_,
144 const v3f &vel_);
146 void fadeSound(sound_handle_t soundid, f32 step, f32 target_gain);
147 void updateSoundPosVel(sound_handle_t sound, const v3f &pos_, const v3f &vel_);
148
149protected:
150 /* Thread stuff */
151
152 void *run() override;
153
154private:
156 {
157 m_queue_to_proxy.push_back(std::move(msg));
158 }
159
164};
165
166} // namespace sound
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
Definition container.h:117
void push_back(const T &t)
Definition container.h:130
Definition thread.h:57
Definition sound_manager.h:35
void loadSoundDataNoCheck(const std::string &name, std::string &&filedata)
Definition sound_manager.cpp:377
ALCcontext *const m_context
Definition sound_manager.h:40
void playSound(sound_handle_t id, const SoundSpec &spec)
Definition sound_manager.cpp:392
int removeDeadSounds()
Deletes sounds that are dead (=finished).
Definition sound_manager.cpp:236
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:122
std::string getLoadedSoundNameFromGroup(const std::string &group_name)
Gets a random sound name from a group.
Definition sound_manager.cpp:94
std::unordered_map< std::string, std::vector< std::string > > m_sound_groups
Definition sound_manager.h:51
f32 m_stream_timer
Definition sound_manager.h:60
MutexedQueue< SoundManagerMsgToProxy > m_queue_to_proxy
Definition sound_manager.h:73
ALCdevice *const m_device
Definition sound_manager.h:39
void loadSoundFileNoCheck(const std::string &name, const std::string &filepath)
Definition sound_manager.cpp:371
std::vector< std::weak_ptr< PlayingSound > > m_sounds_streaming_next_bigstep
Definition sound_manager.h:58
void setListenerGain(f32 gain)
Definition sound_manager.cpp:335
std::unordered_map< std::string, std::shared_ptr< ISoundDataOpen > > m_sound_datas_open
Definition sound_manager.h:49
void addSoundToGroup(const std::string &sound_name, const std::string &group_name)
Definition sound_manager.cpp:383
void fadeSound(sound_handle_t soundid, f32 step, f32 target_gain)
Definition sound_manager.cpp:416
void stepStreams(f32 dtime)
Definition sound_manager.cpp:21
void reportRemovedSound(sound_handle_t id)
Definition sound_manager.h:160
~OpenALSoundManager() override
Definition sound_manager.cpp:269
const ALExtensions m_exts
Definition sound_manager.h:42
bool m_is_paused
Definition sound_manager.h:65
f32 m_time_until_dead_removal
Definition sound_manager.h:45
void updateSoundPosVel(sound_handle_t sound, const v3f &pos_, const v3f &vel_)
Definition sound_manager.cpp:429
std::unordered_map< sound_handle_t, std::shared_ptr< PlayingSound > > m_sounds_playing
Definition sound_manager.h:54
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:138
void step(f32 dtime)
Definition sound_manager.cpp:276
bool loadSoundData(const std::string &name, std::string &&filedata)
Definition sound_manager.cpp:361
void send(SoundManagerMsgToProxy msg)
Definition sound_manager.h:155
void playSoundAt(sound_handle_t id, const SoundSpec &spec, const v3f &pos_, const v3f &vel_)
Definition sound_manager.cpp:398
std::vector< std::weak_ptr< PlayingSound > > m_sounds_streaming_current_bigstep
Definition sound_manager.h:57
void updateListener(const v3f &pos_, const v3f &vel_, const v3f &at_, const v3f &up_)
Definition sound_manager.cpp:320
std::unique_ptr< SoundFallbackPathProvider > m_fallback_path_provider
Definition sound_manager.h:37
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:179
void pauseAll()
Definition sound_manager.cpp:302
void doFades(f32 dtime)
Definition sound_manager.cpp:53
void stopSound(sound_handle_t sound)
Definition sound_manager.cpp:410
std::unordered_map< std::string, std::unique_ptr< ISoundDataUnopen > > m_sound_datas_unopen
Definition sound_manager.h:48
std::shared_ptr< ISoundDataOpen > openSingleSound(const std::string &sound_name)
Gives the open sound for a loaded sound.
Definition sound_manager.cpp:72
void * run() override
Definition sound_manager.cpp:443
std::vector< std::weak_ptr< PlayingSound > > m_sounds_fading
Definition sound_manager.h:62
OpenALSoundManager(SoundManagerSingleton *smg, std::unique_ptr< SoundFallbackPathProvider > fallback_path_provider)
Definition sound_manager.cpp:256
std::unordered_set< std::string > m_warned_positional_stereo_sounds
Definition sound_manager.h:68
bool loadSoundFile(const std::string &name, const std::string &filepath)
Definition sound_manager.cpp:347
void resumeAll()
Definition sound_manager.cpp:311
MutexedQueue< SoundManagerMsgToMgr > m_queue_to_mgr
Definition sound_manager.h:72
Class for the openal device and context.
Definition sound_singleton.h:20
int sound_handle_t
Definition client.h:62
core::vector3df v3f
Definition irr_v3d.h:11
Definition al_extensions.cpp:11
constexpr f32 REMOVE_DEAD_SOUNDS_INTERVAL
Definition sound_constants.h:87
constexpr f32 STREAM_BIGSTEP_TIME
Definition sound_constants.h:93
std::variant< std::monostate, sound_manager_messages_to_proxy::ReportRemovedSound, sound_manager_messages_to_proxy::Stopped > SoundManagerMsgToProxy
Definition sound_manager_messages.h:61
Describes the sound information for playback.
Definition sound.h:20
Struct for AL and ALC extensions.
Definition al_extensions.h:15