/
music_opus.c
569 lines (497 loc) · 15.9 KB
1
2
/*
SDL_mixer: An audio mixer library based on the SDL library
3
Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
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
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.
*/
#ifdef MUSIC_OPUS
/* This file supports Ogg Opus music streams */
#include "SDL_loadso.h"
#include "music_opus.h"
30
31
32
#if defined(OPUS_HEADER)
#include OPUS_HEADER
#else
33
#include <opus/opusfile.h>
34
#endif
35
36
37
38
typedef struct {
int loaded;
void *handle;
39
const OpusTags *(*op_tags)(const OggOpusFile *,int);
40
41
42
43
44
45
OggOpusFile *(*op_open_callbacks)(void *,const OpusFileCallbacks *,const unsigned char *,size_t,int *);
void (*op_free)(OggOpusFile *);
const OpusHead *(*op_head)(const OggOpusFile *,int);
int (*op_seekable)(const OggOpusFile *);
int (*op_read)(OggOpusFile *, opus_int16 *,int,int *);
int (*op_pcm_seek)(OggOpusFile *,ogg_int64_t);
46
47
ogg_int64_t (*op_pcm_tell)(const OggOpusFile *);
ogg_int64_t (*op_pcm_total)(const OggOpusFile *, int);
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
} opus_loader;
static opus_loader opus = {
0, NULL
};
#ifdef OPUS_DYNAMIC
#define FUNCTION_LOADER(FUNC, SIG) \
opus.FUNC = (SIG) SDL_LoadFunction(opus.handle, #FUNC); \
if (opus.FUNC == NULL) { SDL_UnloadObject(opus.handle); return -1; }
#else
#define FUNCTION_LOADER(FUNC, SIG) \
opus.FUNC = FUNC;
#endif
static int OPUS_Load(void)
{
if (opus.loaded == 0) {
#ifdef OPUS_DYNAMIC
opus.handle = SDL_LoadObject(OPUS_DYNAMIC);
if (opus.handle == NULL) {
return -1;
}
#elif defined(__MACOSX__)
extern OggOpusFile *op_open_callbacks(void *,const OpusFileCallbacks *,const unsigned char *,size_t,int *) __attribute__((weak_import));
if (op_open_callbacks == NULL) {
/* Missing weakly linked framework */
75
Mix_SetError("Missing OpusFile.framework");
76
77
78
79
return -1;
}
#endif
FUNCTION_LOADER(op_open_callbacks, OggOpusFile *(*)(void *,const OpusFileCallbacks *,const unsigned char *,size_t,int *))
80
FUNCTION_LOADER(op_tags, const OpusTags *(*)(const OggOpusFile *,int))
81
82
83
84
85
FUNCTION_LOADER(op_free, void (*)(OggOpusFile *))
FUNCTION_LOADER(op_head, const OpusHead *(*)(const OggOpusFile *,int))
FUNCTION_LOADER(op_seekable, int (*)(const OggOpusFile *))
FUNCTION_LOADER(op_read, int (*)(OggOpusFile *, opus_int16 *,int,int *))
FUNCTION_LOADER(op_pcm_seek, int (*)(OggOpusFile *,ogg_int64_t))
86
87
FUNCTION_LOADER(op_pcm_tell, ogg_int64_t (*)(const OggOpusFile *))
FUNCTION_LOADER(op_pcm_total, ogg_int64_t (*)(const OggOpusFile *, int))
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
}
++opus.loaded;
return 0;
}
static void OPUS_Unload(void)
{
if (opus.loaded == 0) {
return;
}
if (opus.loaded == 1) {
#ifdef OPUS_DYNAMIC
SDL_UnloadObject(opus.handle);
#endif
}
--opus.loaded;
}
typedef struct {
SDL_RWops *src;
int freesrc;
int play_count;
int volume;
OggOpusFile *of;
const OpusHead *op_info;
int section;
SDL_AudioStream *stream;
char *buffer;
int buffer_size;
119
120
121
122
int loop;
ogg_int64_t loop_start;
ogg_int64_t loop_end;
ogg_int64_t loop_len;
123
ogg_int64_t full_length;
124
Mix_MusicMetaTags tags;
125
126
127
128
129
} OPUS_music;
static int set_op_error(const char *function, int error)
{
130
#define HANDLE_ERROR_CASE(X) case X: Mix_SetError("%s: %s", function, #X); break;
131
switch (error) {
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
HANDLE_ERROR_CASE(OP_FALSE)
HANDLE_ERROR_CASE(OP_EOF)
HANDLE_ERROR_CASE(OP_HOLE)
HANDLE_ERROR_CASE(OP_EREAD)
HANDLE_ERROR_CASE(OP_EFAULT)
HANDLE_ERROR_CASE(OP_EIMPL)
HANDLE_ERROR_CASE(OP_EINVAL)
HANDLE_ERROR_CASE(OP_ENOTFORMAT)
HANDLE_ERROR_CASE(OP_EBADHEADER)
HANDLE_ERROR_CASE(OP_EVERSION)
HANDLE_ERROR_CASE(OP_ENOTAUDIO)
HANDLE_ERROR_CASE(OP_EBADPACKET)
HANDLE_ERROR_CASE(OP_EBADLINK)
HANDLE_ERROR_CASE(OP_ENOSEEK)
HANDLE_ERROR_CASE(OP_EBADTIMESTAMP)
147
148
149
150
151
152
153
154
155
default:
Mix_SetError("%s: unknown error %d\n", function, error);
break;
}
return -1;
}
static int sdl_read_func(void *datasource, unsigned char *ptr, int size)
{
156
return (int)SDL_RWread((SDL_RWops*)datasource, ptr, 1, (size_t)size);
157
158
}
159
static int sdl_seek_func(void *datasource, opus_int64 offset, int whence)
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
{
return (SDL_RWseek((SDL_RWops*)datasource, offset, whence) < 0)? -1 : 0;
}
static opus_int64 sdl_tell_func(void *datasource)
{
return SDL_RWtell((SDL_RWops*)datasource);
}
static int OPUS_Seek(void*, double);
static void OPUS_Delete(void*);
static int OPUS_UpdateSection(OPUS_music *music)
{
const OpusHead *op_info;
op_info = opus.op_head(music->of, -1);
if (!op_info) {
Mix_SetError("op_head returned NULL");
return -1;
}
if (music->op_info && op_info->channel_count == music->op_info->channel_count) {
return 0;
}
music->op_info = op_info;
if (music->buffer) {
SDL_free(music->buffer);
music->buffer = NULL;
}
if (music->stream) {
SDL_FreeAudioStream(music->stream);
music->stream = NULL;
}
197
music->stream = SDL_NewAudioStream(AUDIO_S16, (Uint8)op_info->channel_count, 48000,
198
199
200
201
202
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
return -1;
}
203
204
music->buffer_size = (int)music_spec.samples * (int)sizeof(opus_int16) * op_info->channel_count;
music->buffer = (char *)SDL_malloc((size_t)music->buffer_size);
205
206
207
208
209
210
if (!music->buffer) {
return -1;
}
return 0;
}
211
212
213
214
215
216
217
/* Parse time string of the form HH:MM:SS.mmm and return equivalent sample
* position */
static ogg_int64_t parse_time(char *time)
{
const ogg_int64_t samplerate_hz = 48000;
char *num_start, *p;
ogg_int64_t result = 0;
218
char c; int val;
219
220
221
/* Time is directly expressed as a sample position */
if (SDL_strchr(time, ':') == NULL) {
222
return SDL_strtoll(time, NULL, 10);
223
224
225
226
227
228
229
230
}
result = 0;
num_start = time;
for (p = time; *p != '\0'; ++p) {
if (*p == '.' || *p == ':') {
c = *p; *p = '\0';
231
232
233
if ((val = SDL_atoi(num_start)) < 0)
return -1;
result = result * 60 + val;
234
235
236
237
238
num_start = p + 1;
*p = c;
}
if (*p == '.') {
239
240
241
double val_f = SDL_atof(p);
if (val_f < 0) return -1;
return result * samplerate_hz + (ogg_int64_t) (val_f * samplerate_hz);
242
243
244
}
}
245
246
if ((val = SDL_atoi(num_start)) < 0) return -1;
return (result * 60 + val) * samplerate_hz;
247
248
249
250
251
252
253
254
255
}
static SDL_bool is_loop_tag(const char *tag)
{
char buf[5];
SDL_strlcpy(buf, tag, 5);
return SDL_strcasecmp(buf, "LOOP") == 0;
}
256
257
258
259
260
/* Load an Opus stream from an SDL_RWops object */
static void *OPUS_CreateFromRW(SDL_RWops *src, int freesrc)
{
OPUS_music *music;
OpusFileCallbacks callbacks;
261
262
263
264
const OpusTags* tags = NULL;
int err = 0, ci;
SDL_bool is_loop_length = SDL_FALSE;
ogg_int64_t full_length;
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
music = (OPUS_music *)SDL_calloc(1, sizeof *music);
if (!music) {
SDL_OutOfMemory();
return NULL;
}
music->src = src;
music->volume = MIX_MAX_VOLUME;
music->section = -1;
SDL_zero(callbacks);
callbacks.read = sdl_read_func;
callbacks.seek = sdl_seek_func;
callbacks.tell = sdl_tell_func;
music->of = opus.op_open_callbacks(src, &callbacks, NULL, 0, &err);
if (music->of == NULL) {
/* set_op_error("op_open_callbacks", err);*/
SDL_SetError("Not an Opus audio stream");
SDL_free(music);
return NULL;
}
if (!opus.op_seekable(music->of)) {
OPUS_Delete(music);
Mix_SetError("Opus stream not seekable");
return NULL;
}
if (OPUS_UpdateSection(music) < 0) {
OPUS_Delete(music);
return NULL;
}
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
tags = opus.op_tags(music->of, -1);
for (ci=0; ci < tags->comments; ci++) {
char *param = SDL_strdup(tags->user_comments[ci]);
char *argument = param;
char *value = SDL_strchr(param, '=');
if (value == NULL) {
value = param + SDL_strlen(param);
} else {
*(value++) = '\0';
}
/* Want to match LOOP-START, LOOP_START, etc. Remove - or _ from
* string if it is present at position 4. */
if (is_loop_tag(argument) && ((argument[4] == '_') || (argument[4] == '-'))) {
SDL_memmove(argument + 4, argument + 5, SDL_strlen(argument) - 4);
}
if (SDL_strcasecmp(argument, "LOOPSTART") == 0)
music->loop_start = parse_time(value);
else if (SDL_strcasecmp(argument, "LOOPLENGTH") == 0) {
319
music->loop_len = SDL_strtoll(value, NULL, 10);
320
321
322
323
is_loop_length = SDL_TRUE;
} else if (SDL_strcasecmp(argument, "LOOPEND") == 0) {
music->loop_end = parse_time(value);
is_loop_length = SDL_FALSE;
324
325
326
327
328
329
330
331
} else if (SDL_strcasecmp(argument, "TITLE") == 0) {
meta_tags_set(&music->tags, MIX_META_TITLE, value);
} else if (SDL_strcasecmp(argument, "ARTIST") == 0) {
meta_tags_set(&music->tags, MIX_META_ARTIST, value);
} else if (SDL_strcasecmp(argument, "ALBUM") == 0) {
meta_tags_set(&music->tags, MIX_META_ALBUM, value);
} else if (SDL_strcasecmp(argument, "COPYRIGHT") == 0) {
meta_tags_set(&music->tags, MIX_META_COPYRIGHT, value);
332
}
333
334
335
336
337
338
339
340
341
SDL_free(param);
}
if (is_loop_length) {
music->loop_end = music->loop_start + music->loop_len;
} else {
music->loop_len = music->loop_end - music->loop_start;
}
342
343
344
345
346
347
348
/* Ignore invalid loop tag */
if (music->loop_start < 0 || music->loop_len < 0 || music->loop_end < 0) {
music->loop_start = 0;
music->loop_len = 0;
music->loop_end = 0;
}
349
full_length = opus.op_pcm_total(music->of, -1);
350
351
if ((music->loop_end > 0) && (music->loop_end <= full_length) &&
(music->loop_start < music->loop_end)) {
352
353
354
music->loop = 1;
}
355
music->full_length = full_length;
356
357
358
359
music->freesrc = freesrc;
return music;
}
360
361
362
363
364
365
static const char* OPUS_GetMetaTag(void *context, Mix_MusicMetaTag tag_type)
{
OPUS_music *music = (OPUS_music *)context;
return meta_tags_get(&music->tags, tag_type);
}
366
367
368
369
370
371
372
/* Set the volume for an Opus stream */
static void OPUS_SetVolume(void *context, int volume)
{
OPUS_music *music = (OPUS_music *)context;
music->volume = volume;
}
373
374
375
376
377
378
379
/* Get the volume for an Opus stream */
static int OPUS_GetVolume(void *context)
{
OPUS_music *music = (OPUS_music *)context;
return music->volume;
}
380
381
382
383
384
385
386
387
388
389
390
391
392
/* Start playback of a given Opus stream */
static int OPUS_Play(void *context, int play_count)
{
OPUS_music *music = (OPUS_music *)context;
music->play_count = play_count;
return OPUS_Seek(music, 0.0);
}
/* Play some of a stream previously started with OPUS_Play() */
static int OPUS_GetSome(void *context, void *data, int bytes, SDL_bool *done)
{
OPUS_music *music = (OPUS_music *)context;
int filled, samples, section;
393
394
395
int result;
SDL_bool looped = SDL_FALSE;
ogg_int64_t pcmPos;
396
397
398
399
400
401
402
403
404
405
406
407
408
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;
409
samples = opus.op_read(music->of, (opus_int16 *)music->buffer, music->buffer_size / (int)sizeof(opus_int16), §ion);
410
411
412
413
414
415
416
417
418
419
420
421
if (samples < 0) {
set_op_error("op_read", samples);
return -1;
}
if (section != music->section) {
music->section = section;
if (OPUS_UpdateSection(music) < 0) {
return -1;
}
}
422
pcmPos = opus.op_pcm_tell(music->of);
423
if (music->loop && (music->play_count != 1) && (pcmPos >= music->loop_end)) {
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
samples -= (int)((pcmPos - music->loop_end) * music->op_info->channel_count) * (int)sizeof(Sint16);
result = opus.op_pcm_seek(music->of, music->loop_start);
if (result < 0) {
set_op_error("ov_pcm_seek", result);
return -1;
} else {
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
music->play_count = play_count;
}
looped = SDL_TRUE;
}
439
440
441
442
443
if (samples > 0) {
filled = samples * music->op_info->channel_count * 2;
if (SDL_AudioStreamPut(music->stream, music->buffer, filled) < 0) {
return -1;
}
444
} else if (!looped) {
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
if (music->play_count == 1) {
music->play_count = 0;
SDL_AudioStreamFlush(music->stream);
} else {
int play_count = -1;
if (music->play_count > 0) {
play_count = (music->play_count - 1);
}
if (OPUS_Play(music, play_count) < 0) {
return -1;
}
}
}
return 0;
}
static int OPUS_GetAudio(void *context, void *data, int bytes)
{
OPUS_music *music = (OPUS_music *)context;
return music_pcm_getaudio(context, data, bytes, music->volume, OPUS_GetSome);
}
/* Jump (seek) to a given position (time is in seconds) */
static int OPUS_Seek(void *context, double time)
{
OPUS_music *music = (OPUS_music *)context;
471
int result = opus.op_pcm_seek(music->of, (ogg_int64_t)(time * 48000));
472
473
474
475
476
477
if (result < 0) {
return set_op_error("op_pcm_seek", result);
}
return 0;
}
478
479
480
481
482
483
static double OPUS_Tell(void *context)
{
OPUS_music *music = (OPUS_music *)context;
return (double)(opus.op_pcm_tell(music->of)) / 48000.0;
}
484
485
486
487
488
489
490
/* Return music duration in seconds */
static double OPUS_Duration(void *context)
{
OPUS_music *music = (OPUS_music *)context;
return music->full_length / 48000.0;
}
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
static double OPUS_LoopStart(void *music_p)
{
OPUS_music *music = (OPUS_music *)music_p;
if (music->loop > 0) {
return (double)music->loop_start / 48000.0;
}
return -1.0;
}
static double OPUS_LoopEnd(void *music_p)
{
OPUS_music *music = (OPUS_music *)music_p;
if (music->loop > 0) {
return (double)music->loop_end / 48000.0;
}
return -1.0;
}
static double OPUS_LoopLength(void *music_p)
{
OPUS_music *music = (OPUS_music *)music_p;
if (music->loop > 0) {
return (double)music->loop_len / 48000.0;
}
return -1.0;
}
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/* Close the given Opus stream */
static void OPUS_Delete(void *context)
{
OPUS_music *music = (OPUS_music *)context;
opus.op_free(music->of);
if (music->stream) {
SDL_FreeAudioStream(music->stream);
}
if (music->buffer) {
SDL_free(music->buffer);
}
if (music->freesrc) {
SDL_RWclose(music->src);
}
SDL_free(music);
}
Mix_MusicInterface Mix_MusicInterface_Opus =
{
"OPUS",
MIX_MUSIC_OPUS,
MUS_OPUS,
SDL_FALSE,
SDL_FALSE,
OPUS_Load,
NULL, /* Open */
OPUS_CreateFromRW,
NULL, /* CreateFromFile */
OPUS_SetVolume,
548
OPUS_GetVolume,
549
550
551
552
OPUS_Play,
NULL, /* IsPlaying */
OPUS_GetAudio,
OPUS_Seek,
553
OPUS_Tell,
554
OPUS_Duration,
555
556
557
OPUS_LoopStart,
OPUS_LoopEnd,
OPUS_LoopLength,
558
OPUS_GetMetaTag,
559
560
561
562
563
NULL, /* Pause */
NULL, /* Resume */
NULL, /* Stop */
OPUS_Delete,
NULL, /* Close */
564
OPUS_Unload
565
566
567
568
569
};
#endif /* MUSIC_OPUS */
/* vi: set ts=4 sw=4 expandtab: */