fluidsynth.c
changeset 561 87bdb4c81c0b
parent 545 32e5ed415a34
child 585 f2943eac3fd7
equal deleted inserted replaced
560:0fc8dffb86c5 561:87bdb4c81c0b
    73 
    73 
    74 	if (!Mix_Init(MIX_INIT_FLUIDSYNTH)) {
    74 	if (!Mix_Init(MIX_INIT_FLUIDSYNTH)) {
    75 		return NULL;
    75 		return NULL;
    76 	}
    76 	}
    77 
    77 
    78 	if ((song = malloc(sizeof(FluidSynthMidiSong)))) {
    78 	if ((song = SDL_malloc(sizeof(FluidSynthMidiSong)))) {
    79 		memset(song, 0, sizeof(FluidSynthMidiSong));
    79 		memset(song, 0, sizeof(FluidSynthMidiSong));
    80 
    80 
    81 		if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) {
    81 		if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) {
    82 			if ((settings = fluidsynth.new_fluid_settings())) {
    82 			if ((settings = fluidsynth.new_fluid_settings())) {
    83 				fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) freq);
    83 				fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) freq);
   100 				Mix_SetError("Failed to create FluidSynth settings");
   100 				Mix_SetError("Failed to create FluidSynth settings");
   101 			}
   101 			}
   102 		} else {
   102 		} else {
   103 			Mix_SetError("Failed to set up audio conversion");
   103 			Mix_SetError("Failed to set up audio conversion");
   104 		}
   104 		}
   105 		free(song);
   105 		SDL_free(song);
   106 	} else {
   106 	} else {
   107 		Mix_SetError("Insufficient memory for song");
   107 		Mix_SetError("Insufficient memory for song");
   108 	}
   108 	}
   109 	return NULL;
   109 	return NULL;
   110 }
   110 }
   119 	offset = SDL_RWtell(rw);
   119 	offset = SDL_RWtell(rw);
   120 	SDL_RWseek(rw, 0, RW_SEEK_END);
   120 	SDL_RWseek(rw, 0, RW_SEEK_END);
   121 	size = SDL_RWtell(rw) - offset;
   121 	size = SDL_RWtell(rw) - offset;
   122 	SDL_RWseek(rw, offset, RW_SEEK_SET);
   122 	SDL_RWseek(rw, offset, RW_SEEK_SET);
   123 
   123 
   124 	if ((buffer = (char*) malloc(size))) {
   124 	if ((buffer = (char*) SDL_malloc(size))) {
   125 		if(SDL_RWread(rw, buffer, size, 1) == 1) {
   125 		if(SDL_RWread(rw, buffer, size, 1) == 1) {
   126 			if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
   126 			if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
   127 				return 1;
   127 				return 1;
   128 			} else {
   128 			} else {
   129 				Mix_SetError("FluidSynth failed to load in-memory song");
   129 				Mix_SetError("FluidSynth failed to load in-memory song");
   130 			}
   130 			}
   131 		} else {
   131 		} else {
   132 			Mix_SetError("Failed to read in-memory song");
   132 			Mix_SetError("Failed to read in-memory song");
   133 		}
   133 		}
   134 		free(buffer);
   134 		SDL_free(buffer);
   135 	} else {
   135 	} else {
   136 		Mix_SetError("Insufficient memory for song");
   136 		Mix_SetError("Insufficient memory for song");
   137 	}
   137 	}
   138 	return 0;
   138 	return 0;
   139 }
   139 }
   153 {
   153 {
   154 	if (!song) return;
   154 	if (!song) return;
   155 	fluidsynth.delete_fluid_player(song->player);
   155 	fluidsynth.delete_fluid_player(song->player);
   156 	fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth));
   156 	fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth));
   157 	fluidsynth.delete_fluid_synth(song->synth);
   157 	fluidsynth.delete_fluid_synth(song->synth);
   158 	free(song);
   158 	SDL_free(song);
   159 }
   159 }
   160 
   160 
   161 void fluidsynth_start(FluidSynthMidiSong *song)
   161 void fluidsynth_start(FluidSynthMidiSong *song)
   162 {
   162 {
   163 	fluidsynth.fluid_player_set_loop(song->player, 1);
   163 	fluidsynth.fluid_player_set_loop(song->player, 1);
   186 	int frames = dest_len / channels / ((format & 0xFF) / 8);
   186 	int frames = dest_len / channels / ((format & 0xFF) / 8);
   187 	int src_len = frames * 4; /* 16-bit stereo */
   187 	int src_len = frames * 4; /* 16-bit stereo */
   188 	void *src = dest;
   188 	void *src = dest;
   189 
   189 
   190 	if (dest_len < src_len) {
   190 	if (dest_len < src_len) {
   191 		if (!(src = malloc(src_len))) {
   191 		if (!(src = SDL_malloc(src_len))) {
   192 			Mix_SetError("Insufficient memory for audio conversion");
   192 			Mix_SetError("Insufficient memory for audio conversion");
   193 			return result;
   193 			return result;
   194 		}
   194 		}
   195 	}
   195 	}
   196 
   196 
   212 
   212 
   213 	result = 0;
   213 	result = 0;
   214 
   214 
   215 finish:
   215 finish:
   216 	if (src != dest)
   216 	if (src != dest)
   217 		free(src);
   217 		SDL_free(src);
   218 
   218 
   219 	return result;
   219 	return result;
   220 }
   220 }
   221 
   221 
   222 #endif /* USE_FLUIDSYNTH_MIDI */
   222 #endif /* USE_FLUIDSYNTH_MIDI */