Minetest 5.10.0-dev
 
Loading...
Searching...
No Matches
sound_constants.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2022 DS
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18*/
19
20#pragma once
21
22/*
23 *
24 * The coordinate space for sounds (sound-space):
25 * ----------------------------------------------
26 *
27 * * The functions from ISoundManager (see sound.h) take spatial vectors in node-space.
28 * * All other `v3f`s here are, if not told otherwise, in sound-space, which is
29 * defined as node-space mirrored along the x-axis.
30 * (This is needed because OpenAL uses a right-handed coordinate system.)
31 * * Use `swap_handedness()` from `al_helpers.h` to convert between those two
32 * coordinate spaces.
33 *
34 *
35 * How sounds are loaded:
36 * ----------------------
37 *
38 * * Step 1:
39 * `loadSoundFile` or `loadSoundFile` is called. This adds an unopen sound with
40 * the given name to `m_sound_datas_unopen`.
41 * Unopen / lazy sounds (`ISoundDataUnopen`) are ogg-vorbis files that we did not yet
42 * start to decode. (Decoding an unopen sound does not fail under normal circumstances
43 * (because we check whether the file exists at least), if it does fail anyways,
44 * we should notify the user.)
45 * * Step 2:
46 * `addSoundToGroup` is called, to add the name from step 1 to a group. If the
47 * group does not yet exist, a new one is created. A group can later be played.
48 * (The mapping is stored in `m_sound_groups`.)
49 * * Step 3:
50 * `playSound` or `playSoundAt` is called.
51 * * Step 3.1:
52 * If the group with the name `spec.name` does not exist, and `spec.use_local_fallback`
53 * is true, a new group is created using the user's sound-pack.
54 * * Step 3.2:
55 * We choose one random sound name from the given group.
56 * * Step 3.3:
57 * We open the sound (see `openSingleSound`).
58 * If the sound is already open (in `m_sound_datas_open`), we take that one.
59 * Otherwise we open it by calling `ISoundDataUnopen::open`. We choose (by
60 * sound length), whether it's a single-buffer (`SoundDataOpenBuffer`) or
61 * streamed (`SoundDataOpenStream`) sound.
62 * Single-buffer sounds are always completely loaded. Streamed sounds can be
63 * partially loaded.
64 * The sound is erased from `m_sound_datas_unopen` and added to `m_sound_datas_open`.
65 * Open sounds are kept forever.
66 * * Step 3.4:
67 * We create the new `PlayingSound`. It has a `shared_ptr` to its open sound.
68 * If the open sound is streaming, the playing sound needs to be stepped using
69 * `PlayingSound::stepStream` for enqueuing buffers. For this purpose, the sound
70 * is added to `m_sounds_streaming` (as `weak_ptr`).
71 * If the sound is fading, it is added to `m_sounds_fading` for regular fade-stepping.
72 * The sound is also added to `m_sounds_playing`, so that one can access it
73 * via its sound handle.
74 * * Step 4:
75 * Streaming sounds are updated. For details see [Streaming of sounds].
76 * * Step 5:
77 * At deinitialization, we can just let the destructors do their work.
78 * Sound sources are deleted (and with this also stopped) by ~PlayingSound.
79 * Buffers can't be deleted while sound sources using them exist, because
80 * PlayingSound has a shared_ptr to its ISoundData.
81 *
82 *
83 * Streaming of sounds:
84 * --------------------
85 *
86 * In each "bigstep", all streamed sounds are stepStream()ed. This means a
87 * sound can be stepped at any point in time in the bigstep's interval.
88 *
89 * In the worst case, a sound is stepped at the start of one bigstep and in the
90 * end of the next bigstep. So between two stepStream()-calls lie at most
91 * 2 * STREAM_BIGSTEP_TIME seconds.
92 * We ensure that there are always enough untouched full buffers left such that
93 * we do not run into an empty queue in this time period, see stepStream().
94 *
95 */
96
97namespace sound {
98
99// constants
100
101// in seconds
102constexpr f32 REMOVE_DEAD_SOUNDS_INTERVAL = 2.0f;
103// maximum length in seconds that a sound can have without being streamed
104constexpr f32 SOUND_DURATION_MAX_SINGLE = 3.0f;
105// minimum time in seconds of a single buffer in a streamed sound
106constexpr f32 MIN_STREAM_BUFFER_LENGTH = 1.0f;
107// duration in seconds of one bigstep
108constexpr f32 STREAM_BIGSTEP_TIME = 0.3f;
109// step duration for the OpenALSoundManager thread, in seconds
110constexpr f32 SOUNDTHREAD_DTIME = 0.016f;
111
113 "There's no benefit in streaming if we can't queue more than 2 buffers.");
114
115} // namespace sound
Definition al_extensions.cpp:26
constexpr f32 MIN_STREAM_BUFFER_LENGTH
Definition sound_constants.h:106
constexpr f32 REMOVE_DEAD_SOUNDS_INTERVAL
Definition sound_constants.h:102
constexpr f32 STREAM_BIGSTEP_TIME
Definition sound_constants.h:108
constexpr f32 SOUND_DURATION_MAX_SINGLE
Definition sound_constants.h:104
constexpr f32 SOUNDTHREAD_DTIME
Definition sound_constants.h:110