nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
foley.cc
Go to the documentation of this file.
1#include "client/foley.h"
2
3#include <algorithm>
4#include <array>
5#include <cmath>
6#include <cstddef>
7#include <numbers>
8
9#include "client/ground.h"
10
11namespace nanommo {
12
13namespace {
14
15// How loud the loudest moment of a step is, whatever it's on.
16constexpr float kStepPeak = 0.6f;
17
18float Seconds(std::size_t i) { return static_cast<float>(i) / kRate; }
19
20enum class Pass { kLow, kBand, kHigh };
21
22// The band at one loudness, whatever its q.
23float Take(const Filter::Out& out, Pass pass, float q) {
24 switch (pass) {
25 case Pass::kLow:
26 return out.low;
27 case Pass::kBand:
28 return out.band / q;
29 case Pass::kHigh:
30 return out.high;
31 }
32 return out.low;
33}
34
35// Noise through a filter, rising over attack and dying away over decay.
36Samples Hiss(Noise& noise, float seconds, float hertz, float q, Pass pass,
37 float attack, float decay) {
38 Samples hiss(SamplesIn(seconds));
39 Filter filter;
40 for (std::size_t i = 0; i < hiss.size(); ++i) {
41 hiss[i] = Take(filter.Run(noise.Next(), hertz, q), pass, q) *
42 Envelope(Seconds(i), attack, decay);
43 }
44 FadeOut(hiss);
45 return hiss;
46}
47
48// Clicks at random times, about per_second of them while the envelope's at
49// its top and fewer as it dies away, each ringing around hertz: grit, crunch
50// and crackle, depending on how many and how high.
51Samples Crackle(Noise& noise, float seconds, float per_second, float hertz,
52 float q, float attack, float decay) {
53 Samples crackle(SamplesIn(seconds));
54 Filter filter;
55 for (std::size_t i = 0; i < crackle.size(); ++i) {
56 const float chance =
57 per_second / kRate * Envelope(Seconds(i), attack, decay);
58 const float click = noise.Uniform() < chance ? noise.Next() : 0.0f;
59 // An impulse through a narrow band rings quietly, so it starts loud.
60 crackle[i] = 4.0f * filter.Run(click, hertz, q).band;
61 }
62 FadeOut(crackle);
63 return crackle;
64}
65
66// The snap at the very start of a hit: a few milliseconds of noise, high up.
67Samples Click(Noise& noise, float hertz) {
68 return Hiss(noise, 0.03f, hertz, 0.7f, Pass::kBand, 0.0005f, 0.004f);
69}
70
71// A sine that glides from one frequency to the other over its length, by the
72// one ratio each moment, as we hear pitch.
73Samples Tone(float seconds, float from, float to, float attack, float decay) {
74 Samples tone(SamplesIn(seconds));
75 Oscillator oscillator;
76 for (std::size_t i = 0; i < tone.size(); ++i) {
77 const float t = Seconds(i);
78 tone[i] = oscillator.Next(from * std::pow(to / from, t / seconds)) *
79 Envelope(t, attack, decay);
80 }
81 FadeOut(tone);
82 return tone;
83}
84
85// The sound again, quieter and duller, as it comes back off walls.
86void Echo(Samples& sound, float after, float gain) {
87 Samples echo = sound;
88 Filter filter;
89 for (float& sample : echo) {
90 sample = filter.Run(sample, 1500.0f).low;
91 }
92 Mix(sound, echo, after, gain);
93}
94
95} // namespace
96
97Ground GroundAt(const Terrain& terrain, int land, float x, float z) {
98 const float height = terrain.Height(x, z);
99 if (height < Terrain::kSeaLevel) {
100 return Ground::kWater;
101 }
102 // How steep it is, from the height half a metre either way.
103 constexpr float kStep = 0.5f;
104 const float dx = terrain.Height(x + kStep, z) - terrain.Height(x - kStep, z);
105 const float dz = terrain.Height(x, z + kStep) - terrain.Height(x, z - kStep);
106 const float up =
107 2.0f * kStep / std::sqrt(dx * dx + dz * dz + 4.0f * kStep * kStep);
108 const LandSound& sound = kLandSounds[land];
109 switch (SurfaceAt(height, up)) {
110 case Surface::kSand:
111 return sound.sand;
112 case Surface::kGrass:
113 return sound.grass;
114 case Surface::kRock:
115 return sound.rock;
116 case Surface::kSnow:
117 return sound.snow;
118 }
119 return sound.grass;
120}
121
122Samples Step(Ground ground, uint32_t seed) {
123 Noise noise(seed);
124 // The rest of the foot comes down a moment after the heel.
125 const float toe = noise.Between(0.04f, 0.07f);
126 Samples step;
127 switch (ground) {
128 case Ground::kGrass:
129 // A soft thud, the blades brushing past, and a few snapping.
130 Mix(step,
131 Hiss(noise, 0.15f, noise.Between(250.0f, 400.0f), 0.7f, Pass::kLow,
132 0.002f, 0.03f),
133 0.0f, 1.0f);
134 Mix(step,
135 Hiss(noise, 0.2f, noise.Between(2000.0f, 3500.0f), 1.0f, Pass::kBand,
136 0.015f, 0.05f),
137 0.0f, 0.2f);
138 Mix(step,
139 Crackle(noise, 0.15f, 300.0f, noise.Between(2000.0f, 3000.0f), 1.5f,
140 0.01f, 0.04f),
141 0.0f, 0.4f);
142 Mix(step,
143 Hiss(noise, 0.12f, noise.Between(300.0f, 500.0f), 0.7f, Pass::kLow,
144 0.004f, 0.025f),
145 toe, 0.6f);
146 break;
147 case Ground::kSand:
148 // Sand gives under the foot, with a hiss of fine grains.
149 Mix(step,
150 Hiss(noise, 0.15f, noise.Between(150.0f, 250.0f), 0.7f, Pass::kLow,
151 0.004f, 0.04f),
152 0.0f, 1.0f);
153 Mix(step,
154 Crackle(noise, 0.3f, 3000.0f, noise.Between(3000.0f, 4500.0f), 0.8f,
155 0.01f, 0.07f),
156 0.0f, 0.4f);
157 Mix(step,
158 Crackle(noise, 0.2f, 2000.0f, noise.Between(3000.0f, 4500.0f), 0.8f,
159 0.005f, 0.04f),
160 toe, 0.3f);
161 break;
162 case Ground::kSnow:
163 // Snow crunches, as the flakes are pressed together.
164 Mix(step,
165 Hiss(noise, 0.15f, noise.Between(120.0f, 180.0f), 0.7f, Pass::kLow,
166 0.006f, 0.04f),
167 0.0f, 0.8f);
168 Mix(step,
169 Crackle(noise, 0.35f, 1500.0f, noise.Between(1500.0f, 2200.0f), 1.5f,
170 0.02f, 0.08f),
171 0.0f, 0.6f);
172 Mix(step,
173 Crackle(noise, 0.25f, 1200.0f, noise.Between(1800.0f, 2600.0f), 1.5f,
174 0.01f, 0.05f),
175 toe, 0.4f);
176 break;
177 case Ground::kAsh:
178 // Cinders, which break with a dry crack.
179 Mix(step,
180 Hiss(noise, 0.15f, noise.Between(160.0f, 220.0f), 0.7f, Pass::kLow,
181 0.003f, 0.05f),
182 0.0f, 1.0f);
183 Mix(step,
184 Crackle(noise, 0.3f, 600.0f, noise.Between(2400.0f, 3200.0f), 4.0f,
185 0.005f, 0.06f),
186 0.0f, 0.5f);
187 Mix(step,
188 Crackle(noise, 0.2f, 500.0f, noise.Between(2800.0f, 3600.0f), 4.0f,
189 0.005f, 0.04f),
190 toe, 0.35f);
191 break;
192 case Ground::kRock:
193 // A hard knock, and grit rolling off.
194 Mix(step, Click(noise, 4500.0f), 0.0f, 0.4f);
195 Mix(step,
196 Hiss(noise, 0.1f, noise.Between(1000.0f, 1400.0f), 3.0f, Pass::kBand,
197 0.001f, 0.015f),
198 0.0f, 1.0f);
199 Mix(step,
200 Crackle(noise, 0.12f, 250.0f, noise.Between(2000.0f, 3000.0f), 6.0f,
201 0.005f, 0.04f),
202 0.01f, 0.5f);
203 Mix(step,
204 Hiss(noise, 0.1f, noise.Between(1100.0f, 1500.0f), 3.0f, Pass::kBand,
205 0.001f, 0.012f),
206 toe, 0.6f);
207 break;
208 case Ground::kStone:
209 // A knock that rings, and comes back off the walls.
210 Mix(step, Click(noise, 4000.0f), 0.0f, 0.3f);
211 Mix(step,
212 Hiss(noise, 0.2f, noise.Between(600.0f, 800.0f), 8.0f, Pass::kBand,
213 0.001f, 0.04f),
214 0.0f, 1.0f);
215 Mix(step,
216 Hiss(noise, 0.2f, noise.Between(650.0f, 850.0f), 8.0f, Pass::kBand,
217 0.001f, 0.03f),
218 toe, 0.6f);
219 Echo(step, 0.06f, 0.35f);
220 Echo(step, 0.13f, 0.15f);
221 break;
222 case Ground::kWater:
223 // A splash, and bubbles, each a note that rises as it closes.
224 Mix(step,
225 Hiss(noise, 0.3f, noise.Between(1200.0f, 2000.0f), 0.6f, Pass::kBand,
226 0.005f, 0.06f),
227 0.0f, 0.7f);
228 Mix(step, Hiss(noise, 0.3f, 400.0f, 0.7f, Pass::kLow, 0.02f, 0.08f), 0.0f,
229 0.8f);
230 for (int bubble = 0, n = static_cast<int>(noise.Between(3.0f, 7.0f));
231 bubble < n; ++bubble) {
232 const float hertz = noise.Between(500.0f, 1300.0f);
233 Mix(step, Tone(0.08f, hertz, hertz * 1.6f, 0.002f, 0.02f),
234 noise.Between(0.01f, 0.18f), 0.3f);
235 }
236 break;
237 }
238 Normalize(step, kStepPeak);
239 return step;
240}
241
242namespace {
243
244constexpr float kPi = std::numbers::pi_v<float>;
245
246// Up from nothing to its top at peak, a share of the way through, and back
247// down to nothing at 1.
248float Swell(float t, float peak) {
249 const float s = t < peak ? std::sin(0.5f * kPi * t / peak)
250 : std::cos(0.5f * kPi * (t - peak) / (1.0f - peak));
251 return std::max(s, 0.0f) * std::max(s, 0.0f);
252}
253
254// Air pushed aside: noise in a band that moves from one frequency towards the
255// other as it swells, and back as it fades.
256Samples Whoosh(Noise& noise, float seconds, float from, float to, float peak) {
257 Samples whoosh(SamplesIn(seconds));
258 Filter filter;
259 constexpr float kQ = 1.4f;
260 for (std::size_t i = 0; i < whoosh.size(); ++i) {
261 const float swell = Swell(Seconds(i) / seconds, peak);
262 const float hertz = from * std::pow(to / from, swell);
263 whoosh[i] = filter.Run(noise.Next(), hertz, kQ).band / kQ * swell;
264 }
265 return whoosh;
266}
267
268// How loud the loudest moment of each kind of fx is: the light's chord stays
269// under the rest, and the shake's boom and the bolt are the loudest.
270float Level(skills::Fx::Kind kind) {
271 switch (kind) {
272 case skills::Fx::GLOW:
273 case skills::Fx::MOTES:
274 return 0.2f;
275 case skills::Fx::SHARDS:
276 return 0.35f;
277 case skills::Fx::SPARKS:
278 return 0.45f;
279 case skills::Fx::TRAIL:
280 case skills::Fx::RING:
281 case skills::Fx::SPIKES:
282 case skills::Fx::WAVE:
283 return 0.5f;
284 case skills::Fx::BOLT:
285 case skills::Fx::SHAKE:
286 return 0.7f;
287 case skills::Fx::EDGE:
288 return 0.15f;
289 case skills::Fx::GHOSTS:
290 return 0.4f;
291 case skills::Fx::SLASH:
292 return 0.55f;
293 case skills::Fx::FLASH:
294 return 0.6f;
295 default:
296 return 0.5f;
297 }
298}
299
300} // namespace
301
302float Coldness(uint32_t color) {
303 const float red = static_cast<float>((color >> 24) & 0xff) / 255.0f;
304 const float blue = static_cast<float>((color >> 8) & 0xff) / 255.0f;
305 return blue - red;
306}
307
308Samples FxSound(const skills::Fx& fx, uint32_t seed) {
309 Noise noise(seed);
310 const float span = static_cast<float>(fx.for_ms()) / 1000.0f;
311 const float size = fx.size() > 0.0f ? fx.size() : 1.0f;
312 const float cold = Coldness(fx.color());
313 // From 110 hertz for red to 440 for blue.
314 const float note = 220.0f * std::exp2(cold);
315 const int count = static_cast<int>(fx.count() > 0 ? fx.count() : 12);
316 Samples sound;
317 switch (fx.kind()) {
318 case skills::Fx::TRAIL:
319 // The blade cutting the air.
320 sound = Whoosh(noise, span + 0.1f, 300.0f, 2000.0f, 0.4f);
321 break;
322 case skills::Fx::GLOW: {
323 // A chord that swells and fades with the light: the note, its fifth and
324 // its octave, each played twice a little apart, so that they beat.
325 sound.resize(SamplesIn(span + 0.5f));
326 std::array<Oscillator, 6> voices;
327 constexpr std::array<float, 6> kRatios = {1.0f, 1.004f, 1.5f,
328 1.503f, 2.0f, 2.005f};
329 for (std::size_t i = 0; i < sound.size(); ++i) {
330 float chord = 0.0f;
331 for (std::size_t v = 0; v < voices.size(); ++v) {
332 chord += voices[v].Next(note * kRatios[v]);
333 }
334 sound[i] = chord * Envelope(Seconds(i), 0.4f * span, 0.4f * span);
335 }
336 FadeOut(sound);
337 break;
338 }
339 case skills::Fx::RING: {
340 // A thump, deeper for a bigger ring, and air rushing out along the
341 // ground.
342 const float low = std::clamp(80.0f / std::sqrt(size), 35.0f, 90.0f);
343 Mix(sound, Tone(0.5f, 2.0f * low, low, 0.004f, 0.18f), 0.0f, 0.9f);
344 Samples rush(SamplesIn(span));
345 Filter filter;
346 for (std::size_t i = 0; i < rush.size(); ++i) {
347 const float t = Seconds(i);
348 rush[i] =
349 filter.Run(noise.Next(), 2500.0f * std::pow(0.12f, t / span), 1.0f)
350 .band *
351 Envelope(t, 0.05f * span, 0.35f * span);
352 }
353 FadeOut(rush);
354 Mix(sound, rush, 0.0f, 0.5f);
355 break;
356 }
357 case skills::Fx::SPARKS: {
358 // Each spark a bright ping, high and short, like metal on metal, most
359 // of them as they fly off. Two partials that aren't whole multiples of
360 // each other ring like metal, where one alone would whistle.
361 for (int i = 0; i < std::min(count, 24); ++i) {
362 const float at = 0.6f * span * noise.Uniform() * noise.Uniform();
363 const float hertz = noise.Between(2500.0f, 5000.0f);
364 const float decay = noise.Between(0.01f, 0.03f);
365 const float gain = noise.Between(0.3f, 1.0f);
366 Mix(sound, Tone(0.15f, hertz, 0.97f * hertz, 0.0005f, decay), at, gain);
367 Mix(sound,
368 Tone(0.1f, 2.76f * hertz, 2.7f * hertz, 0.0005f, 0.5f * decay), at,
369 0.3f * gain);
370 }
371 // Where they fly off someone, a blow landed: a low note that drops
372 // fast, like a drum.
373 if (fx.at() == skills::Fx::TARGET) {
374 Mix(sound, Tone(0.3f, 160.0f, 50.0f, 0.001f, 0.07f), 0.0f, 3.0f);
375 }
376 break;
377 }
378 case skills::Fx::MOTES: {
379 // A note for each speck, from a scale with no notes that clash, two
380 // octaves above the colour's, and rising as they do.
381 constexpr std::array<float, 6> kScale = {
382 1.0f, 9.0f / 8.0f, 5.0f / 4.0f, 3.0f / 2.0f, 5.0f / 3.0f, 2.0f};
383 for (int i = 0; i < std::min(count, 32); ++i) {
384 const float hertz =
385 4.0f * note * kScale[static_cast<int>(noise.Uniform() * 5.99f)];
386 const float at = 0.8f * span * std::pow(noise.Uniform(), 0.7f);
387 Mix(sound, Tone(0.5f, hertz, hertz * 1.03f, 0.004f, 0.12f), at);
388 }
389 break;
390 }
391 case skills::Fx::SHAKE:
392 // Felt as much as heard: a boom below the notes, and a rumble.
393 Mix(sound, Tone(span + 0.4f, 60.0f, 32.0f, 0.005f, 0.5f * span + 0.1f),
394 0.0f, 1.0f);
395 Mix(sound,
396 Hiss(noise, span + 0.4f, 100.0f, 0.7f, Pass::kLow, 0.01f,
397 0.5f * span),
398 0.0f, 1.0f);
399 break;
400 case skills::Fx::SHARDS:
401 // A crack as the ground breaks, and the pieces coming back down: ice
402 // rings like glass, and earth knocks.
403 Mix(sound, Click(noise, 2500.0f), 0.0f, 1.0f);
404 for (int i = 0; i < std::min(count, 32); ++i) {
405 const float at = span * noise.Between(0.4f, 0.95f);
406 if (cold > 0.2f) {
407 const float hertz = noise.Between(2500.0f, 6000.0f);
408 Mix(sound, Tone(0.2f, hertz, hertz, 0.0005f, 0.035f), at, 0.3f);
409 } else {
410 const float hertz = noise.Between(400.0f, 900.0f);
411 Mix(sound, Tone(0.08f, hertz, 0.7f * hertz, 0.0005f, 0.015f), at,
412 0.8f);
413 }
414 }
415 break;
416 case skills::Fx::SPIKES: {
417 // The ground heaves, with a rumble below the notes, and bursts open
418 // spike by spike as the ring goes out: each a thunk, a low note that
419 // drops fast, and a short knock above it. Ice rings like glass, too.
420 Mix(sound, Tone(span, 55.0f, 40.0f, 0.15f * span, 0.4f * span), 0.0f,
421 0.8f);
422 Mix(sound,
423 Hiss(noise, span, 90.0f, 0.7f, Pass::kLow, 0.15f * span, 0.4f * span),
424 0.0f, 1.0f);
425 const int spikes = std::min(count, 24);
426 for (int i = 0; i < spikes; ++i) {
427 const float at = 0.35f * span * static_cast<float>(i) / spikes +
428 noise.Between(0.0f, 0.02f);
429 const float thunk = noise.Between(150.0f, 220.0f);
430 Mix(sound, Tone(0.15f, thunk, 0.35f * thunk, 0.001f, 0.035f), at, 0.6f);
431 const float knock = noise.Between(700.0f, 1200.0f);
432 Mix(sound, Tone(0.06f, knock, 0.8f * knock, 0.0005f, 0.008f), at,
433 0.25f);
434 if (cold > 0.2f) {
435 const float hertz = noise.Between(2000.0f, 4000.0f);
436 Mix(sound, Tone(0.2f, hertz, hertz, 0.0005f, 0.06f), at, 0.15f);
437 }
438 }
439 break;
440 }
441 case skills::Fx::BOLT: {
442 // A crack, the buzz of the bolt, which jumps from one pitch to another,
443 // and thunder rolling after it.
444 Mix(sound, Click(noise, 2500.0f), 0.0f, 1.0f);
445 Samples buzz(SamplesIn(span));
446 Filter high;
447 Filter low;
448 float phase = 0.0f;
449 float hertz = 100.0f;
450 float level = 1.0f;
451 int jump = 0;
452 for (std::size_t i = 0; i < buzz.size(); ++i) {
453 if (--jump <= 0) {
454 jump = SamplesIn(noise.Between(0.008f, 0.025f));
455 hertz = noise.Between(50.0f, 160.0f);
456 level = noise.Between(0.3f, 1.0f);
457 }
458 phase += hertz / kRate;
459 phase -= std::floor(phase);
460 // A sawtooth, which is rich in overtones, and some noise, between
461 // 400 hertz and 5,000.
462 const float raw = 2.0f * phase - 1.0f + 0.5f * noise.Next();
463 buzz[i] = low.Run(high.Run(raw, 400.0f).high, 5000.0f).low * level *
464 Envelope(Seconds(i), 0.003f, 0.6f * span);
465 }
466 FadeOut(buzz);
467 Mix(sound, buzz, 0.0f, 0.5f);
468 Mix(sound, Hiss(noise, 1.6f, 120.0f, 0.7f, Pass::kLow, 0.05f, 0.5f),
469 0.03f, 1.6f);
470 break;
471 }
472 case skills::Fx::WAVE: {
473 // Fire roaring past, and popping.
474 Samples roar(SamplesIn(span + 0.2f));
475 Filter filter;
476 for (std::size_t i = 0; i < roar.size(); ++i) {
477 const float swell = Swell(Seconds(i) / (span + 0.2f), 0.3f);
478 roar[i] =
479 filter.Run(noise.Next(), 400.0f + 1600.0f * swell).low * swell;
480 }
481 Mix(sound, roar, 0.0f, 1.2f);
482 Mix(sound, Crackle(noise, span, 60.0f, 1500.0f, 3.0f, 0.05f, 0.5f * span),
483 0.0f, 0.6f);
484 break;
485 }
486 case skills::Fx::EDGE: {
487 // The blade singing while it's lit: a high note, two octaves above the
488 // colour's, wavering a little, and a second that isn't a whole multiple
489 // of it, so that it rings like metal, from a ping as it lights.
490 const float hertz = 4.0f * note;
491 Samples sing(SamplesIn(span + 0.2f));
492 Oscillator first;
493 Oscillator second;
494 for (std::size_t i = 0; i < sing.size(); ++i) {
495 const float t = Seconds(i);
496 const float waver = 1.0f + 0.006f * std::sin(2.0f * kPi * 5.5f * t);
497 sing[i] = (first.Next(hertz * waver) +
498 0.35f * second.Next(2.76f * hertz * waver)) *
499 Envelope(t, 0.08f, 0.6f * span);
500 }
501 FadeOut(sing);
502 Mix(sound, sing, 0.0f, 1.0f);
503 Mix(sound, Tone(0.6f, 2.0f * hertz, 2.0f * hertz, 0.001f, 0.15f), 0.0f,
504 0.6f);
505 break;
506 }
507 case skills::Fx::GHOSTS:
508 // Air torn as the player goes past, faster than a swing.
509 sound = Whoosh(noise, span + 0.15f, 200.0f, 3500.0f, 0.3f);
510 break;
511 case skills::Fx::SLASH: {
512 // Each cut a hiss of air, high up, a snap and a short ring of metal,
513 // a little after the one before, as they're drawn.
514 const int cuts = static_cast<int>(std::max(fx.count(), 1u));
515 for (int i = 0; i < std::min(cuts, 8); ++i) {
516 const float at = 0.06f * static_cast<float>(i);
517 Mix(sound,
518 Hiss(noise, 0.12f, 6000.0f, 0.8f, Pass::kHigh, 0.002f, 0.04f), at,
519 0.9f);
520 Mix(sound, Click(noise, 5000.0f), at, 0.8f);
521 const float ring = noise.Between(2800.0f, 3600.0f);
522 Mix(sound, Tone(0.4f, ring, 0.99f * ring, 0.0005f, 0.09f), at, 0.35f);
523 Mix(sound, Tone(0.3f, 2.76f * ring, 2.7f * ring, 0.0005f, 0.05f), at,
524 0.12f);
525 }
526 break;
527 }
528 case skills::Fx::FLASH:
529 // A crack, and a thump below it that drops away.
530 Mix(sound, Click(noise, 3000.0f), 0.0f, 1.0f);
531 Mix(sound, Tone(0.5f, 140.0f, 38.0f, 0.002f, 0.12f), 0.0f, 1.4f);
532 break;
533 default:
534 break;
535 }
536 Normalize(sound, Level(fx.kind()));
537 return sound;
538}
539
540Samples SkillSound(const skills::Look& look, uint32_t seed, bool target) {
541 Samples sound;
542 for (int i = 0; i < look.fx_size(); ++i) {
543 const skills::Fx& fx = look.fx(i);
544 if (fx.at() == skills::Fx::TARGET && !target) {
545 continue;
546 }
547 Mix(sound, FxSound(fx, seed * 31u + static_cast<uint32_t>(i)),
548 static_cast<float>(fx.from_ms()) / 1000.0f);
549 }
550 Limit(sound);
551 return sound;
552}
553
554namespace {
555
556// The partials of a struck metal bar, as a glockenspiel's: their frequencies
557// over the note's, how loud each is, and how long each rings. They aren't
558// whole multiples, which is why it shimmers where a string doesn't.
559struct Partial {
560 float ratio;
561 float gain;
562 float decay;
563};
564constexpr std::array<Partial, 5> kBell = {{{1.0f, 1.0f, 1.2f},
565 {2.0f, 0.5f, 0.8f},
566 {2.76f, 0.35f, 0.6f},
567 {5.4f, 0.2f, 0.35f},
568 {8.93f, 0.12f, 0.25f}}};
569
570Samples Bell(float seconds, float hertz) {
571 Samples bell;
572 for (const Partial& partial : kBell) {
573 Mix(bell,
574 Tone(seconds, hertz * partial.ratio, hertz * partial.ratio, 0.002f,
575 partial.decay),
576 0.0f, partial.gain);
577 }
578 return bell;
579}
580
581} // namespace
582
583Samples Departure(uint32_t seed) {
584 Noise noise(seed);
585 constexpr float kLength = 1.0f;
586 Samples sound(SamplesIn(kLength));
587 std::array<Oscillator, 3> voices;
588 Filter filter;
589 float wobble = 0.0f;
590 for (std::size_t i = 0; i < sound.size(); ++i) {
591 const float t = Seconds(i) / kLength;
592 // Two octaves up, wobbling faster and faster.
593 const float hertz = 160.0f * std::exp2(2.0f * t);
594 wobble += (4.0f + 14.0f * t) / kRate;
595 const float tremolo = 0.7f + 0.3f * std::sin(2.0f * kPi * wobble);
596 const float tone = voices[0].Next(hertz) + voices[1].Next(hertz * 1.007f) +
597 0.5f * voices[2].Next(hertz * 1.5f);
598 const float air =
599 filter.Run(noise.Next(), 300.0f * std::exp2(4.0f * t), 1.5f).band /
600 1.5f;
601 const float rise = t * t;
602 const float fall = t > 0.85f ? std::exp(-(t - 0.85f) / 0.05f) : 1.0f;
603 sound[i] = (0.25f * tone * tremolo + 0.6f * air) * rise * fall;
604 }
605 FadeOut(sound);
606 Normalize(sound, 0.55f);
607 return sound;
608}
609
610Samples Arrival(int land) {
611 const LandSound& sound = kLandSounds[land];
612 Samples arrival;
613 // Struck one after the other, low to high, over the root an octave down.
614 for (std::size_t i = 0; i < sound.chord.size(); ++i) {
615 Mix(arrival, Bell(3.0f, sound.chord[i]), 0.06f * static_cast<float>(i));
616 }
617 Mix(arrival,
618 Tone(2.0f, sound.chord[0] / 2.0f, sound.chord[0] / 2.0f, 0.15f, 0.6f),
619 0.0f);
620 Noise noise(static_cast<uint32_t>(land));
621 Mix(arrival, Whoosh(noise, 0.7f, 5000.0f, 800.0f, 0.1f), 0.0f, 1.5f);
622 Normalize(arrival, 0.55f);
623 return arrival;
624}
625
626} // 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
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
static constexpr float kSeaLevel
Definition terrain.h:50
float Height(float x, float z) const
On the triangles between the grid's corners, as the client draws them.
Definition terrain.cc:102
Definition chat.cc:9
std::vector< float > Samples
One channel of sound, at kRate.
Definition synth.h:14
Samples FxSound(const skills::Fx &fx, uint32_t seed)
The sound of one of a skill's fx, from when it starts.
Definition foley.cc:308
Ground GroundAt(const Terrain &terrain, int land, float x, float z)
What's underfoot there: the water we wade in, or the land's surface.
Definition foley.cc:97
Samples SkillSound(const skills::Look &look, uint32_t seed, bool target)
Definition foley.cc:540
Place Arrival(const Gate &gate, float yaw)
Definition lands.cc:43
Ground
What a foot comes down on, which is how the step sounds.
Definition foley.h:15
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
Samples Step(Ground ground, uint32_t seed)
Definition foley.cc:122
void FadeOut(Samples &samples)
Definition synth.cc:97
void Limit(Samples &samples)
Definition synth.cc:105
float Coldness(uint32_t color)
Definition foley.cc:302
Surface SurfaceAt(float height, float up)
Definition ground.cc:21
constexpr std::array< LandSound, 3 > kLandSounds
Definition foley.h:33
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
Samples Departure(uint32_t seed)
Going through a gate: a rush up, as the screen covers the world.
Definition foley.cc:583
std::array< float, 3 > chord
Definition foley.h:28