nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
ambience.cc
Go to the documentation of this file.
1#include "client/ambience.h"
2
3#include <algorithm>
4#include <cmath>
5#include <cstddef>
6#include <cstdint>
7#include <span>
8#include <utility>
9
10#include "client/daylight.h"
11#include "terrain/lands.h"
12
13namespace nanommo {
14
15namespace {
16
17constexpr float kTau = 6.2831853f;
18
19// Each loop is this long, and fades from its end into its start, so that it
20// goes round without a seam.
21constexpr float kLoop = 8.0f;
22constexpr float kSeam = 0.5f;
23
24// How fast a loop glides to a new level: about two thirds of the way in this
25// many seconds.
26constexpr float kGlide = 1.5f;
27
28// Sound comes this many metres a second, so thunder comes after the flash.
29constexpr float kSoundSpeed = 343.0f;
30
31constexpr int kMeadowsLand = 0;
32constexpr int kAshlandsLand = 1;
33
34// Made a little longer than the loop, then folded, so that its last moment
35// leads into its first.
36Samples Looped(Samples made) {
37 const std::size_t length = static_cast<std::size_t>(SamplesIn(kLoop));
38 const std::size_t seam = static_cast<std::size_t>(SamplesIn(kSeam));
39 made.resize(length + seam, 0.0f);
40 for (std::size_t i = 0; i < seam; ++i) {
41 const float in = static_cast<float>(i) / static_cast<float>(seam);
42 made[i] = made[i] * in + made[length + i] * (1.0f - in);
43 }
44 made.resize(length);
45 return made;
46}
47
48// Folds a sound's last moment into its first, so that it goes round without
49// a seam, and as long as it was, less that moment.
50void Fold(Samples& made, std::size_t seam) {
51 if (made.size() <= 2 * seam) {
52 return;
53 }
54 const std::size_t length = made.size() - seam;
55 for (std::size_t i = 0; i < seam; ++i) {
56 const float in = static_cast<float>(i) / static_cast<float>(seam);
57 made[i] = made[i] * in + made[length + i] * (1.0f - in);
58 }
59 made.resize(length);
60}
61
62// The loudness of a sound, as the root of its mean square.
63float Rms(const Samples& samples) {
64 double sum = 0.0;
65 for (const float s : samples) {
66 sum += static_cast<double>(s) * s;
67 }
68 return samples.empty() ? 0.0f
69 : static_cast<float>(std::sqrt(
70 sum / static_cast<double>(samples.size())));
71}
72
73// The same sound from a later place in it, which plays beside it as another
74// part of the rain.
75Samples From(const Samples& samples, std::size_t at) {
76 Samples moved(samples.size());
77 for (std::size_t i = 0; i < samples.size(); ++i) {
78 moved[i] = samples[(at + i) % samples.size()];
79 }
80 return moved;
81}
82
83Samples Silence() {
84 return Samples(static_cast<std::size_t>(SamplesIn(kLoop + kSeam)), 0.0f);
85}
86
87// A short tone that dies away, added in at a sample: what a drop sounds like
88// on a leaf or the ground, or, rising, on water.
89void Drop(Samples& into, std::size_t at, float hertz, float rise, float decay,
90 float gain) {
91 const std::size_t length = static_cast<std::size_t>(SamplesIn(5.0f * decay));
92 float phase = 0.0f;
93 for (std::size_t i = 0; i < length && at + i < into.size(); ++i) {
94 const float t = static_cast<float>(i) / kRate;
95 const float f = hertz * (1.0f + rise * std::min(t / decay, 1.0f));
96 phase += f / kRate;
97 into[at + i] += gain * std::sin(kTau * phase) * std::exp(-t / decay);
98 }
99}
100
101// Rain: drops, many of them, each a short tick of its own, over a soft low
102// bed. Never a hiss, which sounds like a television left on.
103Samples Rain(uint32_t seed, float per_second, float loudness, float body) {
104 Noise noise(seed);
105 Samples s = Silence();
106 const int drops = static_cast<int>(per_second * (kLoop + kSeam));
107 for (int d = 0; d < drops; ++d) {
108 const auto at = static_cast<std::size_t>(noise.Uniform() * s.size());
109 const float g = noise.Between(0.2f, 1.0f);
110 Drop(s, at, noise.Between(1600.0f, 6800.0f), 0.0f,
111 noise.Between(0.0015f, 0.006f), loudness * g * g);
112 }
113 Filter low;
114 for (float& v : s) {
115 v += body * low.Run(noise.Next(), 700.0f, 0.5f).low;
116 }
117 return Looped(std::move(s));
118}
119
120// Drops on water ring up in pitch, as each traps a bubble.
121Samples OnWater(uint32_t seed) {
122 Noise noise(seed);
123 Samples s = Silence();
124 const int drops = static_cast<int>(140.0f * (kLoop + kSeam));
125 for (int d = 0; d < drops; ++d) {
126 const auto at = static_cast<std::size_t>(noise.Uniform() * s.size());
127 const float g = noise.Between(0.3f, 1.0f);
128 Drop(s, at, noise.Between(800.0f, 1900.0f), 0.7f,
129 noise.Between(0.008f, 0.016f), 0.16f * g * g);
130 }
131 return Looped(std::move(s));
132}
133
134// The wind: noise through a band that wanders up and down, swelling and
135// dying away, which is a whoosh, as slow as a breath.
136Samples Wind(uint32_t seed) {
137 Noise noise(seed);
138 Samples s = Silence();
139 Filter band;
140 const float a = noise.Between(0.0f, kTau);
141 const float b = noise.Between(0.0f, kTau);
142 for (std::size_t i = 0; i < s.size(); ++i) {
143 const float t = static_cast<float>(i) / kRate;
144 // Whole turns in a loop, so that it goes round smoothly.
145 const float slow = std::sin(kTau * t / kLoop + a);
146 const float slower = std::sin(kTau * 2.0f * t / kLoop + b);
147 const float hertz = 420.0f + 180.0f * slow + 90.0f * slower;
148 const float swell = 0.55f + 0.3f * slower + 0.15f * slow;
149 s[i] = 0.9f * swell * band.Run(noise.Next(), hertz, 1.6f).band;
150 }
151 return Looped(std::move(s));
152}
153
154// Fire falling: crackles in little bursts, a hiss now and then where it lands,
155// and a deep rumble under it all.
156Samples Fire(uint32_t seed) {
157 Noise noise(seed);
158 Samples s = Silence();
159 Filter high;
160 const int bursts = static_cast<int>(9.0f * (kLoop + kSeam));
161 for (int b = 0; b < bursts; ++b) {
162 auto at = static_cast<std::size_t>(noise.Uniform() * s.size());
163 const int clicks = static_cast<int>(noise.Between(2.0f, 9.0f));
164 for (int c = 0; c < clicks; ++c) {
165 at += static_cast<std::size_t>(SamplesIn(noise.Between(0.004f, 0.04f)));
166 const float g = noise.Between(0.2f, 0.7f);
167 const auto length =
168 static_cast<std::size_t>(SamplesIn(noise.Between(0.0006f, 0.003f)));
169 for (std::size_t i = 0; i < length && at + i < s.size(); ++i) {
170 s[at + i] += g * high.Run(noise.Next(), 2500.0f).high *
171 (1.0f - static_cast<float>(i) / length);
172 }
173 }
174 }
175 const int hisses = static_cast<int>(2.5f * (kLoop + kSeam));
176 for (int h = 0; h < hisses; ++h) {
177 const auto at = static_cast<std::size_t>(noise.Uniform() * s.size());
178 const float hertz = noise.Between(4500.0f, 7000.0f);
179 Filter band;
180 const auto length = static_cast<std::size_t>(SamplesIn(0.25f));
181 for (std::size_t i = 0; i < length && at + i < s.size(); ++i) {
182 const float t = static_cast<float>(i) / kRate;
183 s[at + i] += 0.12f * Envelope(t, 0.01f, 0.07f) *
184 band.Run(noise.Next(), hertz, 2.0f).band;
185 }
186 }
187 Filter low;
188 for (float& v : s) {
189 v += 0.5f * low.Run(noise.Next(), 90.0f, 0.6f).low;
190 }
191 return Looped(std::move(s));
192}
193
194// Lava: slow, low bubbles, each a tone that drops as it bursts, over a
195// rumble.
196Samples Lava(uint32_t seed) {
197 Noise noise(seed);
198 Samples s = Silence();
199 float at = noise.Between(0.0f, 0.5f);
200 while (at < kLoop + kSeam) {
201 const auto start = static_cast<std::size_t>(SamplesIn(at));
202 const float from = noise.Between(110.0f, 190.0f);
203 float phase = 0.0f;
204 const auto length = static_cast<std::size_t>(SamplesIn(0.25f));
205 for (std::size_t i = 0; i < length && start + i < s.size(); ++i) {
206 const float t = static_cast<float>(i) / kRate;
207 phase += from * (1.0f - 0.55f * std::min(t / 0.15f, 1.0f)) / kRate;
208 s[start + i] +=
209 0.35f * Envelope(t, 0.02f, 0.06f) * std::sin(kTau * phase);
210 }
211 at += noise.Between(0.3f, 1.3f);
212 }
213 Filter low;
214 for (float& v : s) {
215 v += 0.35f * low.Run(noise.Next(), 60.0f, 0.6f).low;
216 }
217 return Looped(std::move(s));
218}
219
220// Crickets: a few, each chirping on its own pitch and in its own time, a
221// chirp being three quick pulses of a high tone.
222Samples Crickets(uint32_t seed) {
223 Noise noise(seed);
224 Samples s = Silence();
225 for (int c = 0; c < 4; ++c) {
226 const float hertz = noise.Between(4000.0f, 5200.0f);
227 const float every = noise.Between(0.6f, 1.2f);
228 const float gain = noise.Between(0.05f, 0.12f);
229 float at = noise.Between(0.0f, every);
230 while (at < kLoop + kSeam) {
231 for (int p = 0; p < 3; ++p) {
232 const auto start = static_cast<std::size_t>(SamplesIn(at + 0.045f * p));
233 const auto length = static_cast<std::size_t>(SamplesIn(0.02f));
234 for (std::size_t i = 0; i < length && start + i < s.size(); ++i) {
235 const float t = static_cast<float>(i) / kRate;
236 const float shape = std::sin(3.1415926f * t / 0.02f);
237 s[start + i] += gain * shape * std::sin(kTau * hertz * t);
238 }
239 }
240 at += every * noise.Between(0.9f, 1.1f);
241 }
242 }
243 return Looped(std::move(s));
244}
245
246// A bird: a few quick notes, each sliding from one pitch to another, with a
247// little trill on it.
248Samples Bird(Noise& noise) {
249 Samples s;
250 float at = 0.0f;
251 const int notes = static_cast<int>(noise.Between(2.0f, 6.0f));
252 const float low = noise.Between(2200.0f, 3200.0f);
253 for (int n = 0; n < notes; ++n) {
254 const float from = low * noise.Between(1.0f, 1.7f);
255 const float to = low * noise.Between(1.0f, 1.7f);
256 const float lasts = noise.Between(0.06f, 0.16f);
257 Samples note(static_cast<std::size_t>(SamplesIn(lasts)));
258 float phase = 0.0f;
259 for (std::size_t i = 0; i < note.size(); ++i) {
260 const float t = static_cast<float>(i) / kRate;
261 const float f =
262 from + (to - from) * t / lasts + 120.0f * std::sin(kTau * 32.0f * t);
263 phase += f / kRate;
264 note[i] =
265 0.25f * std::sin(3.1415926f * t / lasts) * std::sin(kTau * phase);
266 }
267 Mix(s, note, at);
268 at += lasts + noise.Between(0.03f, 0.12f);
269 }
270 return s;
271}
272
273// An owl, far off: hoo, then hoo-hoo, low and soft.
274Samples Owl(Noise& noise) {
275 Samples s;
276 const float hertz = noise.Between(330.0f, 400.0f);
277 float at = 0.0f;
278 for (const float lasts : {0.42f, 0.22f, 0.36f}) {
279 Samples note(static_cast<std::size_t>(SamplesIn(lasts)));
280 for (std::size_t i = 0; i < note.size(); ++i) {
281 const float t = static_cast<float>(i) / kRate;
282 const float shape = std::sin(3.1415926f * t / lasts);
283 const float f = hertz * (1.0f - 0.05f * t / lasts);
284 note[i] =
285 0.3f * shape * shape *
286 (std::sin(kTau * f * t) + 0.15f * std::sin(kTau * 2.0f * f * t));
287 }
288 Mix(s, note, at);
289 at += lasts + (at == 0.0f ? 0.45f : 0.08f);
290 }
291 return s;
292}
293
294// A drop of water off the stone, and its echo off the Crypt's walls.
295Samples Drip(Noise& noise) {
296 Samples plink;
297 plink.resize(static_cast<std::size_t>(SamplesIn(0.25f)), 0.0f);
298 Drop(plink, 0, noise.Between(1300.0f, 2600.0f), -0.25f, 0.035f, 0.35f);
299 Samples s;
300 Mix(s, plink, 0.0f);
301 Mix(s, plink, 0.17f, 0.4f);
302 Mix(s, plink, 0.34f, 0.16f);
303 Mix(s, plink, 0.51f, 0.06f);
304 return s;
305}
306
307float Clamp01(float v) { return std::clamp(v, 0.0f, 1.0f); }
308
309} // namespace
310
311Samples ThunderSound(float metres, uint32_t seed) {
312 Noise noise(seed);
313 const float lasts = noise.Between(4.0f, 6.5f);
314 Samples s(static_cast<std::size_t>(SamplesIn(lasts)), 0.0f);
315 // Near, it cracks first, as the air splits.
316 if (metres < 220.0f) {
317 Filter high;
318 const auto length = static_cast<std::size_t>(SamplesIn(0.3f));
319 for (std::size_t i = 0; i < length; ++i) {
320 const float t = static_cast<float>(i) / kRate;
321 s[i] += 0.8f * Envelope(t, 0.002f, 0.06f) *
322 high.Run(noise.Next(), 900.0f).high;
323 }
324 }
325 // Then it rolls: low noise, darker as it goes, swelling a few times as
326 // the sound comes back off the clouds and the hills.
327 Filter low;
328 const float a = noise.Between(0.0f, kTau);
329 for (std::size_t i = 0; i < s.size(); ++i) {
330 const float t = static_cast<float>(i) / kRate;
331 const float hertz = 150.0f - 95.0f * std::min(t / lasts, 1.0f);
332 const float swells = 0.6f + 0.4f * std::sin(kTau * 0.7f * t + a);
333 const float shape = Envelope(t, 0.15f, lasts * 0.35f) * swells;
334 s[i] += 2.2f * shape * low.Run(noise.Next(), hertz, 0.8f).low;
335 }
336 FadeOut(s);
337 return s;
338}
339
340Hearing HearingOf(int land, const protocol::Sky& sky, bool by_water,
341 bool weather) {
342 Hearing h;
343 const float strength = weather ? Clamp01(sky.strength()) : 0.0f;
344 const protocol::Weather kind =
345 weather ? sky.weather() : protocol::WEATHER_CLEAR;
346 const float dark = Darkness(sky.day());
347 if (kLands[static_cast<std::size_t>(land) % kLands.size()].dungeon) {
348 h.drips = 0.9f;
349 h.wind = 0.25f + (kind == protocol::WEATHER_MIST ? 0.25f * strength : 0.0f);
350 return h;
351 }
352 if (land == kAshlandsLand) {
353 h.lava = 0.55f;
354 h.fire = kind == protocol::WEATHER_FIRE ? strength : 0.0f;
355 h.wind = 0.2f + (kind == protocol::WEATHER_ASH ? 0.45f * strength : 0.0f) +
356 0.4f * h.fire;
357 return h;
358 }
359 if (land == kMeadowsLand) {
360 const bool storm = kind == protocol::WEATHER_STORM;
361 h.rain = kind == protocol::WEATHER_RAIN || storm ? strength : 0.0f;
362 h.downpour = storm ? strength : 0.0f;
363 h.water = by_water ? h.rain : 0.0f;
364 h.wind = Clamp01(0.12f + 0.25f * h.rain + 0.55f * h.downpour);
365 const float dry = Clamp01(1.0f - 1.6f * h.rain);
366 h.crickets = dark * dry;
367 h.owls = dark * dry;
368 // The birds sing most in the morning, and none at night.
369 const float morning =
370 1.0f - Clamp01(std::abs(sky.day() - 0.32f) / 0.2f) * 0.6f;
371 h.birds = (1.0f - dark) * dry * morning;
372 }
373 return h;
374}
375
376Ambience::Ambience(uint32_t seed) : noise_(seed) {
377 // Each channel its own, so that it's wide, all round us.
378 const uint32_t s = seed * 31u;
379 beds_[kRain] = {Rain(s + 1, 420.0f, 0.22f, 0.05f),
380 Rain(s + 2, 420.0f, 0.22f, 0.05f)};
381 beds_[kDownpour] = {Rain(s + 3, 2600.0f, 0.12f, 0.16f),
382 Rain(s + 4, 2600.0f, 0.12f, 0.16f)};
383 beds_[kWater] = {OnWater(s + 5), OnWater(s + 6)};
384 beds_[kWind] = {Wind(s + 7), Wind(s + 8)};
385 beds_[kFire] = {Fire(s + 9), Fire(s + 10)};
386 beds_[kLava] = {Lava(s + 11), Lava(s + 12)};
387 beds_[kCrickets] = {Crickets(s + 13), Crickets(s + 14)};
388}
389
390bool Ambience::Recorded(Bed bed, Samples& left, Samples& right) const {
391 const std::size_t length = std::min(left.size(), right.size());
392 left.resize(length);
393 right.resize(length);
394 const std::size_t seam = static_cast<std::size_t>(SamplesIn(kSeam));
395 Fold(left, seam);
396 Fold(right, seam);
397 // As loud as the loop it replaces, whatever the recording's own loudness.
398 const float made = 0.5f * (Rms(beds_[bed].left) + Rms(beds_[bed].right));
399 const float recorded = 0.5f * (Rms(left) + Rms(right));
400 if (left.empty() || recorded <= 0.0f) {
401 return false;
402 }
403 const float gain = made / recorded;
404 for (Samples* channel : {&left, &right}) {
405 for (float& s : *channel) {
406 s *= gain;
407 }
408 }
409 return true;
410}
411
413 if (Recorded(kFire, left, right)) {
414 beds_[kFire] = {.left = std::move(left), .right = std::move(right)};
415 }
416}
417
418void Ambience::UseGust(const Samples& left, const Samples& right) {
419 // From the middle, as it blows past, at about the loudness of the wind.
420 gust_.resize(std::min(left.size(), right.size()));
421 for (std::size_t i = 0; i < gust_.size(); ++i) {
422 gust_[i] = 0.5f * (left[i] + right[i]);
423 }
424 if (const float peak = Peak(gust_); peak > 0.0f) {
425 Normalize(gust_, 0.5f);
426 }
427 FadeOut(gust_);
428}
429
431 if (!Recorded(kRain, left, right)) {
432 return;
433 }
434 // A downpour is more of it, from other places in the minute, and the rain
435 // on the sea is some more again.
436 const std::size_t third = left.size() / 3;
437 beds_[kDownpour] = {.left = From(left, third), .right = From(right, third)};
438 beds_[kWater] = {.left = From(left, 2 * third),
439 .right = From(right, 2 * third)};
440 beds_[kRain] = {.left = std::move(left), .right = std::move(right)};
441}
442
443void Ambience::Call(const Samples& samples, float gain, float pan, float wait) {
444 voices_.push_back({.samples = samples,
445 .wait = static_cast<std::size_t>(SamplesIn(wait)),
446 .gain = gain,
447 .pan = pan});
448}
449
450void Ambience::Thunder(float metres, float pan) {
451 Call(ThunderSound(metres,
452 static_cast<uint32_t>(noise_.Uniform() * 1e6f) + ++takes_),
453 1.0f / (1.0f + metres / 170.0f), pan, metres / kSoundSpeed);
454}
455
456void Ambience::Fill(std::span<float> left, std::span<float> right,
457 const Hearing& hearing) {
458 const std::array<float, kBeds> wanted = {
459 0.55f * hearing.rain, 0.6f * hearing.downpour, 0.45f * hearing.water,
460 0.4f * hearing.wind, 0.5f * hearing.fire, 0.45f * hearing.lava,
461 0.3f * hearing.crickets};
462 const float glide = 1.0f / (kGlide * kRate);
463 const std::size_t frames = std::min(left.size(), right.size());
464 for (std::size_t i = 0; i < frames; ++i) {
465 for (int b = 0; b < kBeds; ++b) {
466 Loop& bed = beds_[b];
467 bed.level += (wanted[b] - bed.level) * glide;
468 if (bed.level > 1e-4f) {
469 const std::size_t at = (played_ + i) % bed.left.size();
470 left[i] += bed.level * bed.left[at];
471 right[i] += bed.level * bed.right[at];
472 }
473 }
474 }
475 played_ += frames;
476
477 // Now and then: birds by day, an owl at night, and drips in the Crypt.
478 const float seconds = static_cast<float>(frames) / kRate;
479 const auto now_and_then = [&](float& next, float level, float every,
480 auto&& make, float gain) {
481 if (level < 0.05f) {
482 return;
483 }
484 next -= seconds;
485 if (next > 0.0f) {
486 return;
487 }
488 Call(make(noise_), gain * level, noise_.Between(-0.8f, 0.8f));
489 next = every * noise_.Between(0.5f, 1.5f) / level;
490 };
491 now_and_then(next_bird_, hearing.birds, 2.5f, Bird, 0.5f);
492 now_and_then(next_owl_, hearing.owls, 28.0f, Owl, 0.45f);
493 now_and_then(next_drip_, hearing.drips, 1.6f, Drip, 0.5f);
494 // A gust of wind, the windier the more often, when there's one recorded.
495 if (!gust_.empty()) {
496 now_and_then(
497 next_gust_, hearing.wind, 6.0f, [this](Noise&) { return gust_; }, 0.9f);
498 }
499
500 for (Voice& voice : voices_) {
501 for (std::size_t i = 0; i < frames; ++i) {
502 if (voice.wait > 0) {
503 --voice.wait;
504 continue;
505 }
506 if (voice.at >= voice.samples.size()) {
507 break;
508 }
509 const float v = voice.gain * voice.samples[voice.at++];
510 left[i] += v * std::min(1.0f, 1.0f - voice.pan);
511 right[i] += v * std::min(1.0f, 1.0f + voice.pan);
512 }
513 }
514 std::erase_if(voices_,
515 [](const Voice& v) { return v.at >= v.samples.size(); });
516}
517
518} // namespace nanommo
void UseGust(const Samples &left, const Samples &right)
Definition ambience.cc:418
void Fill(std::span< float > left, std::span< float > right, const Hearing &hearing)
Definition ambience.cc:456
void UseRain(Samples left, Samples right)
Definition ambience.cc:430
void Thunder(float metres, float pan)
Definition ambience.cc:450
void UseFire(Samples left, Samples right)
Definition ambience.cc:412
Ambience(uint32_t seed)
Definition ambience.cc:376
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 Next()
From -1 to 1: white noise, a sample at a time.
Definition synth.h:29
Definition chat.cc:9
Samples ThunderSound(float metres, uint32_t seed)
A bolt's thunder: a crack when it's near, then a long low roll.
Definition ambience.cc:311
float Darkness(float day)
Definition daylight.cc:227
std::vector< float > Samples
One channel of sound, at kRate.
Definition synth.h:14
constexpr std::array< Land, 3 > kLands
Definition lands.h:31
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
Hearing HearingOf(int land, const protocol::Sky &sky, bool by_water, bool weather)
Definition ambience.cc:340
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
float water
Drops on the sea, when we're by it.
Definition ambience.h:23
float lava
The lava, bubbling.
Definition ambience.h:28
float fire
Fire falling: crackling, and hissing where it lands.
Definition ambience.h:26
float drips
Water dripping in the Crypt, off the stone.
Definition ambience.h:33