nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
synth.cc
Go to the documentation of this file.
1#include "client/synth.h"
2
3#include <algorithm>
4#include <cmath>
5#include <cstddef>
6#include <numbers>
7
8namespace nanommo {
9
10// Seeds next to each other start far apart: MurmurHash3's last steps mix each
11// bit of the seed into the others.
12Noise::Noise(uint32_t seed) : state_(seed + 0x9E3779B9u) {
13 state_ ^= state_ >> 16;
14 state_ *= 0x85EBCA6Bu;
15 state_ ^= state_ >> 13;
16 state_ *= 0xC2B2AE35u;
17 state_ ^= state_ >> 16;
18}
19
21 // A linear congruential generator, whose top bits are the random ones.
22 state_ = state_ * 1664525u + 1013904223u;
23 return static_cast<float>(state_ >> 8) / 16777216.0f;
24}
25
26float Noise::Between(float low, float high) {
27 return low + (high - low) * Uniform();
28}
29
30// A state variable filter, as Andrew Simper solved it for a sample at a time:
31// it stays steady however fast the frequency moves.
32Filter::Out Filter::Run(float in, float hertz, float q) {
33 // Up to just under half the rate, the highest it can carry.
34 const float limited = std::clamp(hertz, 20.0f, 0.45f * kRate);
35 const float g = std::tan(std::numbers::pi_v<float> * limited / kRate);
36 const float k = 1.0f / q;
37 const float a1 = 1.0f / (1.0f + g * (g + k));
38 const float a2 = g * a1;
39 const float a3 = g * a2;
40 const float v3 = in - sum2_;
41 const float band = a1 * sum1_ + a2 * v3;
42 const float low = sum2_ + a2 * sum1_ + a3 * v3;
43 sum1_ = 2.0f * band - sum1_;
44 sum2_ = 2.0f * low - sum2_;
45 return {.low = low, .band = band, .high = in - k * band - low};
46}
47
48float Oscillator::Next(float hertz) {
49 const float sample = std::sin(2.0f * std::numbers::pi_v<float> * phase_);
50 phase_ += hertz / kRate;
51 phase_ -= std::floor(phase_);
52 return sample;
53}
54
55float Envelope(float t, float attack, float decay) {
56 if (t < 0.0f) {
57 return 0.0f;
58 }
59 if (t < attack) {
60 return t / attack;
61 }
62 return std::exp(-(t - attack) / decay);
63}
64
65int SamplesIn(float seconds) {
66 return static_cast<int>(std::lround(std::max(seconds, 0.0f) * kRate));
67}
68
69void Mix(Samples& mix, const Samples& sound, float at, float gain) {
70 const std::size_t start = SamplesIn(at);
71 if (mix.size() < start + sound.size()) {
72 mix.resize(start + sound.size(), 0.0f);
73 }
74 for (std::size_t i = 0; i < sound.size(); ++i) {
75 mix[start + i] += gain * sound[i];
76 }
77}
78
79float Peak(const Samples& samples) {
80 float peak = 0.0f;
81 for (const float sample : samples) {
82 peak = std::max(peak, std::abs(sample));
83 }
84 return peak;
85}
86
87void Normalize(Samples& samples, float peak) {
88 const float loudest = Peak(samples);
89 if (loudest == 0.0f) {
90 return;
91 }
92 for (float& sample : samples) {
93 sample *= peak / loudest;
94 }
95}
96
97void FadeOut(Samples& samples) {
98 const std::size_t fade =
99 std::min<std::size_t>(SamplesIn(0.01f), samples.size());
100 for (std::size_t i = 0; i < fade; ++i) {
101 samples[samples.size() - 1 - i] *= static_cast<float>(i) / fade;
102 }
103}
104
105void Limit(Samples& samples) {
106 for (float& sample : samples) {
107 sample = std::tanh(sample);
108 }
109}
110
111} // namespace nanommo
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(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