Skip to content

Latest commit

 

History

History
222 lines (183 loc) · 6.54 KB

fluidsynth.c

File metadata and controls

222 lines (183 loc) · 6.54 KB
 
Dec 31, 2011
Dec 31, 2011
2
SDL_mixer: An audio mixer library based on the SDL library
Feb 15, 2013
Feb 15, 2013
3
Copyright (C) 1997-2013 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
Jan 1, 2012
Jan 1, 2012
25
26
#ifdef USE_FLUIDSYNTH_MIDI
27
28
29
#include <stdio.h>
#include <sys/types.h>
Jan 1, 2012
Jan 1, 2012
30
31
32
#include "SDL_mixer.h"
#include "fluidsynth.h"
33
34
35
36
37
38
static Uint16 format;
static Uint8 channels;
static int freq;
int fluidsynth_check_soundfont(const char *path, void *data)
{
May 22, 2013
May 22, 2013
39
40
41
42
43
44
45
46
47
FILE *file = fopen(path, "r");
if (file) {
fclose(file);
return 1;
} else {
Mix_SetError("Failed to access the SoundFont %s", path);
return 0;
}
48
49
50
51
}
int fluidsynth_load_soundfont(const char *path, void *data)
{
May 22, 2013
May 22, 2013
52
53
54
/* 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;
55
56
57
58
}
int fluidsynth_init(SDL_AudioSpec *mixer)
{
May 22, 2013
May 22, 2013
59
60
if (!Mix_EachSoundFont(fluidsynth_check_soundfont, NULL))
return -1;
May 22, 2013
May 22, 2013
62
63
64
format = mixer->format;
channels = mixer->channels;
freq = mixer->freq;
May 22, 2013
May 22, 2013
66
return 0;
Jan 4, 2012
Jan 4, 2012
69
static FluidSynthMidiSong *fluidsynth_loadsong_common(int (*function)(FluidSynthMidiSong*, void*), void *data)
May 22, 2013
May 22, 2013
71
72
73
74
75
76
77
78
FluidSynthMidiSong *song;
fluid_settings_t *settings = NULL;
if (!Mix_Init(MIX_INIT_FLUIDSYNTH)) {
return NULL;
}
if ((song = SDL_malloc(sizeof(FluidSynthMidiSong)))) {
Jun 1, 2013
Jun 1, 2013
79
SDL_memset(song, 0, sizeof(FluidSynthMidiSong));
May 22, 2013
May 22, 2013
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
112
static int fluidsynth_loadsong_RW_internal(FluidSynthMidiSong *song, void *data)
Jun 2, 2013
Jun 2, 2013
114
Sint64 offset;
May 22, 2013
May 22, 2013
115
116
size_t size;
char *buffer;
Jun 2, 2013
Jun 2, 2013
117
SDL_RWops *src = (SDL_RWops*) data;
May 22, 2013
May 22, 2013
118
Jun 2, 2013
Jun 2, 2013
119
120
121
122
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
123
124
if ((buffer = (char*) SDL_malloc(size))) {
Jun 2, 2013
Jun 2, 2013
125
if(SDL_RWread(src, buffer, size, 1) == 1) {
May 22, 2013
May 22, 2013
126
127
128
129
130
131
132
133
134
135
136
137
138
if (fluidsynth.fluid_player_add_mem(song->player, buffer, size) == FLUID_OK) {
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;
Jun 2, 2013
Jun 2, 2013
141
FluidSynthMidiSong *fluidsynth_loadsong_RW(SDL_RWops *src, int freesrc)
May 22, 2013
May 22, 2013
143
FluidSynthMidiSong *song;
Dec 31, 2011
Dec 31, 2011
144
Jun 2, 2013
Jun 2, 2013
145
146
147
song = fluidsynth_loadsong_common(fluidsynth_loadsong_RW_internal, (void*) src);
if (song && freesrc) {
SDL_RWclose(src);
May 22, 2013
May 22, 2013
148
149
}
return song;
150
151
152
153
}
void fluidsynth_freesong(FluidSynthMidiSong *song)
{
May 22, 2013
May 22, 2013
154
155
156
157
158
if (!song) return;
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);
159
160
161
162
}
void fluidsynth_start(FluidSynthMidiSong *song)
{
May 22, 2013
May 22, 2013
163
164
fluidsynth.fluid_player_set_loop(song->player, 1);
fluidsynth.fluid_player_play(song->player);
165
166
167
168
}
void fluidsynth_stop(FluidSynthMidiSong *song)
{
May 22, 2013
May 22, 2013
169
fluidsynth.fluid_player_stop(song->player);
170
171
172
173
}
int fluidsynth_active(FluidSynthMidiSong *song)
{
May 22, 2013
May 22, 2013
174
return fluidsynth.fluid_player_get_status(song->player) == FLUID_PLAYER_PLAYING ? 1 : 0;
175
176
177
178
}
void fluidsynth_setvolume(FluidSynthMidiSong *song, int volume)
{
May 22, 2013
May 22, 2013
179
180
/* 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));
181
182
183
184
}
int fluidsynth_playsome(FluidSynthMidiSong *song, void *dest, int dest_len)
{
May 22, 2013
May 22, 2013
185
186
187
188
int result = -1;
int frames = dest_len / channels / ((format & 0xFF) / 8);
int src_len = frames * 4; /* 16-bit stereo */
void *src = dest;
May 22, 2013
May 22, 2013
190
191
192
193
194
195
if (dest_len < src_len) {
if (!(src = SDL_malloc(src_len))) {
Mix_SetError("Insufficient memory for audio conversion");
return result;
}
}
May 22, 2013
May 22, 2013
197
198
199
200
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
202
203
song->convert.buf = src;
song->convert.len = src_len;
May 22, 2013
May 22, 2013
205
206
207
208
if (SDL_ConvertAudio(&song->convert) < 0) {
Mix_SetError("Error during audio conversion");
goto finish;
}
May 22, 2013
May 22, 2013
210
if (src != dest)
Jun 1, 2013
Jun 1, 2013
211
SDL_memcpy(dest, src, dest_len);
May 22, 2013
May 22, 2013
213
result = 0;
May 22, 2013
May 22, 2013
216
217
if (src != dest)
SDL_free(src);
May 22, 2013
May 22, 2013
219
return result;
Jan 1, 2012
Jan 1, 2012
221
222
#endif /* USE_FLUIDSYNTH_MIDI */