chewi@506
|
1 |
/*
|
slouken@518
|
2 |
SDL_mixer: An audio mixer library based on the SDL library
|
slouken@725
|
3 |
Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
|
chewi@506
|
4 |
|
slouken@518
|
5 |
This software is provided 'as-is', without any express or implied
|
slouken@518
|
6 |
warranty. In no event will the authors be held liable for any damages
|
slouken@518
|
7 |
arising from the use of this software.
|
chewi@506
|
8 |
|
slouken@518
|
9 |
Permission is granted to anyone to use this software for any purpose,
|
slouken@518
|
10 |
including commercial applications, and to alter it and redistribute it
|
slouken@518
|
11 |
freely, subject to the following restrictions:
|
chewi@506
|
12 |
|
slouken@518
|
13 |
1. The origin of this software must not be misrepresented; you must not
|
slouken@518
|
14 |
claim that you wrote the original software. If you use this software
|
slouken@518
|
15 |
in a product, an acknowledgment in the product documentation would be
|
slouken@518
|
16 |
appreciated but is not required.
|
slouken@518
|
17 |
2. Altered source versions must be plainly marked as such, and must not be
|
slouken@518
|
18 |
misrepresented as being the original software.
|
slouken@518
|
19 |
3. This notice may not be removed or altered from any source distribution.
|
chewi@506
|
20 |
|
slouken@518
|
21 |
James Le Cuirot
|
slouken@518
|
22 |
chewi@aura-online.co.uk
|
chewi@506
|
23 |
*/
|
chewi@506
|
24 |
|
slouken@777
|
25 |
#ifdef MUSIC_MID_FLUIDSYNTH
|
slouken@532
|
26 |
|
chewi@506
|
27 |
#include <stdio.h>
|
chewi@506
|
28 |
|
slouken@777
|
29 |
#include "SDL_loadso.h"
|
slouken@777
|
30 |
|
slouken@777
|
31 |
#include "music_fluidsynth.h"
|
slouken@777
|
32 |
|
slouken@777
|
33 |
#include <fluidsynth.h>
|
slouken@777
|
34 |
|
slouken@777
|
35 |
|
slouken@777
|
36 |
typedef struct {
|
slouken@777
|
37 |
int loaded;
|
slouken@777
|
38 |
void *handle;
|
slouken@777
|
39 |
|
slouken@777
|
40 |
int (*delete_fluid_player)(fluid_player_t*);
|
slouken@777
|
41 |
void (*delete_fluid_settings)(fluid_settings_t*);
|
slouken@777
|
42 |
int (*delete_fluid_synth)(fluid_synth_t*);
|
slouken@777
|
43 |
int (*fluid_player_add)(fluid_player_t*, const char*);
|
slouken@777
|
44 |
int (*fluid_player_add_mem)(fluid_player_t*, const void*, size_t);
|
slouken@777
|
45 |
int (*fluid_player_get_status)(fluid_player_t*);
|
slouken@777
|
46 |
int (*fluid_player_play)(fluid_player_t*);
|
slouken@777
|
47 |
int (*fluid_player_set_loop)(fluid_player_t*, int);
|
slouken@777
|
48 |
int (*fluid_player_stop)(fluid_player_t*);
|
slouken@777
|
49 |
int (*fluid_settings_setnum)(fluid_settings_t*, const char*, double);
|
slouken@777
|
50 |
fluid_settings_t* (*fluid_synth_get_settings)(fluid_synth_t*);
|
slouken@777
|
51 |
void (*fluid_synth_set_gain)(fluid_synth_t*, float);
|
slouken@777
|
52 |
int (*fluid_synth_sfload)(fluid_synth_t*, const char*, int);
|
slouken@777
|
53 |
int (*fluid_synth_write_s16)(fluid_synth_t*, int, void*, int, int, void*, int, int);
|
slouken@777
|
54 |
fluid_player_t* (*new_fluid_player)(fluid_synth_t*);
|
slouken@777
|
55 |
fluid_settings_t* (*new_fluid_settings)(void);
|
slouken@777
|
56 |
fluid_synth_t* (*new_fluid_synth)(fluid_settings_t*);
|
slouken@777
|
57 |
} fluidsynth_loader;
|
slouken@777
|
58 |
|
slouken@777
|
59 |
static fluidsynth_loader fluidsynth = {
|
slouken@777
|
60 |
0, NULL
|
slouken@777
|
61 |
};
|
slouken@777
|
62 |
|
slouken@777
|
63 |
#ifdef FLUIDSYNTH_DYNAMIC
|
slouken@786
|
64 |
#define FUNCTION_LOADER(FUNC, SIG) \
|
slouken@777
|
65 |
fluidsynth.FUNC = (SIG) SDL_LoadFunction(fluidsynth.handle, #FUNC); \
|
slouken@777
|
66 |
if (fluidsynth.FUNC == NULL) { SDL_UnloadObject(fluidsynth.handle); return -1; }
|
slouken@777
|
67 |
#else
|
slouken@786
|
68 |
#define FUNCTION_LOADER(FUNC, SIG) \
|
slouken@777
|
69 |
fluidsynth.FUNC = FUNC;
|
slouken@777
|
70 |
#endif
|
slouken@777
|
71 |
|
slouken@777
|
72 |
static int FLUIDSYNTH_Load()
|
slouken@777
|
73 |
{
|
slouken@777
|
74 |
if (fluidsynth.loaded == 0) {
|
slouken@777
|
75 |
#ifdef FLUIDSYNTH_DYNAMIC
|
slouken@777
|
76 |
fluidsynth.handle = SDL_LoadObject(FLUIDSYNTH_DYNAMIC);
|
slouken@786
|
77 |
if (fluidsynth.handle == NULL) {
|
slouken@786
|
78 |
return -1;
|
slouken@786
|
79 |
}
|
slouken@777
|
80 |
#endif
|
slouken@786
|
81 |
FUNCTION_LOADER(delete_fluid_player, int (*)(fluid_player_t*))
|
slouken@786
|
82 |
FUNCTION_LOADER(delete_fluid_settings, void (*)(fluid_settings_t*))
|
slouken@786
|
83 |
FUNCTION_LOADER(delete_fluid_synth, int (*)(fluid_synth_t*))
|
slouken@786
|
84 |
FUNCTION_LOADER(fluid_player_add, int (*)(fluid_player_t*, const char*))
|
slouken@786
|
85 |
FUNCTION_LOADER(fluid_player_add_mem, int (*)(fluid_player_t*, const void*, size_t))
|
slouken@786
|
86 |
FUNCTION_LOADER(fluid_player_get_status, int (*)(fluid_player_t*))
|
slouken@786
|
87 |
FUNCTION_LOADER(fluid_player_play, int (*)(fluid_player_t*))
|
slouken@786
|
88 |
FUNCTION_LOADER(fluid_player_set_loop, int (*)(fluid_player_t*, int))
|
slouken@786
|
89 |
FUNCTION_LOADER(fluid_player_stop, int (*)(fluid_player_t*))
|
slouken@786
|
90 |
FUNCTION_LOADER(fluid_settings_setnum, int (*)(fluid_settings_t*, const char*, double))
|
slouken@786
|
91 |
FUNCTION_LOADER(fluid_synth_get_settings, fluid_settings_t* (*)(fluid_synth_t*))
|
slouken@786
|
92 |
FUNCTION_LOADER(fluid_synth_set_gain, void (*)(fluid_synth_t*, float))
|
slouken@786
|
93 |
FUNCTION_LOADER(fluid_synth_sfload, int(*)(fluid_synth_t*, const char*, int))
|
slouken@786
|
94 |
FUNCTION_LOADER(fluid_synth_write_s16, int(*)(fluid_synth_t*, int, void*, int, int, void*, int, int))
|
slouken@786
|
95 |
FUNCTION_LOADER(new_fluid_player, fluid_player_t* (*)(fluid_synth_t*))
|
slouken@786
|
96 |
FUNCTION_LOADER(new_fluid_settings, fluid_settings_t* (*)(void))
|
slouken@786
|
97 |
FUNCTION_LOADER(new_fluid_synth, fluid_synth_t* (*)(fluid_settings_t*))
|
slouken@777
|
98 |
}
|
slouken@777
|
99 |
++fluidsynth.loaded;
|
slouken@777
|
100 |
|
slouken@777
|
101 |
return 0;
|
slouken@777
|
102 |
}
|
slouken@777
|
103 |
|
slouken@777
|
104 |
static void FLUIDSYNTH_Unload()
|
slouken@777
|
105 |
{
|
slouken@777
|
106 |
if (fluidsynth.loaded == 0) {
|
slouken@777
|
107 |
return;
|
slouken@777
|
108 |
}
|
slouken@777
|
109 |
if (fluidsynth.loaded == 1) {
|
slouken@777
|
110 |
#ifdef FLUIDSYNTH_DYNAMIC
|
slouken@777
|
111 |
SDL_UnloadObject(fluidsynth.handle);
|
slouken@777
|
112 |
#endif
|
slouken@777
|
113 |
}
|
slouken@777
|
114 |
--fluidsynth.loaded;
|
slouken@777
|
115 |
}
|
slouken@777
|
116 |
|
slouken@777
|
117 |
|
slouken@777
|
118 |
typedef struct {
|
slouken@777
|
119 |
fluid_synth_t *synth;
|
slouken@797
|
120 |
fluid_player_t *player;
|
slouken@797
|
121 |
SDL_AudioStream *stream;
|
slouken@797
|
122 |
void *buffer;
|
slouken@797
|
123 |
int buffer_size;
|
slouken@797
|
124 |
} FLUIDSYNTH_Music;
|
slouken@532
|
125 |
|
chewi@506
|
126 |
|
slouken@777
|
127 |
static int fluidsynth_check_soundfont(const char *path, void *data)
|
chewi@506
|
128 |
{
|
slouken@617
|
129 |
FILE *file = fopen(path, "r");
|
chewi@506
|
130 |
|
slouken@617
|
131 |
if (file) {
|
slouken@617
|
132 |
fclose(file);
|
slouken@617
|
133 |
return 1;
|
slouken@617
|
134 |
} else {
|
slouken@617
|
135 |
Mix_SetError("Failed to access the SoundFont %s", path);
|
slouken@617
|
136 |
return 0;
|
slouken@617
|
137 |
}
|
chewi@506
|
138 |
}
|
chewi@506
|
139 |
|
slouken@777
|
140 |
static int fluidsynth_load_soundfont(const char *path, void *data)
|
chewi@506
|
141 |
{
|
slouken@617
|
142 |
/* If this fails, it's too late to try Timidity so pray that at least one works. */
|
slouken@617
|
143 |
fluidsynth.fluid_synth_sfload((fluid_synth_t*) data, path, 1);
|
slouken@617
|
144 |
return 1;
|
chewi@506
|
145 |
}
|
chewi@506
|
146 |
|
slouken@777
|
147 |
static int FLUIDSYNTH_Open(const SDL_AudioSpec *spec)
|
chewi@506
|
148 |
{
|
slouken@777
|
149 |
if (!Mix_EachSoundFont(fluidsynth_check_soundfont, NULL)) {
|
slouken@617
|
150 |
return -1;
|
slouken@777
|
151 |
}
|
slouken@617
|
152 |
return 0;
|
chewi@506
|
153 |
}
|
chewi@506
|
154 |
|
slouken@797
|
155 |
static FLUIDSYNTH_Music *FLUIDSYNTH_LoadMusic(void *data)
|
chewi@506
|
156 |
{
|
slouken@797
|
157 |
SDL_RWops *src = (SDL_RWops *)data;
|
slouken@797
|
158 |
FLUIDSYNTH_Music *music;
|
slouken@797
|
159 |
fluid_settings_t *settings;
|
chewi@506
|
160 |
|
slouken@797
|
161 |
if ((music = SDL_calloc(1, sizeof(FLUIDSYNTH_Music)))) {
|
slouken@797
|
162 |
int channels = 2;
|
slouken@797
|
163 |
if ((music->stream = SDL_NewAudioStream(AUDIO_S16SYS, channels, music_spec.freq, music_spec.format, music_spec.channels, music_spec.freq))) {
|
slouken@797
|
164 |
music->buffer_size = music_spec.samples * sizeof(Sint16) * channels;
|
slouken@797
|
165 |
if ((music->buffer = SDL_malloc(music->buffer_size))) {
|
slouken@797
|
166 |
if ((settings = fluidsynth.new_fluid_settings())) {
|
slouken@797
|
167 |
fluidsynth.fluid_settings_setnum(settings, "synth.sample-rate", (double) music_spec.freq);
|
chewi@506
|
168 |
|
slouken@797
|
169 |
if ((music->synth = fluidsynth.new_fluid_synth(settings))) {
|
slouken@797
|
170 |
if (Mix_EachSoundFont(fluidsynth_load_soundfont, (void*) music->synth)) {
|
slouken@797
|
171 |
if ((music->player = fluidsynth.new_fluid_player(music->synth))) {
|
slouken@797
|
172 |
void *buffer;
|
slouken@797
|
173 |
size_t size;
|
slouken@797
|
174 |
|
slouken@797
|
175 |
buffer = SDL_LoadFile_RW(src, &size, SDL_FALSE);
|
slouken@797
|
176 |
if (buffer) {
|
slouken@797
|
177 |
if (fluidsynth.fluid_player_add_mem(music->player, buffer, size) == FLUID_OK) {
|
slouken@797
|
178 |
SDL_free(buffer);
|
slouken@797
|
179 |
return music;
|
slouken@797
|
180 |
} else {
|
slouken@797
|
181 |
Mix_SetError("FluidSynth failed to load in-memory song");
|
slouken@797
|
182 |
}
|
slouken@797
|
183 |
SDL_free(buffer);
|
slouken@797
|
184 |
} else {
|
slouken@797
|
185 |
SDL_OutOfMemory();
|
slouken@797
|
186 |
}
|
slouken@797
|
187 |
fluidsynth.delete_fluid_player(music->player);
|
slouken@797
|
188 |
} else {
|
slouken@797
|
189 |
Mix_SetError("Failed to create FluidSynth player");
|
slouken@797
|
190 |
}
|
slouken@617
|
191 |
}
|
slouken@797
|
192 |
fluidsynth.delete_fluid_synth(music->synth);
|
slouken@797
|
193 |
} else {
|
slouken@797
|
194 |
Mix_SetError("Failed to create FluidSynth synthesizer");
|
slouken@617
|
195 |
}
|
slouken@797
|
196 |
fluidsynth.delete_fluid_settings(settings);
|
slouken@617
|
197 |
} else {
|
slouken@797
|
198 |
Mix_SetError("Failed to create FluidSynth settings");
|
slouken@617
|
199 |
}
|
slouken@617
|
200 |
} else {
|
slouken@797
|
201 |
SDL_OutOfMemory();
|
slouken@617
|
202 |
}
|
slouken@617
|
203 |
}
|
slouken@797
|
204 |
SDL_free(music);
|
slouken@617
|
205 |
} else {
|
slouken@797
|
206 |
SDL_OutOfMemory();
|
slouken@617
|
207 |
}
|
slouken@617
|
208 |
return NULL;
|
chewi@506
|
209 |
}
|
chewi@506
|
210 |
|
slouken@777
|
211 |
static void *FLUIDSYNTH_CreateFromRW(SDL_RWops *src, int freesrc)
|
chewi@506
|
212 |
{
|
slouken@797
|
213 |
FLUIDSYNTH_Music *music;
|
slouken@521
|
214 |
|
slouken@797
|
215 |
music = FLUIDSYNTH_LoadMusic(src);
|
slouken@797
|
216 |
if (music && freesrc) {
|
slouken@625
|
217 |
SDL_RWclose(src);
|
slouken@617
|
218 |
}
|
slouken@797
|
219 |
return music;
|
chewi@506
|
220 |
}
|
chewi@506
|
221 |
|
slouken@777
|
222 |
static void FLUIDSYNTH_SetVolume(void *context, int volume)
|
chewi@506
|
223 |
{
|
slouken@797
|
224 |
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
|
slouken@617
|
225 |
/* FluidSynth's default is 0.2. Make 1.2 the maximum. */
|
slouken@797
|
226 |
fluidsynth.fluid_synth_set_gain(music->synth, (float) (volume * 1.2 / MIX_MAX_VOLUME));
|
chewi@506
|
227 |
}
|
chewi@506
|
228 |
|
slouken@797
|
229 |
static int FLUIDSYNTH_Play(void *context, int play_count)
|
slouken@777
|
230 |
{
|
slouken@797
|
231 |
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
|
slouken@797
|
232 |
fluidsynth.fluid_player_set_loop(music->player, play_count);
|
slouken@797
|
233 |
fluidsynth.fluid_player_play(music->player);
|
slouken@777
|
234 |
return 0;
|
slouken@777
|
235 |
}
|
slouken@777
|
236 |
|
slouken@777
|
237 |
static SDL_bool FLUIDSYNTH_IsPlaying(void *context)
|
slouken@777
|
238 |
{
|
slouken@797
|
239 |
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
|
slouken@797
|
240 |
return fluidsynth.fluid_player_get_status(music->player) == FLUID_PLAYER_PLAYING ? SDL_TRUE : SDL_FALSE;
|
slouken@777
|
241 |
}
|
slouken@777
|
242 |
|
slouken@797
|
243 |
static int FLUIDSYNTH_GetSome(void *context, void *data, int bytes, SDL_bool *done)
|
slouken@797
|
244 |
{
|
slouken@797
|
245 |
int filled;
|
slouken@797
|
246 |
|
slouken@797
|
247 |
filled = SDL_AudioStreamGet(music->stream, data, bytes);
|
slouken@797
|
248 |
if (filled != 0) {
|
slouken@797
|
249 |
return filled;
|
slouken@797
|
250 |
}
|
slouken@797
|
251 |
|
slouken@797
|
252 |
/* FIXME: What happens at end of song? */
|
slouken@797
|
253 |
if (fluidsynth.fluid_synth_write_s16(music->synth, mixer_spec.samples, music->buffer, 0, 2, music->buffer, 1, 2) != FLUID_OK) {
|
slouken@797
|
254 |
Mix_SetError("Error generating FluidSynth audio");
|
slouken@797
|
255 |
return -1;
|
slouken@797
|
256 |
}
|
slouken@797
|
257 |
if (SDL_AudioStreamPut(music->stream, music->buffer, music->buffer_size) < 0) {
|
slouken@797
|
258 |
return -1;
|
slouken@797
|
259 |
}
|
slouken@797
|
260 |
return 0;
|
slouken@797
|
261 |
}
|
slouken@777
|
262 |
static int FLUIDSYNTH_GetAudio(void *context, void *data, int bytes)
|
chewi@506
|
263 |
{
|
slouken@797
|
264 |
return music_pcm_getaudio(context, data, bytes, MIX_MAX_VOLUME, FLUIDSYNTH_GetSome);
|
chewi@506
|
265 |
}
|
slouken@532
|
266 |
|
slouken@777
|
267 |
static void FLUIDSYNTH_Stop(void *context)
|
slouken@777
|
268 |
{
|
slouken@797
|
269 |
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
|
slouken@797
|
270 |
fluidsynth.fluid_player_stop(music->player);
|
slouken@777
|
271 |
}
|
slouken@777
|
272 |
|
slouken@777
|
273 |
static void FLUIDSYNTH_Delete(void *context)
|
slouken@777
|
274 |
{
|
slouken@797
|
275 |
FLUIDSYNTH_Music *music = (FLUIDSYNTH_Music *)context;
|
slouken@797
|
276 |
fluidsynth.delete_fluid_player(music->player);
|
slouken@797
|
277 |
fluidsynth.delete_fluid_settings(fluidsynth.fluid_synth_get_settings(music->synth));
|
slouken@797
|
278 |
fluidsynth.delete_fluid_synth(music->synth);
|
slouken@797
|
279 |
SDL_free(music);
|
slouken@777
|
280 |
}
|
slouken@777
|
281 |
|
slouken@777
|
282 |
Mix_MusicInterface Mix_MusicInterface_FLUIDSYNTH =
|
slouken@777
|
283 |
{
|
slouken@777
|
284 |
"FLUIDSYNTH",
|
slouken@777
|
285 |
MIX_MUSIC_FLUIDSYNTH,
|
slouken@777
|
286 |
MUS_MID,
|
slouken@777
|
287 |
SDL_FALSE,
|
slouken@777
|
288 |
SDL_FALSE,
|
slouken@777
|
289 |
|
slouken@777
|
290 |
FLUIDSYNTH_Load,
|
slouken@777
|
291 |
FLUIDSYNTH_Open,
|
slouken@777
|
292 |
FLUIDSYNTH_CreateFromRW,
|
slouken@777
|
293 |
NULL, /* CreateFromFile */
|
slouken@777
|
294 |
FLUIDSYNTH_SetVolume,
|
slouken@777
|
295 |
FLUIDSYNTH_Play,
|
slouken@777
|
296 |
FLUIDSYNTH_IsPlaying,
|
slouken@777
|
297 |
FLUIDSYNTH_GetAudio,
|
slouken@777
|
298 |
NULL, /* Seek */
|
slouken@777
|
299 |
NULL, /* Pause */
|
slouken@777
|
300 |
NULL, /* Resume */
|
slouken@777
|
301 |
FLUIDSYNTH_Stop,
|
slouken@777
|
302 |
FLUIDSYNTH_Delete,
|
slouken@777
|
303 |
NULL, /* Close */
|
slouken@777
|
304 |
FLUIDSYNTH_Unload,
|
slouken@777
|
305 |
};
|
slouken@777
|
306 |
|
slouken@777
|
307 |
#endif /* MUSIC_MID_FLUIDSYNTH */
|
slouken@777
|
308 |
|
slouken@777
|
309 |
/* vi: set ts=4 sw=4 expandtab: */
|