nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
audio.h
Go to the documentation of this file.
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
18namespace nanommo {
19
20std::string MusicFolder();
21
22/// Where the recorded sounds are, beside the music.
23std::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.
27std::optional<std::pair<Samples, Samples>> LoadStereo(const std::string& path);
28
29/// The same, with its two channels as one.
30std::optional<Samples> LoadMono(const std::string& path);
31
32class 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.
37void 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.
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();
47
48 AudioDevice(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.
57class 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 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.
90class 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 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.
115class 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 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_
static absl::StatusOr< std::unique_ptr< AudioDevice > > Create()
Definition audio.cc:75
AudioDevice & operator=(const AudioDevice &)=delete
AudioDevice(const AudioDevice &)=delete
static std::unique_ptr< Clip > Load(const AudioDevice &device, const Samples &samples, int voices)
Definition audio.cc:109
void Play(float volume, float pan, float pitch)
Definition audio.cc:169
Clip(const Clip &)=delete
Clip & operator=(const Clip &)=delete
Stream(const Stream &)=delete
Stream & operator=(const Stream &)=delete
static std::unique_ptr< Stream > Open(const AudioDevice &device)
Definition audio.cc:140
void SetVolume(float volume)
From nothing to full.
Definition audio.cc:167
void Feed(const std::function< void(std::span< float >, std::span< float >)> &fill)
Definition audio.cc:152
static absl::StatusOr< std::unique_ptr< Track > > Load(const AudioDevice &device, const std::string &path)
Loading needs the device, so it takes one, even though it doesn't use it.
Definition audio.cc:85
bool playing() const
Definition audio.cc:103
void Update()
Definition audio.cc:107
Track(const Track &)=delete
void SetVolume(float volume)
From nothing to the file's own loudness.
Definition audio.cc:105
void Stop()
Definition audio.cc:101
void Play()
From the start.
Definition audio.cc:99
Track & operator=(const Track &)=delete
Definition chat.cc:9
std::vector< float > Samples
One channel of sound, at kRate.
Definition synth.h:14
std::optional< std::pair< Samples, Samples > > LoadStereo(const std::string &path)
Definition audio.cc:53
void UseRecordings(Ambience &ambience)
Definition audio.cc:37
std::string SoundsFolder()
Where the recorded sounds are, beside the music.
Definition audio.cc:49
std::optional< Samples > LoadMono(const std::string &path)
The same, with its two channels as one.
Definition audio.cc:25
std::string MusicFolder()
Definition audio.cc:21