music_opus.c
changeset 855 a7dee77dd60f
child 899 f97f817b94bb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/music_opus.c	Fri Jun 15 08:32:56 2018 +0300
     1.3 @@ -0,0 +1,369 @@
     1.4 +/*
     1.5 +  SDL_mixer:  An audio mixer library based on the SDL library
     1.6 +  Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
     1.7 +
     1.8 +  This software is provided 'as-is', without any express or implied
     1.9 +  warranty.  In no event will the authors be held liable for any damages
    1.10 +  arising from the use of this software.
    1.11 +
    1.12 +  Permission is granted to anyone to use this software for any purpose,
    1.13 +  including commercial applications, and to alter it and redistribute it
    1.14 +  freely, subject to the following restrictions:
    1.15 +
    1.16 +  1. The origin of this software must not be misrepresented; you must not
    1.17 +     claim that you wrote the original software. If you use this software
    1.18 +     in a product, an acknowledgment in the product documentation would be
    1.19 +     appreciated but is not required.
    1.20 +  2. Altered source versions must be plainly marked as such, and must not be
    1.21 +     misrepresented as being the original software.
    1.22 +  3. This notice may not be removed or altered from any source distribution.
    1.23 +*/
    1.24 +
    1.25 +#ifdef MUSIC_OPUS
    1.26 +
    1.27 +/* This file supports Ogg Opus music streams */
    1.28 +
    1.29 +#include "SDL_loadso.h"
    1.30 +
    1.31 +#include "music_opus.h"
    1.32 +
    1.33 +#include <opusfile.h>
    1.34 +
    1.35 +typedef struct {
    1.36 +    int loaded;
    1.37 +    void *handle;
    1.38 +    OggOpusFile *(*op_open_callbacks)(void *,const OpusFileCallbacks *,const unsigned char *,size_t,int *);
    1.39 +    void (*op_free)(OggOpusFile *);
    1.40 +    const OpusHead *(*op_head)(const OggOpusFile *,int);
    1.41 +    int (*op_seekable)(const OggOpusFile *);
    1.42 +    int (*op_read)(OggOpusFile *, opus_int16 *,int,int *);
    1.43 +    int (*op_pcm_seek)(OggOpusFile *,ogg_int64_t);
    1.44 +} opus_loader;
    1.45 +
    1.46 +static opus_loader opus = {
    1.47 +    0, NULL
    1.48 +};
    1.49 +
    1.50 +#ifdef OPUS_DYNAMIC
    1.51 +#define FUNCTION_LOADER(FUNC, SIG) \
    1.52 +    opus.FUNC = (SIG) SDL_LoadFunction(opus.handle, #FUNC); \
    1.53 +    if (opus.FUNC == NULL) { SDL_UnloadObject(opus.handle); return -1; }
    1.54 +#else
    1.55 +#define FUNCTION_LOADER(FUNC, SIG) \
    1.56 +    opus.FUNC = FUNC;
    1.57 +#endif
    1.58 +
    1.59 +static int OPUS_Load(void)
    1.60 +{
    1.61 +    if (opus.loaded == 0) {
    1.62 +#ifdef OPUS_DYNAMIC
    1.63 +        opus.handle = SDL_LoadObject(OPUS_DYNAMIC);
    1.64 +        if (opus.handle == NULL) {
    1.65 +            return -1;
    1.66 +        }
    1.67 +#elif defined(__MACOSX__)
    1.68 +        extern OggOpusFile *op_open_callbacks(void *,const OpusFileCallbacks *,const unsigned char *,size_t,int *) __attribute__((weak_import));
    1.69 +        if (op_open_callbacks == NULL) {
    1.70 +            /* Missing weakly linked framework */
    1.71 +            Mix_SetError("Missing Opus.framework");
    1.72 +            return -1;
    1.73 +        }
    1.74 +#endif
    1.75 +        FUNCTION_LOADER(op_open_callbacks, OggOpusFile *(*)(void *,const OpusFileCallbacks *,const unsigned char *,size_t,int *))
    1.76 +        FUNCTION_LOADER(op_free, void (*)(OggOpusFile *))
    1.77 +        FUNCTION_LOADER(op_head, const OpusHead *(*)(const OggOpusFile *,int))
    1.78 +        FUNCTION_LOADER(op_seekable, int (*)(const OggOpusFile *))
    1.79 +        FUNCTION_LOADER(op_read, int (*)(OggOpusFile *, opus_int16 *,int,int *))
    1.80 +        FUNCTION_LOADER(op_pcm_seek, int (*)(OggOpusFile *,ogg_int64_t))
    1.81 +    }
    1.82 +    ++opus.loaded;
    1.83 +
    1.84 +    return 0;
    1.85 +}
    1.86 +
    1.87 +static void OPUS_Unload(void)
    1.88 +{
    1.89 +    if (opus.loaded == 0) {
    1.90 +        return;
    1.91 +    }
    1.92 +    if (opus.loaded == 1) {
    1.93 +#ifdef OPUS_DYNAMIC
    1.94 +        SDL_UnloadObject(opus.handle);
    1.95 +#endif
    1.96 +    }
    1.97 +    --opus.loaded;
    1.98 +}
    1.99 +
   1.100 +
   1.101 +typedef struct {
   1.102 +    SDL_RWops *src;
   1.103 +    int freesrc;
   1.104 +    int play_count;
   1.105 +    int volume;
   1.106 +    OggOpusFile *of;
   1.107 +    const OpusHead *op_info;
   1.108 +    int section;
   1.109 +    SDL_AudioStream *stream;
   1.110 +    char *buffer;
   1.111 +    int buffer_size;
   1.112 +} OPUS_music;
   1.113 +
   1.114 +
   1.115 +static int set_op_error(const char *function, int error)
   1.116 +{
   1.117 +#define HANDLE_ERROR_CASE(X)    case X: Mix_SetError("%s: %s", function, #X); break;
   1.118 +    switch (error) {
   1.119 +    HANDLE_ERROR_CASE(OP_FALSE);
   1.120 +    HANDLE_ERROR_CASE(OP_EOF);
   1.121 +    HANDLE_ERROR_CASE(OP_HOLE);
   1.122 +    HANDLE_ERROR_CASE(OP_EREAD);
   1.123 +    HANDLE_ERROR_CASE(OP_EFAULT);
   1.124 +    HANDLE_ERROR_CASE(OP_EIMPL);
   1.125 +    HANDLE_ERROR_CASE(OP_EINVAL);
   1.126 +    HANDLE_ERROR_CASE(OP_ENOTFORMAT);
   1.127 +    HANDLE_ERROR_CASE(OP_EBADHEADER);
   1.128 +    HANDLE_ERROR_CASE(OP_EVERSION);
   1.129 +    HANDLE_ERROR_CASE(OP_ENOTAUDIO);
   1.130 +    HANDLE_ERROR_CASE(OP_EBADPACKET);
   1.131 +    HANDLE_ERROR_CASE(OP_EBADLINK);
   1.132 +    HANDLE_ERROR_CASE(OP_ENOSEEK);
   1.133 +    HANDLE_ERROR_CASE(OP_EBADTIMESTAMP);
   1.134 +    default:
   1.135 +        Mix_SetError("%s: unknown error %d\n", function, error);
   1.136 +        break;
   1.137 +    }
   1.138 +    return -1;
   1.139 +}
   1.140 +
   1.141 +static int sdl_read_func(void *datasource, unsigned char *ptr, int size)
   1.142 +{
   1.143 +    return (int)SDL_RWread((SDL_RWops*)datasource, ptr, 1, size);
   1.144 +}
   1.145 +
   1.146 +static int sdl_seek_func(void *datasource, ogg_int64_t offset, int whence)
   1.147 +{
   1.148 +    return (SDL_RWseek((SDL_RWops*)datasource, offset, whence) < 0)? -1 : 0;
   1.149 +}
   1.150 +
   1.151 +static opus_int64 sdl_tell_func(void *datasource)
   1.152 +{
   1.153 +    return SDL_RWtell((SDL_RWops*)datasource);
   1.154 +}
   1.155 +
   1.156 +static int OPUS_Seek(void*, double);
   1.157 +static void OPUS_Delete(void*);
   1.158 +
   1.159 +static int OPUS_UpdateSection(OPUS_music *music)
   1.160 +{
   1.161 +    const OpusHead *op_info;
   1.162 +
   1.163 +    op_info = opus.op_head(music->of, -1);
   1.164 +    if (!op_info) {
   1.165 +        Mix_SetError("op_head returned NULL");
   1.166 +        return -1;
   1.167 +    }
   1.168 +
   1.169 +    if (music->op_info && op_info->channel_count == music->op_info->channel_count) {
   1.170 +        return 0;
   1.171 +    }
   1.172 +    music->op_info = op_info;
   1.173 +
   1.174 +    if (music->buffer) {
   1.175 +        SDL_free(music->buffer);
   1.176 +        music->buffer = NULL;
   1.177 +    }
   1.178 +
   1.179 +    if (music->stream) {
   1.180 +        SDL_FreeAudioStream(music->stream);
   1.181 +        music->stream = NULL;
   1.182 +    }
   1.183 +
   1.184 +    music->stream = SDL_NewAudioStream(AUDIO_S16, op_info->channel_count, 48000,
   1.185 +                                       music_spec.format, music_spec.channels, music_spec.freq);
   1.186 +    if (!music->stream) {
   1.187 +        return -1;
   1.188 +    }
   1.189 +
   1.190 +    music->buffer_size = music_spec.samples * sizeof(opus_int16) * op_info->channel_count;
   1.191 +    music->buffer = (char *)SDL_malloc(music->buffer_size);
   1.192 +    if (!music->buffer) {
   1.193 +        return -1;
   1.194 +    }
   1.195 +    return 0;
   1.196 +}
   1.197 +
   1.198 +/* Load an Opus stream from an SDL_RWops object */
   1.199 +static void *OPUS_CreateFromRW(SDL_RWops *src, int freesrc)
   1.200 +{
   1.201 +    OPUS_music *music;
   1.202 +    OpusFileCallbacks callbacks;
   1.203 +    int err = 0;
   1.204 +
   1.205 +    music = (OPUS_music *)SDL_calloc(1, sizeof *music);
   1.206 +    if (!music) {
   1.207 +        SDL_OutOfMemory();
   1.208 +        return NULL;
   1.209 +    }
   1.210 +    music->src = src;
   1.211 +    music->volume = MIX_MAX_VOLUME;
   1.212 +    music->section = -1;
   1.213 +
   1.214 +    SDL_zero(callbacks);
   1.215 +    callbacks.read = sdl_read_func;
   1.216 +    callbacks.seek = sdl_seek_func;
   1.217 +    callbacks.tell = sdl_tell_func;
   1.218 +
   1.219 +    music->of = opus.op_open_callbacks(src, &callbacks, NULL, 0, &err);
   1.220 +    if (music->of == NULL) {
   1.221 +    /*  set_op_error("op_open_callbacks", err);*/
   1.222 +        SDL_SetError("Not an Opus audio stream");
   1.223 +        SDL_free(music);
   1.224 +        return NULL;
   1.225 +    }
   1.226 +
   1.227 +    if (!opus.op_seekable(music->of)) {
   1.228 +        OPUS_Delete(music);
   1.229 +        Mix_SetError("Opus stream not seekable");
   1.230 +        return NULL;
   1.231 +    }
   1.232 +
   1.233 +    if (OPUS_UpdateSection(music) < 0) {
   1.234 +        OPUS_Delete(music);
   1.235 +        return NULL;
   1.236 +    }
   1.237 +
   1.238 +    music->freesrc = freesrc;
   1.239 +    return music;
   1.240 +}
   1.241 +
   1.242 +/* Set the volume for an Opus stream */
   1.243 +static void OPUS_SetVolume(void *context, int volume)
   1.244 +{
   1.245 +    OPUS_music *music = (OPUS_music *)context;
   1.246 +    music->volume = volume;
   1.247 +}
   1.248 +
   1.249 +/* Start playback of a given Opus stream */
   1.250 +static int OPUS_Play(void *context, int play_count)
   1.251 +{
   1.252 +    OPUS_music *music = (OPUS_music *)context;
   1.253 +    music->play_count = play_count;
   1.254 +    return OPUS_Seek(music, 0.0);
   1.255 +}
   1.256 +
   1.257 +/* Play some of a stream previously started with OPUS_Play() */
   1.258 +static int OPUS_GetSome(void *context, void *data, int bytes, SDL_bool *done)
   1.259 +{
   1.260 +    OPUS_music *music = (OPUS_music *)context;
   1.261 +    int filled, samples, section;
   1.262 +
   1.263 +    filled = SDL_AudioStreamGet(music->stream, data, bytes);
   1.264 +    if (filled != 0) {
   1.265 +        return filled;
   1.266 +    }
   1.267 +
   1.268 +    if (!music->play_count) {
   1.269 +        /* All done */
   1.270 +        *done = SDL_TRUE;
   1.271 +        return 0;
   1.272 +    }
   1.273 +
   1.274 +    section = music->section;
   1.275 +    samples = opus.op_read(music->of, (opus_int16 *)music->buffer, music->buffer_size / sizeof(opus_int16), &section);
   1.276 +    if (samples < 0) {
   1.277 +        set_op_error("op_read", samples);
   1.278 +        return -1;
   1.279 +    }
   1.280 +
   1.281 +    if (section != music->section) {
   1.282 +        music->section = section;
   1.283 +        if (OPUS_UpdateSection(music) < 0) {
   1.284 +            return -1;
   1.285 +        }
   1.286 +    }
   1.287 +
   1.288 +    if (samples > 0) {
   1.289 +        filled = samples * music->op_info->channel_count * 2;
   1.290 +        if (SDL_AudioStreamPut(music->stream, music->buffer, filled) < 0) {
   1.291 +            return -1;
   1.292 +        }
   1.293 +    } else {
   1.294 +        if (music->play_count == 1) {
   1.295 +            music->play_count = 0;
   1.296 +            SDL_AudioStreamFlush(music->stream);
   1.297 +        } else {
   1.298 +            int play_count = -1;
   1.299 +            if (music->play_count > 0) {
   1.300 +                play_count = (music->play_count - 1);
   1.301 +            }
   1.302 +            if (OPUS_Play(music, play_count) < 0) {
   1.303 +                return -1;
   1.304 +            }
   1.305 +        }
   1.306 +    }
   1.307 +    return 0;
   1.308 +}
   1.309 +
   1.310 +static int OPUS_GetAudio(void *context, void *data, int bytes)
   1.311 +{
   1.312 +    OPUS_music *music = (OPUS_music *)context;
   1.313 +    return music_pcm_getaudio(context, data, bytes, music->volume, OPUS_GetSome);
   1.314 +}
   1.315 +
   1.316 +/* Jump (seek) to a given position (time is in seconds) */
   1.317 +static int OPUS_Seek(void *context, double time)
   1.318 +{
   1.319 +    OPUS_music *music = (OPUS_music *)context;
   1.320 +    int result;
   1.321 +    result = opus.op_pcm_seek(music->of, (ogg_int64_t)(time * 48000));
   1.322 +    if (result < 0) {
   1.323 +        return set_op_error("op_pcm_seek", result);
   1.324 +    }
   1.325 +    return 0;
   1.326 +}
   1.327 +
   1.328 +/* Close the given Opus stream */
   1.329 +static void OPUS_Delete(void *context)
   1.330 +{
   1.331 +    OPUS_music *music = (OPUS_music *)context;
   1.332 +    opus.op_free(music->of);
   1.333 +    if (music->stream) {
   1.334 +        SDL_FreeAudioStream(music->stream);
   1.335 +    }
   1.336 +    if (music->buffer) {
   1.337 +        SDL_free(music->buffer);
   1.338 +    }
   1.339 +    if (music->freesrc) {
   1.340 +        SDL_RWclose(music->src);
   1.341 +    }
   1.342 +    SDL_free(music);
   1.343 +}
   1.344 +
   1.345 +Mix_MusicInterface Mix_MusicInterface_Opus =
   1.346 +{
   1.347 +    "OPUS",
   1.348 +    MIX_MUSIC_OPUS,
   1.349 +    MUS_OPUS,
   1.350 +    SDL_FALSE,
   1.351 +    SDL_FALSE,
   1.352 +
   1.353 +    OPUS_Load,
   1.354 +    NULL,   /* Open */
   1.355 +    OPUS_CreateFromRW,
   1.356 +    NULL,   /* CreateFromFile */
   1.357 +    OPUS_SetVolume,
   1.358 +    OPUS_Play,
   1.359 +    NULL,   /* IsPlaying */
   1.360 +    OPUS_GetAudio,
   1.361 +    OPUS_Seek,
   1.362 +    NULL,   /* Pause */
   1.363 +    NULL,   /* Resume */
   1.364 +    NULL,   /* Stop */
   1.365 +    OPUS_Delete,
   1.366 +    NULL,   /* Close */
   1.367 +    OPUS_Unload,
   1.368 +};
   1.369 +
   1.370 +#endif /* MUSIC_OPUS */
   1.371 +
   1.372 +/* vi: set ts=4 sw=4 expandtab: */