Luanti 5.10.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
36public:
37 PlayingSound(ALuint source_id, std::shared_ptr<ISoundDataOpen> data, bool loop,
38 f32 volume, f32 pitch, f32 start_time,
39 const std::optional<std::pair<v3f, v3f>> &pos_vel_opt,
40 const ALExtensions &exts [[maybe_unused]]);
41
42 ~PlayingSound() noexcept
43 {
44 alDeleteSources(1, &m_source_id);
45 }
46
48
49 // return false means streaming finished
50 bool stepStream(bool playback_speed_changed = false);
51
52 // retruns true if it wasn't fading already
53 bool fade(f32 step, f32 target_gain) noexcept;
54
55 // returns true if more fade is needed later
56 bool doFade(f32 dtime) noexcept;
57
58 void updatePosVel(const v3f &pos, const v3f &vel) noexcept;
59
60 void setGain(f32 gain) noexcept;
61
62 f32 getGain() noexcept;
63
64 void setPitch(f32 pitch);
65
66 bool isStreaming() const noexcept { return m_data->isStreaming(); }
67
68 void play() noexcept { alSourcePlay(m_source_id); }
69
70 // returns one of AL_INITIAL, AL_PLAYING, AL_PAUSED, AL_STOPPED
71 ALint getState() noexcept
72 {
73 ALint state;
74 alGetSourcei(m_source_id, AL_SOURCE_STATE, &state);
75 return state;
76 }
77
78 bool isDead() noexcept
79 {
80 // streaming sounds can (but should not) stop because the queue runs empty
81 return m_stopped_means_dead && getState() == AL_STOPPED;
82 }
83
84 void pause() noexcept
85 {
86 // this is a NOP if state != AL_PLAYING
87 alSourcePause(m_source_id);
88 }
89
90 void resume() noexcept
91 {
92 if (getState() == AL_PAUSED)
93 play();
94 }
95};
96
97} // 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:71
void resume() noexcept
Definition playing_sound.h:90
void play() noexcept
Definition playing_sound.h:68
bool isDead() noexcept
Definition playing_sound.h:78
std::optional< FadeState > m_fade_state
Definition playing_sound.h:34
~PlayingSound() noexcept
Definition playing_sound.h:42
bool m_is_positional
Definition playing_sound.h:32
bool isStreaming() const noexcept
Definition playing_sound.h:66
ALuint m_next_sample_pos
Definition playing_sound.h:30
void updatePosVel(const v3f &pos, const v3f &vel) noexcept
Definition playing_sound.cpp:226
bool stepStream(bool playback_speed_changed=false)
Definition playing_sound.cpp:111
bool fade(f32 step, f32 target_gain) noexcept
Definition playing_sound.cpp:176
bool m_looping
Definition playing_sound.h:31
bool doFade(f32 dtime) noexcept
Definition playing_sound.cpp:188
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:259
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:84
f32 getGain() noexcept
Definition playing_sound.cpp:249
ALuint m_source_id
Definition playing_sound.h:28
void setGain(f32 gain) noexcept
Definition playing_sound.cpp:239
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