CPP-TOOLBOX
Loading...
Searching...
No Matches
sound_system.hpp
Go to the documentation of this file.
1#ifndef SOUND_SYSTEM_HPP
2#define SOUND_SYSTEM_HPP
3
4#include <AL/al.h>
5#include <glm/fwd.hpp>
6#include <map>
7#include <queue>
8#include <string>
9#include <glm/glm.hpp>
10#include <unordered_map>
11
13
14/* Sound System:
15 * - play a sound right now, at a position without caring about any of the implementation details
16 * - play a sound right now, have it looping forever until I turn it off
17 * - have a deeper understanding of the openal api, and allow the user to create their own sound sources
18 * which do not interfere with any of the above
19 * - given a sound source we can queue up a sequence of sounds to be played sequentially over time.
20 *
21 */
22
25 glm::vec3 position;
26};
27
29 public:
30 Logger logger = Logger("sound_system");
31
32 // TODO: in the future I specifying the num sources should be optional, otherwise we get new ones as needed.
33 SoundSystem(int num_sources = 100, const std::unordered_map<SoundType, std::string> &sound_type_to_file = {});
34 void queue_sound(SoundType type, glm::vec3 position = glm::vec3(0));
35 // returns the id of the source that will be playing that looping sound
36 [[nodiscard]] unsigned int queue_looping_sound(SoundType type, glm::vec3 position);
37 // pass the id of the source that's playing the sound here to turn it off
38 void stop_looping_sound(const unsigned int &source_id);
39
40 // plays all queued sounds
41 void play_all_sounds();
42
43 void set_listener_position(float x, float y, float z);
44
46
47 void load_sound_into_system_for_playback(const std::string &sound_name, const char *filename);
48 void create_sound_source(const std::string &source_name);
49 void set_source_gain_by_name(const std::string &source_name, float gain);
50
51 void set_source_looping_by_name(const std::string &source_name, bool looping);
52
53 // deprecated
54 void play_sound(const std::string &source_name, const std::string &sound_name);
55 void set_listener_orientation(const glm::vec3 &forward, const glm::vec3 &up);
56
57 private:
58 std::map<std::string, ALuint> sound_name_to_loaded_buffer_id;
59 std::map<std::string, ALuint> source_name_to_source_id;
60
61 // NEW
62 std::vector<ALuint> sound_sources; // Pool of sound sources
63 std::unordered_map<SoundType, ALuint> sound_type_to_buffer_id; // Map of sound buffers
64 std::queue<QueuedSound> sound_to_play_queue; // Queue of sounds to play
65 // NEW
66
67 // Helper functions
68 ALuint get_available_source_id();
69 void init_sound_buffers(const std::unordered_map<SoundType, std::string> &sound_type_to_file);
70 void init_sound_sources(int num_sources);
71
72 void initialize_openal();
73 void deinitialize_openal();
74};
75
76#endif // SOUND_SYSTEM_HPP
Definition logger.hpp:22
void play_all_sounds()
Definition sound_system.cpp:248
void set_listener_orientation(const glm::vec3 &forward, const glm::vec3 &up)
Definition sound_system.cpp:165
void stop_looping_sound(const unsigned int &source_id)
Definition sound_system.cpp:235
~SoundSystem()
Definition sound_system.cpp:18
void create_sound_source(const std::string &source_name)
Definition sound_system.cpp:83
void queue_sound(SoundType type, glm::vec3 position=glm::vec3(0))
Definition sound_system.cpp:214
unsigned int queue_looping_sound(SoundType type, glm::vec3 position)
Definition sound_system.cpp:218
void set_source_looping_by_name(const std::string &source_name, bool looping)
Definition sound_system.cpp:183
void set_source_gain_by_name(const std::string &source_name, float gain)
Definition sound_system.cpp:171
void play_sound(const std::string &source_name, const std::string &sound_name)
Definition sound_system.cpp:99
void load_sound_into_system_for_playback(const std::string &sound_name, const char *filename)
Definition sound_system.cpp:125
void set_listener_position(float x, float y, float z)
Definition sound_system.cpp:141
Logger logger
Definition sound_system.hpp:30
SoundSystem(int num_sources=100, const std::unordered_map< SoundType, std::string > &sound_type_to_file={})
Definition sound_system.cpp:11
@ y
Definition input_state.hpp:51
@ x
Definition input_state.hpp:50
@ z
Definition input_state.hpp:52
void initialize_openal()
Definition openal_utils.cpp:267
SoundType
Definition sound_types.hpp:4
Definition sound_system.hpp:23
SoundType type
Definition sound_system.hpp:24
glm::vec3 position
Definition sound_system.hpp:25