-
Notifications
You must be signed in to change notification settings - Fork 5
/
audio.cpp
346 lines (288 loc) · 8.25 KB
/
audio.cpp
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
Copyright 2013-2015 Rohit Nirmal
This file is part of Clonepoint.
Clonepoint is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clonepoint is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clonepoint. If not, see <http://www.gnu.org/licenses/>.
*/
#include "audio.h"
#include "file.h"
#include "global.h"
#include "stb_vorbis.c"
AudioManager::AudioManager()
{
_device = alcOpenDevice(NULL);
_context = alcCreateContext(_device, NULL);
alcMakeContextCurrent(_context);
ALCint maxmono = 0, maxstereo = 0;
alcGetIntegerv(_device, ALC_MONO_SOURCES, 1, &maxmono);
alcGetIntegerv(_device, ALC_STEREO_SOURCES, 1, &maxstereo);
ALuint src;
for (unsigned int i = 0; i < MAX_SOURCES; i++)
{
src = 0;
alGenSources(1, &src);
LOGF((stdout, "Pushing source %i into buffer.\n", src));
_loadedSources.push_back(src);
}
_musicSource = 0;
_mapMusicBuffer = 0;
_mapMusicCrosslinkBuffer = 0;
alGenSources(1, &_musicSource);
loadWAV("./data/sounds/alarm.wav");
loadWAV("./data/sounds/door_open.wav");
loadWAV("./data/sounds/door_close.wav");
loadWAV("./data/sounds/circuitbox.wav");
loadWAV("./data/sounds/pistol.wav");
loadWAV("./data/sounds/pistol_ready.wav");
loadWAV("./data/sounds/menu_click.wav");
loadWAV("./data/sounds/switch.wav");
loadWAV("./data/sounds/link.wav");
loadWAV("./data/sounds/jump.wav");
loadWAV("./data/sounds/enter_crosslink.wav");
loadWAV("./data/sounds/exit_crosslink.wav");
loadWAV("./data/sounds/punch1.wav");
loadWAV("./data/sounds/punch2.wav");
loadWAV("./data/sounds/glass_break.wav");
loadWAV("./data/sounds/elevator_arrive.wav");
loadWAV("./data/sounds/elevator_leave.wav");
loadWAV("./data/sounds/elevator_decelerate.wav");
_mainmenuMusicBuffer = bufferFromOGG("./data/music/ontheground.ogg", NULL);
_playingMenuMusic = false;
}
AudioManager::~AudioManager()
{
size_t i;
for (i = 0; i < _loadedSources.size(); i++)
{
alDeleteSources(1, &_loadedSources[i]);
}
_loadedSources.clear();
alDeleteSources(1, &_musicSource);
if (_mapMusicBuffer != 0)
{
alDeleteBuffers(1, &_mapMusicBuffer);
}
if (_mainmenuMusicBuffer != 0)
{
alDeleteBuffers(1, &_mainmenuMusicBuffer);
}
if (_mapMusicCrosslinkBuffer != 0)
{
alDeleteBuffers(1, &_mapMusicCrosslinkBuffer);
}
std::map<std::string, ALuint>::iterator it;
for (it = _loadedBuffers.begin(); it != _loadedBuffers.end(); it++)
{
ALuint buf = it->second;
LOGF((stdout, "Deleting OpenAL buffer %i.\n", buf));
alDeleteBuffers(1, &buf);
}
alcMakeContextCurrent(NULL);
if (_context) alcDestroyContext(_context);
if (_device) alcCloseDevice(_device);
}
void AudioManager::playSound(std::string filename)
{
ALuint src = getNextAvailableSource();
ALuint buf = getBuffer(filename);
alSourcei(src, AL_BUFFER, buf);
alSource3f(src, AL_POSITION, 0, 0, 0);
alSourcePlay(src);
}
void AudioManager::playMapMusic(std::string filename)
{
if (_mapMusicBuffer != 0)
{
alDeleteBuffers(1, &_mapMusicBuffer);
alDeleteBuffers(1, &_mapMusicCrosslinkBuffer);
_mapMusicBuffer = 0;
_mapMusicCrosslinkBuffer = 0;
}
if (filename == "")
{
_playingMenuMusic = false;
return;
}
_mapMusicBuffer = bufferFromOGG("./data/music/" + filename, &_mapMusicCrosslinkBuffer);
if (_playingMenuMusic)
{
alSourceStop(_musicSource);
}
_playingMenuMusic = false;
playMusic(_mapMusicBuffer);
}
void AudioManager::playMenuMusic()
{
if (_playingMenuMusic)
return;
_playingMenuMusic = true;
playMusic(_mainmenuMusicBuffer);
}
void AudioManager::playMusic(ALuint buf)
{
if (buf == 0)
{
return;
}
alSourcei(_musicSource, AL_BUFFER, buf);
alSource3f(_musicSource, AL_POSITION, 0, 0, 0);
alSourcei(_musicSource, AL_LOOPING, 1);
alSourcePlay(_musicSource);
}
void AudioManager::stopMusic()
{
alDeleteBuffers(1, &_mapMusicBuffer);
_mapMusicBuffer = 0;
alSourceStop(_musicSource);
}
void AudioManager::playSound3D(const char* filename, float x, float y, float z)
{
ALuint src = getNextAvailableSource();
ALuint buf = getBuffer(filename);
alSourcei(src, AL_BUFFER, buf);
alSource3f(src, AL_POSITION, x, y, z);
alSourcePlay(src);
}
void AudioManager::loadWAV(std::string filename)
{
SDL_AudioSpec wav_spec;
Uint32 wav_length = 0;
Uint8* wav_buffer;
if (SDL_LoadWAV(filename.c_str(), &wav_spec, &wav_buffer, &wav_length) != NULL)
{
ALenum format;
switch(wav_spec.format)
{
case AUDIO_U8:
case AUDIO_S8:
format = wav_spec.channels == 2 ? AL_FORMAT_STEREO8 : AL_FORMAT_MONO8;
break;
case AUDIO_U16:
case AUDIO_S16:
format = wav_spec.channels == 2 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16;
break;
default:
LOGF((stderr, "Wave file %s is of unknown format!\n", filename.c_str()));
SDL_FreeWAV(wav_buffer);
return;
break;
}
ALuint buf;
alGenBuffers(1, &buf);
alBufferData(buf, format, wav_buffer, wav_length, wav_spec.freq);
SDL_FreeWAV(wav_buffer);
int slash = filename.find_last_of("/");
int dot = filename.find_last_of(".");
std::string bufferString = filename.substr(slash + 1, dot - slash - 1);
LOGF((stdout, "Storing %s into buffer %i.\n", bufferString.c_str(), buf));
_loadedBuffers.insert(std::pair<std::string, ALuint>(bufferString, buf));
}
else
{
LOGF((stderr, "Couldn't load wave file %s\n", filename.c_str()));
LOGF((stderr, "%s\n", SDL_GetError()));
}
}
ALuint AudioManager::bufferFromOGG(std::string filename, ALuint* filtered_buf)
{
long size;
char* file_buffer = file_read(filename.c_str(), &size);
if (!file_buffer)
{
return 0;
}
stb_vorbis* ogg = stb_vorbis_open_memory((const unsigned char*)file_buffer, (int)size, NULL, NULL);
if (!ogg)
{
return 0;
}
ALuint ret;
stb_vorbis_info info = stb_vorbis_get_info( ogg );
ALenum format = ( ( info.channels == 1 ) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16);
int const length_samples = stb_vorbis_stream_length_in_samples(ogg) * info.channels;
std::vector<ALshort> ogg_buffer(length_samples);
std::vector<ALshort> filtered(length_samples);
alGenBuffers(1, &ret);
stb_vorbis_get_samples_short_interleaved(ogg, info.channels, ogg_buffer.data(), length_samples);
if (filtered_buf != NULL)
{
//Apply low pass filter for to another buffer of the music for crosslink
int i;
float alpha = 0.05f;
filtered[0] = ogg_buffer[0];
for (i = 1; i < length_samples; i++)
{
float filter = (alpha * (float)ogg_buffer[i]) + (1.0f - alpha) * (float)filtered[i-1];
filtered[i] = (ALshort)filter;
}
alGenBuffers(1, filtered_buf);
}
alBufferData(ret, format, ogg_buffer.data(), (length_samples * sizeof( ALshort)), info.sample_rate);
if (filtered_buf != NULL)
{
alBufferData(*filtered_buf, format, filtered.data(), (length_samples * sizeof( ALshort)), info.sample_rate);
}
stb_vorbis_close(ogg);
delete [] file_buffer;
return ret;
}
ALuint AudioManager::getBuffer(std::string filename)
{
std::map<std::string, ALuint>::iterator it = _loadedBuffers.find(filename);
if (it == _loadedBuffers.end())
{
LOGF((stderr, "Failed to find buffer for %s\n", filename.c_str()));
return 0;
}
return it->second;
}
ALuint AudioManager::getNextAvailableSource()
{
int status;
size_t i;
for (i = 0; i < _loadedSources.size(); i++)
{
alGetSourcei(_loadedSources[i], AL_SOURCE_STATE, &status);
if (status != AL_PLAYING)
{
return _loadedSources[i];
}
}
//all sounds are currently playing!
return 0;
}
void AudioManager::setSoundVolume(float volume)
{
size_t i;
for (i = 0; i < _loadedSources.size(); i++)
{
alSourcef(_loadedSources[i], AL_GAIN, volume);
}
}
float AudioManager::getMusicTime()
{
float result;
alGetSourcef(_musicSource, AL_SEC_OFFSET, &result);
return result;
}
void AudioManager::changeMusicMode(bool crosslink)
{
float time = getMusicTime();
int status;
alGetSourcei(_musicSource, AL_SOURCE_STATE, &status);
alSourceStop(_musicSource);
playMusic(crosslink ? _mapMusicCrosslinkBuffer : _mapMusicBuffer);
alSourcef(_musicSource, AL_SEC_OFFSET, time);
}
void AudioManager::setMusicVolume(float volume)
{
alSourcef(_musicSource, AL_GAIN, volume);
}