nngn
Loading...
Searching...
No Matches
nngn::Audio Class Reference

Audio manager. More...

#include <audio.h>

Collaboration diagram for nngn::Audio:

Public Types

enum class  source : std::size_t
 
enum class  source : std::size_t
 

Public Member Functions

 Audio (void)=default
 
 ~Audio (void)
 
bool init (Math *m, std::size_t rate)
 Initializes the manager to work with a given sample rate.
 
std::size_t rate (void) const
 
std::size_t n_sources (void) const
 
bool read_wav (std::string_view path, std::vector< std::byte > *v) const
 Reads WAV data from a file.
 
bool write_wav (FILE *f, std::span< const std::byte > s) const
 Writes WAV data to a file.
 
void gen_wav_header (std::span< std::byte > dst, std::span< const std::byte > src) const
 Generates WAV header for a buffer.
 
std::vector< std::byte > gen_wav (std::span< const float > s) const
 Generates a WAV file from floating-point data.
 
void trem (std::span< float > s, float a, float freq, float mix) const
 Applies a tremolo effect with amplitude a and frequency f.
 
void gen_sine (std::span< float > s, float freq) const
 Generates a sine wave with amplitude 1 and frequency freq.
 
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.
 
void gen_square (std::span< float > s, float freq) const
 Generates a square wave with amplitude 1 and frequency freq.
 
void gen_saw (std::span< float > s, float freq) const
 Generates a saw-tooth wave with amplitude 1 and frequency freq.
 
void gen_noise (std::span< float > s) const
 Generates white (random) noise.
 
bool set_pos (vec3 p)
 
source add_source (void)
 
source add_source (std::span< const std::byte > v)
 
bool remove_source (source s)
 
vec3 source_pos (source s) const
 
std::size_t source_sample_pos (source s) const
 
bool set_source_data (source s, std::size_t channels, std::size_t bit_depth, std::span< const std::byte > v)
 
bool set_source_pos (source s, vec3 p)
 
bool set_source_sample_pos (source s, std::size_t p)
 
bool set_source_loop (source s, bool l)
 
bool set_source_gain (source s, float g)
 
bool play (source s) const
 
bool stop (source s) const
 
 Audio (void)=default
 
 ~Audio (void)
 
bool init (Math *m, std::size_t rate)
 Initializes the manager to work with a given sample rate.
 
std::size_t rate (void) const
 
std::size_t n_sources (void) const
 
bool read_wav (std::string_view path, std::vector< std::byte > *v) const
 Reads WAV data from a file.
 
bool write_wav (FILE *f, std::span< const std::byte > s) const
 Writes WAV data to a file.
 
void gen_wav_header (std::span< std::byte > dst, std::span< const std::byte > src) const
 Generates WAV header for a buffer.
 
std::vector< std::byte > gen_wav (std::span< const float > s) const
 Generates a WAV file from floating-point data.
 
void trem (std::span< float > s, float a, float freq, float mix) const
 Applies a tremolo effect with amplitude a and frequency f.
 
void gen_sine (std::span< float > s, float freq) const
 Generates a sine wave with amplitude 1 and frequency freq.
 
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.
 
void gen_square (std::span< float > s, float freq) const
 Generates a square wave with amplitude 1 and frequency freq.
 
void gen_saw (std::span< float > s, float freq) const
 Generates a saw-tooth wave with amplitude 1 and frequency freq.
 
void gen_noise (std::span< float > s) const
 Generates white (random) noise.
 
bool set_pos (vec3 p)
 
source add_source (void)
 
source add_source (std::span< const std::byte > v)
 
bool remove_source (source s)
 
vec3 source_pos (source s) const
 
std::size_t source_sample_pos (source s) const
 
bool set_source_data (source s, std::size_t channels, std::size_t bit_depth, std::span< const std::byte > v)
 
bool set_source_pos (source s, vec3 p)
 
bool set_source_sample_pos (source s, std::size_t p)
 
bool set_source_loop (source s, bool l)
 
bool set_source_gain (source s, float g)
 
bool play (source s) const
 
bool stop (source s) const
 

Static Public Member Functions

static constexpr float db (float x)
 Multiplier to decibel conversion.
 
static void gain (std::span< float > s, float g)
 Applies the gain multiplier to each element.
 
static void over (std::span< float > s, float m, float mix)
 Applies an overdrive effect with input multiplier m.
 
static void fade (std::span< float > s, float g0, float g1)
 Linearly fades from g0 to g1.
 
