Skip to content

Latest commit

 

History

History
510 lines (445 loc) · 14.3 KB

music_opus.c

File metadata and controls

510 lines (445 loc) · 14.3 KB
 
1
2
/*
SDL_mixer: An audio mixer library based on the SDL library
Jan 5, 2019
Jan 5, 2019
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
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"
Oct 26, 2018
Oct 26, 2018
30
31
32
#if defined(OPUS_HEADER)
#include OPUS_HEADER
#else
Oct 26, 2018
Oct 26, 2018
33
#include <opus/opusfile.h>
Oct 26, 2018
Oct 26, 2018
34
#endif
35
36
37
38
typedef struct {
int loaded;
void *handle;
Nov 26, 2019
Nov 26, 2019
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);
Nov 26, 2019
Nov 26, 2019
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 */
Oct 26, 2018
Oct 26, 2018
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 *))
Nov 26, 2019
Nov 26, 2019
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))
Nov 26, 2019
Nov 26, 2019
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;
Nov 26, 2019
Nov 26, 2019
119
120
121
122
int loop;
ogg_int64_t loop_start;
ogg_int64_t loop_end;
ogg_int64_t loop_len;
Dec 17, 2019
Dec 17, 2019
123
ogg_int64_t full_length;
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
} OPUS_music;
static int set_op_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(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);
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)
{
Nov 26, 2019
Nov 26, 2019
155
return (int)SDL_RWread((SDL_RWops*)datasource, ptr, 1, (size_t)size);
156
157
}
Nov 17, 2019
Nov 17, 2019
158
static int sdl_seek_func(void *datasource, opus_int64 offset, int whence)
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
{
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;
}
Nov 26, 2019
Nov 26, 2019
196
music->stream = SDL_NewAudioStream(AUDIO_S16, (Uint8)op_info->channel_count, 48000,
197
198
199
200
201
music_spec.format, music_spec.channels, music_spec.freq);
if (!music->stream) {
return -1;
}
Nov 26, 2019
Nov 26, 2019
202
203
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);
204
205
206
207
208
209
if (!music->buffer) {
return -1;
}
return 0;
}
Nov 26, 2019
Nov 26, 2019
210
211
212
213
214
215
216
/* 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;
Dec 19, 2019
Dec 19, 2019
217
char c; int val;
Nov 26, 2019
Nov 26, 2019
218
219
220
/* Time is directly expressed as a sample position */
if (SDL_strchr(time, ':') == NULL) {
Dec 19, 2019
Dec 19, 2019
221
return SDL_strtoll(time, NULL, 10);
Nov 26, 2019
Nov 26, 2019
222
223
224
225
226
227
228
229
}
result = 0;
num_start = time;
for (p = time; *p != '\0'; ++p) {
if (*p == '.' || *p == ':') {
c = *p; *p = '\0';
Dec 19, 2019
Dec 19, 2019
230
231
232
if ((val = SDL_atoi(num_start)) < 0)
return -1;
result = result * 60 + val;
Nov 26, 2019
Nov 26, 2019
233
234
235
236
237
num_start = p + 1;
*p = c;
}
if (*p == '.') {
Dec 19, 2019
Dec 19, 2019
238
239
240
double val_f = SDL_atof(p);
if (val_f < 0) return -1;
return result * samplerate_hz + (ogg_int64_t) (val_f * samplerate_hz);
Nov 26, 2019
Nov 26, 2019
241
242
243
}
}
Dec 19, 2019
Dec 19, 2019
244
245
if ((val = SDL_atoi(num_start)) < 0) return -1;
return (result * 60 + val) * samplerate_hz;
Nov 26, 2019
Nov 26, 2019
246
247
248
249
250
251
252
253
254
}
static SDL_bool is_loop_tag(const char *tag)
{
char buf[5];
SDL_strlcpy(buf, tag, 5);
return SDL_strcasecmp(buf, "LOOP") == 0;
}
255
256
257
258
259
/* Load an Opus stream from an SDL_RWops object */
static void *OPUS_CreateFromRW(SDL_RWops *src, int freesrc)
{
OPUS_music *music;
OpusFileCallbacks callbacks;
Nov 26, 2019
Nov 26, 2019
260
261
262
263
const OpusTags* tags = NULL;
int err = 0, ci;
SDL_bool is_loop_length = SDL_FALSE;
ogg_int64_t full_length;
264
265
266
267
268
269
270
271
272
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;
Nov 26, 2019
Nov 26, 2019
273
music->loop = -1;
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
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;
}
Nov 26, 2019
Nov 26, 2019
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) {
Dec 19, 2019
Dec 19, 2019
319
music->loop_len = SDL_strtoll(value, NULL, 10);
Nov 26, 2019
Nov 26, 2019
320
321
322
323
324
is_loop_length = SDL_TRUE;
} else if (SDL_strcasecmp(argument, "LOOPEND") == 0) {
music->loop_end = parse_time(value);
is_loop_length = SDL_FALSE;
}
Dec 19, 2019
Dec 19, 2019
325
326
327
328
329
330
331
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;
SDL_free(param);
break; /* ignore tag. */
}
Nov 26, 2019
Nov 26, 2019
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;
}
full_length = opus.op_pcm_total(music->of, -1);
Dec 19, 2019
Dec 19, 2019
342
343
if ((music->loop_end > 0) && (music->loop_end <= full_length) &&
(music->loop_start < music->loop_end)) {
Nov 26, 2019
Nov 26, 2019
344
345
346
music->loop = 1;
}
Dec 17, 2019
Dec 17, 2019
347
music->full_length = full_length;
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
music->freesrc = freesrc;
return music;
}
/* Set the volume for an Opus stream */
static void OPUS_SetVolume(void *context, int volume)
{
OPUS_music *music = (OPUS_music *)context;
music->volume = volume;
}
/* 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;
Nov 26, 2019
Nov 26, 2019
372
373
374
int result;
SDL_bool looped = SDL_FALSE;
ogg_int64_t pcmPos;
375
376
377
378
379
380
381
382
383
384
385
386
387
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;
Nov 26, 2019
Nov 26, 2019
388
samples = opus.op_read(music->of, (opus_int16 *)music->buffer, music->buffer_size / (int)sizeof(opus_int16), &section);
389
390
391
392
393
394
395
396
397
398
399
400
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;
}
}
Nov 26, 2019
Nov 26, 2019
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
pcmPos = opus.op_pcm_tell(music->of);
if ((music->loop == 1) && (music->play_count != 1) && (pcmPos >= music->loop_end)) {
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;
}
418
419
420
421
422
if (samples > 0) {
filled = samples * music->op_info->channel_count * 2;
if (SDL_AudioStreamPut(music->stream, music->buffer, filled) < 0) {
return -1;
}
Nov 26, 2019
Nov 26, 2019
423
} else if (!looped) {
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
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;
int result;
result = opus.op_pcm_seek(music->of, (ogg_int64_t)(time * 48000));
if (result < 0) {
return set_op_error("op_pcm_seek", result);
}
return 0;
}
Dec 17, 2019
Dec 17, 2019
458
459
460
461
462
463
464
/* Return music duration in seconds */
static double OPUS_Duration(void *context)
{
OPUS_music *music = (OPUS_music *)context;
return music->full_length / 48000.0;
}
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/* 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,
OPUS_Play,
NULL, /* IsPlaying */
OPUS_GetAudio,
OPUS_Seek,
Dec 17, 2019
Dec 17, 2019
499
OPUS_Duration,
500
501
502
503
504
NULL, /* Pause */
NULL, /* Resume */
NULL, /* Stop */
OPUS_Delete,
NULL, /* Close */
Nov 26, 2019
Nov 26, 2019
505
OPUS_Unload
506
507
508
509
510
};
#endif /* MUSIC_OPUS */
/* vi: set ts=4 sw=4 expandtab: */