Player12
An open source football management simulator
Loading...
Searching...
No Matches
synth.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2// Football Management Project
3// Copyright (c) 2025 - 2026 Flavio Milinanni. All Rights Reserved.
4//
5// This file is part of the Football Management Project.
6// See the LICENSE file in the project root.
7// -----------------------------------------------------------------------------
8
9#pragma once
10
11#include <array>
12#include <cstdint>
13#include <utility>
14
23namespace Audio
24{
25inline constexpr int SAMPLE_RATE = 48000;
26inline constexpr float SAMPLE_RATE_F = 48000.0f;
27inline constexpr float SAMPLE_PERIOD = 1.0f / SAMPLE_RATE_F;
28
30class Noise
31{
32 public:
33 explicit Noise(std::uint32_t seed = 0x9E3779B9U) : state(seed | 1U) {}
34 float next()
35 {
36 state ^= state << 13U;
37 state ^= state >> 17U;
38 state ^= state << 5U;
39 return static_cast<float>(static_cast<std::int32_t>(state)) *
40 (1.0f / 2147483648.0f);
41 }
43 float unit() { return 0.5f * (next() + 1.0f); }
44 void reseed(std::uint32_t seed) { state = seed | 1U; }
45
46 private:
47 std::uint32_t state;
48};
49
51class Svf
52{
53 public:
54 struct Output
55 {
56 float low = 0.0f;
57 float band = 0.0f;
58 float high = 0.0f;
59 };
60
61 void set(float cutoffHz, float q);
62 Output process(float input)
63 {
64 const float v3 = input - ic2;
65 const float v1 = a1 * ic1 + a2 * v3;
66 const float v2 = ic2 + a2 * ic1 + a3 * v3;
67 ic1 = 2.0f * v1 - ic1;
68 ic2 = 2.0f * v2 - ic2;
69 return {v2, v1, input - k * v1 - v2};
70 }
71 void reset()
72 {
73 ic2 = 0.0f;
74 ic1 = 0.0f;
75 }
76
77 private:
78 float ic1 = 0.0f;
79 float ic2 = 0.0f;
80 float k = 1.0f;
81 float a1 = 0.0f;
82 float a2 = 0.0f;
83 float a3 = 0.0f;
84};
85
88{
89 public:
90 void set(float cutoffHz);
91 float process(float input)
92 {
93 state += coefficient * (input - state);
94 return state;
95 }
96 void reset() { state = 0.0f; }
97
98 private:
99 float state = 0.0f;
100 float coefficient = 1.0f;
101};
102
105{
106 public:
107 void set(float cutoffHz)
108 {
109 first.set(cutoffHz);
110 second.set(cutoffHz);
111 }
112 float process(float input) { return second.process(first.process(input)); }
113 void reset()
114 {
115 first.reset();
116 second.reset();
117 }
118
119 private:
122};
123
125class Drift
126{
127 public:
128 explicit Drift(std::uint32_t seed, float ratePerSecond)
129 : noise(seed), rate(ratePerSecond)
130 {
131 }
133 float step(float seconds);
134 float value() const { return current; }
135
136 private:
138 float rate;
139 float current = 0.5f;
140 float target = 0.5f;
141};
142
144enum class Sound : std::uint8_t
145{
146 KICK,
147 NET,
148 WOODWORK,
149 WHISTLE,
150 CROWD_OOH,
153 CROWD_BOO,
156 COUNT
157};
158inline constexpr std::size_t SOUND_COUNT =
159 static_cast<std::size_t>(std::to_underlying(Sound::COUNT));
160
162enum class WhistlePattern : std::uint8_t
163{
164 SHORT,
165 KICK_OFF,
166 HALF_TIME,
168};
169
171bool isCrowdSound(Sound sound);
172
175{
178 float gain = 1.0f;
180 float pan = 0.0f;
182 float intensity = 0.5f;
184 std::uint8_t variant = 0;
186 float delay = 0.0f;
187};
188
194class Voice
195{
196 public:
197 void start(const SoundCommand& command, std::uint32_t seed);
198 bool render(float* out, int frames);
199 void stop() { playing = false; }
200 bool active() const { return playing; }
201 Sound sound() const { return command.sound; }
203 float age() const { return time; }
204
205 private:
206 static constexpr int PARTIALS = 6;
207
208 float renderKick();
209 float renderNet();
210 float renderWoodwork();
211 float renderWhistle();
212 float renderCrowdVowel(float firstFormant, float secondFormant);
213 float renderCrowdWhistles();
214 float renderApplause(float& right);
216 float crowdEnvelope() const;
218 float whistleEnvelope() const;
219 void updateControl();
220
222 bool playing = false;
223 float time = 0.0f;
224 float duration = 0.0f;
225 float delay = 0.0f;
226 float leftGain = 0.0f;
227 float rightGain = 0.0f;
229
231 Noise noiseRight{0x1234567U};
238 std::array<float, PARTIALS> phase{};
239 std::array<float, PARTIALS> frequency{};
240 std::array<float, PARTIALS> amplitude{};
241 std::array<float, PARTIALS> decay{};
242 std::array<float, PARTIALS> onset{};
243 float lfoPhase = 0.0f;
244 float glide = 0.0f;
245 float excitation = 0.0f;
246 float excitationRight = 0.0f;
248 float envelope = 0.0f;
249 float attack = 0.1f;
250 float hold = 0.5f;
251 float release = 1.0f;
252};
253
260{
261 public:
263 void setTargets(float intensity, float level);
265 void render(float* out, int frames, float busGain);
266
267 private:
280 void updateControl();
281
282 Channel left{0xC0FFEEU};
283 Channel right{0xBADA55U};
284 float intensity = 0.3f;
285 float intensityTarget = 0.3f;
286 float level = 0.0f;
287 float levelTarget = 1.0f;
289};
290} // namespace Audio
Definition synth.h:260
float intensityTarget
Definition synth.h:285
float level
Definition synth.h:286
Channel right
Definition synth.h:283
Channel left
Definition synth.h:282
float levelTarget
Definition synth.h:287
void render(float *out, int frames, float busGain)
Definition synth.cpp:599
int controlCounter
Definition synth.h:288
void updateControl()
Definition synth.cpp:585
void setTargets(float intensity, float level)
Definition synth.cpp:579
float intensity
Definition synth.h:284
Definition synth.h:105
float process(float input)
Definition synth.h:112
void reset()
Definition synth.h:113
OnePole first
Definition synth.h:120
void set(float cutoffHz)
Definition synth.h:107
OnePole second
Definition synth.h:121
Definition synth.h:126
float current
Definition synth.h:139
float target
Definition synth.h:140
Noise noise
Definition synth.h:137
Drift(std::uint32_t seed, float ratePerSecond)
Definition synth.h:128
float step(float seconds)
Definition synth.cpp:108
float value() const
Definition synth.h:134
float rate
Definition synth.h:138
Definition synth.h:31
void reseed(std::uint32_t seed)
Definition synth.h:44
float unit()
Definition synth.h:43
std::uint32_t state
Definition synth.h:47
float next()
Definition synth.h:34
Noise(std::uint32_t seed=0x9E3779B9U)
Definition synth.h:33
Definition synth.h:88
float state
Definition synth.h:99
void reset()
Definition synth.h:96
float process(float input)
Definition synth.h:91
float coefficient
Definition synth.h:100
void set(float cutoffHz)
Definition synth.cpp:92
Definition synth.h:52
void set(float cutoffHz, float q)
Definition synth.cpp:98
float a3
Definition synth.h:83
float ic2
Definition synth.h:79
float ic1
Definition synth.h:78
float a1
Definition synth.h:81
Output process(float input)
Definition synth.h:62
float k
Definition synth.h:80
float a2
Definition synth.h:82
void reset()
Definition synth.h:71
Definition synth.h:195
bool playing
Definition synth.h:222
static constexpr int PARTIALS
Definition synth.h:206
Noise noise
Definition synth.h:230
bool render(float *out, int frames)
Definition synth.cpp:484
float leftGain
Definition synth.h:226
Svf filterC
Definition synth.h:234
float renderCrowdVowel(float firstFormant, float secondFormant)
Definition synth.cpp:422
float excitationRight
Definition synth.h:246
std::array< float, PARTIALS > phase
Definition synth.h:238
SoundCommand command
Definition synth.h:221
float excitation
Definition synth.h:245
std::array< float, PARTIALS > frequency
Definition synth.h:239
float envelope
Definition synth.h:248
std::array< float, PARTIALS > decay
Definition synth.h:241
float renderWhistle()
Definition synth.cpp:404
float release
Definition synth.h:251
float attack
Definition synth.h:249
float rightGain
Definition synth.h:227
void updateControl()
Definition synth.cpp:317
void stop()
Definition synth.h:199
float whistleEnvelope() const
Definition synth.cpp:304
Svf filterD
Definition synth.h:235
float renderWoodwork()
Definition synth.cpp:390
float delay
Definition synth.h:225
bool active() const
Definition synth.h:200
std::array< float, PARTIALS > onset
Definition synth.h:242
float crowdEnvelope() const
Definition synth.cpp:295
Sound sound() const
Definition synth.h:201
float renderApplause(float &right)
Definition synth.cpp:462
Svf filterB
Definition synth.h:233
float renderNet()
Definition synth.cpp:372
float age() const
Definition synth.h:203
std::array< float, PARTIALS > amplitude
Definition synth.h:240
float duration
Definition synth.h:224
float renderCrowdWhistles()
Definition synth.cpp:442
Darken toneLeft
Definition synth.h:236
float hold
Definition synth.h:250
float renderKick()
Definition synth.cpp:358
float time
Definition synth.h:223
Darken toneRight
Definition synth.h:237
Svf filterA
Definition synth.h:232
Noise noiseRight
Definition synth.h:231
int controlCounter
Definition synth.h:228
float lfoPhase
Definition synth.h:243
float glide
Definition synth.h:244
Definition audio_device.cpp:17
constexpr float SAMPLE_PERIOD
Definition synth.h:27
bool isCrowdSound(Sound sound)
Definition synth.cpp:115
Sound
Definition synth.h:145
constexpr std::size_t SOUND_COUNT
Definition synth.h:158
constexpr int SAMPLE_RATE
Definition synth.h:25
constexpr float SAMPLE_RATE_F
Definition synth.h:26
WhistlePattern
Definition synth.h:163
Definition synth.h:269
Svf air
Definition synth.h:275
Drift swell
Definition synth.h:276
Svf voicesHigh
Definition synth.h:274
Svf voices
Definition synth.h:273
Svf rumble
Definition synth.h:272
Darken tone
Definition synth.h:271
Noise noise
Definition synth.h:270
Drift babble
Definition synth.h:277
Definition synth.h:175
float delay
Definition synth.h:186
float gain
Definition synth.h:178
Sound sound
Definition synth.h:176
float pan
Definition synth.h:180
std::uint8_t variant
Definition synth.h:184
float intensity
Definition synth.h:182
Definition synth.h:55
float low
Definition synth.h:56
float high
Definition synth.h:58
float band
Definition synth.h:57
float start
Definition synth.cpp:48