static void exp_fade (std::span< float > s, std::size_t ep, float g0, float g1, float exp)
 Exponentially fades from g0 to g1.
 
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.
 
static void mix (std::span< float > dst, std::span< const float > src)
 Adds the samples from both buffers.
 
static void normalize (std::span< i16 > dst, std::span< const float > src)
 Generates 16-bit PCM data from floating-point data.
 
static constexpr float db (float x)
 Multiplier to decibel conversion.
 
static void gain (std::span< float > s, float g)
 Applies the gain multiplier to each element.
 
static void over (std::span< float > s, float m, float mix)
 Applies an overdrive effect with input multiplier m.
 
static void fade (std::span< float > s, float g0, float g1)
 Linearly fades from g0 to g1.
 
static void exp_fade (std::span< float > s, std::size_t ep, float g0, float g1, float exp)
 Exponentially fades from g0 to g1.
 
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.
 
static void mix (std::span< float > dst, std::span< const float > src)
 Adds the samples from both buffers.
 
static void normalize (std::span< i16 > dst, std::span< const float > src)
 Generates 16-bit PCM data from floating-point data.
 

Private Member Functions

bool init_openal (void)
 
bool init_openal (void)
 

Private Attributes

std::unique_ptr< void, void(*)(void *)> data = {nullptr, [](auto){}}
 
Mathmath = nullptr
 
std::size_t m_rate = 0
 

Detailed Description

Audio manager.

Generates, stores, and controls audio streams.

Member Enumeration Documentation

◆ source [1/2]

enum class nngn::Audio::source : std::size_t
strong

◆ source [2/2]

enum class nngn::Audio::source : std::size_t
strong

Constructor & Destructor Documentation

◆ Audio() [1/2]

nngn::Audio::Audio ( void  )
default

◆ ~Audio() [1/2]

nngn::Audio::~Audio ( void  )

◆ Audio() [2/2]

nngn::Audio::Audio ( void  )
default

◆ ~Audio() [2/2]

nngn::Audio::~Audio ( void  )

Member Function Documentation

◆ add_source() [1/4]

auto nngn::Audio::add_source ( std::span< const std::byte >  v)

◆ add_source() [2/4]

source nngn::Audio::add_source ( std::span< const std::byte >  v)

◆ add_source() [3/4]

auto nngn::Audio::add_source ( void  )

◆ add_source() [4/4]

source nngn::Audio::add_source ( void  )

◆ db() [1/2]

static constexpr float nngn::Audio::db ( float  x)
inlinestaticconstexpr

Multiplier to decibel conversion.

Here is the caller graph for this function:

◆ db() [2/2]

static constexpr float nngn::Audio::db ( float  x)
inlinestaticconstexpr

Multiplier to decibel conversion.

◆ env() [1/2]

void nngn::Audio::env ( std::span< float s,
std::size_t  a,
std::size_t  d,
float  st,
std::size_t  r 
)
static

Attack/decay/sustain/release envelope.

Here is the call graph for this function:

◆ env() [2/2]

static void nngn::Audio::env ( std::span< float s,
std::size_t  a,
std::size_t  d,
float  st,
std::size_t  r 
)
static

Attack/decay/sustain/release envelope.

◆ exp_fade() [1/2]

void nngn::Audio::exp_fade ( std::span< float s,
std::size_t  ep,
float  g0,
float  g1,
float  exp 
)
static

Exponentially fades from g0 to g1.

