music_modplug.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 17 Jun 2015 00:11:41 -0700
changeset 705 fe757163b8f7
parent 639 f8901a7ff3f1
child 711 f40c5ac95b12
permissions -rw-r--r--
Fixed bug 3018 - Loading MIDI music using FluidSynth leaks memory.

Philipp Wiesemann

There is a memory leak in fluidsynth.c and fluidsynth_loadsong_RW_internal(). The allocated temporary buffer is not deleted if fluid_player_add_mem() returns FLUID_OK.
slouken@639
     1
/*
slouken@639
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@639
     3
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@639
     4
slouken@639
     5
  This software is provided 'as-is', without any express or implied
slouken@639
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@639
     7
  arising from the use of this software.
slouken@639
     8
slouken@639
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@639
    10
  including commercial applications, and to alter it and redistribute it
slouken@639
    11
  freely, subject to the following restrictions:
slouken@639
    12
slouken@639
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@639
    14
     claim that you wrote the original software. If you use this software
slouken@639
    15
     in a product, an acknowledgment in the product documentation would be
slouken@639
    16
     appreciated but is not required.
slouken@639
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@639
    18
     misrepresented as being the original software.
slouken@639
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@639
    20
*/
slouken@639
    21
slouken@481
    22
#ifdef MODPLUG_MUSIC
slouken@481
    23
slouken@639
    24
#include "SDL_mixer.h"
slouken@639
    25
#include "dynamic_modplug.h"
slouken@481
    26
#include "music_modplug.h"
slouken@481
    27
slouken@481
    28
static int current_output_channels=0;
slouken@481
    29
static int music_swap8=0;
slouken@481
    30
static int music_swap16=0;
slouken@481
    31
static ModPlug_Settings settings;
slouken@481
    32
slouken@481
    33
