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
202 static cgv::math::fvec<float, 3> get_position();
203
209 static cgv::math::fvec<float, 3> get_velocity();
210
216 static cgv::math::fmat<float, 3, 2> get_orientation();
217
223 static void set_position(cgv::math::fvec<float, 3> pos);
224
230 static void set_velocity(cgv::math::fvec<float, 3> vel);
231
238 static void set_orientation(cgv::math::fvec<float, 3> at, cgv::math::fvec<float, 3> up);
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);
298 void set_position(cgv::math::fvec<float, 3> pos);
299
305 void set_velocity(cgv::math::fvec<float, 3> vel);
306
312 void set_pitch(float pitch);
313
319 void set_gain(float gain);
320
327 void set_looping(bool should_loop);
328
334 cgv::math::fvec<float, 3> get_position() const;
335
341 cgv::math::fvec<float, 3> get_velocity() const;
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
This class models the single OpenAL listener.
Definition al_context.h:188
This class describes a sound source in the scene.
Definition al_context.h:255
matrix of fixed size dimensions
Definition fmat.h:23
A vector with zero based index.
Definition fvec.h:26
the cgv namespace
Definition print.h:11
simple descriptor for sounds that can be played back in OAL
Definition al_context.h:22