/
music_ogg.c
525 lines (461 loc) · 14.8 KB
1
/*
2
SDL_mixer: An audio mixer library based on the SDL library
3
Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
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
*/
22
#ifdef MUSIC_OGG
23
24
25
/* This file supports Ogg Vorbis music streams */
26
#include "SDL_loadso.h"
27
28
29
#include "music_ogg.h"
30
#define OV_EXCLUDE_STATIC_CALLBACKS
31
32
33
34
35
36
37
38
#if defined(OGG_HEADER)
#include OGG_HEADER
#elif defined(OGG_USE_TREMOR)
#include <tremor/ivorbisfile.h>
#else
#include <vorbis/vorbisfile.h>
#endif
39
40
41
42
43
44
typedef struct {
int loaded;
void *handle;
int (*ov_clear)(OggVorbis_File *vf);
vorbis_info *(*ov_info)(OggVorbis_File *vf,int link);
45
vorbis_comment *(*ov_comment)(OggVorbis_File *vf,int link);
46
47
48
49
50
51
52
53
54
55
56
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);
57
58
59
60
61
#endif
#ifdef OGG_USE_TREMOR
ogg_int64_t (*ov_time_total)(OggVorbis_File *vf, int i);
#else
double (*ov_time_total)(OggVorbis_File *vf, int i);
62
#endif
63
64
int (*ov_pcm_seek)(OggVorbis_File *vf, ogg_int64_t pos);
ogg_int64_t (*ov_pcm_tell)(OggVorbis_File *vf);
65
66
67
68
69
70
71
} vorbis_loader;
static vorbis_loader vorbis = {
0, NULL
};
#ifdef OGG_DYNAMIC
72
73
74
75
76
77
78
#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
79
80
static int OGG_Load(void)
81
{
82
if (vorbis.loaded == 0) {
83
#ifdef OGG_DYNAMIC
84
85
86
87
vorbis.handle = SDL_LoadObject(OGG_DYNAMIC);
if (vorbis.handle == NULL) {
return -1;
}
88
89
90
91
92
93
#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");
94
95
return -1;
}
96
97
98
99
#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))
100
FUNCTION_LOADER(ov_open_callbacks, int (*)(void *,OggVorbis_File *,const char *,long,ov_callbacks))
101
FUNCTION_LOADER(ov_pcm_total, ogg_int64_t (*)(OggVorbis_File *,int))
102
#ifdef OGG_USE_TREMOR
103
FUNCTION_LOADER(ov_read, long (*)(OggVorbis_File *,char *,int,int *))
104
FUNCTION_LOADER(ov_time_seek, int (*)(OggVorbis_File *,ogg_int64_t))
105
FUNCTION_LOADER(ov_time_total, ogg_int64_t (*)(OggVorbis_File *, int))
106
#else
107
108
FUNCTION_LOADER(ov_read, long (*)(OggVorbis_File *,char *,int,int,int,int,int *))
FUNCTION_LOADER(ov_time_seek, int (*)(OggVorbis_File *,double))
109
FUNCTION_LOADER(ov_time_total, double (*)(OggVorbis_File *, int))
110
#endif
111
112
FUNCTION_LOADER(ov_pcm_seek, int (*)(OggVorbis_File *,ogg_int64_t))
FUNCTION_LOADER(ov_pcm_tell, ogg_int64_t (*)(OggVorbis_File *))
113
114
115
116
}
++vorbis.loaded;
return 0;
117
118
}
119
static void OGG_Unload(void)
120
{
121
122
123
124
if (vorbis.loaded == 0) {
return;
}
if (vorbis.loaded == 1) {
125
#ifdef OGG_DYNAMIC
126
SDL_UnloadObject(vorbis.handle);
127
#endif
128
129
}
--vorbis.loaded;
130
131
}
132
133
134
135
typedef struct {
SDL_RWops *src;
int freesrc;
136
int play_count;
137
138
int volume;
OggVorbis_File vf;
139
vorbis_info vi;
140
int section;
141
142
143
SDL_AudioStream *stream;
char *buffer;
int buffer_size;
144
145
146
147
int loop;
ogg_int64_t loop_start;
ogg_int64_t loop_end;
ogg_int64_t loop_len;
148
149
} OGG_music;
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
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;
}
176
177
178
179
180
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);
}
181
static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
182
{
183
return (int)SDL_RWseek((SDL_RWops*)datasource, offset, whence);
184
185
}
186
static long sdl_tell_func(void *datasource)
187
{
188
return (long)SDL_RWtell((SDL_RWops*)datasource);
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
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;
}
219
music->stream = SDL_NewAudioStream(AUDIO_S16, (Uint8)vi->channels, (int)vi->rate,
220
221
222
223
224
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
return -1;
}
225
226
music->buffer_size = music_spec.samples * (int)sizeof(Sint16) * vi->channels;
music->buffer = (char *)SDL_malloc((size_t)music->buffer_size);
227
228
229
230
231
232
if (!music->buffer) {
return -1;
}
return 0;
}
233
234
/* Parse time string of the form HH:MM:SS.mmm and return equivalent sample
* position */
235
static ogg_int64_t parse_time(char *time, long samplerate_hz)
236
237
238
239
240
{
char *num_start, *p;
ogg_int64_t result = 0;
char c;
241
242
/* Time is directly expressed as a sample position */
if (SDL_strchr(time, ':') == NULL) {
243
return (ogg_int64_t)SDL_strtoull(time, NULL, 10);
244
245
246
247
248
}
result = 0;
num_start = time;
249
250
for (p = time; *p != '\0'; ++p) {
if (*p == '.' || *p == ':') {
251
252
253
254
255
256
c = *p; *p = '\0';
result = result * 60 + SDL_atoi(num_start);
num_start = p + 1;
*p = c;
}
257
if (*p == '.') {
258
return result * samplerate_hz
259
+ (ogg_int64_t) (SDL_atof(p) * samplerate_hz);
260
261
262
263
264
265
}
}
return (result * 60 + SDL_atoi(num_start)) * samplerate_hz;
}
266
/* Load an OGG stream from an SDL_RWops object */
267
static void *OGG_CreateFromRW(SDL_RWops *src, int freesrc)
268
{
269
270
OGG_music *music;
ov_callbacks callbacks;
271
vorbis_comment *vc;
272
long rate;
273
274
ogg_int64_t full_length;
SDL_bool is_loop_length = SDL_FALSE;
275
int i;
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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);
291
292
293
294
callbacks.read_func = sdl_read_func;
callbacks.seek_func = sdl_seek_func;
callbacks.tell_func = sdl_tell_func;
295
296
297
298
299
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;
}
300
301
302
303
304
if (OGG_UpdateSection(music) < 0) {
OGG_Delete(music);
return NULL;
}
305
306
vc = vorbis.ov_comment(&music->vf, -1);
307
rate = music->vi.rate;
308
309
310
311
312
313
314
315
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';
316
317
}
318
319
320
321
322
323
324
/* Want to match LOOP-START, LOOP_START, etc. Remove - or _ from
* string if it is present at position 4. */
if ((argument[4] == '_') || (argument[4] == '-')) {
SDL_memmove(argument + 4, argument + 5, SDL_strlen(argument) - 4);
}
325
if (SDL_strcasecmp(argument, "LOOPSTART") == 0)
326
music->loop_start = parse_time(value, rate);
327
else if (SDL_strcasecmp(argument, "LOOPLENGTH") == 0) {
328
music->loop_len = (ogg_int64_t)SDL_strtoull(value, NULL, 10);
329
is_loop_length = SDL_TRUE;
330
} else if (SDL_strcasecmp(argument, "LOOPEND") == 0) {
331
music->loop_end = parse_time(value, rate);
332
is_loop_length = SDL_FALSE;
333
}
334
335
SDL_free(param);
}
336
337
if (is_loop_length) {
338
music->loop_end = music->loop_start + music->loop_len;
339
} else {
340
341
342
music->loop_len = music->loop_end - music->loop_start;
}
343
full_length = vorbis.ov_pcm_total(&music->vf, -1);
344
345
if (((music->loop_start >= 0) || (music->loop_end > 0)) &&
((music->loop_start < music->loop_end) || (music->loop_end == 0)) &&
346
347
(music->loop_start < full_length) &&
(music->loop_end <= full_length)) {
348
if (music->loop_start < 0) music->loop_start = 0;
349
if (music->loop_end == 0) music->loop_end = full_length;
350
music->loop = 1;
351
}
352
353
music->freesrc = freesrc;
354
355
356
357
358
359
360
361
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;
362
363
}
364
/* Start playback of a given OGG stream */
365
static int OGG_Play(void *context, int play_count)
366
{
367
OGG_music *music = (OGG_music *)context;
368
369
music->play_count = play_count;
return OGG_Seek(music, 0.0);
370
371
}
372
373
/* Play some of a stream previously started with OGG_play() */
static int OGG_GetSome(void *context, void *data, int bytes, SDL_bool *done)
374
{
375
OGG_music *music = (OGG_music *)context;
376
377
SDL_bool looped = SDL_FALSE;
int filled, amount, result;
378
int section;
379
ogg_int64_t pcmPos;
380
381
382
383
384
385
386
387
388
389
390
391
392
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;
393
#ifdef OGG_USE_TREMOR
394
amount = vorbis.ov_read(&music->vf, music->buffer, music->buffer_size, §ion);
396
amount = (int)vorbis.ov_read(&music->vf, music->buffer, music->buffer_size, 0, 2, 1, §ion);
398
399
400
if (amount < 0) {
set_ov_error("ov_read", amount);
return -1;
401
402
}
403
404
405
406
if (section != music->section) {
music->section = section;
if (OGG_UpdateSection(music) < 0) {
return -1;
407
408
409
}
}
410
pcmPos = vorbis.ov_pcm_tell(&music->vf);
411
if ((music->loop == 1) && (music->play_count != 1) && (pcmPos >= music->loop_end)) {
412
amount -= (int)((pcmPos - music->loop_end) * music->vi.channels) * (int)sizeof(Sint16);
413
414
415
416
result = vorbis.ov_pcm_seek(&music->vf, music->loop_start);
if (result < 0) {
set_ov_error("ov_pcm_seek", result);
return -1;
417
418
419
420
421
422
} else {
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
music->play_count = play_count;
423
}
424
looped = SDL_TRUE;
425
}
426
427
428
429
430
431
432
433
434
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);
435
} else {
436
437
438
439
440
441
442
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
if (OGG_Play(music, play_count) < 0) {
return -1;
}
443
444
}
}
445
return 0;
446
}
447
static int OGG_GetAudio(void *context, void *data, int bytes)
448
{
449
OGG_music *music = (OGG_music *)context;
450
return music_pcm_getaudio(context, data, bytes, music->volume, OGG_GetSome);
451
452
}
453
454
455
456
/* Jump (seek) to a given position (time is in seconds) */
static int OGG_Seek(void *context, double time)
{
OGG_music *music = (OGG_music *)context;
457
int result;
458
#ifdef OGG_USE_TREMOR
459
result = vorbis.ov_time_seek(&music->vf, (ogg_int64_t)(time * 1000.0));
460
#else
461
result = vorbis.ov_time_seek(&music->vf, time);
462
#endif
463
464
465
if (result < 0) {
return set_ov_error("ov_time_seek", result);
}
466
467
468
return 0;
}
469
470
471
472
473
474
475
476
477
478
479
/* Return music duration in seconds */
static double OGG_Duration(void *context)
{
OGG_music *music = (OGG_music *)context;
#ifdef OGG_USE_TREMOR
return vorbis.ov_time_total(&music->vf, -1) / 1000.0;
#else
return vorbis.ov_time_total(&music->vf, -1);
#endif
}
480
/* Close the given OGG stream */
481
static void OGG_Delete(void *context)
482
{
483
OGG_music *music = (OGG_music *)context;
484
485
486
487
488
489
490
491
492
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);
493
}
494
SDL_free(music);
495
496
}
497
Mix_MusicInterface Mix_MusicInterface_OGG =
498
{
499
500
501
502
503
504
505
506
507
508
509
510
"OGG",
MIX_MUSIC_OGG,
MUS_OGG,
SDL_FALSE,
SDL_FALSE,
OGG_Load,
NULL, /* Open */
OGG_CreateFromRW,
NULL, /* CreateFromFile */
OGG_SetVolume,
OGG_Play,
511
NULL, /* IsPlaying */
512
513
OGG_GetAudio,
OGG_Seek,
514
OGG_Duration,
515
516
NULL, /* Pause */
NULL, /* Resume */
517
NULL, /* Stop */
518
519
OGG_Delete,
NULL, /* Close */
520
OGG_Unload
521
522
523
};
#endif /* MUSIC_OGG */
524
525
/* vi: set ts=4 sw=4 expandtab: */