nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
ambience.h
Go to the documentation of this file.
1#ifndef NANOMMO_CLIENT_AMBIENCE_H_
2#define NANOMMO_CLIENT_AMBIENCE_H_
3
4#include <array>
5#include <cstddef>
6#include <cstdint>
7#include <span>
8#include <vector>
9
10#include "client/synth.h"
11#include "protocol/nanommo.pb.h"
12
13namespace nanommo {
14
15/// How loud each of the weather's sounds and the time of day's should be,
16/// from nothing to full.
17struct Hearing {
18 /// Drops on the ground and the leaves, and more of them, harder, in a
19 /// storm.
20 float rain = 0.0f;
21 float downpour = 0.0f;
22 /// Drops on the sea, when we're by it.
23 float water = 0.0f;
24 float wind = 0.0f;
25 /// Fire falling: crackling, and hissing where it lands.
26 float fire = 0.0f;
27 /// The lava, bubbling.
28 float lava = 0.0f;
29 float crickets = 0.0f;
30 float birds = 0.0f;
31 float owls = 0.0f;
32 /// Water dripping in the Crypt, off the stone.
33 float drips = 0.0f;
34};
35
36/// What a land sounds like under its sky: the Meadows' rain, wind, birds
37/// and crickets, the Ashlands' lava and fire, the Crypt's drips. By the sea,
38/// the rain falls on the water. Off, the weather's quiet, and the rest
39/// keeps to the time of day.
40Hearing HearingOf(int land, const protocol::Sky& sky, bool by_water,
41 bool weather);
42
43/// The weather and the time of day, as sound: a few loops made as the game
44/// starts, that play all the time, each as loud as it should be now, and
45/// birds, owls, drips and thunder now and then, made as they come.
46class Ambience {
47 public:
48 explicit Ambience(uint32_t seed);
49
50 /// Rain recorded from the real thing, in place of the rain made from
51 /// numbers, its two channels as long as each other. It loops, its end
52 /// fading into its start, and plays as loud as the one it replaces.
53 void UseRain(Samples left, Samples right);
54
55 /// Fire recorded crackling, in place of the fire made from numbers, the
56 /// same way.
57 void UseFire(Samples left, Samples right);
58
59 /// A gust of wind recorded, which blows now and then over the wind made
60 /// from numbers, more often the windier it is.
61 void UseGust(const Samples& left, const Samples& right);
62
63 /// Adds the next of the sound into the two channels, as many samples as
64 /// they have, while each loop glides to how loud the hearing says.
65 void Fill(std::span<float> left, std::span<float> right,
66 const Hearing& hearing);
67
68 /// The thunder of a bolt this many metres away, from that side, from -1
69 /// for the left to 1 for the right. It comes as long after the flash as
70 /// sound takes to come that far.
71 void Thunder(float metres, float pan);
72
73 private:
74 enum Bed { kRain, kDownpour, kWater, kWind, kFire, kLava, kCrickets, kBeds };
75
76 /// Each loop goes round at its own length, so a long recording and a
77 /// short one made from numbers can play side by side.
78 struct Loop {
79 Samples left;
80 Samples right;
81 float level = 0.0f;
82 };
83
84 /// A sound that plays once, after a wait, from one side.
85 struct Voice {
86 Samples samples;
87 std::size_t at = 0;
88 std::size_t wait = 0;
89 float gain = 1.0f;
90 float pan = 0.0f;
91 };
92
93 void Call(const Samples& samples, float gain, float pan, float wait = 0.0f);
94
95 /// Makes a recording into a loop to play in place of the bed: its end
96 /// folded into its start, and as loud as the bed. False if there's
97 /// nothing in it.
98 bool Recorded(Bed bed, Samples& left, Samples& right) const;
99
100 Noise noise_;
101 std::array<Loop, kBeds> beds_;
102 std::size_t played_ = 0;
103 std::vector<Voice> voices_;
104 float next_bird_ = 2.0f;
105 float next_owl_ = 8.0f;
106 float next_drip_ = 1.0f;
107 float next_gust_ = 4.0f;
108 Samples gust_;
109 uint32_t takes_ = 0;
110};
111
112/// A bolt's thunder: a crack when it's near, then a long low roll.
113Samples ThunderSound(float metres, uint32_t seed);
114
115} // namespace nanommo
116
117#endif // NANOMMO_CLIENT_AMBIENCE_H_
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
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
std::vector< float > Samples
One channel of sound, at kRate.
Definition synth.h:14
Hearing HearingOf(int land, const protocol::Sky &sky, bool by_water, bool weather)
Definition ambience.cc:340
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