/
music_fluidsynth.c
309 lines (267 loc) · 10.5 KB
1
/*
2
SDL_mixer: An audio mixer library based on the SDL library
3
Copyright (C) 1997-2018 Sam Lantinga <slouken@libsdl.org>
4
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.
8
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:
12
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.
20
21
22
James Le Cuirot
chewi@aura-online.co.uk
23
24
*/
25
#ifdef MUSIC_MID_FLUIDSYNTH
26
27
28
#include <stdio.h>
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
64
#define FUNCTION_LOADER(FUNC, SIG) \
65
66
67
fluidsynth.FUNC = (SIG) SDL_LoadFunction(fluidsynth.handle, #FUNC); \
if (fluidsynth.FUNC == NULL) { SDL_UnloadObject(fluidsynth.handle); return -1; }
#else
68
#define FUNCTION_LOADER(FUNC, SIG) \
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);
77
78
79
if (fluidsynth.handle == NULL) {
return -1;
}
80
#endif
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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*))
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
}
++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 {
fluid_synth_t *synth;
120
121
122
123
124
fluid_player_t *player;
SDL_AudioStream *stream;
void *buffer;
int buffer_size;
} FLUIDSYNTH_Music;
125
126
127
static int SDLCALL fluidsynth_check_soundfont(const char *path, void *data)
128
{
129
130
131
132
133
134
135
136
137
FILE *file = fopen(path, "r");
if (file) {
fclose(file);
return 1;
} else {
Mix_SetError("Failed to access the SoundFont %s", path);
return 0;
}
138
139
}
140
static int SDLCALL fluidsynth_load_soundfont(const char *path, void *data)
141
{
142
143
144
/* 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;
145
146
}
147
static int FLUIDSYNTH_Open(const SDL_AudioSpec *spec)
148
{
149
if (!Mix_EachSoundFont(fluidsynth_check_soundfont, NULL)) {
150
return -1;
151
}
152
return 0;
153
154
}
155
static FLUIDSYNTH_Music *FLUIDSYNTH_LoadMusic(void *data)
156
{
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
SDL_RWops *src = (SDL_RWops *)data;
FLUIDSYNTH_Music *music;
fluid_settings_t *settings;
if ((music = SDL_calloc(1, sizeof(FLUIDSYNTH_Music)))) {
int channels = 2;
if ((music->stream = SDL_NewAudioStream(AUDIO_S16SYS, channels, music_spec.freq, music_spec.format, music_spec.channels, music_spec.freq))) {
music->buffer_size = music_spec.samples * sizeof(Sint16) * channels;
if ((music->buffer = SDL_malloc(music->buffer_size))) {
if ((settings = fluidsynth.new_fluid_settings())) {
fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) music_spec.freq);
if ((music->synth = fluidsynth.new_fluid_synth(settings))) {
if (Mix_EachSoundFont(fluidsynth_load_soundfont, (void*) music->synth)) {
if ((music->player = fluidsynth.new_fluid_player(music->synth))) {
void *buffer;
size_t size;
buffer = SDL_LoadFile_RW(src, &size, SDL_FALSE);
if (buffer) {
if (fluidsynth.fluid_player_add_mem(music->player, buffer, size) == FLUID_OK) {
SDL_free(buffer);
return music;
} else {
Mix_SetError("FluidSynth failed to load in-memory song");
}
SDL_free(buffer);
} else {
SDL_OutOfMemory();
}
fluidsynth.delete_fluid_player(music->player);
} else {
Mix_SetError("Failed to create FluidSynth player");
}
191
}
192
193
194
fluidsynth.delete_fluid_synth(music->synth);
} else {
Mix_SetError("Failed to create FluidSynth synthesizer");
195
}
196
fluidsynth.delete_fluid_settings(settings);
197
} else {
198
Mix_SetError("Failed to create FluidSynth settings");
199
200
}
} else {
201
SDL_OutOfMemory();
202
203
}
}
204
SDL_free(music);
205
} else {
206
SDL_OutOfMemory();
207
208
}
return NULL;
209
210
}
211
static void *FLUIDSYNTH_CreateFromRW(SDL_RWops *src, int freesrc)
212
{
213
FLUIDSYNTH_Music *music;
214
215
216
music = FLUIDSYNTH_LoadMusic(src);
if (music && freesrc) {
217
SDL_RWclose(src);
218
}
219
return music;
220
221
}
222
static void FLUIDSYNTH_SetVolume(void *context, int volume)
223
{
224
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
225
/* FluidSynth's default is 0.2. Make 1.2 the maximum. */
226
fluidsynth.fluid_synth_set_gain(music->synth, (float) (volume * 1.2 / MIX_MAX_VOLUME));
227
228
}
229
static int FLUIDSYNTH_Play(void *context, int play_count)
230
{
231
232
233
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
fluidsynth.fluid_player_set_loop(music->player, play_count);
fluidsynth.fluid_player_play(music->player);
234
return 0;
235
236
}
237
static SDL_bool FLUIDSYNTH_IsPlaying(void *context)
238
{
239
240
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
return fluidsynth.fluid_player_get_status(music->player) == FLUID_PLAYER_PLAYING ? SDL_TRUE : SDL_FALSE;
241
242
}
243
static int FLUIDSYNTH_GetSome(void *context, void *data, int bytes, SDL_bool *done)
244
{
245
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
246
247
248
249
250
int filled;
filled = SDL_AudioStreamGet(music->stream, data, bytes);
if (filled != 0) {
return filled;
251
}
252
253
if (fluidsynth.fluid_synth_write_s16(music->synth, music_spec.samples, music->buffer, 0, 2, music->buffer, 1, 2) != FLUID_OK) {
254
Mix_SetError("Error generating FluidSynth audio");
255
return -1;
256
}
257
258
if (SDL_AudioStreamPut(music->stream, music->buffer, music->buffer_size) < 0) {
return -1;
259
}
260
261
262
263
264
return 0;
}
static int FLUIDSYNTH_GetAudio(void *context, void *data, int bytes)
{
return music_pcm_getaudio(context, data, bytes, MIX_MAX_VOLUME, FLUIDSYNTH_GetSome);
265
}
266
267
268
static void FLUIDSYNTH_Stop(void *context)
{
269
270
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
fluidsynth.fluid_player_stop(music->player);
271
272
273
274
}
static void FLUIDSYNTH_Delete(void *context)
{
275
276
277
278
279
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
fluidsynth.delete_fluid_player(music->player);
fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(music->synth));
fluidsynth.delete_fluid_synth(music->synth);
SDL_free(music);
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
}
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: */