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