Skip to content

Latest commit

 

History

History
456 lines (402 loc) · 12.8 KB

music_ogg.c

File metadata and controls

456 lines (402 loc) · 12.8 KB
 
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Mar 1, 2018
Mar 1, 2018
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
20
21
*/
Oct 17, 2017
Oct 17, 2017
22
#ifdef MUSIC_OGG
23
24
25
/* This file supports Ogg Vorbis music streams */
Oct 17, 2017
Oct 17, 2017
26
#include "SDL_loadso.h"
27
28
29
#include "music_ogg.h"
Oct 17, 2017
Oct 17, 2017
30
31
32
33
34
35
36
37
38
39
40
41
42
#if defined(OGG_HEADER)
#include OGG_HEADER
#elif defined(OGG_USE_TREMOR)
#include <tremor/ivorbisfile.h>
#else
#include <vorbis/vorbisfile.h>
#endif
typedef struct {
int loaded;
void *handle;
int (*ov_clear)(OggVorbis_File *vf);
vorbis_info *(*ov_info)(OggVorbis_File *vf,int link);
Oct 18, 2017
Oct 18, 2017
43
vorbis_comment *(*ov_comment)(OggVorbis_File *vf,int link);
Oct 17, 2017
Oct 17, 2017
44
45
46
47
48
49
50
51
52
53
54
55
int (*ov_open_callbacks)(void *datasource, OggVorbis_File *vf, const char *initial, long ibytes, ov_callbacks callbacks);
ogg_int64_t (*ov_pcm_total)(OggVorbis_File *vf,int i);
#ifdef OGG_USE_TREMOR
long (*ov_read)(OggVorbis_File *vf,char *buffer,int length, int *bitstream);
#else
long (*ov_read)(OggVorbis_File *vf,char *buffer,int length, int bigendianp,int word,int sgned,int *bitstream);
#endif
#ifdef OGG_USE_TREMOR
int (*ov_time_seek)(OggVorbis_File *vf,ogg_int64_t pos);
#else
int (*ov_time_seek)(OggVorbis_File *vf,double pos);
#endif
Oct 18, 2017
Oct 18, 2017
56
57
int (*ov_pcm_seek)(OggVorbis_File *vf, ogg_int64_t pos);
ogg_int64_t (*ov_pcm_tell)(OggVorbis_File *vf);
Oct 17, 2017
Oct 17, 2017
58
59
60
61
62
63
64
} vorbis_loader;
static vorbis_loader vorbis = {
0, NULL
};
#ifdef OGG_DYNAMIC
Oct 21, 2017
Oct 21, 2017
65
66
67
68
69
70
71
#define FUNCTION_LOADER(FUNC, SIG) \
vorbis.FUNC = (SIG) SDL_LoadFunction(vorbis.handle, #FUNC); \
if (vorbis.FUNC == NULL) { SDL_UnloadObject(vorbis.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
vorbis.FUNC = FUNC;
#endif
Oct 17, 2017
Oct 17, 2017
73
static int OGG_Load(void)
Oct 17, 2017
Oct 17, 2017
75
if (vorbis.loaded == 0) {
Oct 21, 2017
Oct 21, 2017
76
#ifdef OGG_DYNAMIC
Oct 17, 2017
Oct 17, 2017
77
78
79
80
vorbis.handle = SDL_LoadObject(OGG_DYNAMIC);
if (vorbis.handle == NULL) {
return -1;
}
Oct 21, 2017
Oct 21, 2017
81
82
83
84
85
86
#elif defined(__MACOSX__)
extern int ov_open_callbacks(void*, OggVorbis_File*, const char*, long, ov_callbacks) __attribute__((weak_import));
if (ov_open_callbacks == NULL)
{
/* Missing weakly linked framework */
Mix_SetError("Missing Vorbis.framework");
Oct 17, 2017
Oct 17, 2017
87
88
return -1;
}
Oct 21, 2017
Oct 21, 2017
89
90
91
92
93
94
#endif
FUNCTION_LOADER(ov_clear, int (*)(OggVorbis_File *))
FUNCTION_LOADER(ov_info, vorbis_info *(*)(OggVorbis_File *,int))
FUNCTION_LOADER(ov_comment, vorbis_comment *(*)(OggVorbis_File *,int))
FUNCTION_LOADER(ov_open_callbacks, int (*)(void *, OggVorbis_File *, const char *, long, ov_callbacks))
FUNCTION_LOADER(ov_pcm_total, ogg_int64_t (*)(OggVorbis_File *,int))
Oct 17, 2017
Oct 17, 2017
95
#ifdef OGG_USE_TREMOR
Oct 21, 2017
Oct 21, 2017
96
97
FUNCTION_LOADER(ov_read, long (*)(OggVorbis_File *,char *,int,int *))
FUNCTION_LOADER(ov_time_seek, long (*)(OggVorbis_File *,ogg_int64_t))
Oct 17, 2017
Oct 17, 2017
98
#else
Oct 21, 2017
Oct 21, 2017
99
100
FUNCTION_LOADER(ov_read, long (*)(OggVorbis_File *,char *,int,int,int,int,int *))
FUNCTION_LOADER(ov_time_seek, int (*)(OggVorbis_File *,double))
Oct 17, 2017
Oct 17, 2017
101
#endif
Oct 21, 2017
Oct 21, 2017
102
103
FUNCTION_LOADER(ov_pcm_seek, int (*)(OggVorbis_File *,ogg_int64_t))
FUNCTION_LOADER(ov_pcm_tell, ogg_int64_t (*)(OggVorbis_File *))
Oct 17, 2017
Oct 17, 2017
104
105
106
107
}
++vorbis.loaded;
return 0;
108
109
}
Oct 17, 2017
Oct 17, 2017
110
static void OGG_Unload(void)
Oct 17, 2017
Oct 17, 2017
112
113
114
115
if (vorbis.loaded == 0) {
return;
}
if (vorbis.loaded == 1) {
Oct 21, 2017
Oct 21, 2017
116
#ifdef OGG_DYNAMIC
Oct 17, 2017
Oct 17, 2017
117
SDL_UnloadObject(vorbis.handle);
Oct 21, 2017
Oct 21, 2017
118
#endif
Oct 17, 2017
Oct 17, 2017
119
120
}
--vorbis.loaded;
121
122
}
Oct 17, 2017
Oct 17, 2017
123
124
125
126
typedef struct {
SDL_RWops *src;
int freesrc;
Oct 21, 2017
Oct 21, 2017
127
int play_count;
Oct 17, 2017
Oct 17, 2017
128
129
int volume;
OggVorbis_File vf;
Oct 21, 2017
Oct 21, 2017
130
vorbis_info vi;
Oct 17, 2017
Oct 17, 2017
131
int section;
Oct 21, 2017
Oct 21, 2017
132
133
134
SDL_AudioStream *stream;
char *buffer;
int buffer_size;
Oct 18, 2017
Oct 18, 2017
135
136
137
138
139
int loop;
ogg_int64_t loop_start;
ogg_int64_t loop_end;
ogg_int64_t loop_len;
ogg_int64_t channels;
Oct 17, 2017
Oct 17, 2017
140
141
} OGG_music;
Oct 21, 2017
Oct 21, 2017
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
static int set_ov_error(const char *function, int error)
{
#define HANDLE_ERROR_CASE(X) case X: Mix_SetError("%s: %s", function, #X); break;
switch (error) {
HANDLE_ERROR_CASE(OV_FALSE);
HANDLE_ERROR_CASE(OV_EOF);
HANDLE_ERROR_CASE(OV_HOLE);
HANDLE_ERROR_CASE(OV_EREAD);
HANDLE_ERROR_CASE(OV_EFAULT);
HANDLE_ERROR_CASE(OV_EIMPL);
HANDLE_ERROR_CASE(OV_EINVAL);
HANDLE_ERROR_CASE(OV_ENOTVORBIS);
HANDLE_ERROR_CASE(OV_EBADHEADER);
HANDLE_ERROR_CASE(OV_EVERSION);
HANDLE_ERROR_CASE(OV_ENOTAUDIO);
HANDLE_ERROR_CASE(OV_EBADPACKET);
HANDLE_ERROR_CASE(OV_EBADLINK);
HANDLE_ERROR_CASE(OV_ENOSEEK);
default:
Mix_SetError("%s: unknown error %d\n", function, error);
break;
}
return -1;
}
Aug 22, 2004
Aug 22, 2004
168
169
170
171
172
static size_t sdl_read_func(void *ptr, size_t size, size_t nmemb, void *datasource)
{
return SDL_RWread((SDL_RWops*)datasource, ptr, size, nmemb);
}
Jul 7, 2014
Jul 7, 2014
173
static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
Aug 22, 2004
Aug 22, 2004
174
{
Jul 7, 2014
Jul 7, 2014
175
return (int)SDL_RWseek((SDL_RWops*)datasource, offset, whence);
Aug 22, 2004
Aug 22, 2004
176
177
}
Jul 7, 2014
Jul 7, 2014
178
static long sdl_tell_func(void *datasource)
Aug 22, 2004
Aug 22, 2004
179
{
Jul 7, 2014
Jul 7, 2014
180
return (long)SDL_RWtell((SDL_RWops*)datasource);
Aug 22, 2004
Aug 22, 2004
181
182
}
Oct 21, 2017
Oct 21, 2017
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
static int OGG_Seek(void *context, double time);
static void OGG_Delete(void *context);
static int OGG_UpdateSection(OGG_music *music)
{
vorbis_info *vi;
vi = vorbis.ov_info(&music->vf, -1);
if (!vi) {
Mix_SetError("ov_info returned NULL");
return -1;
}
if (vi->channels == music->vi.channels && vi->rate == music->vi.rate) {
return 0;
}
SDL_memcpy(&music->vi, vi, sizeof(*vi));
if (music->buffer) {
SDL_free(music->buffer);
music->buffer = NULL;
}
if (music->stream) {
SDL_FreeAudioStream(music->stream);
music->stream = NULL;
}
music->stream = SDL_NewAudioStream(AUDIO_S16, vi->channels, (int)vi->rate,
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
return -1;
}
music->buffer_size = music_spec.samples * sizeof(Sint16) * vi->channels;
music->buffer = (char *)SDL_malloc(music->buffer_size);
if (!music->buffer) {
return -1;
}
return 0;
}
Aug 22, 2004
Aug 22, 2004
225
/* Load an OGG stream from an SDL_RWops object */
Oct 17, 2017
Oct 17, 2017
226
static void *OGG_CreateFromRW(SDL_RWops *src, int freesrc)
Aug 22, 2004
Aug 22, 2004
227
{
May 22, 2013
May 22, 2013
228
229
OGG_music *music;
ov_callbacks callbacks;
Oct 21, 2017
Oct 21, 2017
230
231
232
vorbis_comment *vc;
int isLoopLength = 0, i;
ogg_int64_t fullLength;
May 22, 2013
May 22, 2013
233
Oct 21, 2017
Oct 21, 2017
234
235
236
237
238
239
240
241
242
243
244
245
246
247
music = (OGG_music *)SDL_calloc(1, sizeof *music);
if (!music) {
SDL_OutOfMemory();
return NULL;
}
music->src = src;
music->volume = MIX_MAX_VOLUME;
music->section = -1;
music->loop = -1;
music->loop_start = -1;
music->loop_end = 0;
music->loop_len = 0;
SDL_zero(callbacks);
May 22, 2013
May 22, 2013
248
249
250
251
callbacks.read_func = sdl_read_func;
callbacks.seek_func = sdl_seek_func;
callbacks.tell_func = sdl_tell_func;
Oct 21, 2017
Oct 21, 2017
252
253
254
255
256
if (vorbis.ov_open_callbacks(src, &music->vf, NULL, 0, callbacks) < 0) {
SDL_SetError("Not an Ogg Vorbis audio stream");
SDL_free(music);
return NULL;
}
Oct 18, 2017
Oct 18, 2017
257
Oct 21, 2017
Oct 21, 2017
258
259
260
261
if (OGG_UpdateSection(music) < 0) {
OGG_Delete(music);
return NULL;
}
Oct 18, 2017
Oct 18, 2017
262
Oct 21, 2017
Oct 21, 2017
263
264
265
266
267
268
269
270
271
vc = vorbis.ov_comment(&music->vf, -1);
for (i = 0; i < vc->comments; i++) {
char *param = SDL_strdup(vc->user_comments[i]);
char *argument = param;
char *value = SDL_strchr(param, '=');
if (value == NULL) {
value = param + SDL_strlen(param);
} else {
*(value++) = '\0';
Oct 18, 2017
Oct 18, 2017
272
273
}
Oct 21, 2017
Oct 21, 2017
274
275
276
277
278
279
280
281
if (SDL_strcasecmp(argument, "LOOPSTART") == 0)
music->loop_start = SDL_strtoull(value, NULL, 0);
else if (SDL_strcasecmp(argument, "LOOPLENGTH") == 0) {
music->loop_len = SDL_strtoull(value, NULL, 0);
isLoopLength = 1;
} else if (SDL_strcasecmp(argument, "LOOPEND") == 0) {
isLoopLength = 0;
music->loop_end = SDL_strtoull(value, NULL, 0);
Oct 18, 2017
Oct 18, 2017
282
}
Oct 21, 2017
Oct 21, 2017
283
284
SDL_free(param);
}
Oct 18, 2017
Oct 18, 2017
285
Oct 21, 2017
Oct 21, 2017
286
287
if (isLoopLength == 1) {
music->loop_end = music->loop_start + music->loop_len;
May 22, 2013
May 22, 2013
288
} else {
Oct 21, 2017
Oct 21, 2017
289
290
291
292
293
294
295
296
297
298
299
music->loop_len = music->loop_end - music->loop_start;
}
fullLength = vorbis.ov_pcm_total(&music->vf, -1);
if (((music->loop_start >= 0) || (music->loop_end > 0)) &&
((music->loop_start < music->loop_end) || (music->loop_end == 0)) &&
(music->loop_start < fullLength) &&
(music->loop_end <= fullLength)) {
if (music->loop_start < 0) music->loop_start = 0;
if (music->loop_end == 0) music->loop_end = fullLength;
music->loop = 1;
May 22, 2013
May 22, 2013
300
}
Oct 21, 2017
Oct 21, 2017
301
302
music->freesrc = freesrc;
Oct 17, 2017
Oct 17, 2017
303
304
305
306
307
308
309
310
return music;
}
/* Set the volume for an OGG stream */
static void OGG_SetVolume(void *context, int volume)
{
OGG_music *music = (OGG_music *)context;
music->volume = volume;
Aug 22, 2004
Aug 22, 2004
311
312
}
313
/* Start playback of a given OGG stream */
Oct 21, 2017
Oct 21, 2017
314
static int OGG_Play(void *context, int play_count)
Oct 17, 2017
Oct 17, 2017
316
OGG_music *music = (OGG_music *)context;
Oct 21, 2017
Oct 21, 2017
317
318
music->play_count = play_count;
return OGG_Seek(music, 0.0);
319
320
}
Oct 21, 2017
Oct 21, 2017
321
322
/* Play some of a stream previously started with OGG_play() */
static int OGG_GetSome(void *context, void *data, int bytes, SDL_bool *done)
Oct 17, 2017
Oct 17, 2017
324
OGG_music *music = (OGG_music *)context;
Oct 21, 2017
Oct 21, 2017
325
326
SDL_bool looped = SDL_FALSE;
int filled, amount, result;
May 22, 2013
May 22, 2013
327
int section;
Oct 18, 2017
Oct 18, 2017
328
ogg_int64_t pcmPos;
Oct 21, 2017
Oct 21, 2017
330
331
332
333
334
335
336
337
338
339
340
341
filled = SDL_AudioStreamGet(music->stream, data, bytes);
if (filled != 0) {
return filled;
}
if (!music->play_count) {
/* All done */
*done = SDL_TRUE;
return 0;
}
section = music->section;
Jul 15, 2007
Jul 15, 2007
342
#ifdef OGG_USE_TREMOR
Oct 21, 2017
Oct 21, 2017
343
amount = vorbis.ov_read(&music->vf, music->buffer, music->buffer_size, &section);
Jul 15, 2007
Jul 15, 2007
344
#else
Oct 21, 2017
Oct 21, 2017
345
amount = (int)vorbis.ov_read(&music->vf, music->buffer, music->buffer_size, 0, 2, 1, &section);
Jul 15, 2007
Jul 15, 2007
346
#endif
Oct 21, 2017
Oct 21, 2017
347
348
349
if (amount < 0) {
set_ov_error("ov_read", amount);
return -1;
Oct 18, 2017
Oct 18, 2017
350
351
}
Oct 21, 2017
Oct 21, 2017
352
353
354
355
if (section != music->section) {
music->section = section;
if (OGG_UpdateSection(music) < 0) {
return -1;
May 22, 2013
May 22, 2013
356
357
358
}
}
Oct 21, 2017
Oct 21, 2017
359
360
pcmPos = vorbis.ov_pcm_tell(&music->vf);
if ((music->loop == 1) && (pcmPos >= music->loop_end)) {
Oct 21, 2017
Oct 21, 2017
361
amount -= (int)((pcmPos - music->loop_end) * music->channels) * sizeof(Sint16);
Oct 21, 2017
Oct 21, 2017
362
363
364
365
result = vorbis.ov_pcm_seek(&music->vf, music->loop_start);
if (result < 0) {
set_ov_error("ov_pcm_seek", result);
return -1;
May 22, 2013
May 22, 2013
366
}
Oct 21, 2017
Oct 21, 2017
367
looped = SDL_TRUE;
May 22, 2013
May 22, 2013
368
}
Oct 21, 2017
Oct 21, 2017
369
370
371
372
373
374
375
376
377
if (amount > 0) {
if (SDL_AudioStreamPut(music->stream, music->buffer, amount) < 0) {
return -1;
}
} else if (!looped) {
if (music->play_count == 1) {
music->play_count = 0;
SDL_AudioStreamFlush(music->stream);
May 22, 2013
May 22, 2013
378
} else {
Oct 21, 2017
Oct 21, 2017
379
380
381
382
383
384
385
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
if (OGG_Play(music, play_count) < 0) {
return -1;
}
May 22, 2013
May 22, 2013
386
387
}
}
Oct 21, 2017
Oct 21, 2017
388
return 0;
Oct 17, 2017
Oct 17, 2017
390
static int OGG_GetAudio(void *context, void *data, int bytes)
Oct 17, 2017
Oct 17, 2017
392
OGG_music *music = (OGG_music *)context;
Oct 21, 2017
Oct 21, 2017
393
return music_pcm_getaudio(context, data, bytes, music->volume, OGG_GetSome);
394
395
}
Oct 17, 2017
Oct 17, 2017
396
397
398
399
/* Jump (seek) to a given position (time is in seconds) */
static int OGG_Seek(void *context, double time)
{
OGG_music *music = (OGG_music *)context;
Oct 21, 2017
Oct 21, 2017
400
int result;
Oct 17, 2017
Oct 17, 2017
401
#ifdef OGG_USE_TREMOR
Oct 21, 2017
Oct 21, 2017
402
result = vorbis.ov_time_seek(&music->vf, (ogg_int64_t)(time * 1000.0));
Oct 17, 2017
Oct 17, 2017
403
#else
Oct 21, 2017
Oct 21, 2017
404
result = vorbis.ov_time_seek(&music->vf, time);
Oct 17, 2017
Oct 17, 2017
405
#endif
Oct 21, 2017
Oct 21, 2017
406
407
408
if (result < 0) {
return set_ov_error("ov_time_seek", result);
}
Oct 17, 2017
Oct 17, 2017
409
410
411
return 0;
}
412
/* Close the given OGG stream */
Oct 17, 2017
Oct 17, 2017
413
static void OGG_Delete(void *context)
Oct 17, 2017
Oct 17, 2017
415
OGG_music *music = (OGG_music *)context;
Oct 21, 2017
Oct 21, 2017
416
417
418
419
420
421
422
423
424
vorbis.ov_clear(&music->vf);
if (music->stream) {
SDL_FreeAudioStream(music->stream);
}
if (music->buffer) {
SDL_free(music->buffer);
}
if (music->freesrc) {
SDL_RWclose(music->src);
May 22, 2013
May 22, 2013
425
}
Oct 21, 2017
Oct 21, 2017
426
SDL_free(music);
427
428
}
Oct 17, 2017
Oct 17, 2017
429
Mix_MusicInterface Mix_MusicInterface_OGG =
Dec 20, 2001
Dec 20, 2001
430
{
Oct 17, 2017
Oct 17, 2017
431
432
433
434
435
436
437
438
439
440
441
442
"OGG",
MIX_MUSIC_OGG,
MUS_OGG,
SDL_FALSE,
SDL_FALSE,
OGG_Load,
NULL, /* Open */
OGG_CreateFromRW,
NULL, /* CreateFromFile */
OGG_SetVolume,
OGG_Play,
Oct 21, 2017
Oct 21, 2017
443
NULL, /* IsPlaying */
Oct 17, 2017
Oct 17, 2017
444
445
446
447
OGG_GetAudio,
OGG_Seek,
NULL, /* Pause */
NULL, /* Resume */
Oct 21, 2017
Oct 21, 2017
448
NULL, /* Stop */
Oct 17, 2017
Oct 17, 2017
449
450
451
452
453
454
OGG_Delete,
NULL, /* Close */
OGG_Unload,
};
#endif /* MUSIC_OGG */
Dec 20, 2001
Dec 20, 2001
455
Oct 17, 2017
Oct 17, 2017
456
/* vi: set ts=4 sw=4 expandtab: */