Skip to content

Latest commit

 

History

History
342 lines (289 loc) · 10.7 KB

music_fluidsynth.c

File metadata and controls

342 lines (289 loc) · 10.7 KB
 
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Jan 2, 2017
Jan 2, 2017
3
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
Dec 31, 2011
Dec 31, 2011
5
6
7
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Dec 31, 2011
Dec 31, 2011
9
10
11
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
Dec 31, 2011
Dec 31, 2011
13
14
15
16
17
18
19
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Dec 31, 2011
Dec 31, 2011
21
22
James Le Cuirot
chewi@aura-online.co.uk
Oct 17, 2017
Oct 17, 2017
25
#ifdef MUSIC_MID_FLUIDSYNTH
Jan 1, 2012
Jan 1, 2012
26
Oct 17, 2017
Oct 17, 2017
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "SDL_loadso.h"
#include "music_fluidsynth.h"
#include <fluidsynth.h>
typedef struct {
int loaded;
void *handle;
int (*delete_fluid_player)(fluid_player_t*);
void (*delete_fluid_settings)(fluid_settings_t*);
int (*delete_fluid_synth)(fluid_synth_t*);
int (*fluid_player_add)(fluid_player_t*, const char*);
int (*fluid_player_add_mem)(fluid_player_t*, const void*, size_t);
int (*fluid_player_get_status)(fluid_player_t*);
int (*fluid_player_play)(fluid_player_t*);
int (*fluid_player_set_loop)(fluid_player_t*, int);
int (*fluid_player_stop)(fluid_player_t*);
int (*fluid_settings_setnum)(fluid_settings_t*, const char*, double);
fluid_settings_t* (*fluid_synth_get_settings)(fluid_synth_t*);
void (*fluid_synth_set_gain)(fluid_synth_t*, float);
int (*fluid_synth_sfload)(fluid_synth_t*, const char*, int);
int (*fluid_synth_write_s16)(fluid_synth_t*, int, void*, int, int, void*, int, int);
fluid_player_t* (*new_fluid_player)(fluid_synth_t*);
fluid_settings_t* (*new_fluid_settings)(void);
fluid_synth_t* (*new_fluid_synth)(fluid_settings_t*);
} fluidsynth_loader;
static fluidsynth_loader fluidsynth = {
0, NULL
};
#ifdef FLUIDSYNTH_DYNAMIC
Oct 18, 2017
Oct 18, 2017
64
#define FUNCTION_LOADER(FUNC, SIG) \
Oct 17, 2017
Oct 17, 2017
65
66
67
fluidsynth.FUNC = (SIG) SDL_LoadFunction(fluidsynth.handle, #FUNC); \
if (fluidsynth.FUNC == NULL) { SDL_UnloadObject(fluidsynth.handle); return -1; }
#else
Oct 18, 2017
Oct 18, 2017
68
#define FUNCTION_LOADER(FUNC, SIG) \
Oct 17, 2017
Oct 17, 2017
69
70
71
72
73
74
75
76
fluidsynth.FUNC = FUNC;
#endif
static int FLUIDSYNTH_Load()
{
if (fluidsynth.loaded == 0) {
#ifdef FLUIDSYNTH_DYNAMIC
fluidsynth.handle = SDL_LoadObject(FLUIDSYNTH_DYNAMIC);
Oct 18, 2017
Oct 18, 2017
77
78
79
if (fluidsynth.handle == NULL) {
return -1;
}
Oct 17, 2017
Oct 17, 2017
80
81
#endif
Oct 18, 2017
Oct 18, 2017
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
FUNCTION_LOADER(delete_fluid_player, int (*)(fluid_player_t*))
FUNCTION_LOADER(delete_fluid_settings, void (*)(fluid_settings_t*))
FUNCTION_LOADER(delete_fluid_synth, int (*)(fluid_synth_t*))
FUNCTION_LOADER(fluid_player_add, int (*)(fluid_player_t*, const char*))
FUNCTION_LOADER(fluid_player_add_mem, int (*)(fluid_player_t*, const void*, size_t))
FUNCTION_LOADER(fluid_player_get_status, int (*)(fluid_player_t*))
FUNCTION_LOADER(fluid_player_play, int (*)(fluid_player_t*))
FUNCTION_LOADER(fluid_player_set_loop, int (*)(fluid_player_t*, int))
FUNCTION_LOADER(fluid_player_stop, int (*)(fluid_player_t*))
FUNCTION_LOADER(fluid_settings_setnum, int (*)(fluid_settings_t*, const char*, double))
FUNCTION_LOADER(fluid_synth_get_settings, fluid_settings_t* (*)(fluid_synth_t*))
FUNCTION_LOADER(fluid_synth_set_gain, void (*)(fluid_synth_t*, float))
FUNCTION_LOADER(fluid_synth_sfload, int(*)(fluid_synth_t*, const char*, int))
FUNCTION_LOADER(fluid_synth_write_s16, int(*)(fluid_synth_t*, int, void*, int, int, void*, int, int))
FUNCTION_LOADER(new_fluid_player, fluid_player_t* (*)(fluid_synth_t*))
FUNCTION_LOADER(new_fluid_settings, fluid_settings_t* (*)(void))
FUNCTION_LOADER(new_fluid_synth, fluid_synth_t* (*)(fluid_settings_t*))
Oct 17, 2017
Oct 17, 2017
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
}
++fluidsynth.loaded;
return 0;
}
static void FLUIDSYNTH_Unload()
{
if (fluidsynth.loaded == 0) {
return;
}
if (fluidsynth.loaded == 1) {
#ifdef FLUIDSYNTH_DYNAMIC
SDL_UnloadObject(fluidsynth.handle);
#endif
}
--fluidsynth.loaded;
}
typedef struct {
SDL_AudioCVT convert;
fluid_synth_t *synth;
fluid_player_t* player;
} FluidSynthMidiSong;
Jan 1, 2012
Jan 1, 2012
124
125
126
127
128
static Uint16 format;
static Uint8 channels;
static int freq;
Oct 17, 2017
Oct 17, 2017
129
static int fluidsynth_check_soundfont(const char *path, void *data)
May 22, 2013
May 22, 2013
131
132
133
134
135
136
137
138
139
FILE *file = fopen(path, "r");
if (file) {
fclose(file);
return 1;
} else {
Mix_SetError("Failed to access the SoundFont %s", path);
return 0;
}
Oct 17, 2017
Oct 17, 2017
142
static int fluidsynth_load_soundfont(const char *path, void *data)
May 22, 2013
May 22, 2013
144
145
146
/* If this fails, it's too late to try Timidity so pray that at least one works. */
fluidsynth.fluid_synth_sfload((fluid_synth_t*) data, path, 1);
return 1;
Oct 17, 2017
Oct 17, 2017
149
static int FLUIDSYNTH_Open(const SDL_AudioSpec *spec)
Oct 17, 2017
Oct 17, 2017
151
if (!Mix_EachSoundFont(fluidsynth_check_soundfont, NULL)) {
May 22, 2013
May 22, 2013
152
return -1;
Oct 17, 2017
Oct 17, 2017
153
}
Oct 17, 2017
Oct 17, 2017
155
156
157
format = spec->format;
channels = spec->channels;
freq = spec->freq;
May 22, 2013
May 22, 2013
159
return 0;
Jan 4, 2012
Jan 4, 2012
162
static FluidSynthMidiSong *fluidsynth_loadsong_common(int (*function)(FluidSynthMidiSong*, void*), void *data)
May 22, 2013
May 22, 2013
164
165
166
FluidSynthMidiSong *song;
fluid_settings_t *settings = NULL;
Oct 17, 2017
Oct 17, 2017
167
if ((song = SDL_calloc(1, sizeof(FluidSynthMidiSong)))) {
May 22, 2013
May 22, 2013
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
if (SDL_BuildAudioCVT(&song->convert, AUDIO_S16, 2, freq, format, channels, freq) >= 0) {
if ((settings = fluidsynth.new_fluid_settings())) {
fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) freq);
if ((song->synth = fluidsynth.new_fluid_synth(settings))) {
if (Mix_EachSoundFont(fluidsynth_load_soundfont, (void*) song->synth)) {
if ((song->player = fluidsynth.new_fluid_player(song->synth))) {
if (function(song, data)) return song;
fluidsynth.delete_fluid_player(song->player);
} else {
Mix_SetError("Failed to create FluidSynth player");
}
}
fluidsynth.delete_fluid_synth(song->synth);
} else {
Mix_SetError("Failed to create FluidSynth synthesizer");
}
fluidsynth.delete_fluid_settings(settings);
} else {
Mix_SetError("Failed to create FluidSynth settings");
}
} else {
Mix_SetError("Failed to set up audio conversion");
}
SDL_free(song);
} else {
Mix_SetError("Insufficient memory for song");
}
return NULL;
Jan 4, 2012
Jan 4, 2012
199
static int fluidsynth_loadsong_RW_internal(FluidSynthMidiSong *song, void *data)
Jun 2, 2013
Jun 2, 2013
201
Sint64 offset;
May 22, 2013
May 22, 2013
202
203
size_t size;
char *buffer;
Jun 2, 2013
Jun 2, 2013
204
SDL_RWops *src = (SDL_RWops*) data;
May 22, 2013
May 22, 2013
205
Jun 2, 2013
Jun 2, 2013
206
207
208
209
offset = SDL_RWtell(src);
SDL_RWseek(src, 0, RW_SEEK_END);
size = (size_t)(SDL_RWtell(src) - offset);
SDL_RWseek(src, offset, RW_SEEK_SET);
May 22, 2013
May 22, 2013
210
211
if ((buffer = (char*) SDL_malloc(size))) {
Jun 2, 2013
Jun 2, 2013
212
if(SDL_RWread(src, buffer, size, 1) == 1) {
May 22, 2013
May 22, 2013
213
if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
Jun 17, 2015
Jun 17, 2015
214
SDL_free(buffer);
May 22, 2013
May 22, 2013
215
216
217
218
219
220
221
222
223
224
225
226
return 1;
} else {
Mix_SetError("FluidSynth failed to load in-memory song");
}
} else {
Mix_SetError("Failed to read in-memory song");
}
SDL_free(buffer);
} else {
Mix_SetError("Insufficient memory for song");
}
return 0;
Oct 17, 2017
Oct 17, 2017
229
static void *FLUIDSYNTH_CreateFromRW(SDL_RWops *src, int freesrc)
May 22, 2013
May 22, 2013
231
FluidSynthMidiSong *song;
Dec 31, 2011
Dec 31, 2011
232
Jun 2, 2013
Jun 2, 2013
233
234
235
song = fluidsynth_loadsong_common(fluidsynth_loadsong_RW_internal, (void*) src);
if (song && freesrc) {
SDL_RWclose(src);
May 22, 2013
May 22, 2013
236
237
}
return song;
Oct 17, 2017
Oct 17, 2017
240
static void FLUIDSYNTH_SetVolume(void *context, int volume)
Oct 17, 2017
Oct 17, 2017
242
243
244
FluidSynthMidiSong *song = (FluidSynthMidiSong *)context;
/* FluidSynth's default is 0.2. Make 1.2 the maximum. */
fluidsynth.fluid_synth_set_gain(song->synth, (float) (volume * 1.2 / MIX_MAX_VOLUME));
Oct 17, 2017
Oct 17, 2017
247
static int FLUIDSYNTH_Play(void *context)
Oct 17, 2017
Oct 17, 2017
249
FluidSynthMidiSong *song = (FluidSynthMidiSong *)context;
May 22, 2013
May 22, 2013
250
251
fluidsynth.fluid_player_set_loop(song->player, 1);
fluidsynth.fluid_player_play(song->player);
Oct 17, 2017
Oct 17, 2017
252
return 0;
Oct 17, 2017
Oct 17, 2017
255
static SDL_bool FLUIDSYNTH_IsPlaying(void *context)
Oct 17, 2017
Oct 17, 2017
257
258
FluidSynthMidiSong *song = (FluidSynthMidiSong *)context;
return fluidsynth.fluid_player_get_status(song->player) == FLUID_PLAYER_PLAYING ? SDL_TRUE : SDL_FALSE;
Oct 17, 2017
Oct 17, 2017
261
static int FLUIDSYNTH_GetAudio(void *context, void *data, int bytes)
May 22, 2013
May 22, 2013
263
int result = -1;
Oct 17, 2017
Oct 17, 2017
264
int frames = bytes / channels / ((format & 0xFF) / 8);
May 22, 2013
May 22, 2013
265
266
int src_len = frames * 4; /* 16-bit stereo */
void *src = dest;
Oct 17, 2017
Oct 17, 2017
268
if (bytes < src_len) {
May 22, 2013
May 22, 2013
269
270
271
272
273
if (!(src = SDL_malloc(src_len))) {
Mix_SetError("Insufficient memory for audio conversion");
return result;
}
}
May 22, 2013
May 22, 2013
275
276
277
278
if (fluidsynth.fluid_synth_write_s16(song->synth, frames, src, 0, 2, src, 1, 2) != FLUID_OK) {
Mix_SetError("Error generating FluidSynth audio");
goto finish;
}
May 22, 2013
May 22, 2013
280
281
song->convert.buf = src;
song->convert.len = src_len;
May 22, 2013
May 22, 2013
283
284
285
286
if (SDL_ConvertAudio(&song->convert) < 0) {
Mix_SetError("Error during audio conversion");
goto finish;
}
May 22, 2013
May 22, 2013
288
if (src != dest)
Oct 17, 2017
Oct 17, 2017
289
SDL_memcpy(dest, src, bytes);
May 22, 2013
May 22, 2013
291
result = 0;
May 22, 2013
May 22, 2013
294
295
if (src != dest)
SDL_free(src);
May 22, 2013
May 22, 2013
297
return result;
Jan 1, 2012
Jan 1, 2012
299
Oct 17, 2017
Oct 17, 2017
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
static void FLUIDSYNTH_Stop(void *context)
{
FluidSynthMidiSong *song = (FluidSynthMidiSong *)context;
fluidsynth.fluid_player_stop(song->player);
}
static void FLUIDSYNTH_Delete(void *context)
{
FluidSynthMidiSong *song = (FluidSynthMidiSong *)context;
fluidsynth.delete_fluid_player(song->player);
fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(song->synth));
fluidsynth.delete_fluid_synth(song->synth);
SDL_free(song);
}
Mix_MusicInterface Mix_MusicInterface_FLUIDSYNTH =
{
"FLUIDSYNTH",
MIX_MUSIC_FLUIDSYNTH,
MUS_MID,
SDL_FALSE,
SDL_FALSE,
FLUIDSYNTH_Load,
FLUIDSYNTH_Open,
FLUIDSYNTH_CreateFromRW,
NULL, /* CreateFromFile */
FLUIDSYNTH_SetVolume,
FLUIDSYNTH_Play,
FLUIDSYNTH_IsPlaying,
FLUIDSYNTH_GetAudio,
NULL, /* Seek */
NULL, /* Pause */
NULL, /* Resume */
FLUIDSYNTH_Stop,
FLUIDSYNTH_Delete,
NULL, /* Close */
FLUIDSYNTH_Unload,
};
#endif /* MUSIC_MID_FLUIDSYNTH */
/* vi: set ts=4 sw=4 expandtab: */