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