Luanti 5.17.0-dev
Loading...
Searching...
No Matches
playing_sound.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 "sound_data.h"
12namespace sound { struct ALExtensions; }
13
14namespace sound {
15
21class PlayingSound final
22{
23 struct FadeState {
24 f32 step;
26 };
27
29 std::shared_ptr<ISoundDataOpen> m_data;
34 std::optional<FadeState> m_fade_state = std::nullopt;
35 u64 m_creation_time_s; // timestamp from porting::getTimeS()
36
37public:
38 PlayingSound(ALuint source_id, std::shared_ptr<ISoundDataOpen> data, bool loop,
39 f32 volume, f32 pitch, f32 start_time,
40 const std::optional<std::pair<v3f, v3f>> &pos_vel_opt,
41 const ALExtensions &exts [[maybe_unused]]);
42
43 ~PlayingSound() noexcept
44 {
45 alDeleteSources(1, &m_source_id);
46 }
47
49
50 // return false means streaming finished
51 bool stepStream(bool playback_speed_changed = false);
52
53 // returns true if it wasn't fading already
54 bool fade(f32 step, f32 target_gain) noexcept;
55
56 // returns true if more fade is needed later
57 bool doFade(f32 dtime) noexcept;
58
59 void updatePosVel(const v3f &pos, const v3f &vel) noexcept;
60
61 void setGain(f32 gain) noexcept;
62
63 f32 getGain() noexcept;
64
65 void setPitch(f32 pitch);
66
67 bool isStreaming() const noexcept { return m_data->isStreaming(); }
68
69 void play() noexcept { alSourcePlay(m_source_id); }
70
71 // returns one of AL_INITIAL, AL_PLAYING, AL_PAUSED, AL_STOPPED
72 ALint getState() noexcept
73 {
74 ALint state;
75 alGetSourcei(m_source_id, AL_SOURCE_STATE, &state);
76 return state;
77 }
78
79 bool isDead() noexcept
80 {
81 // streaming sounds can (but should not) stop because the queue runs empty
82 return m_stopped_means_dead && getState() == AL_STOPPED;
83 }
84
85 void pause() noexcept
86 {
87 // this is a NOP if state != AL_PLAYING
88 alSourcePause(m_source_id);
89 }
90
91 void resume() noexcept
92 {
93 if (getState() == AL_PAUSED)
94 play();
95 }
96
97 std::string getLoggingName() const;
98
99 u64 getCreationTimeS() const { return m_creation_time_s; }
100};
101
102} // namespace sound
#define DISABLE_CLASS_COPY(C)
Definition basic_macros.h:26
A sound that is currently played.
Definition playing_sound.h:22
ALint getState() noexcept
Definition playing_sound.h:72
void resume() noexcept
Definition playing_sound.h:91
void play() noexcept
Definition playing_sound.h:69
bool isDead() noexcept
Definition playing_sound.h:79
std::optional< FadeState > m_fade_state
Definition playing_sound.h:34
~PlayingSound() noexcept
Definition playing_sound.h:43
std::string getLoggingName() const
Definition playing_sound.cpp:270
bool m_is_positional
Definition playing_sound.h:32
bool isStreaming() const noexcept
Definition playing_sound.h:67
ALuint m_next_sample_pos
Definition playing_sound.h:30
void updatePosVel(const v3f &pos, const v3f &vel) noexcept
Definition playing_sound.cpp:230
bool stepStream(bool playback_speed_changed=false)
Definition playing_sound.cpp:115
u64 m_creation_time_s
Definition playing_sound.h:35
u64 getCreationTimeS() const
Definition playing_sound.h:99
bool fade(f32 step, f32 target_gain) noexcept
Definition playing_sound.cpp:180
bool m_looping
Definition playing_sound.h:31
bool doFade(f32 dtime) noexcept
Definition playing_sound.cpp:192
bool m_stopped_means_dead
Definition playing_sound.h:33
std::shared_ptr< ISoundDataOpen > m_data
Definition playing_sound.h:29
void setPitch(f32 pitch)
Definition playing_sound.cpp:263
PlayingSound(ALuint source_id, std::shared_ptr< ISoundDataOpen > data, bool loop, f32 volume, f32 pitch, f32 start_time, const std::optional< std::pair< v3f, v3f > > &pos_vel_opt, const ALExtensions &exts)
Definition playing_sound.cpp:19
void pause() noexcept
Definition playing_sound.h:85
f32 getGain() noexcept
Definition playing_sound.cpp:253
ALuint m_source_id
Definition playing_sound.h:28
void setGain(f32 gain) noexcept
Definition playing_sound.cpp:243
core::vector3df v3f
Definition irr_v3d.h:11
Definition al_extensions.cpp:11
Struct for AL and ALC extensions.
Definition al_extensions.h:15
Definition playing_sound.h:23
f32 step
Definition playing_sound.h:24
f32 target_gain
Definition playing_sound.h:25