-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAudio.h
382 lines (325 loc) · 11.8 KB
/
Audio.h
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#ifndef _AUDIO_H
#define _AUDIO_H
#include "Util.h"
#include <AL/al.h>
//#include <al/alc.h>
//#include <al/alu.h>
#include <ogg/ogg.h>
#include <vorbis/codec.h>
#include <vorbis/vorbisenc.h>
#include <vorbis/vorbisfile.h>
#include <AL/alut.h>
#include <memory>
#include "Log.h"
#include <iostream>
#include "math/common.h"
#pragma warning(disable:4996)
#define BUFFER_SIZE (4096 * 8)
class Audio
{
public:
struct Buffer
{
unsigned int id;
Buffer(){
alGenBuffers(1, &id);
}
Buffer(std::string fn) {
//id = 0;
//ALenum format;
//ALsizei size;
//ALvoid* data;
//ALsizei freq;
//ALboolean loop;
//alGenBuffers(1, &id);
//if(alGetError() != AL_NO_ERROR) {
// id = 0;
// break;
//}
//alutLoadWAVFile((ALbyte*)fn.c_str(), &format, &data, &size, &freq, &loop);
//alBufferData(id, format, data, size, freq);
//alutUnloadWAV(format,data,size,freq);
id = alutCreateBufferFromFile(fn.c_str());
}
virtual ~Buffer() {
if(id)
alDeleteBuffers(1, &id);
}
bool good() { return id!=0; }
};
struct Source
{
unsigned int id;
float pitch;
float gain;
float rolloff;
glm::vec3 pos;
glm::vec3 vel;
unsigned int buffer_id;
enum eFlags {
F_LOOP = BIT(0),
F_AUTOPLAY = BIT(1)
};
unsigned int flags;
Source(unsigned int _flags = 0) {
alGenSources(1, &id);
pitch = 1.0f;
gain = 1.0f;
buffer_id = 0;
flags = _flags;
if(flags & F_AUTOPLAY){
refresh();
play();
}
}
virtual ~Source() {
alDeleteSources(1, &id);
}
void bind(Buffer* buf) {
if(buf)
buffer_id = buf->id;
}
virtual void refresh() {
alSourcei(id, AL_BUFFER, buffer_id);
alSourcef(id, AL_PITCH, pitch);
alSourcef(id, AL_GAIN, gain);
alSourcefv(id, AL_POSITION, glm::value_ptr(pos));
alSourcefv(id, AL_VELOCITY, glm::value_ptr(vel));
//alSourcefv(id, AL_ROLLOFF_FACTOR, glm::value_ptr(rolloff));
alSourcei(id, AL_LOOPING, (flags & F_LOOP) ? AL_TRUE : AL_FALSE);
}
virtual void play() {
alSourcePlay(id);
}
bool playing() {
ALenum state;
alGetSourcei(id, AL_SOURCE_STATE, &state);
return state == AL_PLAYING;
}
void pause() {
alSourcePause(id);
}
void stop() {
alSourceStop(id);
}
bool good() { return id!=0; }
};
struct Stream : public Source
{
private:
//FILE* m_File;
OggVorbis_File m_Ogg;
vorbis_info* m_VorbisInfo;
vorbis_comment* m_VorbisComment;
ALenum m_Format;
ALuint m_Buffers[2];
bool m_bOpen;
public:
Stream(std::string fn) {
m_bOpen = false;
do{
//m_File = fopen(fn.c_str(), "rb");
//if(!m_File)
// break;
int r;
if((r = ov_fopen((char*)&fn[0], &m_Ogg)) < 0)
break;
if(checkErrors())
break;
m_VorbisInfo = ov_info(&m_Ogg, -1);
m_VorbisComment = ov_comment(&m_Ogg, -1);
if(checkErrors())
break;
if(m_VorbisInfo->channels == 1)
m_Format = AL_FORMAT_MONO16;
else
m_Format = AL_FORMAT_STEREO16;
alGenBuffers(2, m_Buffers);
if(checkErrors())
break;
flags |= Source::F_LOOP;
std::cout
<< "version " << m_VorbisInfo->version << "\n"
<< "channels " << m_VorbisInfo->channels << "\n"
<< "rate (hz) " << m_VorbisInfo->rate << "\n"
<< "bitrate upper " << m_VorbisInfo->bitrate_upper << "\n"
<< "bitrate nominal " << m_VorbisInfo->bitrate_nominal << "\n"
<< "bitrate lower " << m_VorbisInfo->bitrate_lower << "\n"
<< "bitrate window " << m_VorbisInfo->bitrate_window << "\n"
<< "\n"
<< "vendor " << m_VorbisComment->vendor << "\n";
for(int i = 0; i < m_VorbisComment->comments; i++)
std::cout << " " << m_VorbisComment->user_comments[i] << "\n";
std::cout << std::endl;
m_bOpen = true;
} while(false);
}
bool update()
{
int processed;
bool active = true;
alGetSourcei(id, AL_BUFFERS_PROCESSED, &processed);
while(processed--)
{
ALuint buffer;
alSourceUnqueueBuffers(id, 1, &buffer);
checkErrors();
active = stream(buffer);
alSourceQueueBuffers(id, 1, &buffer);
checkErrors();
}
return active;
}
void clear()
{
int queued;
alGetSourcei(id, AL_BUFFERS_QUEUED, &queued);
while(queued--)
{
ALuint buffer;
alSourceUnqueueBuffers(id, 1, &buffer);
checkErrors();
}
}
bool stream(unsigned int buffer)
{
char data[BUFFER_SIZE];
int size = 0;
int endian = 0;
int section;
int result;
while(size < BUFFER_SIZE)
{
result = ov_read(&m_Ogg, data + size, BUFFER_SIZE - size, endian, 2, 1, §ion);
if(result > 0)
size += result;
else
{
if(result < 0)
return false;
else
break;
}
}
if(size == 0)
return false;
alBufferData(buffer, m_Format, data, size, m_VorbisInfo->rate);
return true;
}
virtual void refresh() {
//if(playing())
//{
update();
//alSourcei(id, AL_BUFFER, buffer_id);
alSourcef(id, AL_PITCH, pitch);
alSourcef(id, AL_GAIN, gain);
alSourcefv(id, AL_POSITION, glm::value_ptr(pos));
alSourcefv(id, AL_VELOCITY, glm::value_ptr(vel));
alSourcefv(id, AL_DIRECTION, glm::value_ptr(vel));
alSourcef(id, AL_ROLLOFF_FACTOR, 0.0);
alSourcei(id, AL_SOURCE_RELATIVE, AL_TRUE);
//alSourcei(id, AL_LOOPING, (flags & F_LOOP) ? AL_TRUE : AL_FALSE);
//}
}
virtual void play() {
if(playing())
return;
if(!stream(m_Buffers[0]))
return;
if(!stream(m_Buffers[1]))
return;
alSourceQueueBuffers(id, 2, m_Buffers);
alSourcePlay(id);
}
bool good() { return m_bOpen; }
bool checkErrors() {
int error = alGetError();
if(error != AL_NO_ERROR) {
std::pair<std::string, std::string> errpair = errorStringAL(error);
Log::get().write(std::string("OpenAL Error (") + errpair.first + "): " + errpair.second);
return true;
}
return false;
}
static std::pair<std::string, std::string> errorStringAL(int code)
{
switch(code)
{
case AL_INVALID_NAME:
return {"AL_INVALID_NAME", "Invalid name."};
case AL_INVALID_ENUM:
return {"AL_INVALID_ENUM", "Invalid enum."};
case AL_INVALID_VALUE:
return {"AL_INVALID_VALUE", "Invalid value."};
case AL_INVALID_OPERATION:
return {"AL_INVALID_OPERATION", "Invalid operation."};
case AL_OUT_OF_MEMORY:
return {"AL_OUT_OF_MEMORY", "Out of memory."};
}
return (code!=AL_NO_ERROR) ?
std::pair<std::string,std::string>(std::string(""), std::string("No Error.")) :
std::pair<std::string,std::string>(str(code), std::string("Unknown Error Code"));
}
static std::pair<std::string,std::string> errorStringOV(int code)
{
switch(code)
{
// libvorbis return codes http://www.xiph.org/vorbis/doc/libvorbis/return.html
case OV_EREAD:
return {"OC_EREAD","Read from media."};
case OV_ENOTVORBIS:
return {"OC_ENOTVORBIS","Not Vorbis data."};
case OV_EVERSION:
return {"OV_EVERSION", "Vorbis version mismatch."};
case OV_EBADHEADER:
return {"OV_EBADHEADER", "Invalid Vorbis header."};
case OV_EFAULT:
return {"OV_EFAULT", "Internal logic fault (bug or heap/stack corruption."};
}
return code ?
std::pair<std::string,std::string>( str(code), std::string("Unknown Error Code ") + str(code) ) :
std::pair<std::string,std::string>( "","No Error." );
}
virtual ~Stream() {
stop();
clear();
alDeleteBuffers(2, m_Buffers);
ov_clear(&m_Ogg);
}
};
struct Listener
{
glm::vec3 pos, vel;
glm::vec3 at;
glm::vec3 up;
Listener() {
pos = glm::vec3(0.0f, 0.0f, 0.0f);
vel = glm::vec3(0.0f, 0.0f, 0.0f);
at = glm::vec3(0.0f, 0.0f, -1.0f);
up = glm::vec3(0.0f, 1.0f, 0.0f);
}
virtual ~Listener() {}
void listen() {
alListenerfv(AL_POSITION, glm::value_ptr(pos));
alListenerfv(AL_VELOCITY, glm::value_ptr(vel));
float ori[6];
ori[0] = at.x; ori[1] = at.y; ori[2] = at.z;
ori[3] = up.x; ori[4] = up.y; ori[5] = up.z;
alListenerfv(AL_ORIENTATION, ori);
}
};
public:
Audio(){
alutInit(0, NULL);
alGetError();
}
virtual ~Audio(){
alutExit();
}
void listen(Listener* listener) {
if(listener)
listener->listen();
}
bool error() { return alGetError() != AL_NO_ERROR; }
};
#endif