fluidsynth.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 31 Dec 2011 18:32:49 -0500
changeset 521 565549e046b0
parent 518 8bc9b5fd2aae
child 532 b8e8ae4852b2
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   James Le Cuirot
    22   chewi@aura-online.co.uk
    23 */
    24 
    25 #include "fluidsynth.h"
    26 #include "SDL_mixer.h"
    27 #include <stdio.h>
    28 #include <sys/types.h>
    29 
    30 static Uint16 format;
    31 static Uint8 channels;
    32 static int freq;
    33 
    34 int fluidsynth_check_soundfont(const char *path, void *data)
    35 {
    36 	FILE *file = fopen(path, "r");
    37 
    38 	if (file) {
    39 		fclose(file);
    40 		return 1;
    41 	} else {
    42 		Mix_SetError("Failed to access the SoundFont %s", path);
    43 		return 0;
    44 	}
    45 }
    46 
    47 int fluidsynth_load_soundfont(const char *path, void *data)
    48 {
    49 	/* If this fails, it's too late to try Timidity so pray that at least one works. */
    50 	fluidsynth.fluid_synth_sfload((fluid_synth_t*) data, path, 1);
    51 	return 1;
    52 }
    53 
    54 int fluidsynth_init(SDL_AudioSpec *mixer)
    55 {
    56 	if (!Mix_EachSoundFont(fluidsynth_check_soundfont, NULL))
    57 		return -1;
    58 
    59 	format = mixer->format;
    60 	channels = mixer->channels;
    61 	freq = mixer->freq;
    62 
    63 	return 0;
    64 }
    65 
    66 FluidSynthMidiSong *fluidsynth_loadsong_common(int (*function)(FluidSynthMidiSong*, void*), void *data)
    67 {
    68 	FluidSynthMidiSong *song;
    69 	fluid_settings_t *settings = NULL;
    70 
    71 	if ((song = malloc(sizeof(FluidSynthMidiSong)))) {
    72 		memset(song, 0, sizeof(FluidSynthMidiSong));
    73 
    74 		if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) {
    75 			if ((settings = fluidsynth.new_fluid_settings())) {
    76 				fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) freq);
    77 
    78 				if ((song->synth = fluidsynth.new_fluid_synth(settings))) {
    79 					if (Mix_EachSoundFont(fluidsynth_load_soundfont, (void*) song->synth)) {
    80 						if ((song->player = fluidsynth.new_fluid_player(song->synth))) {
    81 							if (function(song, data)) return song;
    82 							fluidsynth.delete_fluid_player(song->player);
    83 						} else {
    84 							Mix_SetError("Failed to create FluidSynth player");
    85 						}
    86 					}
    87 					fluidsynth.delete_fluid_synth(song->synth);
    88 				} else {
    89 					Mix_SetError("Failed to create FluidSynth synthesizer");
    90 				}
    91 				fluidsynth.delete_fluid_settings(settings);
    92 			} else {
    93 				Mix_SetError("Failed to create FluidSynth settings");
    94 			}
    95 		} else {
    96 			Mix_SetError("Failed to set up audio conversion");
    97 		}
    98 		free(song);
    99 	} else {
   100 		Mix_SetError("Insufficient memory for song");
   101 	}
   102 	return NULL;
   103 }
   104 
   105 int fluidsynth_loadsong_internal(FluidSynthMidiSong *song, void *data)
   106 {
   107 	const char* path = (const char*) data;
   108 
   109 	if (fluidsynth.fluid_player_add(song->player, path) == FLUID_OK) {
   110 		return 1;
   111 	} else {
   112 		Mix_SetError("FluidSynth failed to load %s", path);
   113 		return 0;
   114 	}
   115 }
   116 
   117 int fluidsynth_loadsong_RW_internal(FluidSynthMidiSong *song, void *data)
   118 {
   119 	off_t offset;
   120 	size_t size;
   121 	char *buffer;
   122 	SDL_RWops *rw = (SDL_RWops*) data;
   123 
   124 	offset = SDL_RWtell(rw);
   125 	SDL_RWseek(rw, 0, RW_SEEK_END);
   126 	size = SDL_RWtell(rw) - offset;
   127 	SDL_RWseek(rw, offset, RW_SEEK_SET);
   128 
   129 	if ((buffer = (char*) malloc(size))) {
   130 		if(SDL_RWread(rw, buffer, size, 1) == 1) {
   131 			if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
   132 				return 1;
   133 			} else {
   134 				Mix_SetError("FluidSynth failed to load in-memory song");
   135 			}
   136 		} else {
   137 			Mix_SetError("Failed to read in-memory song");
   138 		}
   139 		free(buffer);
   140 	} else {
   141 		Mix_SetError("Insufficient memory for song");
   142 	}
   143 	return 0;
   144 }
   145 
   146 FluidSynthMidiSong *fluidsynth_loadsong(const char *midifile)
   147 {
   148 	return fluidsynth_loadsong_common(fluidsynth_loadsong_internal, (void*) midifile);
   149 }
   150 
   151 FluidSynthMidiSong *fluidsynth_loadsong_RW(SDL_RWops *rw, int freerw)
   152 {
   153 	FluidSynthMidiSong *song;
   154 
   155 	song = fluidsynth_loadsong_common(fluidsynth_loadsong_RW_internal, (void*) rw);
   156 	if (freerw) {
   157 		SDL_RWclose(rw);
   158 	}
   159 	return song;
   160 }
   161 
   162 void fluidsynth_freesong(FluidSynthMidiSong *song)
   163 {
   164 	if (!song) return;
   165 	fluidsynth.delete_fluid_player(song->player);
   166 	fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth));
   167 	fluidsynth.delete_fluid_synth(song->synth);
   168 	free(song);
   169 }
   170 
   171 void fluidsynth_start(FluidSynthMidiSong *song)
   172 {
   173 	fluidsynth.fluid_player_set_loop(song->player, 1);
   174 	fluidsynth.fluid_player_play(song->player);
   175 }
   176 
   177 void fluidsynth_stop(FluidSynthMidiSong *song)
   178 {
   179 	fluidsynth.fluid_player_stop(song->player);
   180 }
   181 
   182 int fluidsynth_active(FluidSynthMidiSong *song)
   183 {
   184 	return fluidsynth.fluid_player_get_status(song->player) == FLUID_PLAYER_PLAYING ? 1 : 0;
   185 }
   186 
   187 void fluidsynth_setvolume(FluidSynthMidiSong *song, int volume)
   188 {
   189 	/* FluidSynth's default is 0.2. Make 0.8 the maximum. */
   190 	fluidsynth.fluid_synth_set_gain(song->synth, (float) (volume * 0.00625));
   191 }
   192 
   193 int fluidsynth_playsome(FluidSynthMidiSong *song, void *dest, int dest_len)
   194 {
   195 	int result = -1;
   196 	int frames = dest_len / channels / ((format & 0xFF) / 8);
   197 	int src_len = frames * 4; /* 16-bit stereo */
   198 	void *src = dest;
   199 
   200 	if (dest_len < src_len) {
   201 		if (!(src = malloc(src_len))) {
   202 			Mix_SetError("Insufficient memory for audio conversion");
   203 			return result;
   204 		}
   205 	}
   206 
   207 	if (fluidsynth.fluid_synth_write_s16(song->synth, frames, src, 0, 2, src, 1, 2) != FLUID_OK) {
   208 		Mix_SetError("Error generating FluidSynth audio");
   209 		goto finish;
   210 	}
   211 
   212 	song->convert.buf = src;
   213 	song->convert.len = src_len;
   214 
   215 	if (SDL_ConvertAudio(&song->convert) < 0) {
   216 		Mix_SetError("Error during audio conversion");
   217 		goto finish;
   218 	}
   219 
   220 	if (src != dest)
   221 		memcpy(dest, src, dest_len);
   222 
   223 	result = 0;
   224 
   225 finish:
   226 	if (src != dest)
   227 		free(src);
   228 
   229 	return result;
   230 }