nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
soundboard.cc
Go to the documentation of this file.
1#include "client/soundboard.h"
2
3#include <cstddef>
4#include <memory>
5#include <optional>
6#include <utility>
7
8namespace nanommo {
9
10namespace {
11
12// Steps vary more than skills, as we hear far more of them.
13constexpr float kStepPitch = 0.08f;
14constexpr float kStepVolume = 0.3f;
15constexpr float kSkillPitch = 0.04f;
16constexpr float kSkillVolume = 0.1f;
17
18// Steps sit under everything else.
19constexpr float kStepLevel = 0.6f;
20
21} // namespace
22
23Soundboard::Soundboard(const AudioDevice& device, const SkillTable& skills,
24 std::optional<Samples> departure)
25 : skill_variety_(1, kSkillPitch, kSkillVolume) {
26 for (const Ground ground : kGrounds) {
27 Takes& takes = steps_[ground];
28 for (int take = 0; take < kStepTakes; ++take) {
29 // A seed for each take of each ground.
30 const auto seed = static_cast<uint32_t>(
31 static_cast<int>(ground) * kStepTakes + take + 1);
32 takes.push_back(Clip::Load(device, nanommo::Step(ground, seed), 4));
33 }
34 step_varieties_.emplace(ground, Variety(static_cast<uint32_t>(ground),
35 kStepPitch, kStepVolume));
36 }
37 for (const auto& [id, skill] : skills.by_id) {
38 if (skill.look().fx().empty()) {
39 continue;
40 }
41 for (const bool target : {false, true}) {
42 for (int take = 0; take < kSkillTakes; ++take) {
43 skills_[{id, target}].push_back(Clip::Load(
44 device,
45 SkillSound(skill.look(), static_cast<uint32_t>(take + 1), target),
46 2));
47 }
48 }
49 }
50 // A recording plays as loud as the sound it replaces, at its loudest.
52 if (departure && Peak(*departure) > 0.0f) {
53 Normalize(*departure, Peak(made));
54 made = *std::move(departure);
55 }
56 departure_ = Clip::Load(device, made, 1);
57 for (std::size_t land = 0; land < kLandSounds.size(); ++land) {
58 arrivals_.push_back(
59 Clip::Load(device, nanommo::Arrival(static_cast<int>(land)), 1));
60 }
61}
62
63void Soundboard::Play(const Takes& takes, Variety& variety, Heard heard) {
64 if (takes.empty() || heard.volume <= 0.0f) {
65 return;
66 }
67 const Variety::Take take = variety.Next(static_cast<int>(takes.size()));
68 takes[take.take]->Play(volume_ * heard.volume * take.volume, heard.pan,
69 take.pitch);
70}
71
72void Soundboard::Step(Ground ground, Heard heard) {
73 heard.volume *= kStepLevel;
74 Play(steps_.at(ground), step_varieties_.at(ground), heard);
75}
76
77void Soundboard::Skill(uint32_t skill, bool target, Heard heard) {
78 const auto takes = skills_.find({skill, target});
79 if (takes != skills_.end()) {
80 Play(takes->second, skill_variety_, heard);
81 }
82}
83
84void Soundboard::Departure() { departure_->Play(volume_, 0.0f, 1.0f); }
85
86void Soundboard::Arrival(int land) {
87 if (land >= 0 && land < static_cast<int>(arrivals_.size())) {
88 arrivals_[land]->Play(volume_, 0.0f, 1.0f);
89 }
90}
91
92} // namespace nanommo
static std::unique_ptr< Clip > Load(const AudioDevice &device, const Samples &samples, int voices)
Definition audio.cc:109
static constexpr int kStepTakes
Takes of each step, and of each skill.
Definition soundboard.h:23
Soundboard(const AudioDevice &device, const SkillTable &skills, std::optional< Samples > departure=std::nullopt)
Definition soundboard.cc:23
static constexpr int kSkillTakes
Definition soundboard.h:24
void Arrival(int land)
Into the land, by its place in the lands.
Definition soundboard.cc:86
void Skill(uint32_t skill, bool target, Heard heard)
Definition soundboard.cc:77
void Step(Ground ground, Heard heard)
Definition soundboard.cc:72
Definition chat.cc:9
std::vector< float > Samples
One channel of sound, at kRate.
Definition synth.h:14
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
constexpr std::array< Ground, 7 > kGrounds
Definition foley.h:17
float Peak(const Samples &samples)
The loudest sample, whichever way it goes.
Definition synth.cc:79
constexpr std::array< LandSound, 3 > kLandSounds
Definition foley.h:33
void Normalize(Samples &samples, float peak)
Louder or quieter, so that the loudest sample is at the peak.
Definition synth.cc:87
Samples Departure(uint32_t seed)
Going through a gate: a rush up, as the screen covers the world.
Definition foley.cc:583
std::map< uint32_t, skills::Skill > by_id
Definition skills.h:17