17constexpr float kTau = 6.2831853f;
21constexpr float kLoop = 8.0f;
22constexpr float kSeam = 0.5f;
26constexpr float kGlide = 1.5f;
29constexpr float kSoundSpeed = 343.0f;
31constexpr int kMeadowsLand = 0;
32constexpr int kAshlandsLand = 1;
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);
50void Fold(
Samples& made, std::size_t seam) {
51 if (made.size() <= 2 * seam) {
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);
63float Rms(
const Samples& samples) {
65 for (
const float s : samples) {
66 sum +=
static_cast<double>(s) * s;
68 return samples.empty() ? 0.0f
69 :
static_cast<float>(std::sqrt(
70 sum /
static_cast<double>(samples.size())));
77 for (std::size_t i = 0; i < samples.size(); ++i) {
78 moved[i] = samples[(at + i) % samples.size()];
89void Drop(
Samples& into, std::size_t at,
float hertz,
float rise,
float decay,
91 const std::size_t length =
static_cast<std::size_t
>(
SamplesIn(5.0f * decay));
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));
97 into[at + i] += gain * std::sin(kTau * phase) * std::exp(-t / decay);
103Samples Rain(uint32_t seed,
float per_second,
float loudness,
float body) {
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);
115 v += body * low.Run(noise.Next(), 700.0f, 0.5f).low;
117 return Looped(std::move(s));
121Samples OnWater(uint32_t seed) {
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);
131 return Looped(std::move(s));
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;
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;
151 return Looped(std::move(s));
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);
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);
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);
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;
189 v += 0.5f * low.Run(noise.Next(), 90.0f, 0.6f).low;
191 return Looped(std::move(s));
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);
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;
209 0.35f *
Envelope(t, 0.02f, 0.06f) * std::sin(kTau * phase);
211 at += noise.Between(0.3f, 1.3f);
215 v += 0.35f * low.Run(noise.Next(), 60.0f, 0.6f).low;
217 return Looped(std::move(s));
222Samples Crickets(uint32_t seed) {
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);
240 at += every * noise.Between(0.9f, 1.1f);
243 return Looped(std::move(s));
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);
259 for (std::size_t i = 0; i < note.size(); ++i) {
260 const float t =
static_cast<float>(i) /
kRate;
262 from + (to - from) * t / lasts + 120.0f * std::sin(kTau * 32.0f * t);
265 0.25f * std::sin(3.1415926f * t / lasts) * std::sin(kTau * phase);
268 at += lasts + noise.Between(0.03f, 0.12f);
276 const float hertz = noise.Between(330.0f, 400.0f);
278 for (
const float lasts : {0.42f, 0.22f, 0.36f}) {
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);
285 0.3f * shape * shape *
286 (std::sin(kTau * f * t) + 0.15f * std::sin(kTau * 2.0f * f * t));
289 at += lasts + (at == 0.0f ? 0.45f : 0.08f);
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);
301 Mix(s, plink, 0.17f, 0.4f);
302 Mix(s, plink, 0.34f, 0.16f);
303 Mix(s, plink, 0.51f, 0.06f);
307float Clamp01(
float v) {
return std::clamp(v, 0.0f, 1.0f); }
313 const float lasts = noise.
Between(4.0f, 6.5f);
316 if (metres < 220.0f) {
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) *
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;
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) {
349 h.
wind = 0.25f + (kind == protocol::WEATHER_MIST ? 0.25f * strength : 0.0f);
352 if (land == kAshlandsLand) {
354 h.
fire = kind == protocol::WEATHER_FIRE ? strength : 0.0f;
355 h.
wind = 0.2f + (kind == protocol::WEATHER_ASH ? 0.45f * strength : 0.0f) +
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;
365 const float dry = Clamp01(1.0f - 1.6f * h.
rain);
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;
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)};
390bool Ambience::Recorded(Bed bed,
Samples& left,
Samples& right)
const {
391 const std::size_t length = std::min(left.size(), right.size());
393 right.resize(length);
394 const std::size_t seam =
static_cast<std::size_t
>(
SamplesIn(kSeam));
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) {
403 const float gain = made / recorded;
404 for (
Samples* channel : {&left, &right}) {
405 for (
float& s : *channel) {
413 if (Recorded(kFire, left, right)) {
414 beds_[kFire] = {.left = std::move(left), .right = std::move(right)};
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]);
424 if (
const float peak =
Peak(gust_); peak > 0.0f) {
431 if (!Recorded(kRain, left, right)) {
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)};
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)),
452 static_cast<uint32_t
>(noise_.Uniform() * 1e6f) + ++takes_),
453 1.0f / (1.0f + metres / 170.0f), pan, metres / kSoundSpeed);
458 const std::array<float, kBeds> wanted = {
460 0.4f * hearing.
wind, 0.5f * hearing.
fire, 0.45f * hearing.
lava,
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];
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) {
488 Call(make(noise_), gain * level, noise_.Between(-0.8f, 0.8f));
489 next = every * noise_.Between(0.5f, 1.5f) / level;
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);
495 if (!gust_.empty()) {
497 next_gust_, hearing.
wind, 6.0f, [
this](
Noise&) { return gust_; }, 0.9f);
500 for (Voice& voice : voices_) {
501 for (std::size_t i = 0; i < frames; ++i) {
502 if (voice.wait > 0) {
506 if (voice.at >= voice.samples.size()) {
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);
514 std::erase_if(voices_,
515 [](
const Voice& v) {
return v.at >= v.samples.size(); });
void UseGust(const Samples &left, const Samples &right)
void Fill(std::span< float > left, std::span< float > right, const Hearing &hearing)
void UseRain(Samples left, Samples right)
void Thunder(float metres, float pan)
void UseFire(Samples left, Samples right)
Out Run(float in, float hertz, float q=0.7f)
float Between(float low, float high)
From low to high.
float Next()
From -1 to 1: white noise, a sample at a time.
Samples ThunderSound(float metres, uint32_t seed)
A bolt's thunder: a crack when it's near, then a long low roll.
float Darkness(float day)
std::vector< float > Samples
One channel of sound, at kRate.
constexpr std::array< Land, 3 > kLands
int SamplesIn(float seconds)
The seconds, as a count of samples.
void Mix(Samples &mix, const Samples &sound, float at, float gain)
void FadeOut(Samples &samples)
float Peak(const Samples &samples)
The loudest sample, whichever way it goes.
Hearing HearingOf(int land, const protocol::Sky &sky, bool by_water, bool weather)
void Normalize(Samples &samples, float peak)
Louder or quieter, so that the loudest sample is at the peak.
float Envelope(float t, float attack, float decay)
float water
Drops on the sea, when we're by it.
float lava
The lava, bubbling.
float fire
Fire falling: crackling, and hissing where it lands.
float drips
Water dripping in the Crypt, off the stone.