Debian Codegraph @

prboom2/src/MUSIC/flplayer.c

raw
1 /* Emacs style mode select -*- C -*-
2 *-----------------------------------------------------------------------------
3 *
4 *
5 * PrBoom: a Doom port merged with LxDoom and LSDLDoom
6 * based on BOOM, a modified and improved DOOM engine
7 *
8 * Copyright (C) 2011 by
9 * Nicholai Main
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA.
25 *
26 * DESCRIPTION:
27 *
28 *---------------------------------------------------------------------
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "musicplayer.h"
36
37 #ifndef HAVE_LIBFLUIDSYNTH
38
39 #include <stddef.h>
40
41 static const char *fl_name (void)
42 {
43 return "fluidsynth midi player (DISABLED)";
44 }
45
46
47 static int fl_init (int samplerate)
48 {
49 return 0;
50 }
51
52 const music_player_t fl_player =
53 {
54 fl_name,
55 fl_init,
56 NULL,
57 NULL,
58 NULL,
59 NULL,
60 NULL,
61 NULL,
62 NULL,
63 NULL,
64 NULL
65 };
66
67 #else // HAVE_LIBFLUIDSYNTH
68
69 #include <fluidsynth.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include "i_system.h" // for I_FindFile()
73 #include "lprintf.h"
74 #include "midifile.h"
75 #include "memio.h"
76 #include "w_wad.h"
77
78 #include "dsda/args.h"
79 #include "dsda/configuration.h"
80
81 #if (FLUIDSYNTH_VERSION_MAJOR < 2 || (FLUIDSYNTH_VERSION_MAJOR == 2 && FLUIDSYNTH_VERSION_MINOR < 2))
82 typedef int fl_sfread_count_t;
83 typedef long fl_sfseek_offset_t;
84 typedef long fl_sftell_t;
85 #else
86 typedef fluid_long_long_t fl_sfread_count_t;
87 typedef fluid_long_long_t fl_sfseek_offset_t;
88 typedef fluid_long_long_t fl_sftell_t;
89 #endif
90
91 static fluid_settings_t *f_set;
92 static fluid_synth_t *f_syn;
93 static int f_font;
94 static midi_event_t **events;
95 static int eventpos;
96 static midi_file_t *midifile;
97
98 static int f_playing;
99 static int f_paused;
100 static int f_looping;
101 static int f_volume;
102 static double spmc;
103 static double f_delta;
104 static int f_soundrate;
105
106 static const char *fl_name (void)
107 {
108 return "fluidsynth midi player";
109 }
110
111 #ifdef _MSC_VER
112 #ifndef WIN32_LEAN_AND_MEAN
113 #define WIN32_LEAN_AND_MEAN 1
114 #endif
115 #include <windows.h>
116 #include <delayimp.h>
117 #endif
118
119 static void *fl_sfopen(const char *lumpname)
120 {
121 MEMFILE *instream;
122 int lumpnum = W_GetNumForName(lumpname);
123 int len = W_LumpLength(lumpnum);
124 const void *data = W_LumpByNum(lumpnum);
125
126 instream = mem_fopen_read(data, len);
127
128 return instream;
129 }
130
131 static int fl_sfread(void *buf, fl_sfread_count_t count, void *handle)
132 {
133 if (mem_fread(buf, sizeof(byte), count, (MEMFILE *)handle) == count)
134 {
135 return FLUID_OK;
136 }
137 return FLUID_FAILED;
138 }
139
140 static int fl_sfseek(void *handle, fl_sfseek_offset_t offset, int origin)
141 {
142 if (mem_fseek((MEMFILE *)handle, offset, origin) < 0)
143 {
144 return FLUID_FAILED;
145 }
146 return FLUID_OK;
147 }
148
149 static int fl_sfclose(void *handle)
150 {
151 mem_fclose((MEMFILE *)handle);
152 return FLUID_OK;
153 }
154
155 static fl_sftell_t fl_sftell(void *handle)
156 {
157 return mem_ftell((MEMFILE *)handle);
158 }
159
160 static void fl_null_logger(int level, const char *message, void *data) {
161 // no op
162 }
163
164 static int fl_init (int samplerate)
165 {
166 int mus_fluidsynth_chorus;
167 int mus_fluidsynth_reverb;
168 int mus_fluidsynth_gain;
169 int mus_fluidsynth_chorus_depth;
170 int mus_fluidsynth_chorus_level;
171 int mus_fluidsynth_reverb_damp;
172 int mus_fluidsynth_reverb_level;
173 int mus_fluidsynth_reverb_width;
174 int mus_fluidsynth_reverb_room_size;
175 const char *filename;
176
177 if (!dsda_Flag(dsda_arg_verbose) || dsda_Flag(dsda_arg_quiet))
178 fluid_set_log_function(FLUID_WARN, fl_null_logger, NULL);
179
180 mus_fluidsynth_chorus = dsda_IntConfig(dsda_config_mus_fluidsynth_chorus);
181 mus_fluidsynth_reverb = dsda_IntConfig(dsda_config_mus_fluidsynth_reverb);
182 mus_fluidsynth_gain = dsda_IntConfig(dsda_config_mus_fluidsynth_gain);
183 mus_fluidsynth_chorus_depth = dsda_IntConfig(dsda_config_mus_fluidsynth_chorus_depth);
184 mus_fluidsynth_chorus_level = dsda_IntConfig(dsda_config_mus_fluidsynth_chorus_level);
185 mus_fluidsynth_reverb_damp = dsda_IntConfig(dsda_config_mus_fluidsynth_reverb_damp);
186 mus_fluidsynth_reverb_level = dsda_IntConfig(dsda_config_mus_fluidsynth_reverb_level);
187 mus_fluidsynth_reverb_width = dsda_IntConfig(dsda_config_mus_fluidsynth_reverb_width);
188 mus_fluidsynth_reverb_room_size = dsda_IntConfig(dsda_config_mus_fluidsynth_reverb_room_size);
189
190 f_soundrate = samplerate;
191 // fluidsynth 1.1.4 supports sample rates as low as 8000hz. earlier versions only go down to 22050hz
192 // since the versions are ABI compatible, detect at runtime, not compile time
193 {
194 int sratemin;
195 int major;
196 int minor;
197 int micro;
198 fluid_version (&major, &minor, &micro);
199 lprintf (LO_DEBUG, "Fluidplayer: Fluidsynth version %i.%i.%i\n", major, minor, micro);
200 if (major >= 2 || (minor >=1 && micro >= 4))
201 sratemin = 8000;
202 else
203 sratemin = 22050;
204 if (f_soundrate < sratemin)
205 {
206 lprintf (LO_WARN, "Fluidplayer: samplerates under %i are not supported\n", sratemin);
207 return 0;
208 }
209 }
210
211
212 f_set = new_fluid_settings ();
213
214 #if FLUIDSYNTH_VERSION_MAJOR == 1
215 #define FSET(a,b,c) if (!fluid_settings_set##a(f_set,b,c))\
216 lprintf (LO_WARN, "fl_init: Couldn't set " b "\n")
217 #else
218 #define FSET(a,b,c) if (fluid_settings_set##a(f_set,b,c) == FLUID_FAILED)\
219 lprintf (LO_WARN, "fl_init: Couldn't set " b "\n")
220 #endif
221
222 FSET (num, "synth.sample-rate", f_soundrate);
223
224 FSET (int, "synth.chorus.active", mus_fluidsynth_chorus);
225 FSET (int, "synth.reverb.active", mus_fluidsynth_reverb);
226
227 if (mus_fluidsynth_chorus)
228 {
229 FSET (num, "synth.chorus.depth", mus_fluidsynth_chorus_depth / 100.0);
230 FSET (num, "synth.chorus.level", mus_fluidsynth_chorus_level / 100.0);
231 }
232
233 if (mus_fluidsynth_reverb)
234 {
235 FSET (num, "synth.reverb.damp", mus_fluidsynth_reverb_damp / 100.0);
236 FSET (num, "synth.reverb.level", mus_fluidsynth_reverb_level / 100.0);
237 FSET (num, "synth.reverb.width", mus_fluidsynth_reverb_width / 100.0);
238 FSET (num, "synth.reverb.room-size", mus_fluidsynth_reverb_room_size / 100.0);
239 }
240
241 // gain control
242 FSET (num, "synth.gain", mus_fluidsynth_gain / 100.0); // 0.0 - 0.2 - 10.0
243 // behavior wrt bank select messages
244 FSET (str, "synth.midi-bank-select", "gs"); // fluidsynth default
245 // general midi spec says 24 voices, but modern midi songs use more
246 FSET (int, "synth.polyphony", 256); // fluidsynth default
247
248 // we're not using the builtin shell or builtin midiplayer,
249 // and our own access to the synth is protected by mutex in i_sound.c
250 FSET (int, "synth.threadsafe-api", 0);
251 #if FLUIDSYNTH_VERSION_MAJOR == 1
252 FSET (int, "synth.parallel-render", 0);
253 #endif
254
255 // prints debugging information to STDOUT
256 //FSET (int, "synth.verbose", 1);
257
258 #undef FSET
259
260 f_syn = new_fluid_synth (f_set);
261 if (!f_syn)
262 {
263 lprintf (LO_WARN, "fl_init: error creating fluidsynth object\n");
264 delete_fluid_settings (f_set);
265 return 0;
266 }
267
268 {
269 int lumpnum;
270 int checked_file = false;
271 dboolean replaced_soundfont = false;
272 const char *checked_f_font = NULL;
273 const char *snd_soundfont;
274
275 lumpnum = W_CheckNumForName("SNDFONT");
276
277 if (lumpnum != LUMP_NOT_FOUND)
278 {
279 replaced_soundfont = !W_LumpNumInPortWad(lumpnum);
280 }
281
282 snd_soundfont = dsda_StringConfig(dsda_config_snd_soundfont);
283
284 if (!replaced_soundfont && snd_soundfont && snd_soundfont[0])
285 {
286 checked_file = true;
287 checked_f_font = snd_soundfont;
288 filename = I_FindFile2(snd_soundfont, ".sf2");
289 f_font = fluid_synth_sfload (f_syn, filename, 1);
290 }
291
292 if ((!checked_file || f_font == FLUID_FAILED) && lumpnum >= 0)
293 {
294 fluid_sfloader_t *sfloader;
295
296 checked_f_font = "SNDFONT";
297 sfloader = new_fluid_defsfloader(f_set);
298 fluid_sfloader_set_callbacks(sfloader, fl_sfopen, fl_sfread, fl_sfseek,
299 fl_sftell, fl_sfclose);
300 fluid_synth_add_sfloader(f_syn, sfloader);
301 f_font = fluid_synth_sfload(f_syn, "SNDFONT", 1);
302 }
303
304 if (!checked_f_font)
305 {
306 lprintf(LO_WARN, "fl_init: no soundfont detected!\n");
307 delete_fluid_synth (f_syn);
308 delete_fluid_settings (f_set);
309 return 0;
310 }
311
312 if (f_font == FLUID_FAILED)
313 {
314 lprintf (LO_WARN, "fl_init: error loading soundfont %s\n", checked_f_font);
315 delete_fluid_synth (f_syn);
316 delete_fluid_settings (f_set);
317 return 0;
318 }
319 }
320
321 return 1;
322 }
323
324 static void fl_shutdown (void)
325 {
326 if (f_syn)
327 {
328 delete_fluid_synth (f_syn);
329 f_syn = NULL;
330 f_font = 0;
331 }
332
333 if (f_set)
334 {
335 delete_fluid_settings (f_set);
336 f_set = NULL;
337 }
338 }
339
340
341
342
343
344 static const void *fl_registersong (const void *data, unsigned len)
345 {
346 midimem_t mf;
347
348 mf.len = len;
349 mf.pos = 0;
350 mf.data = (const byte*)data;
351
352 midifile = MIDI_LoadFile (&mf);
353
354 if (!midifile)
355 {
356 return NULL;
357 }
358
359 events = MIDI_GenerateFlatList (midifile);
360 if (!events)
361 {
362 MIDI_FreeFile (midifile);
363 return NULL;
364 }
365 eventpos = 0;
366
367 // implicit 120BPM (this is correct to spec)
368 //spmc = compute_spmc (MIDI_GetFileTimeDivision (midifile), 500000, f_soundrate);
369 spmc = MIDI_spmc (midifile, NULL, f_soundrate);
370
371 // handle not used
372 return data;
373 }
374
375 static void fl_unregistersong (const void *handle)
376 {
377 if (events)
378 {
379 MIDI_DestroyFlatList (events);
380 events = NULL;
381 }
382 if (midifile)
383 {
384 MIDI_FreeFile (midifile);
385 midifile = NULL;
386 }
387 }
388
389 static void fl_pause (void)
390 {
391 //int i;
392 f_paused = 1;
393 // instead of cutting notes, pause the synth so they can resume seamlessly
394 //for (i = 0; i < 16; i++)
395 // fluid_synth_cc (f_syn, i, 123, 0); // ALL NOTES OFF
396 }
397 static void fl_resume (void)
398 {
399 f_paused = 0;
400 }
401 static void fl_play (const void *handle, int looping)
402 {
403 eventpos = 0;
404 f_looping = looping;
405 f_playing = 1;
406 //f_paused = 0;
407 f_delta = 0.0;
408 fluid_synth_program_reset (f_syn);
409 fluid_synth_system_reset (f_syn);
410 }
411
412 static void fl_stop (void)
413 {
414 int i;
415 f_playing = 0;
416
417 for (i = 0; i < 16; i++)
418 {
419 fluid_synth_cc (f_syn, i, 123, 0); // ALL NOTES OFF
420 fluid_synth_cc (f_syn, i, 121, 0); // RESET ALL CONTROLLERS
421 }
422 }
423
424 static void fl_setvolume (int v)
425 {
426 f_volume = v;
427 }
428
429
430 static void fl_writesamples_ex (short *dest, int nsamp)
431 { // does volume conversion and then writes samples
432 int i;
433 float multiplier = 16384.0f / 15.0f * f_volume;
434
435 static float *fbuff = NULL;
436 static int fbuff_siz = 0;
437
438 if (nsamp * 2 > fbuff_siz)
439 {
440 float *newfbuff = (float*)realloc (fbuff, nsamp * 2 * sizeof (float));
441 if (!newfbuff) return;
442 fbuff = newfbuff;
443 fbuff_siz = nsamp * 2;
444 }
445
446 fluid_synth_write_float (f_syn, nsamp, fbuff, 0, 2, fbuff, 1, 2);
447
448 for (i = 0; i < nsamp * 2; i++)
449 {
450 // data is NOT already clipped
451 float f = fbuff[i];
452 if (f > 1.0f)
453 f = 1.0f;
454 if (f < -1.0f)
455 f = -1.0f;
456 dest[i] = (short) (f * multiplier);
457 }
458 }
459
460 static void fl_render (void *vdest, unsigned length)
461 {
462 short *dest = (short*)vdest;
463
464 unsigned sampleswritten = 0;
465 unsigned samples;
466
467 midi_event_t *currevent;
468
469 if (!f_playing || f_paused)
470 {
471 // save CPU time and allow for seamless resume after pause
472 memset (vdest, 0, length * 4);
473 //fl_writesamples_ex (vdest, length);
474 return;
475 }
476
477
478 while (1)
479 {
480 double eventdelta;
481 currevent = events[eventpos];
482
483 // how many samples away event is
484 eventdelta = currevent->delta_time * spmc;
485
486
487 // how many we will render (rounding down); include delta offset
488 samples = (unsigned) (eventdelta + f_delta);
489
490
491 if (samples + sampleswritten > length)
492 { // overshoot; render some samples without processing an event
493 break;
494 }
495
496
497 if (samples)
498 {
499 fl_writesamples_ex (dest, samples);
500 sampleswritten += samples;
501 f_delta -= samples;
502 dest += samples * 2;
503 }
504
505 // process event
506 switch (currevent->event_type)
507 {
508 case MIDI_EVENT_NOTE_OFF:
509 fluid_synth_noteoff (f_syn, currevent->data.channel.channel, currevent->data.channel.param1);
510 break;
511 case MIDI_EVENT_NOTE_ON:
512 fluid_synth_noteon (f_syn, currevent->data.channel.channel, currevent->data.channel.param1, currevent->data.channel.param2);
513 break;
514 case MIDI_EVENT_AFTERTOUCH:
515 // not suipported?
516 break;
517 case MIDI_EVENT_CONTROLLER:
518 fluid_synth_cc (f_syn, currevent->data.channel.channel, currevent->data.channel.param1, currevent->data.channel.param2);
519 break;
520 case MIDI_EVENT_PROGRAM_CHANGE:
521 fluid_synth_program_change (f_syn, currevent->data.channel.channel, currevent->data.channel.param1);
522 break;
523 case MIDI_EVENT_CHAN_AFTERTOUCH:
524 fluid_synth_channel_pressure (f_syn, currevent->data.channel.channel, currevent->data.channel.param1);
525 break;
526 case MIDI_EVENT_PITCH_BEND:
527 fluid_synth_pitch_bend (f_syn, currevent->data.channel.channel, currevent->data.channel.param1 | currevent->data.channel.param2 << 7);
528 break;
529 case MIDI_EVENT_META:
530 if (currevent->data.meta.type == MIDI_META_SET_TEMPO)
531 spmc = MIDI_spmc (midifile, currevent, f_soundrate);
532 else if (currevent->data.meta.type == MIDI_META_END_OF_TRACK)
533 {
534 if (f_looping)
535 {
536 int i;
537 eventpos = 0;
538 f_delta += eventdelta;
539 // fix buggy songs that forget to terminate notes held over loop point
540 // sdl_mixer does this as well
541 for (i = 0; i < 16; i++)
542 {
543 fluid_synth_cc (f_syn, i, 123, 0); // ALL NOTES OFF
544 fluid_synth_cc (f_syn, i, 121, 0); // RESET ALL CONTROLLERS
545 fluid_synth_cc (f_syn, i, 7, 100); // reset volume
546 }
547 continue;
548 }
549 // stop, write leadout
550 fl_stop ();
551 samples = length - sampleswritten;
552 if (samples)
553 {
554 fl_writesamples_ex (dest, samples);
555 sampleswritten += samples;
556 // timecodes no longer relevant
557 dest += samples * 2;
558
559 }
560 return;
561 }
562 break; // not interested in most metas
563 default: //uhh
564 break;
565
566 }
567 // event processed so advance midiclock
568 f_delta += eventdelta;
569 eventpos++;
570
571 }
572
573
574
575
576 if (samples + sampleswritten > length)
577 { // broke due to next event being past the end of current render buffer
578 // finish buffer, return
579 samples = length - sampleswritten;
580 if (samples)
581 {
582 fl_writesamples_ex (dest, samples);
583 sampleswritten += samples;
584 f_delta -= samples; // save offset
585 dest += samples * 2;
586 }
587 }
588 else
589 { // huh?
590 return;
591 }
592
593
594 }
595
596
597 const music_player_t fl_player =
598 {
599 fl_name,
600 fl_init,
601 fl_shutdown,
602 fl_setvolume,
603 fl_pause,
604 fl_resume,
605 fl_registersong,
606 fl_unregistersong,
607 fl_play,
608 fl_stop,
609 fl_render
610 };
611
612
613 #endif // HAVE_LIBFLUIDSYNTH