-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSound.h
More file actions
259 lines (220 loc) · 7.11 KB
/
Sound.h
File metadata and controls
259 lines (220 loc) · 7.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/* SimShip by Edouard Halbert
This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
http://creativecommons.org/licenses/by-nc-nd/4.0/ */
#pragma once
#include <string>
#include <vector>
#include <map>
#include <stdexcept>
#include <cstdio>
// OpenAL
#include <al.h>
#include <alc.h>
// glm
#include <glm/glm.hpp>
using namespace std;
using namespace glm;
class SoundManager
{
private:
static SoundManager * instance;
ALCdevice * device;
ALCcontext * context;
map<string, ALuint> buffers;
SoundManager()
{
initOpenAL();
}
public:
bool bSound = true;
static SoundManager* getInstance()
{
if (!instance)
instance = new SoundManager();
return instance;
}
~SoundManager()
{
cleanupOpenAL();
}
bool initOpenAL()
{
device = alcOpenDevice(NULL);
if (!device) return false;
context = alcCreateContext(device, NULL);
if (!context) return false;
if (!alcMakeContextCurrent(context)) return false;
// OpenAL offers several mitigation models
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
//alDopplerFactor(1.0f); // Reduces the Doppler effect
return true;
}
void cleanupOpenAL()
{
for (auto& pair : buffers)
alDeleteBuffers(1, &pair.second);
alcMakeContextCurrent(nullptr);
alcDestroyContext(context);
alcCloseDevice(device);
}
ALuint loadSound(const string& fichier)
{
if (buffers.find(fichier) != buffers.end())
return buffers[fichier];
ALuint buffer;
if (loadWAV(fichier, buffer))
{
buffers[fichier] = buffer;
return buffer;
}
return 0;
}
// Methods for the listener
void setListenerPosition(float x, float y, float z)
{
alListener3f(AL_POSITION, x, y, z);
}
void setListenerPosition(vec3 pos)
{
ALfloat listenerPos[] = { pos.x, pos.y, pos.z };
alListenerfv(AL_POSITION, listenerPos);
}
void setListenerOrientation(float atX, float atY, float atZ, float upX, float upY, float upZ)
{
ALfloat orientation[] = { atX, atY, atZ, upX, upY, upZ };
alListenerfv(AL_ORIENTATION, orientation);
}
void setListenerOrientation(vec3 at, vec3 up)
{
ALfloat listenerOri[] = { at.x, at.y, at.z, up.x, up.y, up.z };
alListenerfv(AL_ORIENTATION, listenerOri);
}
private:
bool loadWAV(const string& fichier, ALuint& buffer)
{
FILE* fp = fopen(fichier.c_str(), "rb");
if (!fp)
throw std::runtime_error("Impossible d'ouvrir le fichier " + fichier);
char chunkID[4];
uint32_t chunkSize;
uint32_t taux_echantillonnage;
uint16_t format, canaux, bits_par_echantillon;
uint32_t dataSize = 0;
// Read RIFF header
fread(chunkID, sizeof(char), 4, fp);
if (strncmp(chunkID, "RIFF", 4) != 0)
{
fclose(fp);
throw std::runtime_error("Le fichier n'est pas au format WAV");
}
fread(&chunkSize, sizeof(uint32_t), 1, fp);
fread(chunkID, sizeof(char), 4, fp);
if (strncmp(chunkID, "WAVE", 4) != 0)
{
fclose(fp);
throw std::runtime_error("Le fichier n'est pas au format WAV");
}
// Search for the "fmt" chunk
while (true)
{
fread(chunkID, sizeof(char), 4, fp);
fread(&chunkSize, sizeof(uint32_t), 1, fp);
if (strncmp(chunkID, "fmt ", 4) == 0) {
fread(&format, sizeof(uint16_t), 1, fp);
fread(&canaux, sizeof(uint16_t), 1, fp);
fread(&taux_echantillonnage, sizeof(uint32_t), 1, fp);
fseek(fp, 6, SEEK_CUR); // Ignore ByteRate and BlockAlign
fread(&bits_par_echantillon, sizeof(uint16_t), 1, fp);
break;
}
else
fseek(fp, chunkSize, SEEK_CUR);
}
// Search for the "data" chunk
while (true)
{
fread(chunkID, sizeof(char), 4, fp);
fread(&chunkSize, sizeof(uint32_t), 1, fp);
if (strncmp(chunkID, "data", 4) == 0)
{
dataSize = chunkSize;
break;
}
else
fseek(fp, chunkSize, SEEK_CUR);
}
if (dataSize == 0)
{
fclose(fp);
throw std::runtime_error("Aucune donnée audio trouvée");
}
vector<ALchar> donnees(dataSize);
fread(donnees.data(), sizeof(ALchar), dataSize, fp);
fclose(fp);
ALenum format_al;
if (canaux == 1 && bits_par_echantillon == 8)
format_al = AL_FORMAT_MONO8;
else if (canaux == 1 && bits_par_echantillon == 16)
format_al = AL_FORMAT_MONO16;
else if (canaux == 2 && bits_par_echantillon == 8)
format_al = AL_FORMAT_STEREO8;
else if (canaux == 2 && bits_par_echantillon == 16)
format_al = AL_FORMAT_STEREO16;
else {
throw std::runtime_error("Format audio non supporté");
}
alGenBuffers(1, &buffer);
alBufferData(buffer, format_al, donnees.data(), dataSize, taux_echantillonnage);
return (alGetError() == AL_NO_ERROR);
}
};
class Sound
{
private:
ALuint source;
ALuint buffer;
public:
Sound(const string& fichier)
{
SoundManager* manager = SoundManager::getInstance();
buffer = manager->loadSound(fichier);
if (buffer == 0)
throw std::runtime_error("Impossible de charger le fichier audio: " + fichier);
alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffer);
}
~Sound()
{
alDeleteSources(1, &source);
}
void play() {
alSourcePlay(source);
}
void stop() {
alSourceStop(source);
}
void pause() {
// To pause the sound
alSourcePause(source);
}
void setVolume(float volume) {
alSourcef(source, AL_GAIN, volume);
}
void setPosition(float x, float y, float z) {
alSource3f(source, AL_POSITION, x, y, z);
}
void setPosition(vec3 pos) {
alSource3f(source, AL_POSITION, pos.x, pos.y, pos.z);
}
void setPitch(float pitch) {
alSourcef(source, AL_PITCH, pitch);
}
void setLooping(bool loop) {
alSourcei(source, AL_LOOPING, loop);
}
void adjustDistances() {
alSourcef(source, AL_ROLLOFF_FACTOR, 1.0f); // Rolloff factor
alSourcef(source, AL_REFERENCE_DISTANCE, 50.0f); // The sound starts to decrease after x units
alSourcef(source, AL_MAX_DISTANCE, 5000.0f); // The sound no longer drops after x units
}
};