TDME2  1.9.200
Audio.cpp
Go to the documentation of this file.
1 #include <tdme/audio/Audio.h>
2 
3 #if defined(__APPLE__)
4  #define AL_SILENCE_DEPRECATION
5  #include <OpenAL/al.h>
6  #include <OpenAL/alc.h>
7 #elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__linux__) || defined(_WIN32) || defined(_WIN64) || defined(__HAIKU__)
8  #include <AL/al.h>
9  #include <AL/alc.h>
10 #endif
11 
12 #include <array>
13 #include <map>
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include <tdme/tdme.h>
19 #include <tdme/audio/AudioEntity.h>
20 #include <tdme/math/Vector3.h>
21 #include <tdme/utilities/Console.h>
22 
23 using std::array;
24 using std::map;
25 using std::string;
26 using std::unique_ptr;
27 using std::vector;
28 
29 using tdme::audio::Audio;
33 
34 Audio* Audio::instance = nullptr;
35 
36 
37 void Audio::shutdown()
38 {
39  if (Audio::instance == nullptr) return;
40  delete Audio::instance;
41  Audio::instance = nullptr;
42 }
43 
45 {
46  // TODO: error handling
47  device = alcOpenDevice(NULL);
48  context = alcCreateContext(device, NULL);
49  alcMakeContextCurrent(context);
50 
51  //
52  listenerPosition.set(0.0f, 0.0f, 0.0);
53  listenerVelocity.set(0.0f, 0.0f, 0.0);
54  listenerOrientationAt.set(0.0f, 0.0f, 1.0f);
55  listenerOrientationUp.set(0.0f, 1.0f, 0.0f);
56 
57  // init listener position
58  update();
59 }
60 
62 {
63  reset();
64  alcCloseDevice(device);
65 }
66 
68 {
69  if (entity->initialize() == true) {
70  auto audioEntityIt = audioEntities.find(entity->getId());
71  if (audioEntityIt != audioEntities.end()) {
72  // check if we want to add this entity a second time
73  if (entity == audioEntityIt->second) {
74  Console::println("Audio::addEntity(): " + entity->getId() + ": entity already added!");
75  return;
76  }
77  // remove old other entity
78  removeEntity(entity->getId());
79  }
80  audioEntities[entity->getId()] = entity;
81  } else {
82  Console::println(string("Audio::addEntity(): adding '" + entity->getId() + "' failed"));
83  }
84 }
85 
86 void Audio::removeEntity(const string& id)
87 {
88  auto audioEntityIt = audioEntities.find(id);
89  if (audioEntityIt != audioEntities.end()) {
90  auto audioEntity = audioEntityIt->second;
91  audioEntity->stop();
92  audioEntity->dispose();
93  audioEntities.erase(audioEntityIt);
94  delete audioEntity;
95  }
96 }
97 
99 {
100  // determine keys to remove
101  vector<string> audioEntitiesToRemove;
102  for (const auto& [audioEntityId, audioEntity]: audioEntities) {
103  audioEntitiesToRemove.push_back(audioEntityId);
104  }
105 
106  // remove entities
107  for (const auto& key: audioEntitiesToRemove) {
108  removeEntity(key);
109  }
110 }
112 {
113  // update audio entities
114  for (const auto& [audioEntityId, audioEntity]: audioEntities) {
115  audioEntity->update();
116  }
117 
118  // update listener position
119  alListenerfv(AL_POSITION, listenerPosition.getArray().data());
120  alListenerfv(AL_VELOCITY, listenerVelocity.getArray().data());
121  array<float, 6> listenerOrientation = {
128  };
129  alListenerfv(AL_ORIENTATION, listenerOrientation.data());
130 }
Audio entity base class.
Definition: AudioEntity.h:19
virtual bool initialize()=0
Initiates this OpenAL entity to OpenAl.
virtual const string & getId() const
Definition: AudioEntity.h:66
Interface to audio module.
Definition: Audio.h:29
Vector3 listenerOrientationAt
Definition: Audio.h:47
Vector3 listenerPosition
Definition: Audio.h:45
ALCdevice * device
Definition: Audio.h:39
void addEntity(AudioEntity *entity)
Adds a audio entity.
Definition: Audio.cpp:67
Vector3 listenerOrientationUp
Definition: Audio.h:48
unordered_map< string, AudioEntity * > audioEntities
Definition: Audio.h:42
void removeEntity(const string &id)
Removes an audio entity.
Definition: Audio.cpp:86
void update()
Update and transfer audio entity states to open AL.
Definition: Audio.cpp:111
static STATIC_DLL_IMPEXT Audio * instance
Definition: Audio.h:37
void reset()
Clears all audio entities.
Definition: Audio.cpp:98
~Audio()
Private destructor.
Definition: Audio.cpp:61
Vector3 listenerVelocity
Definition: Audio.h:46
Audio()
Private constructor.
Definition: Audio.cpp:44
ALCcontext * context
Definition: Audio.h:40
Vector3 class representing vector3 mathematical structure and operations with x, y,...
Definition: Vector3.h:20
const array< float, 3 > & getArray() const
Definition: Vector3.h:366
Vector3 & set(float x, float y, float z)
Sets this vector3 by its components.
Definition: Vector3.h:70
Console class.
Definition: Console.h:29