music_mad.h
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 18:32:49 -0500
changeset 521 565549e046b0
parent 518 8bc9b5fd2aae
child 542 3de4970b36d4
permissions -rw-r--r--
Fixed bugs 1003, 1021, 1168 - fixed memory leak loading music

jjs@jjs.at 2011-03-11 11:37:57 PST
When using SDL_Mixer to play WAVE music, the call to Mix_FreeMusic does not
close the associated file handle.

There is a check in WAVStream_FreeSong() of wavestream.c like this:

if ( wave->freerw ) {
SDL_FreeRW(wave->rw);
}

But the variable freerw is not referenced anywhere else. SDL_FreeRW would also
not close the file from what I can tell.
     1 /*
     2   SDL_mixer:  An audio mixer library based on the SDL library
     3   Copyright (C) 1997-2012 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #ifdef MP3_MAD_MUSIC
    23 
    24 #include "mad.h"
    25 #include "SDL_rwops.h"
    26 #include "SDL_audio.h"
    27 #include "SDL_mixer.h"
    28 
    29 #define MAD_INPUT_BUFFER_SIZE	(5*8192)
    30 #define MAD_OUTPUT_BUFFER_SIZE	8192
    31 
    32 enum {
    33   MS_input_eof    = 0x0001,
    34   MS_input_error  = 0x0001,
    35   MS_decode_eof   = 0x0002,
    36   MS_decode_error = 0x0004,
    37   MS_error_flags  = 0x000f,
    38 
    39   MS_playing      = 0x0100,
    40   MS_cvt_decoded  = 0x0200,
    41 };
    42 
    43 typedef struct {
    44   SDL_RWops *rw;
    45   int freerw;
    46   struct mad_stream stream;
    47   struct mad_frame frame;
    48   struct mad_synth synth;
    49   int frames_read;
    50   mad_timer_t next_frame_start;
    51   int volume;
    52   int status;
    53   int output_begin, output_end;
    54   SDL_AudioSpec mixer;
    55   SDL_AudioCVT cvt;
    56 
    57   unsigned char input_buffer[MAD_INPUT_BUFFER_SIZE + MAD_BUFFER_GUARD];
    58   unsigned char output_buffer[MAD_OUTPUT_BUFFER_SIZE];
    59 } mad_data;
    60 
    61 mad_data *mad_openFile(const char *filename, SDL_AudioSpec *mixer);
    62 mad_data *mad_openFileRW(SDL_RWops *rw, SDL_AudioSpec *mixer, int freerw);
    63 void mad_closeFile(mad_data *mp3_mad);
    64 
    65 void mad_start(mad_data *mp3_mad);
    66 void mad_stop(mad_data *mp3_mad);
    67 int mad_isPlaying(mad_data *mp3_mad);
    68 
    69 int mad_getSamples(mad_data *mp3_mad, Uint8 *stream, int len);
    70 void mad_seek(mad_data *mp3_mad, double position);
    71 void mad_setVolume(mad_data *mp3_mad, int volume);
    72 
    73 #endif