cgv
Loading...
Searching...
No Matches
cgv::audio::OALContext Class Reference

This class provides easy sample loading, device enumeration and error retrieval. More...

#include <al_context.h>

Public Member Functions

 OALContext (const std::string &device_name)
 Construct context, connect to output device and make context current.
 
 OALContext (const OALContext &other)=delete
 
 OALContext (OALContext &&other)=default
 
OALContextoperator= (const OALContext &other)=delete
 
OALContextoperator= (OALContext &&other)=default
 
 ~OALContext ()
 detach context from output device and destruct
 
std::string get_device_name () const
 return device name of contexts device
 
void make_current ()
 in case of multiple contexts, this makes this context current
 
void create_buffer (const std::string &symbolic_name, const OALSoundFormat &format, const void *data, size_t data_length)
 create a named sound buffer from a format descriptor and a data buffer of given length in bytes
 
void load_sample (std::string filepath, std::string symbolic_name="")
 Loads a sound into the internal buffer list.
 
void load_sample (std::string symbolic_name, const void *data, size_t data_length)
 Loads a sound into the internal buffer list.
 
void load_samples (std::string folder, bool recursive=false)
 Loads all audio files in a folder into the internal buffer list.
 
ALuint get_buffer_id (std::string sound_name) const
 Gets the buffer identifier for the symbolic name.
 
std::string get_error_string ()
 Returns the last error from the OpenAL context.
 
bool is_no_error ()
 Determines if there were any errors since last call.
 
void set_HRTF (bool active)
 Toggles the OpenAL output mode to use HRTF.
 
ALCdevice * get_native_device ()
 Gets the native device handle for direct OpenAL calls.
 
ALCcontext * get_native_context ()
 Gets the native context handle for direct OpenAL calls.
 

Static Public Member Functions

static std::vector< std::string > enumerate_devices ()
 Enumerate audio output devices available on system.
 
static std::string get_default_device_name ()
 returns name of system's default device
 
static bool decode_sound_file (const void *data, size_t data_length, OALSoundFormat &format, std::vector< int16_t > &memory_buffer)
 decode sound file that is already in memory data with data_length bytes to a newly constructed memory buffer
 
static bool load_sound_file (const std::string &filepath, OALSoundFormat &format, std::vector< int16_t > &memory_buffer)
 load and decode sound file to newly constructed memory buffer
 

Detailed Description

This class provides easy sample loading, device enumeration and error retrieval.

Definition at line 39 of file al_context.h.

Constructor & Destructor Documentation

◆ OALContext()

cgv::audio::OALContext::OALContext ( const std::string &  device_name)
explicit

Construct context, connect to output device and make context current.

Parameters
[in]deviceThe OpenAL device string identifier
See also
enumerate_devices()

Definition at line 41 of file al_context.cxx.

◆ ~OALContext()

cgv::audio::OALContext::~OALContext ( )

detach context from output device and destruct

Definition at line 58 of file al_context.cxx.

Member Function Documentation

◆ create_buffer()

void cgv::audio::OALContext::create_buffer ( const std::string &  symbolic_name,
const OALSoundFormat format,
const void *  data,
size_t  data_length 
)

create a named sound buffer from a format descriptor and a data buffer of given length in bytes

Definition at line 149 of file al_context.cxx.

Referenced by load_sample(), and load_sample().

◆ decode_sound_file()

bool cgv::audio::OALContext::decode_sound_file ( const void *  data,
size_t  data_length,
OALSoundFormat format,
std::vector< int16_t > &  memory_buffer 
)
static

decode sound file that is already in memory data with data_length bytes to a newly constructed memory buffer

Definition at line 92 of file al_context.cxx.

Referenced by load_sample().

◆ enumerate_devices()

std::vector< std::string > cgv::audio::OALContext::enumerate_devices ( )
static

Enumerate audio output devices available on system.

Returns
vector of device names which can be passed to constructor
See also
explicit OALContext(std::string)

Definition at line 25 of file al_context.cxx.

◆ get_buffer_id()

ALuint cgv::audio::OALContext::get_buffer_id ( std::string  sound_name) const

Gets the buffer identifier for the symbolic name.

Parameters
[in]sound_nameThe symbolic sound name of the buffer.
Returns
The buffer ID as used by OpenAL.
See also
load_sample()
load_samples()

Definition at line 215 of file al_context.cxx.

◆ get_default_device_name()