int modplug_init(SDL_AudioSpec *spec)
slouken@481
    34
{
slouken@639
    35
    if ( !Mix_Init(MIX_INIT_MODPLUG) ) {
slouken@639
    36
        return -1;
slouken@639
    37
    }
slouken@639
    38
slouken@639
    39
    modplug.ModPlug_GetSettings(&settings);
slouken@617
    40
    settings.mFlags=MODPLUG_ENABLE_OVERSAMPLING;
slouken@617
    41
    current_output_channels=spec->channels;
slouken@617
    42
    settings.mChannels=spec->channels>1?2:1;
slouken@617
    43
    settings.mBits=spec->format&0xFF;
slouken@481
    44
slouken@617
    45
    music_swap8 = 0;
slouken@617
    46
    music_swap16 = 0;
slouken@481
    47
slouken@617
    48
    switch(spec->format)
slouken@617
    49
    {
slouken@617
    50
        case AUDIO_U8:
slouken@617
    51
        case AUDIO_S8: {
slouken@617
    52
            if ( spec->format == AUDIO_S8 ) {
slouken@617
    53
                music_swap8 = 1;
slouken@617
    54
            }
slouken@617
    55
            settings.mBits=8;
slouken@617
    56
        }
slouken@617
    57
        break;
slouken@481
    58
slouken@617
    59
        case AUDIO_S16LSB:
slouken@617
    60
        case AUDIO_S16MSB: {
slouken@617
    61
            /* See if we need to correct MikMod mixing */
slouken@481
    62
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@617
    63
            if ( spec->format == AUDIO_S16MSB ) {
slouken@481
    64
#else
slouken@617
    65
            if ( spec->format == AUDIO_S16LSB ) {
slouken@481
    66
#endif
slouken@617
    67
                music_swap16 = 1;
slouken@617
    68
            }
slouken@617
    69
            settings.mBits=16;
slouken@617
    70
        }
slouken@617
    71
        break;
slouken@481
    72
slouken@617
    73
        default: {
slouken@617
    74
            Mix_SetError("Unknown hardware audio format");
slouken@617
    75
            return -1;
slouken@617
    76
        }
slouken@481
    77
slouken@617
    78
    }
slouken@481
    79
slouken@617
    80
    settings.mFrequency=spec->freq; /*TODO: limit to 11025, 22050, or 44100 ? */
slouken@617
    81
    settings.mResamplingMode=MODPLUG_RESAMPLE_FIR;
slouken@617
    82
    settings.mReverbDepth=0;
slouken@617
    83
    settings.mReverbDelay=100;
slouken@617
    84
    settings.mBassAmount=0;
slouken@617
    85
    settings.mBassRange=50;
slouken@617
    86
    settings.mSurroundDepth=0;
slouken@617
    87
    settings.mSurroundDelay=10;
slouken@617
    88
    settings.mLoopCount=0;
slouken@639
    89
    modplug.ModPlug_SetSettings(&settings);
slouken@617
    90
    return 0;
slouken@481
    91
}
slouken@481
    92
slouken@481
    93
/* Uninitialize the music players */
slouken@481
    94
void modplug_exit()
slouken@481
    95
{
slouken@481
    96
}
slouken@481
    97
slouken@481
    98
/* Set the volume for a modplug stream */
slouken@481
    99
void modplug_setvolume(modplug_data *music, int volume)
slouken@481
   100
{
slouken@639
   101
    modplug.ModPlug_SetMasterVolume(music->file, volume*4);
slouken@481
   102
}
slouken@481
   103
slouken@481
   104
/* Load a modplug stream from an SDL_RWops object */
slouken@625
   105
modplug_data *modplug_new_RW(SDL_RWops *src, int freesrc)
slouken@481
   106
{
slouken@625
   107
    modplug_data *music = NULL;
slouken@625
   108
    Sint64 offset;
slouken@625
   109
    size_t sz;
slouken@625
   110
    char *buf;
slouken@481
   111
slouken@639
   112
    /* Make sure the modplug library is loaded */
slouken@639
   113
    if ( !Mix_Init(MIX_INIT_MODPLUG) ) {
slouken@639
   114
        return NULL;
slouken@639
   115
    }
slouken@639
   116
slouken@625
   117
    offset = SDL_RWtell(src);
slouken@625
   118
    SDL_RWseek(src, 0, RW_SEEK_END);
slouken@625
   119
    sz = (size_t)(SDL_RWtell(src) - offset);
slouken@625
   120
    SDL_RWseek(src, offset, RW_SEEK_SET);
slouken@625
   121
    buf = (char*)SDL_malloc(sz);
slouken@625
   122
    if (buf) {
slouken@625
   123
        if (SDL_RWread(src, buf, sz, 1) == 1) {
slouken@625
   124
            music = (modplug_data*)SDL_malloc(sizeof(modplug_data));
slouken@625
   125
            if (music) {
slouken@625
   126
                music->playing = 0;
slouken@639
   127
                music->file = modplug.ModPlug_Load(buf, sz);
slouken@625
   128
                if (!music->file) {
slouken@617
   129
                    SDL_free(music);
slouken@625
   130
                    music = NULL;
slouken@617
   131
                }
slouken@625
   132
            } else {
slouken@617
   133
                SDL_OutOfMemory();
slouken@617
   134
            }
slouken@617
   135
        }
slouken@617
   136
        SDL_free(buf);
slouken@625
   137
    } else {
slouken@617
   138
        SDL_OutOfMemory();
slouken@617
   139
    }
slouken@625
   140
    if (music && freesrc) {
slouken@625
   141
        SDL_RWclose(src);
slouken@617
   142
    }
slouken@617
   143
    return music;
slouken@481
   144
}
slouken@481
   145
slouken@481
   146
/* Start playback of a given modplug stream */
slouken@481
   147
void modplug_play(modplug_data *music)
slouken@481
   148
{
slouken@639
   149
    modplug.ModPlug_Seek(music->file,0);
slouken@617
   150
    music->playing=1;
slouken@481
   151
}
slouken@481
   152
slouken@481
   153
/* Return non-zero if a stream is currently playing */
slouken@481
   154
int modplug_playing(modplug_data *music)
slouken@481
   155
{
slouken@617
   156
    return music && music->playing;
slouken@481
   157
}
slouken@481
   158
slouken@481
   159
/* Play some of a stream previously started with modplug_play() */
slouken@481
   160
int modplug_playAudio(modplug_data *music, Uint8 *stream, int len)
slouken@481
   161
{
slouken@617
   162
    if (current_output_channels > 2) {
slouken@617
   163
        int small_len = 2 * len / current_output_channels;
slouken@617
   164
        int i;
slouken@617
   165
        Uint8 *src, *dst;
slouken@481
   166
slouken@639
   167
        i=modplug.ModPlug_Read(music->file, stream, small_len);
slouken@617
   168
        if(i<small_len)
slouken@617
   169
        {
slouken@621
   170
            SDL_memset(stream+i,0,small_len-i);
slouken@617
   171
            music->playing=0;
slouken@617
   172
        }
slouken@617
   173
        /* and extend to len by copying channels */
slouken@617
   174
        src = stream + small_len;
slouken@617
   175
        dst = stream + len;
slouken@481
   176
slouken@617
   177
        switch (settings.mBits) {
slouken@617
   178
            case 8:
slouken@617
   179
                for ( i=small_len/2; i; --i ) {
slouken@617
   180
                    src -= 2;
slouken@617
   181
                    dst -= current_output_channels;
slouken@617
   182
                    dst[0] = src[0];
slouken@617
   183
                    dst[1] = src[1];
slouken@617
   184
                    dst[2] = src[0];
slouken@617
   185
                    dst[3] = src[1];
slouken@617
   186
                    if (current_output_channels == 6) {
slouken@617
   187
                        dst[4] = src[0];
slouken@617
   188
                        dst[5] = src[1];
slouken@617
   189
                    }
slouken@617
   190
                }
slouken@617
   191
                break;
slouken@617
   192
            case 16:
slouken@617
   193
                for ( i=small_len/4; i; --i ) {
slouken@617
   194
                    src -= 4;
slouken@617
   195
                    dst -= 2 * current_output_channels;
slouken@617
   196
                    dst[0] = src[0];
slouken@617
   197
                    dst[1] = src[1];
slouken@617
   198
                    dst[2] = src[2];
slouken@617
   199
                    dst[3] = src[3];
slouken@617
   200
                    dst[4] = src[0];
slouken@617
   201
                    dst[5] = src[1];
slouken@617
   202
                    dst[6] = src[2];
slouken@617
   203
                    dst[7] = src[3];
slouken@617
   204
                    if (current_output_channels == 6) {
slouken@617
   205
                        dst[8] = src[0];
slouken@617
   206
                        dst[9] = src[1];
slouken@617
   207
                        dst[10] = src[2];
slouken@617
   208
                        dst[11] = src[3];
slouken@617
   209
                    }
slouken@617
   210
                }
slouken@617
   211
                break;
slouken@617
   212
        }
slouken@617
   213
    } else {
slouken@639
   214
        int i=modplug.ModPlug_Read(music->file, stream, len);
slouken@617
   215
        if(i<len)
slouken@617
   216
        {
slouken@621
   217
            SDL_memset(stream+i,0,len-i);
slouken@617
   218
            music->playing=0;
slouken@617
   219
        }
slouken@617
   220
    }
slouken@617
   221
    if ( music_swap8 ) {
slouken@617
   222
        Uint8 *dst;
slouken@617
   223
        int i;
slouken@481
   224
slouken@617
   225
        dst = stream;
slouken@617
   226
        for ( i=len; i; --i ) {
slouken@617
   227
            *dst++ ^= 0x80;
slouken@617
   228
        }
slouken@617
   229
    } else
slouken@617
   230
    if ( music_swap16 ) {
slouken@617
   231
        Uint8 *dst, tmp;
slouken@617
   232
        int i;
slouken@481
   233
slouken@617
   234
        dst = stream;
slouken@617
   235
        for ( i=(len/2); i; --i ) {
slouken@617
   236
            tmp = dst[0];
slouken@617
   237
            dst[0] = dst[1];
slouken@617
   238
            dst[1] = tmp;
slouken@617
   239
            dst += 2;
slouken@617
   240
        }
slouken@617
   241
    }
slouken@617
   242
    return 0;
slouken@481
   243
}
slouken@481
   244
slouken@481
   245
/* Stop playback of a stream previously started with modplug_play() */
slouken@481
   246
void modplug_stop(modplug_data *music)
slouken@481
   247
{
slouken@617
   248
    music->playing=0;
slouken@481
   249
}
slouken@481
   250
slouken@481
   251
/* Close the given modplug stream */
slouken@481
   252
void modplug_delete(modplug_data *music)
slouken@481
   253
{
slouken@639
   254
    modplug.ModPlug_Unload(music->file);
slouken@617
   255
    SDL_free(music);
slouken@481
   256
}
slouken@481
   257
slouken@481
   258
/* Jump (seek) to a given position (time is in seconds) */
slouken@481
   259
void modplug_jump_to_time(modplug_data *music, double time)
slouken@481
   260
{
slouken@639
   261
    modplug.ModPlug_Seek(music->file,(int)(time*1000));
slouken@481
   262
}
slouken@481
   263
slouken@639
   264
#endif /* MODPLUG_MUSIC */