nngn
Loading...
Searching...
No Matches
audio.h
Go to the documentation of this file.
1#ifndef NNGN_AUDIO_AUDIO_H
2#define NNGN_AUDIO_AUDIO_H
3
4#include <cstddef>
5#include <memory>
6#include <span>
7#include <string_view>
8#include <vector>
9
10#include "math/math.h"
11#include "math/vec3.h"
12#include "utils/def.h"
13
14namespace nngn {
15
17class Audio {
18public:
19 enum class source : std::size_t {};
20 // Utility
22 static constexpr float db(float x) { return std::pow(10.0f, x / 20.0f); }
23 // Audio generation
25 static void gain(std::span<float> s, float g);
27 static void over(std::span<float> s, float m, float mix);
29 static void fade(std::span<float> s, float g0, float g1);
37 static void exp_fade(
38 std::span<float> s, std::size_t ep, float g0, float g1, float exp);
40 static void env(
41 std::span<float> s,
42 std::size_t a, std::size_t d, float st, std::size_t r);
44 static void mix(std::span<float> dst, std::span<const float> src);
46 static void normalize(std::span<i16> dst, std::span<const float> src);
47 // Constructors, destructors
49 Audio(void) = default;
50 ~Audio(void);
51 // Initialization, accessors
57 bool init(Math *m, std::size_t rate);
58 std::size_t rate(void) const { return this->m_rate; }
59 std::size_t n_sources(void) const;
60 // I/O
62 bool read_wav(std::string_view path, std::vector<std::byte> *v) const;
64 bool write_wav(FILE *f, std::span<const std::byte> s) const;
66 void gen_wav_header(
67 std::span<std::byte> dst, std::span<const std::byte> src) const;
69 std::vector<std::byte> gen_wav(std::span<const float> s) const;
70 // Audio generation
72 void trem(std::span<float> s, float a, float freq, float mix) const;
74 void gen_sine(std::span<float> s, float freq) const;
83 void gen_sine_fm(
84 std::span<float> s, float freq,
85 float lfo_a, float lfo_freq, float lfo_d) const;
87 void gen_square(std::span<float> s, float freq) const;
89 void gen_saw(std::span<float> s, float freq) const;
91 void gen_noise(std::span<float> s) const;
92 // Sources
93 bool set_pos(vec3 p);
94 source add_source(void);
95 source add_source(std::span<const std::byte> v);
97 vec3 source_pos(source s) const;
98 std::size_t source_sample_pos(source s) const;
99 bool set_source_data(
100 source s, std::size_t channels, std::size_t bit_depth,
101 std::span<const std::byte> v);
102 bool set_source_pos(source s, vec3 p);
103 bool set_source_sample_pos(source s, std::size_t p);
104 bool set_source_loop(source s, bool l);
105 bool set_source_gain(source s, float g);
106 bool play(source s) const;
107 bool stop(source s) const;
108private:
109 bool init_openal(void);
110 std::unique_ptr<void, void(*)(void*)> data = {nullptr, [](auto){}};
111 Math *math = nullptr;
112 std::size_t m_rate = 0;
113};
114
115}
116
117#endif
Audio manager.
Definition audio.h:17
void gen_square(std::span< float > s, float freq) const
Generates a square wave with amplitude 1 and frequency freq.
Definition gen.cpp:106
bool remove_source(source s)
Definition openal.cpp:26
vec3 source_pos(source s) const
Definition openal.cpp:27
bool stop(source s) const
Definition openal.cpp:34
static constexpr float db(float x)
Multiplier to decibel conversion.
Definition audio.h:22
static void normalize(std::span< i16 > dst, std::span< const float > src)
Generates 16-bit PCM data from floating-point data.
Definition gen.cpp:78
bool play(source s) const
Definition openal.cpp:33
std::size_t m_rate
Definition audio.h:112
void gen_wav_header(std::span< std::byte > dst, std::span< const std::byte > src) const
Generates WAV header for a buffer.
Definition audio.cpp:20
bool init_openal(void)
Definition openal.cpp:9
static void exp_fade(std::span< float > s, std::size_t ep, float g0, float g1, float exp)
Exponentially fades from g0 to g1.
Definition gen.cpp:48
bool set_source_sample_pos(source s, std::size_t p)
Definition openal.cpp:30
std::size_t n_sources(void) const
Definition openal.cpp:22
void gen_saw(std::span< float > s, float freq) const
Generates a saw-tooth wave with amplitude 1 and frequency freq.
Definition gen.cpp:114
bool set_source_loop(source s, bool l)
Definition openal.cpp:31
bool set_source_gain(source s, float g)
Definition openal.cpp:32
bool write_wav(FILE *f, std::span< const std::byte > s) const
Writes WAV data to a file.
Definition audio.cpp:9
std::vector< std::byte > gen_wav(std::span< const float > s) const
Generates a WAV file from floating-point data.
Definition audio.cpp:30
static void env(std::span< float > s, std::size_t a, std::size_t d, float st, std::size_t r)
Attack/decay/sustain/release envelope.
Definition gen.cpp:57
static void over(std::span< float > s, float m, float mix)
Applies an overdrive effect with input multiplier m.
Definition gen.cpp:36
Math * math
Definition audio.h:111
static void gain(std::span< float > s, float g)
Applies the gain multiplier to each element.
Definition gen.cpp:31
static void fade(std::span< float > s, float g0, float g1)
Linearly fades from g0 to g1.
Definition gen.cpp:44
std::size_t rate(void) const
Definition audio.h:58
void gen_sine(std::span< float > s, float freq) const
Generates a sine wave with amplitude 1 and frequency freq.
Definition gen.cpp:93
source
Definition audio.h:19
bool set_pos(vec3 p)
Definition openal.cpp:23
std::unique_ptr< void, void(*)(void *)> data
Definition audio.h:110
std::size_t source_sample_pos(source s) const
Definition openal.cpp:28
static void mix(std::span< float > dst, std::span< const float > src)
Adds the samples from both buffers.
Definition gen.cpp:72
void gen_noise(std::span< float > s) const
Generates white (random) noise.
Definition gen.cpp:120
bool set_source_data(source s, std::size_t channels, std::size_t bit_depth, std::span< const std::byte > v)
Definition openal.cpp:36
void trem(std::span< float > s, float a, float freq, float mix) const
Applies a tremolo effect with amplitude a and frequency f.
Definition gen.cpp:85
bool read_wav(std::string_view path, std::vector< std::byte > *v) const
Reads WAV data from a file.
Definition openal.cpp:15
source add_source(void)
Definition openal.cpp:24
void gen_sine_fm(std::span< float > s, float freq, float lfo_a, float lfo_freq, float lfo_d) const
Generates a sine wave with frequency modulation.
Definition gen.cpp:97
bool set_source_pos(source s, vec3 p)
Definition openal.cpp:29
Definition math.h:22
local r
Definition gamma.lua:7
local g
Definition gamma.lua:7
init
Definition img_common.lua:34
local l
Definition light.lua:23
Definition audio.cpp:7
Definition debug.h:13
local a
Definition input.lua:35
local m
Definition input.lua:28
v[1]
Definition math.lua:22
local function f()) end
#define NNGN_MOVE_ONLY(x)
Definition utils.h:39
std::chrono::seconds s
Definition timing.cpp:6