nanoMMO 0.97.8Test coverage / Every file / client/audio.h

Created: 2026-11-26 01:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/client/audio.h
Line
Count
Source
1
#ifndef NANOMMO_CLIENT_AUDIO_H_
2
#define NANOMMO_CLIENT_AUDIO_H_
3
4
#include <raylib.h>
5
6
#include <cstddef>
7
#include <functional>
8
#include <memory>
9
#include <optional>
10
#include <span>
11
#include <string>
12
#include <utility>
13
#include <vector>
14
15
#include "absl/status/statusor.h"
16
#include "client/synth.h"
17
18
namespace nanommo {
19
20
std::string MusicFolder();
21
22
/// Where the recorded sounds are, beside the music.
23
std::string SoundsFolder();
24
25
/// A sound file's two channels, at our rate, or nothing if there's no such
26
/// file. It needs no audio device, so a film can read one too.
27
std::optional<std::pair<Samples, Samples>> LoadStereo(const std::string& path);
28
29
/// The same, with its two channels as one.
30
std::optional<Samples> LoadMono(const std::string& path);
31
32
class Ambience;
33
34
/// Gives the ambience the recordings in the sounds folder, of the rain, the
35
/// fire and a gust of wind, in place of what it makes from numbers, for each
36
/// that's there.
37
void UseRecordings(Ambience& ambience);
38
39
/// raylib's audio device, which plays our sound through the system's. Like
40
/// the window, there's one, opened after the window and closed before it.
41
class AudioDevice {
42
 public:
43
  /// The device, or why there's none, such as in a container with no sound
44
  /// card.
45
  static absl::StatusOr<std::unique_ptr<AudioDevice>> Create();
46
  ~AudioDevice();
47
48
  AudioDevice(const AudioDevice&) = delete;
49
  AudioDevice& operator=(const AudioDevice&) = delete;
50
51
 private:
52
  AudioDevice() = default;
53
};
54
55
/// A piece of music, which raylib streams from its file as it plays, a little
56
/// at a time, so that the whole song never sits decoded in memory.
57
class Track {
58
 public:
59
  /// Loading needs the device, so it takes one, even though it doesn't use it.
60
  static absl::StatusOr<std::unique_ptr<Track>> Load(const AudioDevice& device,
61
                                                     const std::string& path);
62
  ~Track();
63
64
  Track(const Track&) = delete;
65
  Track& operator=(const Track&) = delete;
66
67
  /// From the start.
68
  void Play();
69
70
  void Stop();
71
72
  bool playing() const;
73
74
  /// From nothing to the file's own loudness.
75
  void SetVolume(float volume);
76
77
  /// Refills the buffer the track plays from with what comes next in its file.
78
  /// raylib needs this each frame, or the buffer runs dry and the track goes
79
  /// quiet.
80
  void Update();
81
82
 private:
83
0
  explicit Track(Music music) : music_(music) {}
84
85
  Music music_;
86
};
87
88
/// A sound we made, ready to play, on a few voices that share its samples, so
89
/// that it can play again before it's over, as steps do.
90
class Clip {
91
 public:
92
  static std::unique_ptr<Clip> Load(const AudioDevice& device,
93
                                    const Samples& samples, int voices);
94
  ~Clip();
95
96
  Clip(const Clip&) = delete;
97
  Clip& operator=(const Clip&) = delete;
98
99
  /// On the voice that started longest ago: at a volume from 0 to 1, a pan
100
  /// from -1 for the left to 1 for the right, and a pitch, where 2 is an
101
  /// octave up.
102
  void Play(float volume, float pan, float pitch);
103
104
 private:
105
0
  explicit Clip(Sound sound) : sound_(sound) {}
106
107
  Sound sound_;
108
  /// raylib's aliases of the sound, which play its samples without copies.
109
  std::vector<Sound> voices_;
110
  std::size_t next_ = 0;
111
};
112
113
/// Sound we make as it plays, two channels a buffer at a time, for what goes
114
/// on and on, such as the rain.
115
class Stream {
116
 public:
117
  static std::unique_ptr<Stream> Open(const AudioDevice& device);
118
  ~Stream();
119
120
  Stream(const Stream&) = delete;
121
  Stream& operator=(const Stream&) = delete;
122
123
  /// Asks for more, into the left and the right, for each buffer that's
124
  /// played out. raylib needs this each frame, as a track's Update.
125
  void Feed(
126
      const std::function<void(std::span<float>, std::span<float>)>& fill);
127
128
  /// From nothing to full.
129
  void SetVolume(float volume);
130
131
 private:
132
0
  explicit Stream(AudioStream stream) : stream_(stream) {}
133
134
  AudioStream stream_;
135
  std::vector<float> left_;
136
  std::vector<float> right_;
137
  std::vector<float> both_;
138
};
139
140
}  // namespace nanommo
141
142
#endif  // NANOMMO_CLIENT_AUDIO_H_