std::string cgv::audio::OALContext::get_default_device_name ( )
static

returns name of system's default device

Definition at line 19 of file al_context.cxx.

◆ get_device_name()

std::string cgv::audio::OALContext::get_device_name ( ) const

return device name of contexts device

Definition at line 53 of file al_context.cxx.

◆ get_error_string()

std::string cgv::audio::OALContext::get_error_string ( )

Returns the last error from the OpenAL context.

Returns
The error string.
Warning
This function call clears the last error in the OpenAL context.
See also
is_no_error()

Definition at line 220 of file al_context.cxx.

◆ get_native_context()

ALCcontext * cgv::audio::OALContext::get_native_context ( )

Gets the native context handle for direct OpenAL calls.

Returns
The native OpenAL context handle.

Definition at line 235 of file al_context.cxx.

Referenced by cgv::audio::OALSource::append_sound(), and cgv::audio::OALSource::init().

◆ get_native_device()

ALCdevice * cgv::audio::OALContext::get_native_device ( )

Gets the native device handle for direct OpenAL calls.

Returns
The native OpenAL device handle.

Definition at line 233 of file al_context.cxx.

Referenced by set_HRTF().

◆ is_no_error()

bool cgv::audio::OALContext::is_no_error ( )

Determines if there were any errors since last call.

Returns
True if no errors were encountered, False otherwise.
Warning
This function call clears the last error in the OpenAL context.
See also
get_error_string()

Definition at line 221 of file al_context.cxx.

Referenced by cgv::audio::OALSource::init().

◆ load_sample() [1/2]

void cgv::audio::OALContext::load_sample ( std::string  filepath,
std::string  symbolic_name = "" 
)

Loads a sound into the internal buffer list.

Parameters
[in]filepathThe filepath to the sample file
[in]symbolic_nameSound is named by file stem or if given this parameter
Remarks
The supported filetypes are .WAV, .AIFF, .FLAC, etc. For detailed information visit http://www.mega-nerd.com/libsndfile/

Definition at line 169 of file al_context.cxx.

References create_buffer(), and load_sound_file().

Referenced by load_samples().

◆ load_sample() [2/2]

void cgv::audio::OALContext::load_sample ( std::string  symbolic_name,
const void *  data,
size_t  data_length 
)

Loads a sound into the internal buffer list.

Parameters
[in]symbolic_nameThe symbolic name used as key for later retrieval
[in]dataA pointer to the first sample of the audio data
[in]data_lengthThe length of the audio data buffer in bytes
Remarks
This function is implemented via virtual IO on top of the supplied buffer. Therefore the supplied buffer needs to be an in memory copy of a ordinary audio file (for example contain the necessary .WAV headers etc.).
Each file will be transformed into a buffer ID with the filename stem as symbolic key. For example: ./sounds/Wind.wav will have the key "Wind".

Definition at line 184 of file al_context.cxx.

References create_buffer(), and decode_sound_file().

◆ load_samples()

void cgv::audio::OALContext::load_samples ( std::string  folder,
bool  recursive = false 
)

Loads all audio files in a folder into the internal buffer list.

Parameters
[in]folderThe path to the folder
[in]recursiveTrue if subfolders should be traversed as well, False otherwise.
Remarks
This function is a convenience wrapper around the tedious process of reading all files in a folder and uploading each to a buffer.
See also
load_sample(std::string)

Definition at line 192 of file al_context.cxx.

References load_sample().

◆ load_sound_file()

bool cgv::audio::OALContext::load_sound_file ( const std::string &  filepath,
OALSoundFormat format,
std::vector< int16_t > &  memory_buffer 
)
static

load and decode sound file to newly constructed memory buffer

Definition at line 137 of file al_context.cxx.

Referenced by load_sample().

◆ make_current()

void cgv::audio::OALContext::make_current ( )

in case of multiple contexts, this makes this context current

Definition at line 49 of file al_context.cxx.

◆ set_HRTF()

void cgv::audio::OALContext::set_HRTF ( bool  active)

Toggles the OpenAL output mode to use HRTF.

Parameters
[in]activeTrue if the output should be filtered with a head related transfer function, False otherwise.
Remarks
HRTFs improve the perceived localization of sounds in the scene. Therefore it is advisable to enable HRTF output with headphones. For loudspeakers however this should be disabled as the users head already has a specific HRTF.

Definition at line 222 of file al_context.cxx.

References get_native_device().


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