The interpolation is done in reverse (i.e. 1 - ((1 - x) ^ e) in case g1 < g0 so that the same exponent generates similar curves when fading either in or out. ep is the real end position, which has to satisfy s.size() <= ep.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ exp_fade() [2/2]

static void nngn::Audio::exp_fade ( std::span< float s,
std::size_t  ep,
float  g0,
float  g1,
float  exp 
)
static

Exponentially fades from g0 to g1.

The interpolation is done in reverse (i.e. 1 - ((1 - x) ^ e) in case g1 < g0 so that the same exponent generates similar curves when fading either in or out. ep is the real end position, which has to satisfy s.size() <= ep.

◆ fade() [1/2]

void nngn::Audio::fade ( std::span< float s,
float  g0,
float  g1 
)
static

Linearly fades from g0 to g1.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ fade() [2/2]

static void nngn::Audio::fade ( std::span< float s,
float  g0,
float  g1 
)
static

Linearly fades from g0 to g1.

◆ gain() [1/2]

void nngn::Audio::gain ( std::span< float s,
float  g 
)
static

Applies the gain multiplier to each element.

Here is the caller graph for this function:

◆ gain() [2/2]

static void nngn::Audio::gain ( std::span< float s,
float  g 
)
static

Applies the gain multiplier to each element.

◆ gen_noise() [1/2]

void nngn::Audio::gen_noise ( std::span< float s) const

Generates white (random) noise.

Here is the call graph for this function:

◆ gen_noise() [2/2]

void nngn::Audio::gen_noise ( std::span< float s) const

Generates white (random) noise.

◆ gen_saw() [1/2]

void nngn::Audio::gen_saw ( std::span< float s,
float  freq 
) const

Generates a saw-tooth wave with amplitude 1 and frequency freq.

◆ gen_saw() [2/2]

void nngn::Audio::gen_saw ( std::span< float s,
float  freq 
) const

Generates a saw-tooth wave with amplitude 1 and frequency freq.

◆ gen_sine() [1/2]

void nngn::Audio::gen_sine ( std::span< float s,
float  freq 
) const

Generates a sine wave with amplitude 1 and frequency freq.

◆ gen_sine() [2/2]

void nngn::Audio::gen_sine ( std::span< float s,
float  freq 
) const

Generates a sine wave with amplitude 1 and frequency freq.

◆ gen_sine_fm() [1/2]

void nngn::Audio::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.

Parameters
lfo_aAmplitude of the message signal.
lfo_freqFrequency of the message signal.
lfo_dPhase delta (i.e. angular displacement, in radians) to apply to the message signal.

◆ gen_sine_fm() [2/2]

void nngn::Audio::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.

Parameters
lfo_aAmplitude of the message signal.
lfo_freqFrequency of the message signal.
lfo_dPhase delta (i.e. angular displacement, in radians) to apply to the message signal.

◆ gen_square() [1/2]

void nngn::Audio::gen_square ( std::span< float s,
float  freq 
) const

Generates a square wave with amplitude 1 and frequency freq.

◆ gen_square() [2/2]

void nngn::Audio::gen_square ( std::span< float s,
float  freq 
) const

Generates a square wave with amplitude 1 and frequency freq.

◆ gen_wav() [1/2]

std::vector< std::byte > nngn::Audio::gen_wav ( std::span< const float s) const

Generates a WAV file from floating-point data.

Here is the call graph for this function:

◆ gen_wav() [2/2]

std::vector< std::byte > nngn::Audio::gen_wav ( std::span< const float s) const

Generates a WAV file from floating-point data.

◆ gen_wav_header() [1/2]

void nngn::Audio::gen_wav_header ( std::span< std::byte >  dst,
std::span< const std::byte >  src 
) const

Generates WAV header for a buffer.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ gen_wav_header() [2/2]

void nngn::Audio::gen_wav_header ( std::span< std::byte >  dst,
std::span< const std::byte >  src 
) const

Generates WAV header for a buffer.

◆ init() [1/2]

bool nngn::Audio::init ( nngn::Math m,
std::size_t  rate 
)

Initializes the manager to work with a given sample rate.

Must be called before any other non-static member function.

Parameters
mMay be null if noise is never called.
Here is the call graph for this function:

◆ init() [2/2]

bool nngn::Audio::init ( Math m,
std::size_t  rate 
)

Initializes the manager to work with a given sample rate.

Must be called before any other non-static member function.

Parameters
mMay be null if noise is never called.

◆ init_openal() [1/2]

bool nngn::Audio::init_openal ( void  )
private
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init_openal() [2/2]

bool nngn::Audio::init_openal ( void  )
private

◆ mix() [1/2]

void nngn::Audio::mix ( std::span< float dst,
std::span< const float src 
)
static

Adds the samples from both buffers.

Here is the caller graph for this function:

◆ mix() [2/2]

static void nngn::Audio::mix ( std::span< float dst,
std::span< const float src 
)
static

Adds the samples from both buffers.

◆ n_sources() [1/2]

std::size_t nngn::Audio::n_sources ( void  ) const

◆ n_sources() [2/2]

std::size_t nngn::Audio::n_sources ( void  ) const

◆ normalize() [1/2]

void nngn::Audio::normalize ( std::span< i16 dst,
std::span< const float src 
)
static

Generates 16-bit PCM data from floating-point data.

Here is the caller graph for this function:

◆ normalize() [2/2]

static void nngn::Audio::normalize ( std::span< i16 dst,
std::span< const float src 
)
static

Generates 16-bit PCM data from floating-point data.

◆ over() [1/2]

void nngn::Audio::over ( std::span< float s,
float  m,
float  mix 
)
static

Applies an overdrive effect with input multiplier m.

Here is the call graph for this function:

◆ over() [2/2]

static void nngn::Audio::over ( std::span< float s,
float  m,
float  mix 
)
static

Applies an overdrive effect with input multiplier m.

◆ play() [1/2]

bool nngn::Audio::play ( source  s) const

◆ play() [2/2]

bool nngn::Audio::play ( source  s) const

◆ rate() [1/2]

std::size_t nngn::Audio::rate ( void  ) const
inline
Here is the caller graph for this function:

◆ rate() [2/2]

std::size_t nngn::Audio::rate ( void  ) const
inline

◆ read_wav() [1/2]

bool nngn::Audio::read_wav ( std::string_view  path,
std::vector< std::byte > *  v 
) const

Reads WAV data from a file.

Here is the call graph for this function:

◆ read_wav() [2/2]

bool nngn::Audio::read_wav ( std::string_view  path,
std::vector< std::byte > *  v 
) const

Reads WAV data from a file.

◆ remove_source() [1/2]

bool nngn::Audio::remove_source ( source  s)

◆ remove_source() [2/2]

bool nngn::Audio::remove_source ( source  s)

◆ set_pos() [1/2]

bool nngn::Audio::set_pos ( vec3  p)

◆ set_pos() [2/2]

bool nngn::Audio::set_pos ( vec3  p)

◆ set_source_data() [1/2]

bool nngn::Audio::set_source_data ( source  s,
std::size_t  channels,
std::size_t  bit_depth,
std::span< const std::byte >  v 
)

◆ set_source_data() [2/2]

bool nngn::Audio::set_source_data ( source  s,
std::size_t  channels,
std::size_t  bit_depth,
std::span< const std::byte >  v 
)

◆ set_source_gain() [1/2]

bool nngn::Audio::set_source_gain ( source  s,
float  g 
)

◆ set_source_gain() [2/2]

bool nngn::Audio::set_source_gain ( source  s,
float  g 
)

◆ set_source_loop() [1/2]

bool nngn::Audio::set_source_loop ( source  s,
bool  l 
)

◆ set_source_loop() [2/2]

bool nngn::Audio::set_source_loop ( source  s,
bool  l 
)

◆ set_source_pos() [1/2]

bool nngn::Audio::set_source_pos ( source  s,
vec3  p 
)

◆ set_source_pos() [2/2]

bool nngn::Audio::set_source_pos ( source  s,
vec3  p 
)

◆ set_source_sample_pos() [1/2]

bool nngn::Audio::set_source_sample_pos ( source  s,
std::size_t  p 
)

◆ set_source_sample_pos() [2/2]

bool nngn::Audio::set_source_sample_pos ( source  s,
std::size_t  p 
)

◆ source_pos() [1/2]

vec3 nngn::Audio::source_pos ( source  s) const

◆ source_pos() [2/2]

vec3 nngn::Audio::source_pos ( source  s) const

◆ source_sample_pos() [1/2]

std::size_t nngn::Audio::source_sample_pos ( source  s) const

◆ source_sample_pos() [2/2]

std::size_t nngn::Audio::source_sample_pos ( source  s) const

◆ stop() [1/2]

bool nngn::Audio::stop ( source  s) const
Here is the caller graph for this function:

◆ stop() [2/2]

bool nngn::Audio::stop ( source  s) const

◆ trem() [1/2]

void nngn::Audio::trem ( std::span< float s,
float  a,
float  freq,
float  mix 
) const

Applies a tremolo effect with amplitude a and frequency f.

Here is the call graph for this function:

◆ trem() [2/2]

void nngn::Audio::trem ( std::span< float s,
float  a,
float  freq,
float  mix 
) const

Applies a tremolo effect with amplitude a and frequency f.

◆ write_wav() [1/2]

bool nngn::Audio::write_wav ( FILE *  f,
std::span< const std::byte >  s 
) const

Writes WAV data to a file.

Here is the call graph for this function:

◆ write_wav() [2/2]

bool nngn::Audio::write_wav ( FILE *  f,
std::span< const std::byte >  s 
) const

Writes WAV data to a file.

Member Data Documentation

◆ data

std::unique_ptr< void, void(*)(void *)> nngn::Audio::data = {nullptr, [](auto){}}
private

◆ m_rate

std::size_t nngn::Audio::m_rate = 0
private

◆ math

Math * nngn::Audio::math = nullptr
private

The documentation for this class was generated from the following files: