music_mpg.c
branchSDL-1.2
changeset 902 6c862e733898
child 1079 7a242c02ac24
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/music_mpg.c	Thu Oct 11 11:50:10 2018 +0300
     1.3 @@ -0,0 +1,383 @@
     1.4 +/*
     1.5 +  SDL_mixer:    An audio mixer library based on the SDL library
     1.6 +  Copyright (C) 1997-2017 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 MP3_MUSIC
    1.26 +
    1.27 +#include "SDL_mixer.h"
    1.28 +#include "music_mpg.h"
    1.29 +#include "dynamic_mp3.h"
    1.30 +
    1.31 +static int
    1.32 +snd_format_to_mpg123(uint16_t sdl_fmt)
    1.33 +{
    1.34 +    switch (sdl_fmt)
    1.35 +    {
    1.36 +        case AUDIO_U8:     return MPG123_ENC_UNSIGNED_8;
    1.37 +        case AUDIO_U16SYS: return MPG123_ENC_UNSIGNED_16;
    1.38 +        case AUDIO_S8:     return MPG123_ENC_SIGNED_8;
    1.39 +        case AUDIO_S16SYS: return MPG123_ENC_SIGNED_16;
    1.40 +    }
    1.41 +    return -1;
    1.42 +}
    1.43 +
    1.44 +static Uint16
    1.45 +mpg123_format_to_sdl(int fmt)
    1.46 +{
    1.47 +    switch (fmt)
    1.48 +    {
    1.49 +        case MPG123_ENC_UNSIGNED_8:  return AUDIO_U8;
    1.50 +        case MPG123_ENC_UNSIGNED_16: return AUDIO_U16SYS;
    1.51 +        case MPG123_ENC_SIGNED_8:    return AUDIO_S8;
    1.52 +        case MPG123_ENC_SIGNED_16:   return AUDIO_S16SYS;
    1.53 +    }
    1.54 +    return -1;
    1.55 +}
    1.56 +
    1.57 +static char const*
    1.58 +mpg123_format_str(int fmt)
    1.59 +{
    1.60 +    switch (fmt)
    1.61 +    {
    1.62 +#define f(x) case x: return #x;
    1.63 +        f(MPG123_ENC_UNSIGNED_8)
    1.64 +        f(MPG123_ENC_UNSIGNED_16)
    1.65 +        f(MPG123_ENC_SIGNED_8)
    1.66 +        f(MPG123_ENC_SIGNED_16)
    1.67 +#undef f
    1.68 +    }
    1.69 +    return "unknown";
    1.70 +}
    1.71 +
    1.72 +static char const*
    1.73 +mpg_err(mpg123_handle* mpg, int code)
    1.74 +{
    1.75 +    char const* err = "unknown error";
    1.76 +
    1.77 +    if (mpg && code == MPG123_ERR) {
    1.78 +        err = mpg123.mpg123_strerror(mpg);
    1.79 +    } else {
    1.80 +        err = mpg123.mpg123_plain_strerror(code);
    1.81 +    }
    1.82 +
    1.83 +    return err;
    1.84 +}
    1.85 +
    1.86 +/* we're gonna override mpg123's I/O with these wrappers for RWops */
    1.87 +static
    1.88 +ssize_t rwops_read(void* p, void* dst, size_t n) {
    1.89 +    return (ssize_t)SDL_RWread((SDL_RWops*)p, dst, 1, n);
    1.90 +}
    1.91 +
    1.92 +static
    1.93 +off_t rwops_seek(void* p, off_t offset, int whence) {
    1.94 +    return (off_t)SDL_RWseek((SDL_RWops*)p, (Sint64)offset, whence);
    1.95 +}
    1.96 +
    1.97 +static
    1.98 +void rwops_cleanup(void* p) {
    1.99 +    (void)p;
   1.100 +    /* do nothing, we will free the file later */
   1.101 +}
   1.102 +
   1.103 +static int getsome(mpg_data* m);
   1.104 +
   1.105 +mpg_data*
   1.106 +mpg_new_rw(SDL_RWops *src, SDL_AudioSpec* mixer, int freesrc)
   1.107 +{
   1.108 +    int fmt, result;
   1.109 +    mpg_data* m = NULL;
   1.110 +
   1.111 +    if (!Mix_Init(MIX_INIT_MP3)) {
   1.112 +        goto fail;
   1.113 +    }
   1.114 +
   1.115 +    m = (mpg_data*)SDL_malloc(sizeof(mpg_data));
   1.116 +    if (!m) {
   1.117 +        SDL_OutOfMemory();
   1.118 +        goto fail;
   1.119 +    }
   1.120 +
   1.121 +    SDL_memset(m, 0, sizeof(mpg_data));
   1.122 +
   1.123 +    m->src = src;
   1.124 +    m->freesrc = freesrc;
   1.125 +
   1.126 +    m->handle = mpg123.mpg123_new(0, &result);
   1.127 +    if (result != MPG123_OK) {
   1.128 +        goto fail;
   1.129 +    }
   1.130 +
   1.131 +    result = mpg123.mpg123_replace_reader_handle(
   1.132 +        m->handle,
   1.133 +        rwops_read, rwops_seek, rwops_cleanup
   1.134 +    );
   1.135 +    if (result != MPG123_OK) {
   1.136 +        goto fail;
   1.137 +    }
   1.138 +
   1.139 +    result = mpg123.mpg123_format_none(m->handle);
   1.140 +    if (result != MPG123_OK) {
   1.141 +        goto fail;
   1.142 +    }
   1.143 +
   1.144 +    fmt = snd_format_to_mpg123(mixer->format);
   1.145 +    if (fmt == -1) {
   1.146 +        goto fail;
   1.147 +    }
   1.148 +
   1.149 +    result = mpg123.mpg123_format(m->handle, mixer->freq, mixer->channels, fmt);
   1.150 +    if (result != MPG123_OK) {
   1.151 +        goto fail;
   1.152 +    }
   1.153 +
   1.154 +    result = mpg123.mpg123_open_handle(m->handle, m->src);
   1.155 +    if (result != MPG123_OK) {
   1.156 +        goto fail;
   1.157 +    }
   1.158 +
   1.159 +    m->volume = MIX_MAX_VOLUME;
   1.160 +    m->mixer = *mixer;
   1.161 +
   1.162 +    /* hacky: read until we can figure out the format then rewind */
   1.163 +    while (!m->gotformat)
   1.164 +    {
   1.165 +        if (!getsome(m)) {
   1.166 +            goto fail;
   1.167 +        }
   1.168 +    }
   1.169 +
   1.170 +    /* rewind */
   1.171 +    mpg123.mpg123_seek(m->handle, 0, SEEK_SET);
   1.172 +
   1.173 +    m->len_available = 0;
   1.174 +    m->snd_available = m->cvt.buf;
   1.175 +
   1.176 +    return m;
   1.177 +
   1.178 +    fail:
   1.179 +    if (!m && freesrc) {
   1.180 +        SDL_RWclose(src);
   1.181 +    }
   1.182 +    mpg_delete(m);
   1.183 +    return NULL;
   1.184 +}
   1.185 +
   1.186 +void
   1.187 +mpg_delete(mpg_data* m)
   1.188 +{
   1.189 +    if (!m) {
   1.190 +        return;
   1.191 +    }
   1.192 +
   1.193 +    if (m->freesrc) {
   1.194 +        SDL_RWclose(m->src);
   1.195 +    }
   1.196 +
   1.197 +    if (m->cvt.buf) {
   1.198 +        SDL_free(m->cvt.buf);
   1.199 +    }
   1.200 +
   1.201 +    mpg123.mpg123_close(m->handle);
   1.202 +    mpg123.mpg123_delete(m->handle);
   1.203 +    SDL_free(m);
   1.204 +}
   1.205 +
   1.206 +void
   1.207 +mpg_start(mpg_data* m) {
   1.208 +    m->playing = 1;
   1.209 +}
   1.210 +
   1.211 +void
   1.212 +mpg_stop(mpg_data* m) {
   1.213 +    m->playing = 0;
   1.214 +}
   1.215 +
   1.216 +int
   1.217 +mpg_playing(mpg_data* m) {
   1.218 +    return m->playing;
   1.219 +}
   1.220 +
   1.221 +/*
   1.222 +    updates the convert struct and buffer to match the format queried from
   1.223 +    mpg123.
   1.224 +*/
   1.225 +static int
   1.226 +update_format(mpg_data* m)
   1.227 +{
   1.228 +    int code;
   1.229 +    long rate;
   1.230 +    int channels, encoding;
   1.231 +    Uint16 sdlfmt;
   1.232 +    size_t bufsize;
   1.233 +
   1.234 +    m->gotformat = 1;
   1.235 +
   1.236 +    code =
   1.237 +        mpg123.mpg123_getformat(
   1.238 +            m->handle,
   1.239 +            &rate, &channels, &encoding
   1.240 +        );
   1.241 +
   1.242 +    if (code != MPG123_OK) {
   1.243 +        Mix_SetError("mpg123_getformat: %s", mpg_err(m->handle, code));
   1.244 +        return 0;
   1.245 +    }
   1.246 +
   1.247 +    sdlfmt = mpg123_format_to_sdl(encoding);
   1.248 +    if (sdlfmt == (Uint16)-1)
   1.249 +    {
   1.250 +        Mix_SetError(
   1.251 +            "Format %s is not supported by SDL",
   1.252 +            mpg123_format_str(encoding)
   1.253 +        );
   1.254 +        return 0;
   1.255 +    }
   1.256 +
   1.257 +    SDL_BuildAudioCVT(
   1.258 +        &m->cvt,
   1.259 +        sdlfmt, channels, rate,
   1.260 +        m->mixer.format,
   1.261 +        m->mixer.channels,
   1.262 +        m->mixer.freq
   1.263 +    );
   1.264 +
   1.265 +    if (m->cvt.buf) {
   1.266 +        SDL_free(m->cvt.buf);
   1.267 +    }
   1.268 +
   1.269 +    bufsize = sizeof(m->buf) * m->cvt.len_mult;
   1.270 +    m->cvt.buf = SDL_malloc(bufsize);
   1.271 +
   1.272 +    if (!m->cvt.buf)
   1.273 +    {
   1.274 +        Mix_SetError("Out of memory");
   1.275 +        mpg_stop(m);
   1.276 +        return 0;
   1.277 +    }
   1.278 +
   1.279 +    return 1;
   1.280 +}
   1.281 +
   1.282 +/* read some mp3 stream data and convert it for output */
   1.283 +static int
   1.284 +getsome(mpg_data* m)
   1.285 +{
   1.286 +    int code;
   1.287 +    size_t len = 0;
   1.288 +    Uint8* data = m->buf;
   1.289 +    const size_t cbdata = sizeof(m->buf);
   1.290 +    SDL_AudioCVT* cvt = &m->cvt;
   1.291 +
   1.292 +    code = mpg123.mpg123_read(m->handle, data, cbdata, &len);
   1.293 +    switch (code)
   1.294 +    {
   1.295 +    case MPG123_NEW_FORMAT:
   1.296 +        if (!update_format(m)) {
   1.297 +            return 0;
   1.298 +        }
   1.299 +        break;
   1.300 +
   1.301 +    case MPG123_DONE:
   1.302 +        mpg_stop(m);
   1.303 +    case MPG123_OK:
   1.304 +        break;
   1.305 +
   1.306 +    default:
   1.307 +        Mix_SetError("mpg123_read: %s", mpg_err(m->handle, code));
   1.308 +        return 0;
   1.309 +    }
   1.310 +
   1.311 +    SDL_memcpy(cvt->buf, data, cbdata);
   1.312 +
   1.313 +    if (cvt->needed) {
   1.314 +        /* we need to convert the audio to SDL's format */
   1.315 +        cvt->len = len;
   1.316 +        SDL_ConvertAudio(cvt);
   1.317 +    }
   1.318 +    else {
   1.319 +        /* no conversion needed, just copy */
   1.320 +        cvt->len_cvt = len;
   1.321 +    }
   1.322 +
   1.323 +    m->len_available = cvt->len_cvt;
   1.324 +    m->snd_available = cvt->buf;
   1.325 +
   1.326 +    return 1;
   1.327 +}
   1.328 +
   1.329 +int
   1.330 +mpg_get_samples(mpg_data* m, Uint8 *stream, int len)
   1.331 +{
   1.332 +    int mixable;
   1.333 +
   1.334 +    while (len > 0 && m->playing)
   1.335 +    {
   1.336 +        if (!m->len_available)
   1.337 +        {
   1.338 +            if (!getsome(m)) {
   1.339 +                m->playing = 0;
   1.340 +                return len;
   1.341 +            }
   1.342 +        }
   1.343 +
   1.344 +        mixable = len;
   1.345 +
   1.346 +        if (mixable > m->len_available) {
   1.347 +            mixable = m->len_available;
   1.348 +        }
   1.349 +
   1.350 +        if (m->volume == MIX_MAX_VOLUME) {
   1.351 +            SDL_memcpy(stream, m->snd_available, mixable);
   1.352 +        }
   1.353 +        else {
   1.354 +            SDL_MixAudio(
   1.355 +                stream,
   1.356 +                m->snd_available,
   1.357 +                mixable,
   1.358 +                m->volume
   1.359 +            );
   1.360 +        }
   1.361 +
   1.362 +        m->len_available -= mixable;
   1.363 +        m->snd_available += mixable;
   1.364 +        len -= mixable;
   1.365 +        stream += mixable;
   1.366 +    }
   1.367 +
   1.368 +    return len;
   1.369 +}
   1.370 +
   1.371 +void
   1.372 +mpg_seek(mpg_data* m, double secs)
   1.373 +{
   1.374 +    off_t offset = m->mixer.freq * secs;
   1.375 +
   1.376 +    if ((offset = mpg123.mpg123_seek(m->handle, offset, SEEK_SET)) < 0) {
   1.377 +        Mix_SetError("mpg123_seek: %s", mpg_err(m->handle, -offset));
   1.378 +    }
   1.379 +}
   1.380 +
   1.381 +void
   1.382 +mpg_volume(mpg_data* m, int volume) {
   1.383 +    m->volume = volume;
   1.384 +}
   1.385 +
   1.386 +#endif    /* MP3_MUSIC */