nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
synth.h
Go to the documentation of this file.
1#ifndef NANOMMO_CLIENT_SYNTH_H_
2#define NANOMMO_CLIENT_SYNTH_H_
3
4#include <cstdint>
5#include <vector>
6
7namespace nanommo {
8
9/// Sound, as numbers: where the speaker's cone is, from -1 to 1, this many
10/// times a second.
11constexpr int kRate = 44100;
12
13/// One channel of sound, at kRate.
14using Samples = std::vector<float>;
15
16/// Random numbers, which a seed decides, so that a sound made again from its
17/// seed comes out as it did before.
18class Noise {
19 public:
20 explicit Noise(uint32_t seed);
21
22 /// From 0 to 1.
23 float Uniform();
24
25 /// From low to high.
26 float Between(float low, float high);
27
28 /// From -1 to 1: white noise, a sample at a time.
29 float Next() { return Between(-1.0f, 1.0f); }
30
31 private:
32 uint32_t state_;
33};
34
35/// A filter that passes what's below a frequency, around it, or above it, all
36/// three at once, from two running sums of what came through. The frequency
37/// can change from one sample to the next, so that a sound can sweep.
38class Filter {
39 public:
40 struct Out {
41 float low;
42 float band;
43 float high;
44 };
45
46 /// One sample through. The higher the q, the narrower the band, and the
47 /// longer it rings.
48 Out Run(float in, float hertz, float q = 0.7f);
49
50 private:
51 float sum1_ = 0.0f;
52 float sum2_ = 0.0f;
53};
54
55/// A sine wave whose frequency can glide without a click, as it keeps where it
56/// is in its turn.
58 public:
59 float Next(float hertz);
60
61 private:
62 /// In turns.
63 float phase_ = 0.0f;
64};
65
66/// How loud a sound is, t seconds in: up from nothing over attack, then down
67/// by about two thirds each decay seconds.
68float Envelope(float t, float attack, float decay);
69
70/// The seconds, as a count of samples.
71int SamplesIn(float seconds);
72
73/// Adds the sound into the mix, starting at that time and at that gain, making
74/// the mix longer, if it needs to be.
75void Mix(Samples& mix, const Samples& sound, float at, float gain = 1.0f);
76
77/// The loudest sample, whichever way it goes.
78float Peak(const Samples& samples);
79
80/// Louder or quieter, so that the loudest sample is at the peak.
81void Normalize(Samples& samples, float peak);
82
83/// Brings the last few milliseconds down to nothing, as a sound cut off
84/// mid-wave clicks.
85void FadeOut(Samples& samples);
86
87/// Rounds off the loudest parts, so that a mix doesn't go past 1, where the
88/// speaker can't follow it and it crackles.
89void Limit(Samples& samples);
90
91} // namespace nanommo
92
93#endif // NANOMMO_CLIENT_SYNTH_H_
Out Run(float in, float hertz, float q=0.7f)
Definition synth.cc:32
float Between(float low, float high)
From low to high.
Definition synth.cc:26
float Uniform()
From 0 to 1.
Definition synth.cc:20
Noise(uint32_t seed)
Definition synth.cc:12
float Next()
From -1 to 1: white noise, a sample at a time.
Definition synth.h:29
float Next(float hertz)
Definition synth.cc:48
Definition chat.cc:9
std::vector< float > Samples
One channel of sound, at kRate.
Definition synth.h:14
int SamplesIn(float seconds)
The seconds, as a count of samples.
Definition synth.cc:65
void Mix(Samples &mix, const Samples &sound, float at, float gain)
Definition synth.cc:69
void FadeOut(Samples &samples)
Definition synth.cc:97
float Peak(const Samples &samples)
The loudest sample, whichever way it goes.
Definition synth.cc:79
void Limit(Samples &samples)
Definition synth.cc:105
constexpr int kRate
Definition synth.h:11
void Normalize(Samples &samples, float peak)
Louder or quieter, so that the loudest sample is at the peak.
Definition synth.cc:87
float Envelope(float t, float attack, float decay)
Definition synth.cc:55