Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
sound.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irr_v3d.h"
8#include "config.h"
9#include <limits>
10#include <string>
11#include <unordered_map>
12#include <unordered_set>
13#include <vector>
14
15#if !IS_CLIENT_BUILD
16#error Do not include in server builds
17#endif
18
19struct SoundSpec;
20
22{
23public:
24 virtual ~SoundFallbackPathProvider() = default;
25 std::vector<std::string> getLocalFallbackPathsForSoundname(const std::string &name);
26protected:
27 virtual void addThePaths(const std::string &name, std::vector<std::string> &paths);
28 // adds <common>.ogg, <common>.1.ogg, ..., <common>.9.ogg to paths
29 void addAllAlternatives(const std::string &common, std::vector<std::string> &paths);
30private:
31 std::unordered_set<std::string> m_done_names;
32};
33
34
41using sound_handle_t = int;
42
43constexpr sound_handle_t SOUND_HANDLE_T_MAX = std::numeric_limits<sound_handle_t>::max();
44
46{
47private:
48 std::unordered_map<sound_handle_t, u32> m_occupied_ids;
50 std::vector<sound_handle_t> m_removed_sounds;
51
52protected:
54
55public:
56 virtual ~ISoundManager() = default;
57
63 virtual void step(f32 dtime) = 0;
67 virtual void pauseAll() = 0;
71 virtual void resumeAll() = 0;
72
79 virtual void updateListener(const v3f &pos, const v3f &vel, const v3f &at,
80 const v3f &up) = 0;
81 virtual void setListenerGain(f32 gain) = 0;
82
90 virtual bool loadSoundFile(const std::string &name, const std::string &filepath) = 0;
94 virtual bool loadSoundData(const std::string &name, std::string &&filedata) = 0;
101 virtual void addSoundToGroup(const std::string &sound_name,
102 const std::string &group_name) = 0;
103
108 virtual void playSound(sound_handle_t id, const SoundSpec &spec) = 0;
114 virtual void playSoundAt(sound_handle_t id, const SoundSpec &spec, const v3f &pos,
115 const v3f &vel) = 0;
120 virtual void stopSound(sound_handle_t sound) = 0;
121 virtual void fadeSound(sound_handle_t sound, f32 step, f32 target_gain) = 0;
127 virtual void updateSoundPosVel(sound_handle_t sound, const v3f &pos,
128 const v3f &vel) = 0;
129
133 std::vector<sound_handle_t> pollRemovedSounds()
134 {
135 std::vector<sound_handle_t> ret;
136 std::swap(m_removed_sounds, ret);
137 return ret;
138 }
139
146 sound_handle_t allocateId(u32 num_owners);
147
153 void freeId(sound_handle_t id, u32 num_owners = 1);
154};
155
157{
158public:
159 void step(f32 dtime) override {}
160 void pauseAll() override {}
161 void resumeAll() override {}
162
163 void updateListener(const v3f &pos, const v3f &vel, const v3f &at, const v3f &up) override {}
164 void setListenerGain(f32 gain) override {}
165
166 bool loadSoundFile(const std::string &name, const std::string &filepath) override { return true; }
167 bool loadSoundData(const std::string &name, std::string &&filedata) override { return true; }
168 void addSoundToGroup(const std::string &sound_name, const std::string &group_name) override {};
169
170 void playSound(sound_handle_t id, const SoundSpec &spec) override { reportRemovedSound(id); }
171 void playSoundAt(sound_handle_t id, const SoundSpec &spec, const v3f &pos,
172 const v3f &vel) override { reportRemovedSound(id); }
174 void fadeSound(sound_handle_t sound, f32 step, f32 target_gain) override {}
175 void updateSoundPosVel(sound_handle_t sound, const v3f &pos, const v3f &vel) override {}
176};
177
182void sound_volume_control(ISoundManager *sound_mgr, bool is_window_active);
Definition sound.h:157
void playSound(sound_handle_t id, const SoundSpec &spec) override
Plays a random sound from a sound group (position-less).
Definition sound.h:170
void addSoundToGroup(const std::string &sound_name, const std::string &group_name) override
Adds sound with name sound_name to group group_name.
Definition sound.h:168
void pauseAll() override
Pause all sound playback.
Definition sound.h:160
void stopSound(sound_handle_t sound) override
Request the sound to be stopped.
Definition sound.h:173
void step(f32 dtime) override
Removes finished sounds, steps streamed sounds, and does similar tasks.
Definition sound.h:159
void fadeSound(sound_handle_t sound, f32 step, f32 target_gain) override
Definition sound.h:174
bool loadSoundData(const std::string &name, std::string &&filedata) override
Same as loadSoundFile, but reads the OggVorbis file from memory.
Definition sound.h:167
void resumeAll() override
Resume sound playback after pause.
Definition sound.h:161
bool loadSoundFile(const std::string &name, const std::string &filepath) override
Adds a sound to load from a file (only OggVorbis).
Definition sound.h:166
void updateSoundPosVel(sound_handle_t sound, const v3f &pos, const v3f &vel) override
Update position and velocity of positional sound.
Definition sound.h:175
void updateListener(const v3f &pos, const v3f &vel, const v3f &at, const v3f &up) override
Definition sound.h:163
void playSoundAt(sound_handle_t id, const SoundSpec &spec, const v3f &pos, const v3f &vel) override
Same as playSound, but at a position.
Definition sound.h:171
void setListenerGain(f32 gain) override
Definition sound.h:164
Definition sound.h:46
virtual void pauseAll()=0
Pause all sound playback.
virtual void playSound(sound_handle_t id, const SoundSpec &spec)=0
Plays a random sound from a sound group (position-less).
virtual void addSoundToGroup(const std::string &sound_name, const std::string &group_name)=0
Adds sound with name sound_name to group group_name.
std::unordered_map< sound_handle_t, u32 > m_occupied_ids
Definition sound.h:48
virtual ~ISoundManager()=default
virtual void step(f32 dtime)=0
Removes finished sounds, steps streamed sounds, and does similar tasks.
std::vector< sound_handle_t > pollRemovedSounds()
Get and reset the list of sounds that were stopped.
Definition sound.h:133
virtual void updateListener(const v3f &pos, const v3f &vel, const v3f &at, const v3f &up)=0
virtual void resumeAll()=0
Resume sound playback after pause.
std::vector< sound_handle_t > m_removed_sounds
Definition sound.h:50
sound_handle_t m_next_id
Definition sound.h:49
virtual void setListenerGain(f32 gain)=0
virtual void stopSound(sound_handle_t sound)=0
Request the sound to be stopped.
virtual void updateSoundPosVel(sound_handle_t sound, const v3f &pos, const v3f &vel)=0
Update position and velocity of positional sound.
void reportRemovedSound(sound_handle_t id)
Definition sound.cpp:53
void freeId(sound_handle_t id, u32 num_owners=1)
Free an id allocated via allocateId.
Definition sound.cpp:74
virtual bool loadSoundData(const std::string &name, std::string &&filedata)=0
Same as loadSoundFile, but reads the OggVorbis file from memory.
sound_handle_t allocateId(u32 num_owners)
Returns a positive id.
Definition sound.cpp:62
virtual void playSoundAt(sound_handle_t id, const SoundSpec &spec, const v3f &pos, const v3f &vel)=0
Same as playSound, but at a position.
virtual bool loadSoundFile(const std::string &name, const std::string &filepath)=0
Adds a sound to load from a file (only OggVorbis).
virtual void fadeSound(sound_handle_t sound, f32 step, f32 target_gain)=0
Definition sound.h:22
std::vector< std::string > getLocalFallbackPathsForSoundname(const std::string &name)
Definition sound.cpp:17
virtual ~SoundFallbackPathProvider()=default
std::unordered_set< std::string > m_done_names
Definition sound.h:31
virtual void addThePaths(const std::string &name, std::vector< std::string > &paths)
Definition sound.cpp:46
void addAllAlternatives(const std::string &common, std::vector< std::string > &paths)
Definition sound.cpp:36
constexpr sound_handle_t SOUND_HANDLE_T_MAX
Definition sound.h:43
void sound_volume_control(ISoundManager *sound_mgr, bool is_window_active)
A helper function to control sound volume based on some values: sound volume settings,...
Definition sound.cpp:85
int sound_handle_t
IDs for playing sounds.
Definition sound.h:41
int sound_handle_t
Definition client.h:62
core::vector3df v3f
Definition irr_v3d.h:11
Definition al_extensions.cpp:11
Describes the sound information for playback.
Definition sound.h:20