OpenBF1942-Plus
Refractor 1, rebuilt from scratch
Loading...
Searching...
No Matches
audio.h
Go to the documentation of this file.
1// Audio: RIFF PCM .wav, .ssc sound scripts, and a software mixer (no SDL; the executable feeds a device).
2// Spec: docs/formats/sound.md
3#pragma once
4#include <cstdint>
5#include <functional>
6#include <memory>
7#include <mutex>
8#include <string>
9#include <vector>
10
11namespace obf {
12
13struct Wav {
14 int rate = 0, channels = 1;
15 std::vector<int16_t> samples; // interleaved
16 size_t frames() const { return channels ? samples.size() / static_cast<size_t>(channels) : 0; }
17};
18Wav wav_parse(const std::vector<uint8_t>& data); // PCM 8 / 16-bit, mono or stereo; throws std::runtime_error
19
20// beginEffect ... endEffect: one control curve on a layer.
24 enum Shape { Ramp, Linear } shape = Ramp;
25 std::vector<float> params;
26 float eval(float x) const; // Ramp: x0 x1 y0 dy -> y0 + dy * clamp((x - x0) / (x1 - x0)); Linear: a + b * x
27};
28
29// One "load" in a patch.
30struct SoundLayer {
31 std::string wav; // VFS path, @ROOT / @RTD resolved (sound/44khz/...)
32 bool loop = false, stereo = false; // stereo: not positioned (first-person sounds)
33 bool finish = false; // stop FinishSample: a released loop plays its pass to the end
34 bool trigger = false; // trigger Volume: starts when its time / control volume first rises above 0
35 bool on_release = false; // trigger Release: starts when the patch is released (gear stops)
36 float volume = 1, min_distance = 1;
37 std::vector<SoundEffect> effects;
38};
39
40struct SoundPatch {
41 std::vector<SoundLayer> layers;
42};
43
44// Parses one template level (HIGH / MEDIUM / LOW) of a script; read(path) returns a file's text or "" if missing.
45// #include paths are relative to the including file.
46SoundPatch ssc_parse(const std::string& path, const std::function<std::string(const std::string&)>& read,
47 const std::string& level = "HIGH"); // some scripts ship only HIGH (Elco torpedoes)
48// The same, one patch per newPatch. Hand weapons: 0 single shot, 1 reload, 2 distant shot, 5 automatic fire (loop).
49std::vector<SoundPatch> ssc_patches(const std::string& path, const std::function<std::string(const std::string&)>& read,
50 const std::string& level = "HIGH");
51
52struct Listener {
53 float pos[3] = {}, right[3] = {1, 0, 0};
54};
55
56// Mixes playing patches into interleaved stereo float. Thread-safe: the game thread adds and moves voices while the
57// audio thread mixes; the lock covers only the voice list, so callers load files and parse scripts outside it.
58class Mixer {
59public:
60 explicit Mixer(int rate) : rate_(rate) {}
61 // wavs: one per layer (null skips the layer). Returns a handle for update / release.
62 int play(const SoundPatch& patch, const std::vector<std::shared_ptr<const Wav>>& wavs, const float pos[3], float control = 0);
63 void update(int handle, const float pos[3], float control, float speed = 0, float accel = 0);
64 void release(int handle); // loops fade by their TimeRelease effects (or stop), one-shots finish
65 void mix(float* out, int frames, const Listener& l); // adds into out (2 * frames)
66 void listen(const Listener& l); // the listener mix(out, frames) uses
67 void mix(float* out, int frames);
68 size_t voices() const {
69 std::lock_guard<std::mutex> g(m_);
70 return voices_.size();
71 }
72
73private:
74 struct Voice {
75 int handle;
76 SoundLayer layer;
77 std::shared_ptr<const Wav> wav;
78 float pos[3], control = 0, time = 0, release_time = -1, speed = 0, accel = 0;
79 double phase = 0;
80 bool started = false; // a trigger layer that has begun
81 };
82 static constexpr size_t kMaxVoices = 256;
83 int rate_, next_ = 1;
84 std::vector<Voice> voices_;
85 Listener listener_;
86 mutable std::mutex m_;
87};
88
89} // namespace obf
void update(int handle, const float pos[3], float control, float speed=0, float accel=0)
Definition audio.cpp:199
Mixer(int rate)
Definition audio.h:60
void listen(const Listener &l)
Definition audio.cpp:214
void mix(float *out, int frames, const Listener &l)
Definition audio.cpp:228
int play(const SoundPatch &patch, const std::vector< std::shared_ptr< const Wav > > &wavs, const float pos[3], float control=0)
Definition audio.cpp:182
size_t voices() const
Definition audio.h:68
void release(int handle)
Definition audio.cpp:205
Definition anim.cpp:7
Wav wav_parse(const std::vector< uint8_t > &d)
Definition audio.cpp:13
std::vector< SoundPatch > ssc_patches(const std::string &path, const std::function< std::string(const std::string &)> &read, const std::string &level)
Definition audio.cpp:169
SoundPatch ssc_parse(const std::string &path, const std::function< std::string(const std::string &)> &read, const std::string &level)
Definition audio.cpp:176
Definition audio.h:52
float pos[3]
Definition audio.h:53
float right[3]
Definition audio.h:53
Definition audio.h:21
enum obf::SoundEffect::Dest dest
float eval(float x) const
Definition audio.cpp:48
enum obf::SoundEffect::Source source
enum obf::SoundEffect::Shape shape
Source
Definition audio.h:23
@ Acceleration
Definition audio.h:23
@ Unknown
Definition audio.h:23
@ Distance
Definition audio.h:23
@ Time
Definition audio.h:23
@ TimeRelease
Definition audio.h:23
@ Speed
Definition audio.h:23
@ Control
Definition audio.h:23
std::vector< float > params
Definition audio.h:25
Dest
Definition audio.h:22
@ Volume
Definition audio.h:22
@ Pitch
Definition audio.h:22
Shape
Definition audio.h:24
@ Ramp
Definition audio.h:24
@ Linear
Definition audio.h:24
Definition audio.h:30
std::string wav
Definition audio.h:31
float min_distance
Definition audio.h:36
bool stereo
Definition audio.h:32
bool loop
Definition audio.h:32
bool finish
Definition audio.h:33
float volume
Definition audio.h:36
std::vector< SoundEffect > effects
Definition audio.h:37
bool trigger
Definition audio.h:34
bool on_release
Definition audio.h:35
Definition audio.h:40
std::vector< SoundLayer > layers
Definition audio.h:41
Definition audio.h:13
size_t frames() const
Definition audio.h:16
int rate
Definition audio.h:14
std::vector< int16_t > samples
Definition audio.h:15
int channels
Definition audio.h:14