-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOggStream.cpp
208 lines (165 loc) · 4.2 KB
/
OggStream.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
#include "src/OggStream.h"
OggStream::OggStream()
{
}
OggStream& OggStream::getInstance()
{
static OggStream instance;
return instance;
}
bool OggStream::playback()
{
if(playing())
return true;
if(!stream(buffers[0]))
return false;
if(!stream(buffers[1]))
return false;
alSourceQueueBuffers(source, 2, buffers);
alSourcePlay(source);
return true;
}
bool OggStream::playing()
{
ALenum state;
alGetSourcei(source, AL_SOURCE_STATE, &state);
return (state == AL_PLAYING);
}
bool OggStream::update()
{
int processed;
bool active = true;
alGetSourcei(source, AL_BUFFERS_PROCESSED, &processed);
while(processed--)
{
ALuint buffer;
alSourceUnqueueBuffers(source, 1, &buffer);
check();
active = stream(buffer);
alSourceQueueBuffers(source, 1, &buffer);
check();
}
return active;
}
void OggStream::open(const char* path)
{
int result;
if(!(oggFile = fopen(path, "rb")))
{
throw std::string("Could not open Ogg file");
}
if((result = ov_open(oggFile, &oggStream, NULL, 0)) < 0)
{
fclose(oggFile);
throw std::string("Could not open ogg stream")+errorString(result);
}
vorbisInfo = ov_info(&oggStream, -1);
vorbisComment = ov_comment(&oggStream, -1);
if(vorbisInfo->channels == 1)
format = AL_FORMAT_MONO16;
else
format = AL_FORMAT_STEREO16;
//Extract OpenAL enumerator based on channels
alGenBuffers(2, buffers);
check();
alGenSources(1, &source);
//2D BACKGROUND SOUND
alSource3f(source, AL_POSITION, 0.0f, 0.0f, 0.0f);
alSource3f(source, AL_VELOCITY, 0.0f, 0.0f, 0.0f);
alSource3f(source, AL_DIRECTION, 0.0f, 0.0f, 0.0f);
alSourcef(source, AL_ROLLOFF_FACTOR, 0.0f);
alSourcei(source, AL_SOURCE_RELATIVE, AL_TRUE);
}
void OggStream::open(std::string path)
{
open(path.c_str());
}
void OggStream::release()
{
alSourceStop(source);
empty();
alDeleteSources(1, &source);
check();
alDeleteBuffers(1, buffers);
check();
ov_clear(&oggStream);
}
void OggStream::stop()
{
alSourceStop(source);
}
bool OggStream::stream(ALuint buffer)
{
char data[BUFFER_SIZE];
int section;
int result;
unsigned int size = 0;
while(size < BUFFER_SIZE)
{
result = ov_read(&oggStream, data + size, BUFFER_SIZE-size,
0, 2, 1, §ion);
if(result > 0)
size += result;
else
if(result < 0)
{
throw errorString(result);
}
else
break;
}
if(size == 0)
return false;
alBufferData(buffer, format, data, size, vorbisInfo->rate);
check();
return true;
}
std::string OggStream::errorString(int code)
{
switch(code)
{
case OV_EREAD:
return std::string("Read from media");
case OV_ENOTVORBIS:
return std::string("Not vorbis data");
case OV_EVERSION:
return std::string("Vorbis version mismatch");
case OV_EBADHEADER:
return std::string("Invalid Vorbis Header");
case OV_EFAULT:
return std::string("Internal Logic Fault(Heap/Stack Corruption)");
default:
return std::string("Danger Zone");
}
}
void OggStream::check()
{
int error = alGetError();
if(error != AL_NO_ERROR)
switch(error)
{
case AL_INVALID_NAME:
throw std::string("OpenAL error");
case AL_INVALID_ENUM:
throw std::string("OpenAL error");
case AL_INVALID_VALUE:
throw std::string("OpenAL error");
case AL_INVALID_OPERATION:
throw std::string("OpenAL error");
case AL_OUT_OF_MEMORY:
throw std::string("OpenAL error");
default:
throw std::string("OpenAL error");
}
}
void OggStream::empty()
{
int queued;
alGetSourcei(source, AL_BUFFERS_QUEUED, &queued);
while(queued--)
{
ALuint buffer;
alSourceUnqueueBuffers(source, 1, &buffer);
check();
}
}