OpenBF1942-Plus
Refractor 1, rebuilt from scratch
Loading...
Searching...
No Matches
sound.h
Go to the documentation of this file.
1// Game sounds: finds a template's sound script (loadSoundScript), loads its .wav files once, plays them on a Mixer.
2// Spec: docs/formats/sound.md
3#pragma once
4#include <map>
5#include <memory>
6#include <string>
7
8#include "audio.h"
9#include "con.h"
10#include "vfs.h"
11
12namespace obf {
13
14class Sounds {
15public:
16 Sounds(const Vfs& vfs, const Con& con, int rate) : mixer(rate), vfs_(vfs), con_(con) {}
17 // A FireArms template fired at `pos` by `source` (any id stable per shooter) at time `now` (s). Single shots play
18 // patch 0 and the distant patch 2; automatic weapons (a looping patch 5) loop it while their shooter keeps firing.
19 void shot(const std::string& tmpl, const float pos[3], int source, float now);
20 void reload(const std::string& tmpl, const float pos[3]); // patch 1
21 void effect(const std::string& tmpl, const float pos[3]); // an EffectBundle's script: every patch, once
22 // A soldier sound (script file of SoldierSound.inc, e.g. "soldierrun.ssc") of the soldier template tmpl: one layer
23 // of the patch, the next variant each time; voices in tmpl's radio language.
24 void soldier(const std::string& script, int patch, const float pos[3], const std::string& tmpl);
25 void tick(float now); // ends automatic fire that stopped (no shot for 0.25 s)
26 // Looping part sounds (engines, tracks, turrets, wings): each template's own script, one loop per key; a key
27 // missing from the list stops its loop.
28 struct Running {
29 long long key;
30 std::string tmpl;
31 float pos[3];
32 float control, speed, accel; // curve sources Default, Speed (m/s), Acceleration (m/s2)
33 int patch = -1; // only that patch of the script (landing gear: 0 down, 1 up); -1 all
34 std::string script{}; // a soldier's SoldierSound.inc script ("soldierfallinghigh.ssc"), tmpl the soldier
35 };
36 void loops(const std::vector<Running>& running);
37 // Script path of a template, or (children) of its first child (addTemplate, depth-first) of the given type; "" if none.
38 std::string script(const std::string& tmpl, const std::string& type = "", bool children = true) const;
39
41
42private:
43 const SoundPatch* patch(const std::string& path);
44 std::string language(const std::string& tmpl) const;
45 SoundPatch soldier_patch(const std::string& script, int patch, const std::string& tmpl);
46 const SoundPatch* weapon_patch(const std::string& tmpl, size_t index);
47 int play_patch(const SoundPatch& p, const float pos[3], float control, bool once);
48 int play(const std::string& path, const float pos[3], float control);
49
50 const Vfs& vfs_;
51 const Con& con_;
52 std::map<std::string, std::unique_ptr<SoundPatch>> patches_; // by script path; null: none
53 std::map<std::string, std::shared_ptr<const Wav>> wavs_; // by path; null: missing or bad
54 std::map<std::string, std::string> scripts_; // template (+ type) -> script
55 std::map<std::string, std::vector<SoundPatch>> weapon_; // by template: its script's patches
56 struct Burst {
57 int handle;
58 float last;
59 };
60 std::map<int, Burst> bursts_; // automatic fire by source
61 std::map<long long, int> loops_; // part key -> mixer handle (-1: no script)
62 unsigned variant_ = 0; // rotates through a patch's variants
63};
64
65} // namespace obf
Definition con.h:21
Definition audio.h:58
std::string script(const std::string &tmpl, const std::string &type="", bool children=true) const
Definition sound.cpp:17
Sounds(const Vfs &vfs, const Con &con, int rate)
Definition sound.h:16
void tick(float now)
Definition sound.cpp:169
Mixer mixer
Definition sound.h:40
void shot(const std::string &tmpl, const float pos[3], int source, float now)
Definition sound.cpp:95
void effect(const std::string &tmpl, const float pos[3])
Definition sound.cpp:113
void loops(const std::vector< Running > &running)
Definition sound.cpp:184
void reload(const std::string &tmpl, const float pos[3])
Definition sound.cpp:109
void soldier(const std::string &script, int patch, const float pos[3], const std::string &tmpl)
Definition sound.cpp:164
Definition vfs.h:11
Definition con.py:1
Definition anim.cpp:7
Definition audio.h:40
Definition sound.h:28
float pos[3]
Definition sound.h:31
int patch
Definition sound.h:33
long long key
Definition sound.h:29
float control
Definition sound.h:32
std::string tmpl
Definition sound.h:30
std::string script
Definition sound.h:34
float speed
Definition sound.h:32
float accel
Definition sound.h:32