-
Notifications
You must be signed in to change notification settings - Fork 15
/
OALOgg.cpp
358 lines (294 loc) · 8.47 KB
/
OALOgg.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
347
348
349
350
351
352
353
354
355
356
357
358
#include "OALOgg.h"
OALOgg::OALOgg() :
OALSound() {
//this->streaming = false;
}
OALOgg::OALOgg(bool streaming) :
OALSound(streaming) {
}
OALOgg::OALOgg(AAssetManager *mgr, char * fileName, bool streaming):OALSound(streaming){
load(mgr,fileName);
}
OALOgg::~OALOgg() {
ov_clear(&vf);
}
unsigned int suiCurrPos = 0;
unsigned int suiSize = 0;
//unsigned char* pvOggBuffer;
static size_t read_func(void* ptr, size_t size, size_t nmemb,
void* datasource) {
unsigned int uiBytes = Min(suiSize - suiCurrPos,
(unsigned int) nmemb * (unsigned int) size);
memcpy(ptr, (unsigned char*) datasource + suiCurrPos, uiBytes);
suiCurrPos += uiBytes;
//
return uiBytes;
}
static size_t read_func_streaming(void* ptr, size_t size, size_t nmemb,
void* datasource) {
unsigned int uiBytes = Min(suiSize - suiCurrPos,
(unsigned int) nmemb * (unsigned int) size);
//LOGI("sizeof = %i", sizeof(datasource));
/*if(datasource == NULL || ((AAssetFile*)datasource)->null())
LOGI("null f )=");*/
//LOGI("before suiCurrPos = %i , suiSize %i",suiCurrPos, suiSize);
((AAssetFile*) datasource)->seek(suiCurrPos, 0);
//LOGI("-_- ");
//LOGI("uiBytes = %i",uiBytes);
((AAssetFile*) datasource)->read(ptr, uiBytes, 1);
suiCurrPos += uiBytes;
//LOGI("after suiCurrPos = %i , suiSize %i",suiCurrPos, suiSize);
return uiBytes;
}
static int seek_func(void* datasource, ogg_int64_t offset, int whence) {
if (whence == SEEK_SET)
suiCurrPos = (unsigned int) offset;
else if (whence == SEEK_CUR)
suiCurrPos = suiCurrPos + (unsigned int) offset;
else if (whence == SEEK_END)
suiCurrPos = suiSize;
//LOGI("suiCurrPos = %i , suiSize %i",suiCurrPos, suiSize);
return 0;
}
static int close_func(void* datasource) {
return 0;
}
static long tell_func(void* datasource) {
return (long) suiCurrPos;
}
void OALOgg::Log() {
LOGI("vi->channels = %i", vi->channels);
LOGI("vi->rate = %i", vi->rate);
LOGI("vi->version = %i", vi->version);
LOGI("uiPCMSamples = %i", uiPCMSamples);
}
void OALOgg::getInfo(unsigned int uiOggSize, unsigned char* pvOggBuffer) {
// Çàìåíÿåì êîëáýêè
ov_callbacks callbacks;
callbacks.read_func = &read_func;
callbacks.seek_func = &seek_func;
callbacks.close_func = &close_func;
callbacks.tell_func = &tell_func;
suiCurrPos = 0;
suiSize = uiOggSize;
int iRet = ov_open_callbacks(pvOggBuffer, &vf, NULL, 0, callbacks);
// Çàãîëîâêè
vi = ov_info(&vf, -1);
uiPCMSamples = (unsigned int) ov_pcm_total(&vf, -1);
Log();
}
void OALOgg::getInfo() {
// pvOggBuffer = (char*)malloc(4096);
//f->read(pvOggBuffer,4096,1);
// Çàìåíÿåì êîëáýêè
ov_callbacks callbacks;
callbacks.read_func = (streaming) ? &read_func_streaming : &read_func;
callbacks.seek_func = &seek_func;
callbacks.close_func = &close_func;
callbacks.tell_func = &tell_func;
suiCurrPos = 0;
suiSize = f->size();
//LOGI("f->size() = %i",f->size());
//int iRet = ov_open_callbacks(pvOggBuffer, &vf, NULL, 0, callbacks);
int iRet = ov_open_callbacks(f, &vf, NULL, 0, callbacks);
uiPCMSamples = (unsigned int) ov_pcm_total(&vf, -1);
// Çàãîëîâêè
vi = ov_info(&vf, -1);
Log();
//free(pvOggBuffer);
//delete(f);
}
unsigned char* OALOgg::readBlock(size_t size) {
// if(buf != NULL)
// free(buf);
// buf = (unsigned char *) malloc(size);
// iRead = ov_read(&vf, buf, size, ¤t_section);
// return buf;
unsigned char * data = (unsigned char *) malloc(size);
iRead = ov_read(&vf, data, size, ¤t_section);
return data;
// if(buf != NULL)
// free(buf);
// buf = (unsigned char *) malloc(size);
// iRead = ov_read(&vf, buf, size, ¤t_section);
// return buf;
}
void * OALOgg::ConvertOggToPCM(unsigned int uiOggSize,
unsigned char* pvOggBuffer) {
if (suiSize == 0) {
getInfo(uiOggSize, pvOggBuffer);
current_section = 0;
iRead = 0;
uiCurrPos = 0;
}
void* pvPCMBuffer = malloc(uiPCMSamples * vi->channels * sizeof(short));
// Äåêîäèì
do {
iRead = ov_read(&vf, (char*) pvPCMBuffer + uiCurrPos, 4096,¤t_section);
//iRead = ov_read(&vf, (char*) buf + uiCurrPos, 4096,¤t_section);
uiCurrPos += (unsigned int) iRead;
} while (iRead != 0);
free(pvOggBuffer);
buf = (unsigned char *)pvPCMBuffer;
return buf;
//return pvPCMBuffer;
}
void OALOgg::createBufferFromOgg(unsigned char* data, int size, int index) {
/*alGenBuffers(1,&buffer);
alBufferData(buffer,format,data,size,header.samplesPerSec);
if (source) {
alDeleteSources(1, &source);
delete data;
}
source = 0;
alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffer);*/
//alGenBuffers(1,&buffers[index]);
alBufferData(buffers[index], format, data, size, vi->rate);
}
void OALOgg::creatBuffer(int index) {
//int count = Min(BUFFER_SIZE, this->riffCount -curPos);
//int size = DEFAUULT_COUNT;//Min(BUFFER_SIZE, header.dataSize -curPos);
//this->datas[index] = readRiffs(size);
//unsigned char * data = readBlock(DEFAUULT_COUNT * vi->channels * sizeof(short));
unsigned char * data = readBlock(BUFFER_SIZE);
if (!data)
return;
createBufferFromOgg(data, BUFFER_SIZE, index);
//GenSources(index);
}
void OALOgg:: loadWithStreaming(){
getInfo();
//LOGI("suiCurrPos = %i , suiSize %i", suiCurrPos, suiSize);
if (vi->channels == 1)
format = AL_FORMAT_MONO16;
else
format = AL_FORMAT_STEREO16;
source = 0;
alGenBuffers(BUFF_COUNT, buffers);
alGenSources(1, &source);
//buf = (unsigned char *)malloc(BUFFER_SIZE);
/*
LOGI("source %i",source);
for(int i=0;i<BUFF_COUNT;++i)
LOGI("buf [%i] = %i",i,buffers[i]);*/
if (alGetError() != AL_NO_ERROR) {
LOGI("Error generating :(\n");
return;
}
//LOGI("=/\n");
for (int i = 0; i < BUFF_COUNT; ++i)
creatBuffer(i);
//LOGI("=/\n");
if (alGetError() != AL_NO_ERROR) {
LOGI("Error loading :(\n");
return;
}
LOGI("suiCurrPos = %i , suiSize %i", suiCurrPos, suiSize);
alSourceQueueBuffers(source, BUFF_COUNT, buffers);
if (alGetError() != AL_NO_ERROR) {
LOGI("Error alSourceQueueBuffers :(\n");
return;
}
}
void OALOgg:: loadWithoutStreaming(){
buf = 0;
buf = (unsigned char*) malloc(f->size());
if (buf) {
if (f->read(buf, f->size(), 1)) {
} else {
free(buf);
f->close();
return;
}
}
//LOGI("f.size() = %i",f.size());
//PreConvert(f.size(),buf);
unsigned char * data = (unsigned char *) ConvertOggToPCM(f->size(), buf);
f->close();
//free(buf);
if (vi->channels == 1)
format = AL_FORMAT_MONO16;
else
format = AL_FORMAT_STEREO16;
//alGenBuffers(1, &buffer);
alGenBuffers(1, &buffers[0]);
//LOGI("uiPCMSamples = %i", uiPCMSamples);
//Log();
//LOGI("sizeof(short) = %i",sizeof(short));
//LOGI("Size = %u",uiPCMSamples * vi->channels * sizeof(short));
//alBufferData(buffer, format, data, uiPCMSamples * vi->channels * sizeof(short), vi->rate);
alBufferData(buffers[0], format, data, uiPCMSamples * vi->channels * sizeof(short), vi->rate);
source = 0;
alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffers[0]);
//alSourcei(source, AL_BUFFER, buffer);
}
void OALOgg::load(AAssetManager *mgr, const char* filename) {
//this->filename = filename;
//unsigned char* buf = 0;
f = new AAssetFile(mgr, filename);
if (f->null()) {
LOGE("no file %s in readOgg", filename);
return;
}
if(!streaming)
loadWithoutStreaming();
else
loadWithStreaming();
/*buf = 0;
buf = (char*)malloc(f.size());
if (buf){
if(f.read(buf,f.size(),1)){
}
else {
free(buf);
f.close();
return;
}
}
//LOGI("f.size() = %i",f.size());
//PreConvert(f.size(),buf);
char * data = (char *)ConvertOggToPCM(f.size(),buf);
f.close();
if (vi->channels == 1)
format = AL_FORMAT_MONO16;
else
format = AL_FORMAT_STEREO16;
alGenBuffers(1,&buffer);
//LOGI("uiPCMSamples = %i", uiPCMSamples);
//Log();
//LOGI("sizeof(short) = %i",sizeof(short));
//LOGI("Size = %u",uiPCMSamples * vi->channels * sizeof(short));
alBufferData(buffer,format,data,uiPCMSamples * vi->channels * sizeof(short),vi->rate);
source = 0;
alGenSources(1, &source);
alSourcei(source, AL_BUFFER, buffer);*/
}
void OALOgg::play() {
if (!streaming) {
alSourcePlay(source);
do {
} while (isPlaying());
} else {
alSourcePlay(source);
while (true) {
//while(curPos<ov_pcm_total(mVF, -1) * 4){
ALint val;
ALuint buffer;
alGetSourcei(source, AL_BUFFERS_PROCESSED, &val);
if (val <= 0)
continue;
while (val--) {
unsigned char * data = this->readBlock(BUFFER_SIZE);// readRiffs(size);
alSourceUnqueueBuffers(source, 1, &buffer);
alBufferData(buffer, format, data, BUFFER_SIZE, vi->rate);
alSourceQueueBuffers(source, 1, &buffer);
if (alGetError() != AL_NO_ERROR) {
LOGI("Error buffering :(\n");
}
}
}
//}
}
}