cgv
Loading...
Searching...
No Matches
al_context.h
1#pragma once
2
3#include <list>
4#include <map>
5#include <string>
6
7#include <cgv/math/fvec.h>
8#include <cgv/math/fmat.h>
9#include <cgv/type/info/type_id.h>
10
11#include "lib_begin.h"
12
13struct ALCdevice;
14struct ALCcontext;
15using ALuint = unsigned int;
16
17namespace cgv {
18namespace audio {
19
22{
23 enum Type {
24 SFT_UINT8,
25 SFT_INT16
26 } value_type;
27 enum Channels {
28 SFC_MONO = 1,
29 SFC_STEREO = 2
30 } nr_channels;
31 int sampling_rate;
32 int nr_frames;
33};
34
39class CGV_API OALContext
40{
41public:
45 static std::vector<std::string> enumerate_devices();
47 static std::string get_default_device_name();
51 explicit OALContext(const std::string& device_name);
52 OALContext(const OALContext& other) = delete;
53 OALContext(OALContext&& other) = default;
54 OALContext& operator=(const OALContext& other) = delete;
55 OALContext& operator=(OALContext&& other) = default;
59 std::string get_device_name() const;
61 void make_current();
63 static bool decode_sound_file(const void* data, size_t data_length, OALSoundFormat& format, std::vector<int16_t>& memory_buffer);
65 static bool load_sound_file(const std::string& filepath, OALSoundFormat& format, std::vector<int16_t>& memory_buffer);
67 void create_buffer(const std::string& symbolic_name, const OALSoundFormat& format, const void* data, size_t data_length);
77 void load_sample(std::string filepath, std::string symbolic_name = "");
78
96 void load_sample(std::string symbolic_name, const void* data, size_t data_length);
97
112 void load_samples(std::string folder, bool recursive = false);
113
124 ALuint get_buffer_id(std::string sound_name) const;
133 std::string get_error_string();
134
143 bool is_no_error();
144
156 void set_HRTF(bool active);
157
163 ALCdevice* get_native_device();
164
170 ALCcontext* get_native_context();
171
172 private:
173 ALCdevice* oal_device = nullptr;
174 ALCcontext* oal_context = nullptr;
175
177 std::map<std::string, ALuint> sample_buffers;
178};
179
187class CGV_API OALListener final
188{
189 public:
190 OALListener() = delete;
191 OALListener(const OALListener& other) = delete;
192 OALListener(OALListener&& other) = delete;
193 OALListener& operator=(const OALListener& other) = delete;
194 OALListener& operator=(OALListener&& other) = delete;
195 ~OALListener() = delete;
196
203
210
217
224
231
239};
240
244enum OALSourceState {
245 OALSS_INITIAL,
246 OALSS_PLAYING,
247 OALSS_PAUSED,
248 OALSS_STOPPED
249};
250
254class CGV_API OALSource
255{
256 public:
257 OALSource() = default;
258 OALSource(const OALSource&) = delete;
259 OALSource(OALSource&&) = default;
260 OALSource& operator=(const OALSource&) = delete;
261 OALSource& operator=(OALSource&&) = default;
262 ~OALSource();
263
277 bool init(OALContext& ctx, std::string sound_name = "");
279 void clear();
292 bool append_sound(std::string sound_name);
299
306
312 void set_pitch(float pitch);
313
319 void set_gain(float gain);
320
327 void set_looping(bool should_loop);
328
335
342
348 float get_pitch() const;
349
355 float get_gain() const;
356
362 bool is_looping() const;
363
369 bool is_playing() const;
370
371
373 OALSourceState get_state() const;
374
378 void play();
379
384 void pause();
385
396 void play_pause(bool should_play);
397
402 void stop();
403
407 void rewind();
408protected:
409 bool append_sound_impl(std::string sound_name);
410 private:
411 OALContext* ctx_ptr = nullptr;
412 ALuint src_id = 0;
413};
414
415} // namespace audio
416} // namespace cgv
417
418#include <cgv/config/lib_end.h>
This class provides easy sample loading, device enumeration and error retrieval.
Definition al_context.h:40
void load_samples(std::string folder, bool recursive=false)
Loads all audio files in a folder into the internal buffer list.
ALCdevice * get_native_device()
Gets the native device handle for direct OpenAL calls.
void make_current()
in case of multiple contexts, this makes this context current
std::string get_device_name() const
return device name of contexts device
static std::vector< std::string > enumerate_devices()
Enumerate audio output devices available on system.
std::string get_error_string()
Returns the last error from the OpenAL context.
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...
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.
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
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.
OALContext(const std::string &device_name)
Construct context, connect to output device and make context current.
ALuint get_buffer_id(std::string sound_name) const
Gets the buffer identifier for the symbolic name.
ALCcontext * get_native_context()
Gets the native context handle for direct OpenAL calls.
static cgv::math::fvec< float, 3 > get_position()
Gets the listener position.
static cgv::math::fvec< float, 3 > get_velocity()
Gets the listener velocity.
static void set_position(cgv::math::fvec< float, 3 > pos)
Sets the listener position.
static void set_orientation(cgv::math::fvec< float, 3 > at, cgv::math::fvec< float, 3 > up)
Sets the listener orientation.
static cgv::math::fmat< float, 3, 2 > get_orientation()
Gets the listener orientation.
static void set_velocity(cgv::math::fvec< float, 3 > vel)
Sets the listener velocity.
void clear()
destruct source and restore uninitialized state
bool append_sound(std::string sound_name)
Append buffer of named sound to source's playback list.
void stop()
Stops the playback of the sound buffer and moves the playhead to the beginning.
void set_looping(bool should_loop)
Sets the looping property of the source.
void set_position(cgv::math::fvec< float, 3 > pos)
Sets the source position.
void play_pause(bool should_play)
Explicitly toggles between pausing and playing the sound buffer.
bool init(OALContext &ctx, std::string sound_name="")
This function links a source to a context.
void set_velocity(cgv::math::fvec< float, 3 > vel)
Sets the source velocity.
void play()
Commences the playback of the sound buffer.
void rewind()
Moves the playhead to the beginning of the buffer.
void pause()
Pauses the playback of the sound buffer without moving the playhead.
cgv::math::fvec< float, 3 > get_velocity() const
Gets the source velocity.
bool is_looping() const
Determines if the source has the looping property.
bool is_playing() const
Determines if the source is playing.
OALSourceState get_state() const
returns state of this source
float get_gain() const
Gets the source gain.
float get_pitch() const
Gets the source pitch.
cgv::math::fvec< float, 3 > get_position() const
Gets the source position.
void set_gain(float gain)
Sets the source gain.
void set_pitch(float pitch)
Sets the source pitch.
matrix of fixed size dimensions
Definition fmat.h:23
A vector with zero based index.
Definition fvec.h:29
namespace for data management components
this header is dependency free
Definition print.h:11
simple descriptor for sounds that can be played back in OAL
Definition al_context.h:22