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