forked from yiming-cai/OursCraft
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSound.cpp
More file actions
358 lines (309 loc) · 8.68 KB
/
Copy pathSound.cpp
File metadata and controls
358 lines (309 loc) · 8.68 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
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
347
348
349
350
351
352
353
354
355
356
#include "Sound.h"
void Sound::updateListener()
{
glm::vec3 at = camera->camera_lookAt - camera->camera_pos;
glm::vec3 up = camera->camera_up;
glm::vec3 pos = camera->camera_pos;
ALfloat listenerOri[] = {at[0],at[1],at[2],up[0],up[1],up[2]};
alListener3f(AL_POSITION, pos[0], pos[1], pos[2]);
alListener3f(AL_VELOCITY, 0.0f, 0, 0);
alListenerfv(AL_ORIENTATION, listenerOri);
}
ALuint Sound::generateSource(glm::vec3 position)
{
ALuint source;
glm::vec3 curr_pos = position;
alGenSources((ALuint)1, &source);
alSourcef(source, AL_PITCH, 1.0f);
alSourcef(source, AL_GAIN, 1.0f);
alSource3f(source, AL_POSITION, curr_pos[0], curr_pos[1], curr_pos[2]);
alSource3f(source, AL_VELOCITY, 0, 0, 0);
alSourcei(source, AL_LOOPING, AL_FALSE);
alSourcef(source, AL_ROLLOFF_FACTOR, 0.1f);
allGeneratedSources.insert(source);
bindedSources[source] = 0;
// check for errors
checkError("Error generating a source");
return source;
}
void Sound::updateSourcePosition(ALuint source, glm::vec3 curr_pos)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
alSource3f(source, AL_POSITION, curr_pos[0], curr_pos[1], curr_pos[2]);
}
void Sound::updateSourceVelocity(ALuint source, glm::vec3 velocity)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
alSource3f(source, AL_VELOCITY, velocity[0], velocity[1], velocity[2]);
}
void Sound::updateSourceGain(ALuint source, float gain)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
alSourcef(source, AL_POSITION, gain);
}
void Sound::updateSourcePitch(ALuint source, float pitch)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
alSourcef(source, AL_POSITION, pitch);
}
void Sound::setSourceLooping(ALuint source, bool shouldLoop)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
alSourcei(source, AL_LOOPING, (shouldLoop? 1 : 0) );
}
ALuint Sound::generateBuffer(std::string filepath)
{
// set max buffer len to 50mb
const static int MAX_BUFFER_LEN = 1024 * 1024 * 25;
SoundAttributes curr;
auto it = soundBuffers.find(filepath);
if (it == soundBuffers.end())
{
std::vector<char> bytes(filepath.begin(), filepath.end());
bytes.push_back('\0');
char *c = &bytes[0];
SndfileHandle file;
fprintf(stderr, "Reading file named '%s'\n", c);
file = SndfileHandle(c);
printf("Opened file '%s'\n", c);
printf(" Sample rate : %d\n", file.samplerate());
printf(" Channels : %d\n", file.channels());
printf(" Formats : %d\n", file.format());
ALenum format = 0;
if (file.channels() == 2)
{
if (file.format() && SF_FORMAT_PCM_16 == SF_FORMAT_PCM_16)
{
format = AL_FORMAT_STEREO16;
std::cerr << "Reading in STEREO 16bit sound file" << std::endl;
}
else if (file.format() && SF_FORMAT_PCM_S8 == SF_FORMAT_PCM_S8)
{
format = AL_FORMAT_STEREO8;
std::cerr << "Reading in STEREO 8bit sound file" << std::endl;
}
}
else if (file.channels() == 1)
{
if (file.format() && SF_FORMAT_PCM_16 == SF_FORMAT_PCM_16)
{
format = AL_FORMAT_MONO16;
std::cerr << "Reading in MONO 16bit sound file" << std::endl;
}
else if (file.format() && SF_FORMAT_PCM_S8 == SF_FORMAT_PCM_S8)
{
std::cerr << "Reading in MONO 8bit sound file" << std::endl;
format = AL_FORMAT_MONO8;
}
}
if (format == 0)
{
std::cerr << "Does not support " << file.channels() << " channels with sample size that are not 8 or 16bits" << std::endl;
return 0;
}
short *data = new short[MAX_BUFFER_LEN];
int size = file.read(data, MAX_BUFFER_LEN) * sizeof(short);
curr = { data, size, file.samplerate(), format };
soundBuffers[filepath] = curr;
std::cerr << "Generating new sound data!" << std::endl;
}
else
{
curr = (*it).second;
std::cerr << "Reusing old sound data" << std::endl;
}
ALuint buffer;
alGenBuffers((ALuint)1, &buffer);
if (checkError("Error generating a new buffer data")) return NULL;
alBufferData(buffer, curr.format, curr.data, curr.size, curr.frequency);
std::cerr << "Buffered a file " << filepath << " of size " << curr.size << std::endl;
if (checkError("Error buffering data")) return NULL;
allGeneratedBuffers.insert(buffer);
return buffer;
}
void Sound::bindSourceToBuffer(ALuint source, ALuint buffer)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
if (allGeneratedBuffers.find(buffer) == allGeneratedBuffers.end())
{
std::cerr << "This buffer does not exist!" << std::endl;
return;
}
alSourcei(source, AL_BUFFER, buffer);
if (checkError("Error binding source to buffer")) { return; }
bindedSources[source] = buffer;
}
void Sound::playSourceSound(ALuint source)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
else
{
std::cerr << "Playing this source!" << std::endl;
}
alSourcePlay(source);
checkError("Error occurred in playing sound!" );
}
void Sound::pauseSourceSound(ALuint source)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
alSourcePause(source);
}
void Sound::stopSourceSound(ALuint source)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
alSourceStop(source);
}
void Sound::rewindSourceSound(ALuint source)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
alSourceRewind(source);
}
bool Sound::isSourcePlaying(ALuint source)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return false;
}
ALint source_state;
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
if (checkError("Error detecting if sound is playing")) return false;
if (source_state == AL_PLAYING) return true;
else return false;
}
void Sound::pauseUntilFinishPlaying(ALuint source)
{
if (allGeneratedSources.find(source) == allGeneratedSources.end())
{
std::cerr << "This source does not exist!" << std::endl;
return;
}
ALint source_state;
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
if (checkError("Error pausing")) return;
while (source_state == AL_PLAYING) {
alGetSourcei(source, AL_SOURCE_STATE, &source_state);
if (checkError("Error pausing")) return;
}
}
Sound::Sound(Camera * cam)
{
camera = cam;
if (cam != nullptr)
{
updateListener();
}
ALCboolean enumeration = alcIsExtensionPresent(NULL, "ALC_ENUMERATION_EXT");
if (enumeration == AL_FALSE)
{
// enumeration not supported
std::cerr << "AL Enumeration is not supported" << std::endl;
}
// reset error stack
alGetError();
const ALCchar *default_device = alcGetString(NULL, ALC_DEFAULT_DEVICE_SPECIFIER);
device = alcOpenDevice( default_device );
if (!device)
{
std::cerr << "Bad audio input!" << std::endl;
}
else
{
std::cerr << "Playing default device: " << default_device << std::endl;
}
context = alcCreateContext(device, NULL);
if (!alcMakeContextCurrent(context))
{
checkError("Error creating context");
return;
}
//alDistanceModel(AL_LINEAR_DISTANCE);
//alutInitWithoutContext(NULL, NULL);
}
// for cleaning up everything
Sound::~Sound()
{
for (auto source : allGeneratedSources)
{
alDeleteSources(1, &source);
}
for (auto buffer : allGeneratedBuffers)
{
alDeleteBuffers(1, &buffer);
}
device = alcGetContextsDevice(context);
alcMakeContextCurrent(NULL);
alcDestroyContext(context);
alcCloseDevice(device);
for (auto it : soundBuffers)
{
delete[] it.second.data;
}
//alutExit();
}
void Sound::list_audio_devices(const ALCchar *devices)
{
const ALCchar *device = devices, *next = devices + 1;
size_t len = 0;
fprintf(stdout, "Devices list:\n");
fprintf(stdout, "----------\n");
while (device && *device != '\0' && next && *next != '\0') {
fprintf(stdout, "%s\n", device);
len = strlen(device);
device += (len + 1);
next += (len + 2);
}
fprintf(stdout, "----------\n");
}
bool Sound::checkError(std::string comment)
{
ALCenum error;
error = alGetError();
if (error != AL_NO_ERROR)
{
//std::cerr << alutGetErrorString(error) << std::endl;
std::cerr << "checkError() returned an error: " << comment << std::endl;
return true;
}
return false;
}