fluidsynth.c
changeset 617 87116a42526e
parent 601 05123263dab3
child 621 944412baab72
     1.1 --- a/fluidsynth.c	Tue May 21 21:09:26 2013 -0700
     1.2 +++ b/fluidsynth.c	Tue May 21 21:21:23 2013 -0700
     1.3 @@ -36,187 +36,187 @@
     1.4  
     1.5  int fluidsynth_check_soundfont(const char *path, void *data)
     1.6  {
     1.7 -	FILE *file = fopen(path, "r");
     1.8 +    FILE *file = fopen(path, "r");
     1.9  
    1.10 -	if (file) {
    1.11 -		fclose(file);
    1.12 -		return 1;
    1.13 -	} else {
    1.14 -		Mix_SetError("Failed to access the SoundFont %s", path);
    1.15 -		return 0;
    1.16 -	}
    1.17 +    if (file) {
    1.18 +        fclose(file);
    1.19 +        return 1;
    1.20 +    } else {
    1.21 +        Mix_SetError("Failed to access the SoundFont %s", path);
    1.22 +        return 0;
    1.23 +    }
    1.24  }
    1.25  
    1.26  int fluidsynth_load_soundfont(const char *path, void *data)
    1.27  {
    1.28 -	/* If this fails, it's too late to try Timidity so pray that at least one works. */
    1.29 -	fluidsynth.fluid_synth_sfload((fluid_synth_t*) data, path, 1);
    1.30 -	return 1;
    1.31 +    /* If this fails, it's too late to try Timidity so pray that at least one works. */
    1.32 +    fluidsynth.fluid_synth_sfload((fluid_synth_t*) data, path, 1);
    1.33 +    return 1;
    1.34  }
    1.35  
    1.36  int fluidsynth_init(SDL_AudioSpec *mixer)
    1.37  {
    1.38 -	if (!Mix_EachSoundFont(fluidsynth_check_soundfont, NULL))
    1.39 -		return -1;
    1.40 +    if (!Mix_EachSoundFont(fluidsynth_check_soundfont, NULL))
    1.41 +        return -1;
    1.42  
    1.43 -	format = mixer->format;
    1.44 -	channels = mixer->channels;
    1.45 -	freq = mixer->freq;
    1.46 +    format = mixer->format;
    1.47 +    channels = mixer->channels;
    1.48 +    freq = mixer->freq;
    1.49  
    1.50 -	return 0;
    1.51 +    return 0;
    1.52  }
    1.53  
    1.54  static FluidSynthMidiSong *fluidsynth_loadsong_common(int (*function)(FluidSynthMidiSong*, void*), void *data)
    1.55  {
    1.56 -	FluidSynthMidiSong *song;
    1.57 -	fluid_settings_t *settings = NULL;
    1.58 +    FluidSynthMidiSong *song;
    1.59 +    fluid_settings_t *settings = NULL;
    1.60  
    1.61 -	if (!Mix_Init(MIX_INIT_FLUIDSYNTH)) {
    1.62 -		return NULL;
    1.63 -	}
    1.64 +    if (!Mix_Init(MIX_INIT_FLUIDSYNTH)) {
    1.65 +        return NULL;
    1.66 +    }
    1.67  
    1.68 -	if ((song = SDL_malloc(sizeof(FluidSynthMidiSong)))) {
    1.69 -		memset(song, 0, sizeof(FluidSynthMidiSong));
    1.70 +    if ((song = SDL_malloc(sizeof(FluidSynthMidiSong)))) {
    1.71 +        memset(song, 0, sizeof(FluidSynthMidiSong));
    1.72  
    1.73 -		if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) {
    1.74 -			if ((settings = fluidsynth.new_fluid_settings())) {
    1.75 -				fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) freq);
    1.76 +        if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) {
    1.77 +            if ((settings = fluidsynth.new_fluid_settings())) {
    1.78 +                fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) freq);
    1.79  
    1.80 -				if ((song->synth = fluidsynth.new_fluid_synth(settings))) {
    1.81 -					if (Mix_EachSoundFont(fluidsynth_load_soundfont, (void*) song->synth)) {
    1.82 -						if ((song->player = fluidsynth.new_fluid_player(song->synth))) {
    1.83 -							if (function(song, data)) return song;
    1.84 -							fluidsynth.delete_fluid_player(song->player);
    1.85 -						} else {
    1.86 -							Mix_SetError("Failed to create FluidSynth player");
    1.87 -						}
    1.88 -					}
    1.89 -					fluidsynth.delete_fluid_synth(song->synth);
    1.90 -				} else {
    1.91 -					Mix_SetError("Failed to create FluidSynth synthesizer");
    1.92 -				}
    1.93 -				fluidsynth.delete_fluid_settings(settings);
    1.94 -			} else {
    1.95 -				Mix_SetError("Failed to create FluidSynth settings");
    1.96 -			}
    1.97 -		} else {
    1.98 -			Mix_SetError("Failed to set up audio conversion");
    1.99 -		}
   1.100 -		SDL_free(song);
   1.101 -	} else {
   1.102 -		Mix_SetError("Insufficient memory for song");
   1.103 -	}
   1.104 -	return NULL;
   1.105 +                if ((song->synth = fluidsynth.new_fluid_synth(settings))) {
   1.106 +                    if (Mix_EachSoundFont(fluidsynth_load_soundfont, (void*) song->synth)) {
   1.107 +                        if ((song->player = fluidsynth.new_fluid_player(song->synth))) {
   1.108 +                            if (function(song, data)) return song;
   1.109 +                            fluidsynth.delete_fluid_player(song->player);
   1.110 +                        } else {
   1.111 +                            Mix_SetError("Failed to create FluidSynth player");
   1.112 +                        }
   1.113 +                    }
   1.114 +                    fluidsynth.delete_fluid_synth(song->synth);
   1.115 +                } else {
   1.116 +                    Mix_SetError("Failed to create FluidSynth synthesizer");
   1.117 +                }
   1.118 +                fluidsynth.delete_fluid_settings(settings);
   1.119 +            } else {
   1.120 +                Mix_SetError("Failed to create FluidSynth settings");
   1.121 +            }
   1.122 +        } else {
   1.123 +            Mix_SetError("Failed to set up audio conversion");
   1.124 +        }
   1.125 +        SDL_free(song);
   1.126 +    } else {
   1.127 +        Mix_SetError("Insufficient memory for song");
   1.128 +    }
   1.129 +    return NULL;
   1.130  }
   1.131  
   1.132  static int fluidsynth_loadsong_RW_internal(FluidSynthMidiSong *song, void *data)
   1.133  {
   1.134 -	off_t offset;
   1.135 -	size_t size;
   1.136 -	char *buffer;
   1.137 -	SDL_RWops *rw = (SDL_RWops*) data;
   1.138 +    off_t offset;
   1.139 +    size_t size;
   1.140 +    char *buffer;
   1.141 +    SDL_RWops *rw = (SDL_RWops*) data;
   1.142  
   1.143 -	offset = SDL_RWtell(rw);
   1.144 -	SDL_RWseek(rw, 0, RW_SEEK_END);
   1.145 -	size = SDL_RWtell(rw) - offset;
   1.146 -	SDL_RWseek(rw, offset, RW_SEEK_SET);
   1.147 +    offset = SDL_RWtell(rw);
   1.148 +    SDL_RWseek(rw, 0, RW_SEEK_END);
   1.149 +    size = SDL_RWtell(rw) - offset;
   1.150 +    SDL_RWseek(rw, offset, RW_SEEK_SET);
   1.151  
   1.152 -	if ((buffer = (char*) SDL_malloc(size))) {
   1.153 -		if(SDL_RWread(rw, buffer, size, 1) == 1) {
   1.154 -			if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
   1.155 -				return 1;
   1.156 -			} else {
   1.157 -				Mix_SetError("FluidSynth failed to load in-memory song");
   1.158 -			}
   1.159 -		} else {
   1.160 -			Mix_SetError("Failed to read in-memory song");
   1.161 -		}
   1.162 -		SDL_free(buffer);
   1.163 -	} else {
   1.164 -		Mix_SetError("Insufficient memory for song");
   1.165 -	}
   1.166 -	return 0;
   1.167 +    if ((buffer = (char*) SDL_malloc(size))) {
   1.168 +        if(SDL_RWread(rw, buffer, size, 1) == 1) {
   1.169 +            if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
   1.170 +                return 1;
   1.171 +            } else {
   1.172 +                Mix_SetError("FluidSynth failed to load in-memory song");
   1.173 +            }
   1.174 +        } else {
   1.175 +            Mix_SetError("Failed to read in-memory song");
   1.176 +        }
   1.177 +        SDL_free(buffer);
   1.178 +    } else {
   1.179 +        Mix_SetError("Insufficient memory for song");
   1.180 +    }
   1.181 +    return 0;
   1.182  }
   1.183  
   1.184  FluidSynthMidiSong *fluidsynth_loadsong_RW(SDL_RWops *rw, int freerw)
   1.185  {
   1.186 -	FluidSynthMidiSong *song;
   1.187 +    FluidSynthMidiSong *song;
   1.188  
   1.189 -	song = fluidsynth_loadsong_common(fluidsynth_loadsong_RW_internal, (void*) rw);
   1.190 -	if (freerw) {
   1.191 -		SDL_RWclose(rw);
   1.192 -	}
   1.193 -	return song;
   1.194 +    song = fluidsynth_loadsong_common(fluidsynth_loadsong_RW_internal, (void*) rw);
   1.195 +    if (freerw) {
   1.196 +        SDL_RWclose(rw);
   1.197 +    }
   1.198 +    return song;
   1.199  }
   1.200  
   1.201  void fluidsynth_freesong(FluidSynthMidiSong *song)
   1.202  {
   1.203 -	if (!song) return;
   1.204 -	fluidsynth.delete_fluid_player(song->player);
   1.205 -	fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth));
   1.206 -	fluidsynth.delete_fluid_synth(song->synth);
   1.207 -	SDL_free(song);
   1.208 +    if (!song) return;
   1.209 +    fluidsynth.delete_fluid_player(song->player);
   1.210 +    fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth));
   1.211 +    fluidsynth.delete_fluid_synth(song->synth);
   1.212 +    SDL_free(song);
   1.213  }
   1.214  
   1.215  void fluidsynth_start(FluidSynthMidiSong *song)
   1.216  {
   1.217 -	fluidsynth.fluid_player_set_loop(song->player, 1);
   1.218 -	fluidsynth.fluid_player_play(song->player);
   1.219 +    fluidsynth.fluid_player_set_loop(song->player, 1);
   1.220 +    fluidsynth.fluid_player_play(song->player);
   1.221  }
   1.222  
   1.223  void fluidsynth_stop(FluidSynthMidiSong *song)
   1.224  {
   1.225 -	fluidsynth.fluid_player_stop(song->player);
   1.226 +    fluidsynth.fluid_player_stop(song->player);
   1.227  }
   1.228  
   1.229  int fluidsynth_active(FluidSynthMidiSong *song)
   1.230  {
   1.231 -	return fluidsynth.fluid_player_get_status(song->player) == FLUID_PLAYER_PLAYING ? 1 : 0;
   1.232 +    return fluidsynth.fluid_player_get_status(song->player) == FLUID_PLAYER_PLAYING ? 1 : 0;
   1.233  }
   1.234  
   1.235  void fluidsynth_setvolume(FluidSynthMidiSong *song, int volume)
   1.236  {
   1.237 -	/* FluidSynth's default is 0.2. Make 1.2 the maximum. */
   1.238 -	fluidsynth.fluid_synth_set_gain(song->synth, (float) (volume * 1.2 / MIX_MAX_VOLUME));
   1.239 +    /* FluidSynth's default is 0.2. Make 1.2 the maximum. */
   1.240 +    fluidsynth.fluid_synth_set_gain(song->synth, (float) (volume * 1.2 / MIX_MAX_VOLUME));
   1.241  }
   1.242  
   1.243  int fluidsynth_playsome(FluidSynthMidiSong *song, void *dest, int dest_len)
   1.244  {
   1.245 -	int result = -1;
   1.246 -	int frames = dest_len / channels / ((format & 0xFF) / 8);
   1.247 -	int src_len = frames * 4; /* 16-bit stereo */
   1.248 -	void *src = dest;
   1.249 +    int result = -1;
   1.250 +    int frames = dest_len / channels / ((format & 0xFF) / 8);
   1.251 +    int src_len = frames * 4; /* 16-bit stereo */
   1.252 +    void *src = dest;
   1.253  
   1.254 -	if (dest_len < src_len) {
   1.255 -		if (!(src = SDL_malloc(src_len))) {
   1.256 -			Mix_SetError("Insufficient memory for audio conversion");
   1.257 -			return result;
   1.258 -		}
   1.259 -	}
   1.260 +    if (dest_len < src_len) {
   1.261 +        if (!(src = SDL_malloc(src_len))) {
   1.262 +            Mix_SetError("Insufficient memory for audio conversion");
   1.263 +            return result;
   1.264 +        }
   1.265 +    }
   1.266  
   1.267 -	if (fluidsynth.fluid_synth_write_s16(song->synth, frames, src, 0, 2, src, 1, 2) != FLUID_OK) {
   1.268 -		Mix_SetError("Error generating FluidSynth audio");
   1.269 -		goto finish;
   1.270 -	}
   1.271 +    if (fluidsynth.fluid_synth_write_s16(song->synth, frames, src, 0, 2, src, 1, 2) != FLUID_OK) {
   1.272 +        Mix_SetError("Error generating FluidSynth audio");
   1.273 +        goto finish;
   1.274 +    }
   1.275  
   1.276 -	song->convert.buf = src;
   1.277 -	song->convert.len = src_len;
   1.278 +    song->convert.buf = src;
   1.279 +    song->convert.len = src_len;
   1.280  
   1.281 -	if (SDL_ConvertAudio(&song->convert) < 0) {
   1.282 -		Mix_SetError("Error during audio conversion");
   1.283 -		goto finish;
   1.284 -	}
   1.285 +    if (SDL_ConvertAudio(&song->convert) < 0) {
   1.286 +        Mix_SetError("Error during audio conversion");
   1.287 +        goto finish;
   1.288 +    }
   1.289  
   1.290 -	if (src != dest)
   1.291 -		memcpy(dest, src, dest_len);
   1.292 +    if (src != dest)
   1.293 +        memcpy(dest, src, dest_len);
   1.294  
   1.295 -	result = 0;
   1.296 +    result = 0;
   1.297  
   1.298  finish:
   1.299 -	if (src != dest)
   1.300 -		SDL_free(src);
   1.301 +    if (src != dest)
   1.302 +        SDL_free(src);
   1.303  
   1.304 -	return result;
   1.305 +    return result;
   1.306  }
   1.307  
   1.308  #endif /* USE_FLUIDSYNTH_MIDI */