music_mikmod.c
changeset 777 92882ef2ab81
parent 776 07b8e286bc03
child 789 e48fddd3fadb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/music_mikmod.c	Tue Oct 17 02:33:47 2017 -0700
     1.3 @@ -0,0 +1,681 @@
     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 MUSIC_MOD_MIKMOD
    1.26 +
    1.27 +/* This file supports MOD tracker music streams */
    1.28 +
    1.29 +#include "music_mikmod.h"
    1.30 +
    1.31 +#include "mikmod.h"
    1.32 +
    1.33 +
    1.34 +#define MAX_OUTPUT_CHANNELS 6
    1.35 +
    1.36 +/* libmikmod >= 3.3.2 constified several funcs */
    1.37 +#if (LIBMIKMOD_VERSION < 0x030302)
    1.38 +#define MIKMOD3_CONST
    1.39 +#else
    1.40 +#define MIKMOD3_CONST const
    1.41 +#endif
    1.42 +
    1.43 +typedef struct {
    1.44 +    int loaded;
    1.45 +    void *handle;
    1.46 +
    1.47 +    void (*MikMod_Exit)(void);
    1.48 +    CHAR* (*MikMod_InfoDriver)(void);
    1.49 +    CHAR* (*MikMod_InfoLoader)(void);
    1.50 +    int (*MikMod_Init)(MIKMOD3_CONST CHAR*);
    1.51 +    void (*MikMod_RegisterAllLoaders)(void);
    1.52 +    void (*MikMod_RegisterDriver)(struct MDRIVER*);
    1.53 +    int* MikMod_errno;
    1.54 +    MIKMOD3_CONST char* (*MikMod_strerror)(int);
    1.55 +    void (*MikMod_free)(void*);
    1.56 +    BOOL (*Player_Active)(void);
    1.57 +    void (*Player_Free)(MODULE*);
    1.58 +    MODULE* (*Player_LoadGeneric)(MREADER*,int,BOOL);
    1.59 +    void (*Player_SetPosition)(UWORD);
    1.60 +    void (*Player_SetVolume)(SWORD);
    1.61 +    void (*Player_Start)(MODULE*);
    1.62 +    void (*Player_Stop)(void);
    1.63 +    ULONG (*VC_WriteBytes)(SBYTE*,ULONG);
    1.64 +    struct MDRIVER* drv_nos;
    1.65 +    UWORD* md_device;
    1.66 +    UWORD* md_mixfreq;
    1.67 +    UWORD* md_mode;
    1.68 +    UBYTE* md_musicvolume;
    1.69 +    UBYTE* md_pansep;
    1.70 +    UBYTE* md_reverb;
    1.71 +    UBYTE* md_sndfxvolume;
    1.72 +    UBYTE* md_volume;
    1.73 +} mikmod_loader;
    1.74 +
    1.75 +static mikmod_loader mikmod = {
    1.76 +    0, NULL
    1.77 +};
    1.78 +
    1.79 +#ifdef MOD_DYNAMIC
    1.80 +
    1.81 +static int MIKMOD_Load()
    1.82 +{
    1.83 +    if (mikmod.loaded == 0) {
    1.84 +        mikmod.handle = SDL_LoadObject(MOD_DYNAMIC);
    1.85 +        if (mikmod.handle == NULL) {
    1.86 +            return -1;
    1.87 +        }
    1.88 +        mikmod.MikMod_Exit =
    1.89 +            (void (*)(void))
    1.90 +            SDL_LoadFunction(mikmod.handle, "MikMod_Exit");
    1.91 +        if (mikmod.MikMod_Exit == NULL) {
    1.92 +            SDL_UnloadObject(mikmod.handle);
    1.93 +            return -1;
    1.94 +        }
    1.95 +        mikmod.MikMod_InfoDriver =
    1.96 +            (CHAR* (*)(void))
    1.97 +            SDL_LoadFunction(mikmod.handle, "MikMod_InfoDriver");
    1.98 +        if (mikmod.MikMod_InfoDriver == NULL) {
    1.99 +            SDL_UnloadObject(mikmod.handle);
   1.100 +            return -1;
   1.101 +        }
   1.102 +        mikmod.MikMod_InfoLoader =
   1.103 +            (CHAR* (*)(void))
   1.104 +            SDL_LoadFunction(mikmod.handle, "MikMod_InfoLoader");
   1.105 +        if (mikmod.MikMod_InfoLoader == NULL) {
   1.106 +            SDL_UnloadObject(mikmod.handle);
   1.107 +            return -1;
   1.108 +        }
   1.109 +        mikmod.MikMod_Init =
   1.110 +            (int (*)(MIKMOD3_CONST CHAR*))
   1.111 +            SDL_LoadFunction(mikmod.handle, "MikMod_Init");
   1.112 +        if (mikmod.MikMod_Init == NULL) {
   1.113 +            SDL_UnloadObject(mikmod.handle);
   1.114 +            return -1;
   1.115 +        }
   1.116 +        mikmod.MikMod_RegisterAllLoaders =
   1.117 +            (void (*)(void))
   1.118 +            SDL_LoadFunction(mikmod.handle, "MikMod_RegisterAllLoaders");
   1.119 +        if (mikmod.MikMod_RegisterAllLoaders == NULL) {
   1.120 +            SDL_UnloadObject(mikmod.handle);
   1.121 +            return -1;
   1.122 +        }
   1.123 +        mikmod.MikMod_RegisterDriver =
   1.124 +            (void (*)(struct MDRIVER*))
   1.125 +            SDL_LoadFunction(mikmod.handle, "MikMod_RegisterDriver");
   1.126 +        if (mikmod.MikMod_RegisterDriver == NULL) {
   1.127 +            SDL_UnloadObject(mikmod.handle);
   1.128 +            return -1;
   1.129 +        }
   1.130 +        mikmod.MikMod_errno =
   1.131 +            (int*)
   1.132 +            SDL_LoadFunction(mikmod.handle, "MikMod_errno");
   1.133 +        if (mikmod.MikMod_errno == NULL) {
   1.134 +            SDL_UnloadObject(mikmod.handle);
   1.135 +            return -1;
   1.136 +        }
   1.137 +        mikmod.MikMod_strerror =
   1.138 +            (MIKMOD3_CONST char* (*)(int))
   1.139 +            SDL_LoadFunction(mikmod.handle, "MikMod_strerror");
   1.140 +        if (mikmod.MikMod_strerror == NULL) {
   1.141 +            SDL_UnloadObject(mikmod.handle);
   1.142 +            return -1;
   1.143 +        }
   1.144 +        mikmod.MikMod_free =
   1.145 +            (void (*)(void*))
   1.146 +            SDL_LoadFunction(mikmod.handle, "MikMod_free");
   1.147 +        if (mikmod.MikMod_free == NULL) {
   1.148 +            /* libmikmod 3.1 and earlier doesn't have it */
   1.149 +            mikmod.MikMod_free = free;
   1.150 +        }
   1.151 +        mikmod.Player_Active =
   1.152 +            (BOOL (*)(void))
   1.153 +            SDL_LoadFunction(mikmod.handle, "Player_Active");
   1.154 +        if (mikmod.Player_Active == NULL) {
   1.155 +            SDL_UnloadObject(mikmod.handle);
   1.156 +            return -1;
   1.157 +        }
   1.158 +        mikmod.Player_Free =
   1.159 +            (void (*)(MODULE*))
   1.160 +            SDL_LoadFunction(mikmod.handle, "Player_Free");
   1.161 +        if (mikmod.Player_Free == NULL) {
   1.162 +            SDL_UnloadObject(mikmod.handle);
   1.163 +            return -1;
   1.164 +        }
   1.165 +        mikmod.Player_LoadGeneric =
   1.166 +            (MODULE* (*)(MREADER*,int,BOOL))
   1.167 +            SDL_LoadFunction(mikmod.handle, "Player_LoadGeneric");
   1.168 +        if (mikmod.Player_LoadGeneric == NULL) {
   1.169 +            SDL_UnloadObject(mikmod.handle);
   1.170 +            return -1;
   1.171 +        }
   1.172 +        mikmod.Player_SetPosition =
   1.173 +            (void (*)(UWORD))
   1.174 +            SDL_LoadFunction(mikmod.handle, "Player_SetPosition");
   1.175 +        if (mikmod.Player_SetPosition == NULL) {
   1.176 +            SDL_UnloadObject(mikmod.handle);
   1.177 +            return -1;
   1.178 +        }
   1.179 +        mikmod.Player_SetVolume =
   1.180 +            (void (*)(SWORD))
   1.181 +            SDL_LoadFunction(mikmod.handle, "Player_SetVolume");
   1.182 +        if (mikmod.Player_SetVolume == NULL) {
   1.183 +            SDL_UnloadObject(mikmod.handle);
   1.184 +            return -1;
   1.185 +        }
   1.186 +        mikmod.Player_Start =
   1.187 +            (void (*)(MODULE*))
   1.188 +            SDL_LoadFunction(mikmod.handle, "Player_Start");
   1.189 +        if (mikmod.Player_Start == NULL) {
   1.190 +            SDL_UnloadObject(mikmod.handle);
   1.191 +            return -1;
   1.192 +        }
   1.193 +        mikmod.Player_Stop =
   1.194 +            (void (*)(void))
   1.195 +            SDL_LoadFunction(mikmod.handle, "Player_Stop");
   1.196 +        if (mikmod.Player_Stop == NULL) {
   1.197 +            SDL_UnloadObject(mikmod.handle);
   1.198 +            return -1;
   1.199 +        }
   1.200 +        mikmod.VC_WriteBytes =
   1.201 +            (ULONG (*)(SBYTE*,ULONG))
   1.202 +            SDL_LoadFunction(mikmod.handle, "VC_WriteBytes");
   1.203 +        if (mikmod.VC_WriteBytes == NULL) {
   1.204 +            SDL_UnloadObject(mikmod.handle);
   1.205 +            return -1;
   1.206 +        }
   1.207 +        mikmod.drv_nos =
   1.208 +            (MDRIVER*)
   1.209 +            SDL_LoadFunction(mikmod.handle, "drv_nos");
   1.210 +        if (mikmod.drv_nos == NULL) {
   1.211 +            SDL_UnloadObject(mikmod.handle);
   1.212 +            return -1;
   1.213 +        }
   1.214 +        mikmod.md_device =
   1.215 +            (UWORD*)
   1.216 +            SDL_LoadFunction(mikmod.handle, "md_device");
   1.217 +        if (mikmod.md_device == NULL) {
   1.218 +            SDL_UnloadObject(mikmod.handle);
   1.219 +            return -1;
   1.220 +        }
   1.221 +        mikmod.md_mixfreq =
   1.222 +            (UWORD*)
   1.223 +            SDL_LoadFunction(mikmod.handle, "md_mixfreq");
   1.224 +        if (mikmod.md_mixfreq == NULL) {
   1.225 +            SDL_UnloadObject(mikmod.handle);
   1.226 +            return -1;
   1.227 +        }
   1.228 +        mikmod.md_mode =
   1.229 +            (UWORD*)
   1.230 +            SDL_LoadFunction(mikmod.handle, "md_mode");
   1.231 +        if (mikmod.md_mode == NULL) {
   1.232 +            SDL_UnloadObject(mikmod.handle);
   1.233 +            return -1;
   1.234 +        }
   1.235 +        mikmod.md_musicvolume =
   1.236 +            (UBYTE*)
   1.237 +            SDL_LoadFunction(mikmod.handle, "md_musicvolume");
   1.238 +        if (mikmod.md_musicvolume == NULL) {
   1.239 +            SDL_UnloadObject(mikmod.handle);
   1.240 +            return -1;
   1.241 +        }
   1.242 +        mikmod.md_pansep =
   1.243 +            (UBYTE*)
   1.244 +            SDL_LoadFunction(mikmod.handle, "md_pansep");
   1.245 +        if (mikmod.md_pansep == NULL) {
   1.246 +            SDL_UnloadObject(mikmod.handle);
   1.247 +            return -1;
   1.248 +        }
   1.249 +        mikmod.md_reverb =
   1.250 +            (UBYTE*)
   1.251 +            SDL_LoadFunction(mikmod.handle, "md_reverb");
   1.252 +        if (mikmod.md_reverb == NULL) {
   1.253 +            SDL_UnloadObject(mikmod.handle);
   1.254 +            return -1;
   1.255 +        }
   1.256 +        mikmod.md_sndfxvolume =
   1.257 +            (UBYTE*)
   1.258 +            SDL_LoadFunction(mikmod.handle, "md_sndfxvolume");
   1.259 +        if (mikmod.md_sndfxvolume == NULL) {
   1.260 +            SDL_UnloadObject(mikmod.handle);
   1.261 +            return -1;
   1.262 +        }
   1.263 +        mikmod.md_volume =
   1.264 +            (UBYTE*)
   1.265 +            SDL_LoadFunction(mikmod.handle, "md_volume");
   1.266 +        if (mikmod.md_volume == NULL) {
   1.267 +            SDL_UnloadObject(mikmod.handle);
   1.268 +            return -1;
   1.269 +        }
   1.270 +    }
   1.271 +    ++mikmod.loaded;
   1.272 +
   1.273 +    return 0;
   1.274 +}
   1.275 +
   1.276 +static void MIKMOD_Unload()
   1.277 +{
   1.278 +    if (mikmod.loaded == 0) {
   1.279 +        return;
   1.280 +    }
   1.281 +    if (mikmod.loaded == 1) {
   1.282 +        SDL_UnloadObject(mikmod.handle);
   1.283 +    }
   1.284 +    --mikmod.loaded;
   1.285 +}
   1.286 +
   1.287 +#else /* !MOD_DYNAMIC */
   1.288 +
   1.289 +static int MIKMOD_Load()
   1.290 +{
   1.291 +    if (mikmod.loaded == 0) {
   1.292 +#ifdef __MACOSX__
   1.293 +        extern void Player_Start(MODULE*) __attribute__((weak_import));
   1.294 +        if (Player_Start == NULL)
   1.295 +        {
   1.296 +            /* Missing weakly linked framework */
   1.297 +            Mix_SetError("Missing mikmod.framework");
   1.298 +            return -1;
   1.299 +        }
   1.300 +#endif // __MACOSX__
   1.301 +
   1.302 +        mikmod.MikMod_Exit = MikMod_Exit;
   1.303 +        mikmod.MikMod_InfoDriver = MikMod_InfoDriver;
   1.304 +        mikmod.MikMod_InfoLoader = MikMod_InfoLoader;
   1.305 +        mikmod.MikMod_Init = MikMod_Init;
   1.306 +        mikmod.MikMod_RegisterAllLoaders = MikMod_RegisterAllLoaders;
   1.307 +        mikmod.MikMod_RegisterDriver = MikMod_RegisterDriver;
   1.308 +        mikmod.MikMod_errno = &MikMod_errno;
   1.309 +        mikmod.MikMod_strerror = MikMod_strerror;
   1.310 +#if LIBMIKMOD_VERSION < ((3<<16)|(2<<8))
   1.311 +        mikmod.MikMod_free = free;
   1.312 +#else
   1.313 +        mikmod.MikMod_free = MikMod_free;
   1.314 +#endif
   1.315 +        mikmod.Player_Active = Player_Active;
   1.316 +        mikmod.Player_Free = Player_Free;
   1.317 +        mikmod.Player_LoadGeneric = Player_LoadGeneric;
   1.318 +        mikmod.Player_SetPosition = Player_SetPosition;
   1.319 +        mikmod.Player_SetVolume = Player_SetVolume;
   1.320 +        mikmod.Player_Start = Player_Start;
   1.321 +        mikmod.Player_Stop = Player_Stop;
   1.322 +        mikmod.VC_WriteBytes = VC_WriteBytes;
   1.323 +        mikmod.drv_nos = &drv_nos;
   1.324 +        mikmod.md_device = &md_device;
   1.325 +        mikmod.md_mixfreq = &md_mixfreq;
   1.326 +        mikmod.md_mode = &md_mode;
   1.327 +        mikmod.md_musicvolume = &md_musicvolume;
   1.328 +        mikmod.md_pansep = &md_pansep;
   1.329 +        mikmod.md_reverb = &md_reverb;
   1.330 +        mikmod.md_sndfxvolume = &md_sndfxvolume;
   1.331 +        mikmod.md_volume = &md_volume;
   1.332 +    }
   1.333 +    ++mikmod.loaded;
   1.334 +
   1.335 +    return 0;
   1.336 +}
   1.337 +
   1.338 +static void MIKMOD_Unload()
   1.339 +{
   1.340 +    if (mikmod.loaded == 0) {
   1.341 +        return;
   1.342 +    }
   1.343 +    if (mikmod.loaded == 1) {
   1.344 +    }
   1.345 +    --mikmod.loaded;
   1.346 +}
   1.347 +
   1.348 +#endif /* MOD_DYNAMIC */
   1.349 +
   1.350 +
   1.351 +/* Reference for converting mikmod output to 4/6 channels */
   1.352 +static int current_output_channels;
   1.353 +static Uint16 current_output_format;
   1.354 +
   1.355 +static int music_swap8;
   1.356 +static int music_swap16;
   1.357 +
   1.358 +/* Initialize the MOD player, with the given mixer settings
   1.359 +   This function returns 0, or -1 if there was an error.
   1.360 + */
   1.361 +static int MIKMOD_Open(const SDL_AudioSpec *spec)
   1.362 +{
   1.363 +    CHAR *list;
   1.364 +
   1.365 +    /* Set the MikMod music format */
   1.366 +    music_swap8 = 0;
   1.367 +    music_swap16 = 0;
   1.368 +    switch (spec->format) {
   1.369 +
   1.370 +        case AUDIO_U8:
   1.371 +        case AUDIO_S8: {
   1.372 +            if (spec->format == AUDIO_S8) {
   1.373 +                music_swap8 = 1;
   1.374 +            }
   1.375 +            *mikmod.md_mode = 0;
   1.376 +        }
   1.377 +        break;
   1.378 +
   1.379 +        case AUDIO_S16LSB:
   1.380 +        case AUDIO_S16MSB: {
   1.381 +            /* See if we need to correct MikMod mixing */
   1.382 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.383 +            if (spec->format == AUDIO_S16MSB) {
   1.384 +#else
   1.385 +            if (spec->format == AUDIO_S16LSB) {
   1.386 +#endif
   1.387 +                music_swap16 = 1;
   1.388 +            }
   1.389 +            *mikmod.md_mode = DMODE_16BITS;
   1.390 +        }
   1.391 +        break;
   1.392 +
   1.393 +        default: {
   1.394 +            Mix_SetError("Unknown hardware audio format");
   1.395 +            return -1;
   1.396 +        }
   1.397 +    }
   1.398 +    current_output_channels = spec->channels;
   1.399 +    current_output_format = spec->format;
   1.400 +    if (spec->channels > 1) {
   1.401 +        if (spec->channels > MAX_OUTPUT_CHANNELS) {
   1.402 +            Mix_SetError("Hardware uses more channels than supported");
   1.403 +            return -1;
   1.404 +        }
   1.405 +        *mikmod.md_mode |= DMODE_STEREO;
   1.406 +    }
   1.407 +    *mikmod.md_mixfreq = spec->freq;
   1.408 +    *mikmod.md_device  = 0;
   1.409 +    *mikmod.md_volume  = 96;
   1.410 +    *mikmod.md_musicvolume = 128;
   1.411 +    *mikmod.md_sndfxvolume = 128;
   1.412 +    *mikmod.md_pansep  = 128;
   1.413 +    *mikmod.md_reverb  = 0;
   1.414 +    *mikmod.md_mode    |= DMODE_HQMIXER|DMODE_SOFT_MUSIC|DMODE_SURROUND;
   1.415 +
   1.416 +    list = mikmod.MikMod_InfoDriver();
   1.417 +    if (list)
   1.418 +      mikmod.MikMod_free(list);
   1.419 +    else
   1.420 +      mikmod.MikMod_RegisterDriver(mikmod.drv_nos);
   1.421 +
   1.422 +    list = mikmod.MikMod_InfoLoader();
   1.423 +    if (list)
   1.424 +      mikmod.MikMod_free(list);
   1.425 +    else
   1.426 +      mikmod.MikMod_RegisterAllLoaders();
   1.427 +
   1.428 +    if (mikmod.MikMod_Init(NULL)) {
   1.429 +        Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno));
   1.430 +        return -1;
   1.431 +    }
   1.432 +
   1.433 +    return 0;
   1.434 +}
   1.435 +
   1.436 +/* Uninitialize the music players */
   1.437 +static void MIKMOD_Close(void)
   1.438 +{
   1.439 +    if (mikmod.MikMod_Exit) {
   1.440 +        mikmod.MikMod_Exit();
   1.441 +    }
   1.442 +}
   1.443 +
   1.444 +typedef struct
   1.445 +{
   1.446 +    MREADER mr;
   1.447 +    /* struct MREADER in libmikmod <= 3.2.0-beta2
   1.448 +     * doesn't have iobase members. adding them here
   1.449 +     * so that if we compile against 3.2.0-beta2, we
   1.450 +     * can still run OK against 3.2.0b3 and newer. */
   1.451 +    long iobase, prev_iobase;
   1.452 +    Sint64 offset;
   1.453 +    Sint64 eof;
   1.454 +    SDL_RWops *src;
   1.455 +} LMM_MREADER;
   1.456 +
   1.457 +int LMM_Seek(struct MREADER *mr,long to,int dir)
   1.458 +{
   1.459 +	Sint64 offset = to;
   1.460 +    LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
   1.461 +    if (dir == SEEK_SET) {
   1.462 +        offset += lmmmr->offset;
   1.463 +        if (offset < lmmmr->offset)
   1.464 +            return -1;
   1.465 +    }
   1.466 +    return (int)(SDL_RWseek(lmmmr->src, offset, dir));
   1.467 +}
   1.468 +long LMM_Tell(struct MREADER *mr)
   1.469 +{
   1.470 +    LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
   1.471 +    return (long)(SDL_RWtell(lmmmr->src) - lmmmr->offset);
   1.472 +}
   1.473 +BOOL LMM_Read(struct MREADER *mr,void *buf,size_t sz)
   1.474 +{
   1.475 +    LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
   1.476 +    return SDL_RWread(lmmmr->src, buf, sz, 1);
   1.477 +}
   1.478 +int LMM_Get(struct MREADER *mr)
   1.479 +{
   1.480 +    unsigned char c;
   1.481 +    LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
   1.482 +    if (SDL_RWread(lmmmr->src, &c, 1, 1)) {
   1.483 +        return c;
   1.484 +    }
   1.485 +    return EOF;
   1.486 +}
   1.487 +BOOL LMM_Eof(struct MREADER *mr)
   1.488 +{
   1.489 +    Sint64 offset;
   1.490 +    LMM_MREADER* lmmmr = (LMM_MREADER*)mr;
   1.491 +    offset = LMM_Tell(mr);
   1.492 +    return offset >= lmmmr->eof;
   1.493 +}
   1.494 +MODULE *MikMod_LoadSongRW(SDL_RWops *src, int maxchan)
   1.495 +{
   1.496 +    LMM_MREADER lmmmr = {
   1.497 +        { LMM_Seek, LMM_Tell, LMM_Read, LMM_Get, LMM_Eof },
   1.498 +        0,
   1.499 +        0,
   1.500 +        0
   1.501 +    };
   1.502 +    lmmmr.offset = SDL_RWtell(src);
   1.503 +    SDL_RWseek(src, 0, RW_SEEK_END);
   1.504 +    lmmmr.eof = SDL_RWtell(src);
   1.505 +    SDL_RWseek(src, lmmmr.offset, RW_SEEK_SET);
   1.506 +    lmmmr.src = src;
   1.507 +    return mikmod.Player_LoadGeneric((MREADER*)&lmmmr, maxchan, 0);
   1.508 +}
   1.509 +
   1.510 +/* Load a MOD stream from an SDL_RWops object */
   1.511 +void *MIKMOD_CreateFromRW(SDL_RWops *src, int freesrc)
   1.512 +{
   1.513 +    MODULE *module;
   1.514 +
   1.515 +    module = MikMod_LoadSongRW(src, 64);
   1.516 +    if (!module) {
   1.517 +        Mix_SetError("%s", mikmod.MikMod_strerror(*mikmod.MikMod_errno));
   1.518 +        return NULL;
   1.519 +    }
   1.520 +
   1.521 +    /* Stop implicit looping, fade out and other flags. */
   1.522 +    module->extspd  = 1;
   1.523 +    module->panflag = 1;
   1.524 +    module->wrap    = 0;
   1.525 +    module->loop    = 0;
   1.526 +#if 0 /* Don't set fade out by default - unfortunately there's no real way
   1.527 +to query the status of the song or set trigger actions.  Hum. */
   1.528 +    module->fadeout = 1;
   1.529 +#endif
   1.530 +
   1.531 +    if (freesrc) {
   1.532 +        SDL_RWclose(src);
   1.533 +    }
   1.534 +    return module;
   1.535 +}
   1.536 +
   1.537 +/* Set the volume for a MOD stream */
   1.538 +static void MIKMOD_SetVolume(void *context, int volume)
   1.539 +{
   1.540 +    mikmod.Player_SetVolume((SWORD)volume);
   1.541 +}
   1.542 +
   1.543 +/* Start playback of a given MOD stream */
   1.544 +static int MIKMOD_Play(void *context)
   1.545 +{
   1.546 +    MODULE *music = (MODULE *)context;
   1.547 +    mikmod.Player_Start(music);
   1.548 +    mikmod.Player_SetVolume((SWORD)music_volume);
   1.549 +}
   1.550 +
   1.551 +/* Return non-zero if a stream is currently playing */
   1.552 +static SDL_bool MIKMOD_IsPlaying(void *context)
   1.553 +{
   1.554 +    return mikmod.Player_Active() ? SDL_TRUE : SDL_FALSE;
   1.555 +}
   1.556 +
   1.557 +/* Play some of a stream previously started with MOD_play() */
   1.558 +static int MIKMOD_GetAudio(void *context, void *data, int bytes)
   1.559 +{
   1.560 +    MODULE *music = (MODULE *)context;
   1.561 +    Uint8 *stream = (Uint8 *)data;
   1.562 +    int len = bytes;
   1.563 +
   1.564 +    if (current_output_channels > 2) {
   1.565 +        int small_len = 2 * len / current_output_channels;
   1.566 +        int i;
   1.567 +        Uint8 *src, *dst;
   1.568 +
   1.569 +        mikmod.VC_WriteBytes((SBYTE *)stream, small_len);
   1.570 +        /* and extend to len by copying channels */
   1.571 +        src = stream + small_len;
   1.572 +        dst = stream + len;
   1.573 +
   1.574 +        switch (current_output_format & 0xFF) {
   1.575 +            case 8:
   1.576 +                for (i=small_len/2; i; --i) {
   1.577 +                    src -= 2;
   1.578 +                    dst -= current_output_channels;
   1.579 +                    dst[0] = src[0];
   1.580 +                    dst[1] = src[1];
   1.581 +                    dst[2] = src[0];
   1.582 +                    dst[3] = src[1];
   1.583 +                    if (current_output_channels == 6) {
   1.584 +                        dst[4] = src[0];
   1.585 +                        dst[5] = src[1];
   1.586 +                    }
   1.587 +                }
   1.588 +                break;
   1.589 +            case 16:
   1.590 +                for (i=small_len/4; i; --i) {
   1.591 +                    src -= 4;
   1.592 +                    dst -= 2 * current_output_channels;
   1.593 +                    dst[0] = src[0];
   1.594 +                    dst[1] = src[1];
   1.595 +                    dst[2] = src[2];
   1.596 +                    dst[3] = src[3];
   1.597 +                    dst[4] = src[0];
   1.598 +                    dst[5] = src[1];
   1.599 +                    dst[6] = src[2];
   1.600 +                    dst[7] = src[3];
   1.601 +                    if (current_output_channels == 6) {
   1.602 +                        dst[8] = src[0];
   1.603 +                        dst[9] = src[1];
   1.604 +                        dst[10] = src[2];
   1.605 +                        dst[11] = src[3];
   1.606 +                    }
   1.607 +                }
   1.608 +                break;
   1.609 +        }
   1.610 +    } else {
   1.611 +        mikmod.VC_WriteBytes((SBYTE *)stream, len);
   1.612 +    }
   1.613 +    if (music_swap8) {
   1.614 +        Uint8 *dst;
   1.615 +        int i;
   1.616 +
   1.617 +        dst = stream;
   1.618 +        for (i=len; i; --i) {
   1.619 +            *dst++ ^= 0x80;
   1.620 +        }
   1.621 +    } else
   1.622 +    if (music_swap16) {
   1.623 +        Uint8 *dst, tmp;
   1.624 +        int i;
   1.625 +
   1.626 +        dst = stream;
   1.627 +        for (i=(len/2); i; --i) {
   1.628 +            tmp = dst[0];
   1.629 +            dst[0] = dst[1];
   1.630 +            dst[1] = tmp;
   1.631 +            dst += 2;
   1.632 +        }
   1.633 +    }
   1.634 +    return 0;
   1.635 +}
   1.636 +
   1.637 +/* Jump (seek) to a given position (time is in seconds) */
   1.638 +static int MIKMOD_Seek(void *context, double position)
   1.639 +{
   1.640 +    mikmod.Player_SetPosition((UWORD)position);
   1.641 +    return 0;
   1.642 +}
   1.643 +
   1.644 +/* Stop playback of a stream previously started with MOD_play() */
   1.645 +static void MIKMOD_Stop(void *context)
   1.646 +{
   1.647 +    mikmod.Player_Stop();
   1.648 +}
   1.649 +
   1.650 +/* Close the given MOD stream */
   1.651 +static void MIKMOD_Delete(void *context)
   1.652 +{
   1.653 +    MODULE *music = (MODULE *)context;
   1.654 +    mikmod.Player_Free(music);
   1.655 +}
   1.656 +
   1.657 +Mix_MusicInterface Mix_MusicInterface_MIKMOD =
   1.658 +{
   1.659 +    "MIKMOD",
   1.660 +    MIX_MUSIC_MIKMOD,
   1.661 +    MUS_MOD,
   1.662 +    SDL_FALSE,
   1.663 +    SDL_FALSE,
   1.664 +
   1.665 +    MIKMOD_Load,
   1.666 +    MIKMOD_Open,
   1.667 +    MIKMOD_CreateFromRW,
   1.668 +    NULL,   /* CreateFromFile */
   1.669 +    MIKMOD_SetVolume,
   1.670 +    MIKMOD_Play,
   1.671 +    MIKMOD_IsPlaying,
   1.672 +    MIKMOD_GetAudio,
   1.673 +    MIKMOD_Seek,
   1.674 +    NULL,   /* Pause */
   1.675 +    NULL,   /* Resume */
   1.676 +    NULL,   /* Stop */
   1.677 +    MIKMOD_Delete,
   1.678 +    MIKMOD_Close,
   1.679 +    MIKMOD_Unload,
   1.680 +};
   1.681 +
   1.682 +#endif /* MUSIC_MOD_MIKMOD */
   1.683 +
   1.684 +/* vi: set ts=4 sw=4 expandtab: */