Update Fluidsynth to v2.0.3
[ardour.git] / libs / fluidsynth / src / fluid_defsfont.c
1 /* FluidSynth - A Software Synthesizer
2  *
3  * Copyright (C) 2003  Peter Hanappe and others.
4  *
5  * SoundFont file loading code borrowed from Smurf SoundFont Editor
6  * Copyright (C) 1999-2001 Josh Green
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301, USA
22  */
23
24
25 #include "fluid_defsfont.h"
26 #include "fluid_sfont.h"
27 #include "fluid_sys.h"
28 #include "fluid_synth.h"
29 #include "fluid_samplecache.h"
30
31 /* EMU8k/10k hardware applies this factor to initial attenuation generator values set at preset and
32  * instrument level in a soundfont. We apply this factor when loading the generator values to stay
33  * compatible as most existing soundfonts expect exactly this (strange, non-standard) behaviour. */
34 #define EMU_ATTENUATION_FACTOR (0.4f)
35
36 /* Dynamic sample loading functions */
37 static int load_preset_samples(fluid_defsfont_t *defsfont, fluid_preset_t *preset);
38 static int unload_preset_samples(fluid_defsfont_t *defsfont, fluid_preset_t *preset);
39 static void unload_sample(fluid_sample_t *sample);
40 static int dynamic_samples_preset_notify(fluid_preset_t *preset, int reason, int chan);
41 static int dynamic_samples_sample_notify(fluid_sample_t *sample, int reason);
42 static int fluid_preset_zone_create_voice_zones(fluid_preset_zone_t *preset_zone);
43 static fluid_inst_t *find_inst_by_idx(fluid_defsfont_t *defsfont, int idx);
44
45
46 /***************************************************************
47  *
48  *                           SFONT LOADER
49  */
50
51 /**
52  * Creates a default soundfont2 loader that can be used with fluid_synth_add_sfloader().
53  * By default every synth instance has an initial default soundfont loader instance.
54  * Calling this function is usually only necessary to load a soundfont from memory, by providing custom callback functions via fluid_sfloader_set_callbacks().
55  *
56  * @param settings A settings instance obtained by new_fluid_settings()
57  * @return A default soundfont2 loader struct
58  */
59 fluid_sfloader_t *new_fluid_defsfloader(fluid_settings_t *settings)
60 {
61     fluid_sfloader_t *loader;
62     fluid_return_val_if_fail(settings != NULL, NULL);
63
64     loader = new_fluid_sfloader(fluid_defsfloader_load, delete_fluid_sfloader);
65
66     if(loader == NULL)
67     {
68         FLUID_LOG(FLUID_ERR, "Out of memory");
69         return NULL;
70     }
71
72     fluid_sfloader_set_data(loader, settings);
73
74     return loader;
75 }
76
77 fluid_sfont_t *fluid_defsfloader_load(fluid_sfloader_t *loader, const char *filename)
78 {
79     fluid_defsfont_t *defsfont;
80     fluid_sfont_t *sfont;
81
82     defsfont = new_fluid_defsfont(fluid_sfloader_get_data(loader));
83
84     if(defsfont == NULL)
85     {
86         return NULL;
87     }
88
89     sfont = new_fluid_sfont(fluid_defsfont_sfont_get_name,
90                             fluid_defsfont_sfont_get_preset,
91                             fluid_defsfont_sfont_iteration_start,
92                             fluid_defsfont_sfont_iteration_next,
93                             fluid_defsfont_sfont_delete);
94
95     if(sfont == NULL)
96     {
97         delete_fluid_defsfont(defsfont);
98         return NULL;
99     }
100
101     fluid_sfont_set_data(sfont, defsfont);
102
103     defsfont->sfont = sfont;
104
105     if(fluid_defsfont_load(defsfont, &loader->file_callbacks, filename) == FLUID_FAILED)
106     {
107         fluid_sfont_delete_internal(sfont);
108         return NULL;
109     }
110
111     return sfont;
112 }
113
114
115
116 /***************************************************************
117  *
118  *                           PUBLIC INTERFACE
119  */
120
121 int fluid_defsfont_sfont_delete(fluid_sfont_t *sfont)
122 {
123     if(delete_fluid_defsfont(fluid_sfont_get_data(sfont)) != FLUID_OK)
124     {
125         return -1;
126     }
127
128     delete_fluid_sfont(sfont);
129     return 0;
130 }
131
132 const char *fluid_defsfont_sfont_get_name(fluid_sfont_t *sfont)
133 {
134     return fluid_defsfont_get_name(fluid_sfont_get_data(sfont));
135 }
136
137 fluid_preset_t *
138 fluid_defsfont_sfont_get_preset(fluid_sfont_t *sfont, int bank, int prenum)
139 {
140     return fluid_defsfont_get_preset(fluid_sfont_get_data(sfont), bank, prenum);
141 }
142
143 void fluid_defsfont_sfont_iteration_start(fluid_sfont_t *sfont)
144 {
145     fluid_defsfont_iteration_start(fluid_sfont_get_data(sfont));
146 }
147
148 fluid_preset_t *fluid_defsfont_sfont_iteration_next(fluid_sfont_t *sfont)
149 {
150     return fluid_defsfont_iteration_next(fluid_sfont_get_data(sfont));
151 }
152
153 void fluid_defpreset_preset_delete(fluid_preset_t *preset)
154 {
155     fluid_defsfont_t *defsfont;
156     fluid_defpreset_t *defpreset;
157
158     defsfont = fluid_sfont_get_data(preset->sfont);
159     defpreset = fluid_preset_get_data(preset);
160
161     if(defsfont)
162     {
163         defsfont->preset = fluid_list_remove(defsfont->preset, defpreset);
164     }
165
166     delete_fluid_defpreset(defpreset);
167     delete_fluid_preset(preset);
168 }
169
170 const char *fluid_defpreset_preset_get_name(fluid_preset_t *preset)
171 {
172     return fluid_defpreset_get_name(fluid_preset_get_data(preset));
173 }
174
175 int fluid_defpreset_preset_get_banknum(fluid_preset_t *preset)
176 {
177     return fluid_defpreset_get_banknum(fluid_preset_get_data(preset));
178 }
179
180 int fluid_defpreset_preset_get_num(fluid_preset_t *preset)
181 {
182     return fluid_defpreset_get_num(fluid_preset_get_data(preset));
183 }
184
185 int fluid_defpreset_preset_noteon(fluid_preset_t *preset, fluid_synth_t *synth,
186                                   int chan, int key, int vel)
187 {
188     return fluid_defpreset_noteon(fluid_preset_get_data(preset), synth, chan, key, vel);
189 }
190
191
192 /***************************************************************
193  *
194  *                           SFONT
195  */
196
197 /*
198  * new_fluid_defsfont
199  */
200 fluid_defsfont_t *new_fluid_defsfont(fluid_settings_t *settings)
201 {
202     fluid_defsfont_t *defsfont;
203
204     defsfont = FLUID_NEW(fluid_defsfont_t);
205
206     if(defsfont == NULL)
207     {
208         FLUID_LOG(FLUID_ERR, "Out of memory");
209         return NULL;
210     }
211
212     FLUID_MEMSET(defsfont, 0, sizeof(*defsfont));
213
214     fluid_settings_getint(settings, "synth.lock-memory", &defsfont->mlock);
215     fluid_settings_getint(settings, "synth.dynamic-sample-loading", &defsfont->dynamic_samples);
216
217     return defsfont;
218 }
219
220 /*
221  * delete_fluid_defsfont
222  */
223 int delete_fluid_defsfont(fluid_defsfont_t *defsfont)
224 {
225     fluid_list_t *list;
226     fluid_preset_t *preset;
227     fluid_sample_t *sample;
228
229     fluid_return_val_if_fail(defsfont != NULL, FLUID_OK);
230
231     /* Check that no samples are currently used */
232     for(list = defsfont->sample; list; list = fluid_list_next(list))
233     {
234         sample = (fluid_sample_t *) fluid_list_get(list);
235
236         if(sample->refcount != 0)
237         {
238             return FLUID_FAILED;
239         }
240     }
241
242     if(defsfont->filename != NULL)
243     {
244         FLUID_FREE(defsfont->filename);
245     }
246
247     for(list = defsfont->sample; list; list = fluid_list_next(list))
248     {
249         delete_fluid_sample((fluid_sample_t *) fluid_list_get(list));
250     }
251
252     if(defsfont->sample)
253     {
254         delete_fluid_list(defsfont->sample);
255     }
256
257     if(defsfont->sampledata != NULL)
258     {
259         fluid_samplecache_unload(defsfont->sampledata);
260     }
261
262     for(list = defsfont->preset; list; list = fluid_list_next(list))
263     {
264         preset = (fluid_preset_t *)fluid_list_get(list);
265         fluid_defpreset_preset_delete(preset);
266     }
267
268     delete_fluid_list(defsfont->preset);
269
270     for(list = defsfont->inst; list; list = fluid_list_next(list))
271     {
272         delete_fluid_inst(fluid_list_get(list));
273     }
274
275     delete_fluid_list(defsfont->inst);
276
277     FLUID_FREE(defsfont);
278     return FLUID_OK;
279 }
280
281 /*
282  * fluid_defsfont_get_name
283  */
284 const char *fluid_defsfont_get_name(fluid_defsfont_t *defsfont)
285 {
286     return defsfont->filename;
287 }
288
289 /* Load sample data for a single sample from the Soundfont file.
290  * Returns FLUID_OK on error, otherwise FLUID_FAILED
291  */
292 int fluid_defsfont_load_sampledata(fluid_defsfont_t *defsfont, SFData *sfdata, fluid_sample_t *sample)
293 {
294     int num_samples;
295     unsigned int source_end = sample->source_end;
296
297     /* For uncompressed samples we want to include the 46 zero sample word area following each sample
298      * in the Soundfont. Otherwise samples with loopend > end, which we have decided not to correct, would
299      * be corrected after all in fluid_sample_sanitize_loop */
300     if(!(sample->sampletype & FLUID_SAMPLETYPE_OGG_VORBIS))
301     {
302         source_end += 46;  /* Length of zero sample word after each sample, according to SF specs */
303
304         /* Safeguard against Soundfonts that are not quite valid and don't include 46 sample words after the
305          * last sample */
306         if(source_end >= (defsfont->samplesize  / sizeof(short)))
307         {
308             source_end = defsfont->samplesize  / sizeof(short);
309         }
310     }
311
312     num_samples = fluid_samplecache_load(
313                       sfdata, sample->source_start, source_end, sample->sampletype,
314                       defsfont->mlock, &sample->data, &sample->data24);
315
316     if(num_samples < 0)
317     {
318         return FLUID_FAILED;
319     }
320
321     if(num_samples == 0)
322     {
323         sample->start = sample->end = 0;
324         sample->loopstart = sample->loopend = 0;
325         return FLUID_OK;
326     }
327
328     /* Ogg Vorbis samples already have loop pointers relative to the invididual decompressed sample,
329      * but SF2 samples are relative to sample chunk start, so they need to be adjusted */
330     if(!(sample->sampletype & FLUID_SAMPLETYPE_OGG_VORBIS))
331     {
332         sample->loopstart = sample->source_loopstart - sample->source_start;
333         sample->loopend = sample->source_loopend - sample->source_start;
334     }
335
336     /* As we've just loaded an individual sample into it's own buffer, we need to adjust the start
337      * and end pointers */
338     sample->start = 0;
339     sample->end = num_samples - 1;
340
341     return FLUID_OK;
342 }
343
344 /* Loads the sample data for all samples from the Soundfont file. For SF2 files, it loads the data in
345  * one large block. For SF3 files, each compressed sample gets loaded individually.
346  * Returns FLUID_OK on success, otherwise FLUID_FAILED
347  */
348 int fluid_defsfont_load_all_sampledata(fluid_defsfont_t *defsfont, SFData *sfdata)
349 {
350     fluid_list_t *list;
351     fluid_sample_t *sample;
352     int sf3_file = (sfdata->version.major == 3);
353
354     /* For SF2 files, we load the sample data in one large block */
355     if(!sf3_file)
356     {
357         int read_samples;
358         int num_samples = sfdata->samplesize / sizeof(short);
359
360         read_samples = fluid_samplecache_load(sfdata, 0, num_samples - 1, 0, defsfont->mlock,
361                                               &defsfont->sampledata, &defsfont->sample24data);
362
363         if(read_samples != num_samples)
364         {
365             FLUID_LOG(FLUID_ERR, "Attempted to read %d words of sample data, but got %d instead",
366                       num_samples, read_samples);
367             return FLUID_FAILED;
368         }
369     }
370
371     for(list = defsfont->sample; list; list = fluid_list_next(list))
372     {
373         sample = fluid_list_get(list);
374
375         if(sf3_file)
376         {
377             /* SF3 samples get loaded individually, as most (or all) of them are in Ogg Vorbis format
378              * anyway */
379             if(fluid_defsfont_load_sampledata(defsfont, sfdata, sample) == FLUID_FAILED)
380             {
381                 FLUID_LOG(FLUID_ERR, "Failed to load sample '%s'", sample->name);
382                 return FLUID_FAILED;
383             }
384
385             fluid_sample_sanitize_loop(sample, (sample->end + 1) * sizeof(short));
386         }
387         else
388         {
389             /* Data pointers of SF2 samples point to large sample data block loaded above */
390             sample->data = defsfont->sampledata;
391             sample->data24 = defsfont->sample24data;
392             fluid_sample_sanitize_loop(sample, defsfont->samplesize);
393         }
394
395         fluid_voice_optimize_sample(sample);
396     }
397
398     return FLUID_OK;
399 }
400
401 /*
402  * fluid_defsfont_load
403  */
404 int fluid_defsfont_load(fluid_defsfont_t *defsfont, const fluid_file_callbacks_t *fcbs, const char *file)
405 {
406     SFData *sfdata;
407     fluid_list_t *p;
408     SFPreset *sfpreset;
409     SFSample *sfsample;
410     fluid_sample_t *sample;
411     fluid_defpreset_t *defpreset = NULL;
412
413     defsfont->filename = FLUID_STRDUP(file);
414
415     if(defsfont->filename == NULL)
416     {
417         FLUID_LOG(FLUID_ERR, "Out of memory");
418         return FLUID_FAILED;
419     }
420
421     defsfont->fcbs = fcbs;
422
423     /* The actual loading is done in the sfont and sffile files */
424     sfdata = fluid_sffile_open(file, fcbs);
425
426     if(sfdata == NULL)
427     {
428         FLUID_LOG(FLUID_ERR, "Couldn't load soundfont file");
429         return FLUID_FAILED;
430     }
431
432     if(fluid_sffile_parse_presets(sfdata) == FLUID_FAILED)
433     {
434         FLUID_LOG(FLUID_ERR, "Couldn't parse presets from soundfont file");
435         goto err_exit;
436     }
437
438     /* Keep track of the position and size of the sample data because
439        it's loaded separately (and might be unoaded/reloaded in future) */
440     defsfont->samplepos = sfdata->samplepos;
441     defsfont->samplesize = sfdata->samplesize;
442     defsfont->sample24pos = sfdata->sample24pos;
443     defsfont->sample24size = sfdata->sample24size;
444
445     /* Create all samples from sample headers */
446     p = sfdata->sample;
447
448     while(p != NULL)
449     {
450         sfsample = (SFSample *)fluid_list_get(p);
451
452         sample = new_fluid_sample();
453
454         if(sample == NULL)
455         {
456             goto err_exit;
457         }
458
459         if(fluid_sample_import_sfont(sample, sfsample, defsfont) == FLUID_OK)
460         {
461             fluid_defsfont_add_sample(defsfont, sample);
462         }
463         else
464         {
465             delete_fluid_sample(sample);
466             sample = NULL;
467         }
468
469         /* Store reference to FluidSynth sample in SFSample for later IZone fixups */
470         sfsample->fluid_sample = sample;
471
472         p = fluid_list_next(p);
473     }
474
475     /* If dynamic sample loading is disabled, load all samples in the Soundfont */
476     if(!defsfont->dynamic_samples)
477     {
478         if(fluid_defsfont_load_all_sampledata(defsfont, sfdata) == FLUID_FAILED)
479         {
480             FLUID_LOG(FLUID_ERR, "Unable to load all sample data");
481             goto err_exit;
482         }
483     }
484
485     /* Load all the presets */
486     p = sfdata->preset;
487
488     while(p != NULL)
489     {
490         sfpreset = (SFPreset *)fluid_list_get(p);
491         defpreset = new_fluid_defpreset(defsfont);
492
493         if(defpreset == NULL)
494         {
495             goto err_exit;
496         }
497
498         if(fluid_defpreset_import_sfont(defpreset, sfpreset, defsfont) != FLUID_OK)
499         {
500             goto err_exit;
501         }
502
503         if(fluid_defsfont_add_preset(defsfont, defpreset) == FLUID_FAILED)
504         {
505             goto err_exit;
506         }
507
508         p = fluid_list_next(p);
509     }
510
511     fluid_sffile_close(sfdata);
512
513     return FLUID_OK;
514
515 err_exit:
516     fluid_sffile_close(sfdata);
517     delete_fluid_defpreset(defpreset);
518     return FLUID_FAILED;
519 }
520
521 /* fluid_defsfont_add_sample
522  *
523  * Add a sample to the SoundFont
524  */
525 int fluid_defsfont_add_sample(fluid_defsfont_t *defsfont, fluid_sample_t *sample)
526 {
527     defsfont->sample = fluid_list_append(defsfont->sample, sample);
528     return FLUID_OK;
529 }
530
531 /* fluid_defsfont_add_preset
532  *
533  * Add a preset to the SoundFont
534  */
535 int fluid_defsfont_add_preset(fluid_defsfont_t *defsfont, fluid_defpreset_t *defpreset)
536 {
537     fluid_preset_t *preset;
538
539     preset = new_fluid_preset(defsfont->sfont,
540                               fluid_defpreset_preset_get_name,
541                               fluid_defpreset_preset_get_banknum,
542                               fluid_defpreset_preset_get_num,
543                               fluid_defpreset_preset_noteon,
544                               fluid_defpreset_preset_delete);
545
546     if(defsfont->dynamic_samples)
547     {
548         preset->notify = dynamic_samples_preset_notify;
549     }
550
551     if(preset == NULL)
552     {
553         return FLUID_FAILED;
554     }
555
556     fluid_preset_set_data(preset, defpreset);
557
558     defsfont->preset = fluid_list_append(defsfont->preset, preset);
559
560     return FLUID_OK;
561 }
562
563 /*
564  * fluid_defsfont_get_preset
565  */
566 fluid_preset_t *fluid_defsfont_get_preset(fluid_defsfont_t *defsfont, int bank, int num)
567 {
568     fluid_preset_t *preset;
569     fluid_list_t *list;
570
571     for(list = defsfont->preset; list != NULL; list = fluid_list_next(list))
572     {
573         preset = (fluid_preset_t *)fluid_list_get(list);
574
575         if((fluid_preset_get_banknum(preset) == bank) && (fluid_preset_get_num(preset) == num))
576         {
577             return preset;
578         }
579     }
580
581     return NULL;
582 }
583
584 /*
585  * fluid_defsfont_iteration_start
586  */
587 void fluid_defsfont_iteration_start(fluid_defsfont_t *defsfont)
588 {
589     defsfont->preset_iter_cur = defsfont->preset;
590 }
591
592 /*
593  * fluid_defsfont_iteration_next
594  */
595 fluid_preset_t *fluid_defsfont_iteration_next(fluid_defsfont_t *defsfont)
596 {
597     fluid_preset_t *preset = (fluid_preset_t *)fluid_list_get(defsfont->preset_iter_cur);
598
599     defsfont->preset_iter_cur = fluid_list_next(defsfont->preset_iter_cur);
600
601     return preset;
602 }
603
604 /***************************************************************
605  *
606  *                           PRESET
607  */
608
609 /*
610  * new_fluid_defpreset
611  */
612 fluid_defpreset_t *
613 new_fluid_defpreset(fluid_defsfont_t *defsfont)
614 {
615     fluid_defpreset_t *defpreset = FLUID_NEW(fluid_defpreset_t);
616
617     if(defpreset == NULL)
618     {
619         FLUID_LOG(FLUID_ERR, "Out of memory");
620         return NULL;
621     }
622
623     defpreset->next = NULL;
624     defpreset->defsfont = defsfont;
625     defpreset->name[0] = 0;
626     defpreset->bank = 0;
627     defpreset->num = 0;
628     defpreset->global_zone = NULL;
629     defpreset->zone = NULL;
630     return defpreset;
631 }
632
633 /*
634  * delete_fluid_defpreset
635  */
636 void
637 delete_fluid_defpreset(fluid_defpreset_t *defpreset)
638 {
639     fluid_preset_zone_t *zone;
640
641     fluid_return_if_fail(defpreset != NULL);
642
643     delete_fluid_preset_zone(defpreset->global_zone);
644     defpreset->global_zone = NULL;
645
646     zone = defpreset->zone;
647
648     while(zone != NULL)
649     {
650         defpreset->zone = zone->next;
651         delete_fluid_preset_zone(zone);
652         zone = defpreset->zone;
653     }
654
655     FLUID_FREE(defpreset);
656 }
657
658 int
659 fluid_defpreset_get_banknum(fluid_defpreset_t *defpreset)
660 {
661     return defpreset->bank;
662 }
663
664 int
665 fluid_defpreset_get_num(fluid_defpreset_t *defpreset)
666 {
667     return defpreset->num;
668 }
669
670 const char *
671 fluid_defpreset_get_name(fluid_defpreset_t *defpreset)
672 {
673     return defpreset->name;
674 }
675
676 /*
677  * fluid_defpreset_next
678  */
679 fluid_defpreset_t *
680 fluid_defpreset_next(fluid_defpreset_t *defpreset)
681 {
682     return defpreset->next;
683 }
684
685 /*
686  * Adds global and local modulators list to the voice. This is done in 2 steps:
687  * - Step 1: Local modulators replace identic global modulators.
688  * - Step 2: global + local modulators are added to the voice using mode.
689  *
690  * Instrument zone list (local/global) must be added using FLUID_VOICE_OVERWRITE.
691  * Preset zone list (local/global) must be added using FLUID_VOICE_ADD.
692  *
693  * @param voice voice instance.
694  * @param global_mod global list of modulators.
695  * @param local_mod local list of modulators.
696  * @param mode Determines how to handle an existing identical modulator.
697  *   #FLUID_VOICE_ADD to add (offset) the modulator amounts,
698  *   #FLUID_VOICE_OVERWRITE to replace the modulator,
699 */
700 static void
701 fluid_defpreset_noteon_add_mod_to_voice(fluid_voice_t *voice,
702                                         fluid_mod_t *global_mod, fluid_mod_t *local_mod,
703                                         int mode)
704 {
705     fluid_mod_t *mod;
706     /* list for 'sorting' global/local modulators */
707     fluid_mod_t *mod_list[FLUID_NUM_MOD];
708     int mod_list_count, i;
709
710     /* identity_limit_count is the modulator upper limit number to handle with
711      * existing identical modulators.
712      * When identity_limit_count is below the actual number of modulators, this
713      * will restrict identity check to this upper limit,
714      * This is useful when we know by advance that there is no duplicate with
715      * modulators at index above this limit. This avoid wasting cpu cycles at
716      * noteon.
717      */
718     int identity_limit_count;
719
720     /* Step 1: Local modulators replace identic global modulators. */
721
722     /* local (instrument zone/preset zone), modulators: Put them all into a list. */
723     mod_list_count = 0;
724
725     while(local_mod)
726     {
727         /* As modulators number in local_mod list was limited to FLUID_NUM_MOD at
728            soundfont loading time (fluid_limit_mod_list()), here we don't need
729            to check if mod_list is full.
730          */
731         mod_list[mod_list_count++] = local_mod;
732         local_mod = local_mod->next;
733     }
734
735     /* global (instrument zone/preset zone), modulators.
736      * Replace modulators with the same definition in the global list:
737      * (Instrument zone: SF 2.01 page 69, 'bullet' 8)
738      * (Preset zone:     SF 2.01 page 69, second-last bullet).
739      *
740      * mod_list contains local modulators. Now we know that there
741      * is no global modulator identic to another global modulator (this has
742      * been checked at soundfont loading time). So global modulators
743      * are only checked against local modulators number.
744      */
745
746     /* Restrict identity check to the number of local modulators */
747     identity_limit_count = mod_list_count;
748
749     while(global_mod)
750     {
751         /* 'Identical' global modulators are ignored.
752          *  SF2.01 section 9.5.1
753          *  page 69, 'bullet' 3 defines 'identical'.  */
754
755         for(i = 0; i < identity_limit_count; i++)
756         {
757             if(fluid_mod_test_identity(global_mod, mod_list[i]))
758             {
759                 break;
760             }
761         }
762
763         /* Finally add the new modulator to the list. */
764         if(i >= identity_limit_count)
765         {
766             /* Although local_mod and global_mod lists was limited to
767                FLUID_NUM_MOD at soundfont loading time, it is possible that
768                local + global modulators exceeds FLUID_NUM_MOD.
769                So, checks if mod_list_count reachs the limit.
770             */
771             if(mod_list_count >= FLUID_NUM_MOD)
772             {
773                 /* mod_list is full, we silently forget this modulator and
774                    next global modulators. When mod_list will be added to the
775                    voice, a warning will be displayed if the voice list is full.
776                    (see fluid_voice_add_mod_local()).
777                 */
778                 break;
779             }
780
781             mod_list[mod_list_count++] = global_mod;
782         }
783
784         global_mod = global_mod->next;
785     }
786
787     /* Step 2: global + local modulators are added to the voice using mode. */
788
789     /*
790      * mod_list contains local and global modulators, we know that:
791      * - there is no global modulator identic to another global modulator,
792      * - there is no local modulator identic to another local modulator,
793      * So these local/global modulators are only checked against
794      * actual number of voice modulators.
795      */
796
797     /* Restrict identity check to the actual number of voice modulators */
798     /* Acual number of voice modulators : defaults + [instruments] */
799     identity_limit_count = voice->mod_count;
800
801     for(i = 0; i < mod_list_count; i++)
802     {
803
804         mod = mod_list[i];
805         /* in mode FLUID_VOICE_OVERWRITE disabled instruments modulators CANNOT be skipped. */
806         /* in mode FLUID_VOICE_ADD disabled preset modulators can be skipped. */
807
808         if((mode == FLUID_VOICE_OVERWRITE) || (mod->amount != 0))
809         {
810             /* Instrument modulators -supersede- existing (default) modulators.
811                SF 2.01 page 69, 'bullet' 6 */
812
813             /* Preset modulators -add- to existing instrument modulators.
814                SF2.01 page 70 first bullet on page */
815             fluid_voice_add_mod_local(voice, mod, mode, identity_limit_count);
816         }
817     }
818 }
819
820 /*
821  * fluid_defpreset_noteon
822  */
823 int
824 fluid_defpreset_noteon(fluid_defpreset_t *defpreset, fluid_synth_t *synth, int chan, int key, int vel)
825 {
826     fluid_preset_zone_t *preset_zone, *global_preset_zone;
827     fluid_inst_t *inst;
828     fluid_inst_zone_t *inst_zone, *global_inst_zone;
829     fluid_voice_zone_t *voice_zone;
830     fluid_list_t *list;
831     fluid_voice_t *voice;
832     int i;
833
834     global_preset_zone = fluid_defpreset_get_global_zone(defpreset);
835
836     /* run thru all the zones of this preset */
837     preset_zone = fluid_defpreset_get_zone(defpreset);
838
839     while(preset_zone != NULL)
840     {
841
842         /* check if the note falls into the key and velocity range of this
843            preset */
844         if(fluid_zone_inside_range(&preset_zone->range, key, vel))
845         {
846
847             inst = fluid_preset_zone_get_inst(preset_zone);
848             global_inst_zone = fluid_inst_get_global_zone(inst);
849
850             /* run thru all the zones of this instrument that could start a voice */
851             for(list = preset_zone->voice_zone; list != NULL; list = fluid_list_next(list))
852             {
853                 voice_zone = fluid_list_get(list);
854
855                 /* check if the instrument zone is ignored and the note falls into
856                    the key and velocity range of this  instrument zone.
857                    An instrument zone must be ignored when its voice is already running
858                    played by a legato passage (see fluid_synth_noteon_monopoly_legato()) */
859                 if(fluid_zone_inside_range(&voice_zone->range, key, vel))
860                 {
861
862                     inst_zone = voice_zone->inst_zone;
863
864                     /* this is a good zone. allocate a new synthesis process and initialize it */
865                     voice = fluid_synth_alloc_voice_LOCAL(synth, inst_zone->sample, chan, key, vel, &voice_zone->range);
866
867                     if(voice == NULL)
868                     {
869                         return FLUID_FAILED;
870                     }
871
872
873                     /* Instrument level, generators */
874
875                     for(i = 0; i < GEN_LAST; i++)
876                     {
877
878                         /* SF 2.01 section 9.4 'bullet' 4:
879                          *
880                          * A generator in a local instrument zone supersedes a
881                          * global instrument zone generator.  Both cases supersede
882                          * the default generator -> voice_gen_set */
883
884                         if(inst_zone->gen[i].flags)
885                         {
886                             fluid_voice_gen_set(voice, i, inst_zone->gen[i].val);
887
888                         }
889                         else if((global_inst_zone != NULL) && (global_inst_zone->gen[i].flags))
890                         {
891                             fluid_voice_gen_set(voice, i, global_inst_zone->gen[i].val);
892
893                         }
894                         else
895                         {
896                             /* The generator has not been defined in this instrument.
897                              * Do nothing, leave it at the default.
898                              */
899                         }
900
901                     } /* for all generators */
902
903                     /* Adds instrument zone modulators (global and local) to the voice.*/
904                     fluid_defpreset_noteon_add_mod_to_voice(voice,
905                                                             /* global instrument modulators */
906                                                             global_inst_zone ? global_inst_zone->mod : NULL,
907                                                             inst_zone->mod, /* local instrument modulators */
908                                                             FLUID_VOICE_OVERWRITE); /* mode */
909
910                     /* Preset level, generators */
911
912                     for(i = 0; i < GEN_LAST; i++)
913                     {
914
915                         /* SF 2.01 section 8.5 page 58: If some generators are
916                          encountered at preset level, they should be ignored.
917                          However this check is not necessary when the soundfont
918                          loader has ignored invalid preset generators.
919                          Actually load_pgen()has ignored these invalid preset
920                          generators:
921                            GEN_STARTADDROFS,      GEN_ENDADDROFS,
922                            GEN_STARTLOOPADDROFS,  GEN_ENDLOOPADDROFS,
923                            GEN_STARTADDRCOARSEOFS,GEN_ENDADDRCOARSEOFS,
924                            GEN_STARTLOOPADDRCOARSEOFS,
925                            GEN_KEYNUM, GEN_VELOCITY,
926                            GEN_ENDLOOPADDRCOARSEOFS,
927                            GEN_SAMPLEMODE, GEN_EXCLUSIVECLASS,GEN_OVERRIDEROOTKEY
928                         */
929
930                         /* SF 2.01 section 9.4 'bullet' 9: A generator in a
931                          * local preset zone supersedes a global preset zone
932                          * generator.  The effect is -added- to the destination
933                          * summing node -> voice_gen_incr */
934
935                         if(preset_zone->gen[i].flags)
936                         {
937                             fluid_voice_gen_incr(voice, i, preset_zone->gen[i].val);
938                         }
939                         else if((global_preset_zone != NULL) && global_preset_zone->gen[i].flags)
940                         {
941                             fluid_voice_gen_incr(voice, i, global_preset_zone->gen[i].val);
942                         }
943                         else
944                         {
945                             /* The generator has not been defined in this preset
946                              * Do nothing, leave it unchanged.
947                              */
948                         }
949                     } /* for all generators */
950
951                     /* Adds preset zone modulators (global and local) to the voice.*/
952                     fluid_defpreset_noteon_add_mod_to_voice(voice,
953                                                             /* global preset modulators */
954                                                             global_preset_zone ? global_preset_zone->mod : NULL,
955                                                             preset_zone->mod, /* local preset modulators */
956                                                             FLUID_VOICE_ADD); /* mode */
957
958                     /* add the synthesis process to the synthesis loop. */
959                     fluid_synth_start_voice(synth, voice);
960
961                     /* Store the ID of the first voice that was created by this noteon event.
962                      * Exclusive class may only terminate older voices.
963                      * That avoids killing voices, which have just been created.
964                      * (a noteon event can create several voice processes with the same exclusive
965                      * class - for example when using stereo samples)
966                      */
967                 }
968             }
969         }
970
971         preset_zone = fluid_preset_zone_next(preset_zone);
972     }
973
974     return FLUID_OK;
975 }
976
977 /*
978  * fluid_defpreset_set_global_zone
979  */
980 int
981 fluid_defpreset_set_global_zone(fluid_defpreset_t *defpreset, fluid_preset_zone_t *zone)
982 {
983     defpreset->global_zone = zone;
984     return FLUID_OK;
985 }
986
987 /*
988  * fluid_defpreset_import_sfont
989  */
990 int
991 fluid_defpreset_import_sfont(fluid_defpreset_t *defpreset,
992                              SFPreset *sfpreset,
993                              fluid_defsfont_t *defsfont)
994 {
995     fluid_list_t *p;
996     SFZone *sfzone;
997     fluid_preset_zone_t *zone;
998     int count;
999     char zone_name[256];
1000
1001     if(FLUID_STRLEN(sfpreset->name) > 0)
1002     {
1003         FLUID_STRCPY(defpreset->name, sfpreset->name);
1004     }
1005     else
1006     {
1007         FLUID_SNPRINTF(defpreset->name, sizeof(defpreset->name), "Bank%d,Pre%d", sfpreset->bank, sfpreset->prenum);
1008     }
1009
1010     defpreset->bank = sfpreset->bank;
1011     defpreset->num = sfpreset->prenum;
1012     p = sfpreset->zone;
1013     count = 0;
1014
1015     while(p != NULL)
1016     {
1017         sfzone = (SFZone *)fluid_list_get(p);
1018         FLUID_SNPRINTF(zone_name, sizeof(zone_name), "%s/%d", defpreset->name, count);
1019         zone = new_fluid_preset_zone(zone_name);
1020
1021         if(zone == NULL)
1022         {
1023             return FLUID_FAILED;
1024         }
1025
1026         if(fluid_preset_zone_import_sfont(zone, sfzone, defsfont) != FLUID_OK)
1027         {
1028             delete_fluid_preset_zone(zone);
1029             return FLUID_FAILED;
1030         }
1031
1032         if((count == 0) && (fluid_preset_zone_get_inst(zone) == NULL))
1033         {
1034             fluid_defpreset_set_global_zone(defpreset, zone);
1035         }
1036         else if(fluid_defpreset_add_zone(defpreset, zone) != FLUID_OK)
1037         {
1038             return FLUID_FAILED;
1039         }
1040
1041         p = fluid_list_next(p);
1042         count++;
1043     }
1044
1045     return FLUID_OK;
1046 }
1047
1048 /*
1049  * fluid_defpreset_add_zone
1050  */
1051 int
1052 fluid_defpreset_add_zone(fluid_defpreset_t *defpreset, fluid_preset_zone_t *zone)
1053 {
1054     if(defpreset->zone == NULL)
1055     {
1056         zone->next = NULL;
1057         defpreset->zone = zone;
1058     }
1059     else
1060     {
1061         zone->next = defpreset->zone;
1062         defpreset->zone = zone;
1063     }
1064
1065     return FLUID_OK;
1066 }
1067
1068 /*
1069  * fluid_defpreset_get_zone
1070  */
1071 fluid_preset_zone_t *
1072 fluid_defpreset_get_zone(fluid_defpreset_t *defpreset)
1073 {
1074     return defpreset->zone;
1075 }
1076
1077 /*
1078  * fluid_defpreset_get_global_zone
1079  */
1080 fluid_preset_zone_t *
1081 fluid_defpreset_get_global_zone(fluid_defpreset_t *defpreset)
1082 {
1083     return defpreset->global_zone;
1084 }
1085
1086 /***************************************************************
1087  *
1088  *                           PRESET_ZONE
1089  */
1090
1091 /*
1092  * fluid_preset_zone_next
1093  */
1094 fluid_preset_zone_t *
1095 fluid_preset_zone_next(fluid_preset_zone_t *zone)
1096 {
1097     return zone->next;
1098 }
1099
1100 /*
1101  * new_fluid_preset_zone
1102  */
1103 fluid_preset_zone_t *
1104 new_fluid_preset_zone(char *name)
1105 {
1106     fluid_preset_zone_t *zone = NULL;
1107     zone = FLUID_NEW(fluid_preset_zone_t);
1108
1109     if(zone == NULL)
1110     {
1111         FLUID_LOG(FLUID_ERR, "Out of memory");
1112         return NULL;
1113     }
1114
1115     zone->next = NULL;
1116     zone->voice_zone = NULL;
1117     zone->name = FLUID_STRDUP(name);
1118
1119     if(zone->name == NULL)
1120     {
1121         FLUID_LOG(FLUID_ERR, "Out of memory");
1122         FLUID_FREE(zone);
1123         return NULL;
1124     }
1125
1126     zone->inst = NULL;
1127     zone->range.keylo = 0;
1128     zone->range.keyhi = 128;
1129     zone->range.vello = 0;
1130     zone->range.velhi = 128;
1131     zone->range.ignore = FALSE;
1132
1133     /* Flag all generators as unused (default, they will be set when they are found
1134      * in the sound font).
1135      * This also sets the generator values to default, but that is of no concern here.*/
1136     fluid_gen_set_default_values(&zone->gen[0]);
1137     zone->mod = NULL; /* list of modulators */
1138     return zone;
1139 }
1140
1141 /*
1142  * delete list of modulators.
1143  */
1144 static void delete_fluid_list_mod(fluid_mod_t *mod)
1145 {
1146     fluid_mod_t *tmp;
1147
1148     while(mod)  /* delete the modulators */
1149     {
1150         tmp = mod;
1151         mod = mod->next;
1152         delete_fluid_mod(tmp);
1153     }
1154 }
1155
1156 /*
1157  * delete_fluid_preset_zone
1158  */
1159 void
1160 delete_fluid_preset_zone(fluid_preset_zone_t *zone)
1161 {
1162     fluid_list_t *list;
1163
1164     fluid_return_if_fail(zone != NULL);
1165
1166     delete_fluid_list_mod(zone->mod);
1167
1168     for(list = zone->voice_zone; list != NULL; list = fluid_list_next(list))
1169     {
1170         FLUID_FREE(fluid_list_get(list));
1171     }
1172
1173     delete_fluid_list(zone->voice_zone);
1174
1175     FLUID_FREE(zone->name);
1176     FLUID_FREE(zone);
1177 }
1178
1179 static int fluid_preset_zone_create_voice_zones(fluid_preset_zone_t *preset_zone)
1180 {
1181     fluid_inst_zone_t *inst_zone;
1182     fluid_sample_t *sample;
1183     fluid_voice_zone_t *voice_zone;
1184     fluid_zone_range_t *irange;
1185     fluid_zone_range_t *prange = &preset_zone->range;
1186
1187     fluid_return_val_if_fail(preset_zone->inst != NULL, FLUID_FAILED);
1188
1189     inst_zone = fluid_inst_get_zone(preset_zone->inst);
1190
1191     while(inst_zone != NULL)
1192     {
1193
1194         /* We only create voice ranges for zones that could actually start a voice,
1195          * i.e. that have a sample and don't point to ROM */
1196         sample = fluid_inst_zone_get_sample(inst_zone);
1197
1198         if((sample == NULL) || fluid_sample_in_rom(sample))
1199         {
1200             inst_zone = fluid_inst_zone_next(inst_zone);
1201             continue;
1202         }
1203
1204         voice_zone = FLUID_NEW(fluid_voice_zone_t);
1205
1206         if(voice_zone == NULL)
1207         {
1208             FLUID_LOG(FLUID_ERR, "Out of memory");
1209             return FLUID_FAILED;
1210         }
1211
1212         voice_zone->inst_zone = inst_zone;
1213
1214         irange = &inst_zone->range;
1215
1216         voice_zone->range.keylo = (prange->keylo > irange->keylo) ? prange->keylo : irange->keylo;
1217         voice_zone->range.keyhi = (prange->keyhi < irange->keyhi) ? prange->keyhi : irange->keyhi;
1218         voice_zone->range.vello = (prange->vello > irange->vello) ? prange->vello : irange->vello;
1219         voice_zone->range.velhi = (prange->velhi < irange->velhi) ? prange->velhi : irange->velhi;
1220         voice_zone->range.ignore = FALSE;
1221
1222         preset_zone->voice_zone = fluid_list_append(preset_zone->voice_zone, voice_zone);
1223
1224         inst_zone = fluid_inst_zone_next(inst_zone);
1225     }
1226
1227     return FLUID_OK;
1228 }
1229
1230 /**
1231  * Checks if modulator mod is identic to another modulator in the list
1232  * (specs SF 2.0X  7.4, 7.8).
1233  * @param mod, modulator list.
1234  * @param name, if not NULL, pointer on a string displayed as warning.
1235  * @return TRUE if mod is identic to another modulator, FALSE otherwise.
1236  */
1237 static int
1238 fluid_zone_is_mod_identic(fluid_mod_t *mod, char *name)
1239 {
1240     fluid_mod_t *next = mod->next;
1241
1242     while(next)
1243     {
1244         /* is mod identic to next ? */
1245         if(fluid_mod_test_identity(mod, next))
1246         {
1247             if(name)
1248             {
1249                 FLUID_LOG(FLUID_WARN, "Ignoring identic modulator %s", name);
1250             }
1251
1252             return TRUE;
1253         }
1254
1255         next = next->next;
1256     }
1257
1258     return FALSE;
1259 }
1260
1261 /**
1262  * Limits the number of modulators in a modulator list.
1263  * This is appropriate to internal synthesizer modulators tables
1264  * which have a fixed size (FLUID_NUM_MOD).
1265  *
1266  * @param zone_name, zone name
1267  * @param list_mod, address of pointer on modulator list.
1268  */
1269 static void fluid_limit_mod_list(char *zone_name, fluid_mod_t **list_mod)
1270 {
1271     int mod_idx = 0; /* modulator index */
1272     fluid_mod_t *prev_mod = NULL; /* previous modulator in list_mod */
1273     fluid_mod_t *mod = *list_mod; /* first modulator in list_mod */
1274
1275     while(mod)
1276     {
1277         if((mod_idx + 1) > FLUID_NUM_MOD)
1278         {
1279             /* truncation of list_mod */
1280             if(mod_idx)
1281             {
1282                 prev_mod->next = NULL;
1283             }
1284             else
1285             {
1286                 *list_mod = NULL;
1287             }
1288
1289             delete_fluid_list_mod(mod);
1290             FLUID_LOG(FLUID_WARN, "%s, modulators count limited to %d", zone_name,
1291                       FLUID_NUM_MOD);
1292             break;
1293         }
1294
1295         mod_idx++;
1296         prev_mod = mod;
1297         mod = mod->next;
1298     }
1299 }
1300
1301 /**
1302  * Checks and remove invalid modulators from a zone modulators list.
1303  * - checks valid modulator sources (specs SF 2.01  7.4, 7.8, 8.2.1).
1304  * - checks identic modulators in the list (specs SF 2.01  7.4, 7.8).
1305  * @param zone_name, zone name.
1306  * @param list_mod, address of pointer on modulators list.
1307  */
1308 static void
1309 fluid_zone_check_mod(char *zone_name, fluid_mod_t **list_mod)
1310 {
1311     fluid_mod_t *prev_mod = NULL; /* previous modulator in list_mod */
1312     fluid_mod_t *mod = *list_mod; /* first modulator in list_mod */
1313     int mod_idx = 0; /* modulator index */
1314
1315     while(mod)
1316     {
1317         char zone_mod_name[256];
1318         fluid_mod_t *next = mod->next;
1319
1320         /* prepare modulator name: zonename/#modulator */
1321         FLUID_SNPRINTF(zone_mod_name, sizeof(zone_mod_name), "%s/mod%d", zone_name, mod_idx);
1322
1323         /* has mod invalid sources ? */
1324         if(!fluid_mod_check_sources(mod,  zone_mod_name)
1325                 /* or is mod identic to any following modulator ? */
1326                 || fluid_zone_is_mod_identic(mod, zone_mod_name))
1327         {
1328             /* the modulator is useless so we remove it */
1329             if(prev_mod)
1330             {
1331                 prev_mod->next = next;
1332             }
1333             else
1334             {
1335                 *list_mod = next;
1336             }
1337
1338             delete_fluid_mod(mod); /* freeing */
1339         }
1340         else
1341         {
1342             prev_mod = mod;
1343         }
1344
1345         mod = next;
1346         mod_idx++;
1347     }
1348
1349     /* limits the size of modulators list */
1350     fluid_limit_mod_list(zone_name, list_mod);
1351 }
1352
1353 /*
1354  * fluid_zone_gen_import_sfont
1355  * Imports generators from sfzone to gen and range.
1356  * @param gen, pointer on destination generators table.
1357  * @param range, pointer on destination range generators.
1358  * @param sfzone, pointer on soundfont zone generators.
1359  */
1360 static void
1361 fluid_zone_gen_import_sfont(fluid_gen_t *gen, fluid_zone_range_t *range, SFZone *sfzone)
1362 {
1363     fluid_list_t *r;
1364     SFGen *sfgen;
1365
1366     for(r = sfzone->gen; r != NULL;)
1367     {
1368         sfgen = (SFGen *)fluid_list_get(r);
1369
1370         switch(sfgen->id)
1371         {
1372         case GEN_KEYRANGE:
1373             range->keylo = sfgen->amount.range.lo;
1374             range->keyhi = sfgen->amount.range.hi;
1375             break;
1376
1377         case GEN_VELRANGE:
1378             range->vello = sfgen->amount.range.lo;
1379             range->velhi = sfgen->amount.range.hi;
1380             break;
1381
1382         case GEN_ATTENUATION:
1383             /* EMU8k/10k hardware applies a scale factor to initial attenuation generator values set at
1384              * preset and instrument level */
1385             gen[sfgen->id].val = (fluid_real_t) sfgen->amount.sword * EMU_ATTENUATION_FACTOR;
1386             gen[sfgen->id].flags = GEN_SET;
1387             break;
1388
1389         default:
1390             /* FIXME: some generators have an unsigne word amount value but i don't know which ones */
1391             gen[sfgen->id].val = (fluid_real_t) sfgen->amount.sword;
1392             gen[sfgen->id].flags = GEN_SET;
1393             break;
1394         }
1395
1396         r = fluid_list_next(r);
1397     }
1398 }
1399
1400 /*
1401  * fluid_zone_mod_source_import_sfont
1402  * Imports source information from sf_source to src and flags.
1403  * @param src, pointer on destination modulator source.
1404  * @param flags, pointer on destination modulator flags.
1405  * @param sf_source, soundfont modulator source.
1406  * @return return TRUE if success, FALSE if source type is unknow.
1407  */
1408 static int
1409 fluid_zone_mod_source_import_sfont(unsigned char *src, unsigned char *flags, unsigned short sf_source)
1410 {
1411     int type;
1412     unsigned char flags_dest; /* destination flags */
1413
1414     /* sources */
1415     *src = sf_source & 127; /* index of source, seven-bit value, SF2.01 section 8.2, page 50 */
1416
1417     /* Bit 7: CC flag SF 2.01 section 8.2.1 page 50*/
1418     flags_dest = 0;
1419
1420     if(sf_source & (1 << 7))
1421     {
1422         flags_dest |= FLUID_MOD_CC;
1423     }
1424     else
1425     {
1426         flags_dest |= FLUID_MOD_GC;
1427     }
1428
1429     /* Bit 8: D flag SF 2.01 section 8.2.2 page 51*/
1430     if(sf_source & (1 << 8))
1431     {
1432         flags_dest |= FLUID_MOD_NEGATIVE;
1433     }
1434     else
1435     {
1436         flags_dest |= FLUID_MOD_POSITIVE;
1437     }
1438
1439     /* Bit 9: P flag SF 2.01 section 8.2.3 page 51*/
1440     if(sf_source & (1 << 9))
1441     {
1442         flags_dest |= FLUID_MOD_BIPOLAR;
1443     }
1444     else
1445     {
1446         flags_dest |= FLUID_MOD_UNIPOLAR;
1447     }
1448
1449     /* modulator source types: SF2.01 section 8.2.1 page 52 */
1450     type = sf_source >> 10;
1451     type &= 63; /* type is a 6-bit value */
1452
1453     if(type == 0)
1454     {
1455         flags_dest |= FLUID_MOD_LINEAR;
1456     }
1457     else if(type == 1)
1458     {
1459         flags_dest |= FLUID_MOD_CONCAVE;
1460     }
1461     else if(type == 2)
1462     {
1463         flags_dest |= FLUID_MOD_CONVEX;
1464     }
1465     else if(type == 3)
1466     {
1467         flags_dest |= FLUID_MOD_SWITCH;
1468     }
1469     else
1470     {
1471         *flags = flags_dest;
1472         /* This shouldn't happen - unknown type! */
1473         return FALSE;
1474     }
1475
1476     *flags = flags_dest;
1477     return TRUE;
1478 }
1479
1480 /*
1481  * fluid_zone_mod_import_sfont
1482  * Imports modulators from sfzone to modulators list mod.
1483  * @param zone_name, zone name.
1484  * @param mod, address of pointer on modulators list to return.
1485  * @param sfzone, pointer on soundfont zone.
1486  * @return FLUID_OK if success, FLUID_FAILED otherwise.
1487  */
1488 static int
1489 fluid_zone_mod_import_sfont(char *zone_name, fluid_mod_t **mod, SFZone *sfzone)
1490 {
1491     fluid_list_t *r;
1492     int count;
1493
1494     /* Import the modulators (only SF2.1 and higher) */
1495     for(count = 0, r = sfzone->mod; r != NULL; count++)
1496     {
1497
1498         SFMod *mod_src = (SFMod *)fluid_list_get(r);
1499         fluid_mod_t *mod_dest = new_fluid_mod();
1500
1501         if(mod_dest == NULL)
1502         {
1503             return FLUID_FAILED;
1504         }
1505
1506         mod_dest->next = NULL; /* pointer to next modulator, this is the end of the list now.*/
1507
1508         /* *** Amount *** */
1509         mod_dest->amount = mod_src->amount;
1510
1511         /* *** Source *** */
1512         if(!fluid_zone_mod_source_import_sfont(&mod_dest->src1, &mod_dest->flags1, mod_src->src))
1513         {
1514             /* This shouldn't happen - unknown type!
1515              * Deactivate the modulator by setting the amount to 0. */
1516             mod_dest->amount = 0;
1517         }  
1518
1519         /* Note: When primary source input (src1) is set to General Controller 'No Controller',
1520            output will be forced to 0.0 at synthesis time (see fluid_mod_get_value()).
1521            That means that the minimum value of the modulator will be always 0.0.
1522            We need to force amount value to 0 to ensure a correct evaluation of the minimum
1523            value later (see fluid_voice_get_lower_boundary_for_attenuation()).
1524         */
1525         if(((mod_dest->flags1 & FLUID_MOD_CC) == FLUID_MOD_GC) && 
1526             (mod_dest->src1 == FLUID_MOD_NONE))
1527         {
1528             mod_dest->amount = 0;
1529         }
1530
1531         /* *** Dest *** */
1532         mod_dest->dest = mod_src->dest; /* index of controlled generator */
1533
1534         /* *** Amount source *** */
1535         if(!fluid_zone_mod_source_import_sfont(&mod_dest->src2, &mod_dest->flags2, mod_src->amtsrc))
1536         {
1537             /* This shouldn't happen - unknown type!
1538              * Deactivate the modulator by setting the amount to 0. */
1539             mod_dest->amount = 0;
1540         }  
1541         /* Note: When secondary source input (src2) is set to General Controller 'No Controller',
1542            output will be forced to +1.0 at synthesis time (see fluid_mod_get_value()).
1543            That means that this source will behave unipolar only. We need to force the
1544            unipolar flag to ensure to ensure a correct evaluation of the minimum
1545            value later (see fluid_voice_get_lower_boundary_for_attenuation()).
1546         */
1547         if(((mod_dest->flags2 & FLUID_MOD_CC) == FLUID_MOD_GC) && 
1548             (mod_dest->src2 == FLUID_MOD_NONE))
1549         {
1550             mod_dest->flags2 &= ~FLUID_MOD_BIPOLAR;
1551         }
1552
1553         /* *** Transform *** */
1554         /* SF2.01 only uses the 'linear' transform (0).
1555          * Deactivate the modulator by setting the amount to 0 in any other case.
1556          */
1557         if(mod_src->trans != 0)
1558         {
1559             mod_dest->amount = 0;
1560         }
1561
1562         /* Store the new modulator in the zone The order of modulators
1563          * will make a difference, at least in an instrument context: The
1564          * second modulator overwrites the first one, if they only differ
1565          * in amount. */
1566         if(count == 0)
1567         {
1568             *mod = mod_dest;
1569         }
1570         else
1571         {
1572             fluid_mod_t *last_mod = *mod;
1573
1574             /* Find the end of the list */
1575             while(last_mod->next != NULL)
1576             {
1577                 last_mod = last_mod->next;
1578             }
1579
1580             last_mod->next = mod_dest;
1581         }
1582
1583         r = fluid_list_next(r);
1584     } /* foreach modulator */
1585
1586     /* checks and removes invalid modulators in modulators list*/
1587     fluid_zone_check_mod(zone_name, mod);
1588     return FLUID_OK;
1589 }
1590
1591 /*
1592  * fluid_preset_zone_import_sfont
1593  */
1594 int
1595 fluid_preset_zone_import_sfont(fluid_preset_zone_t *zone, SFZone *sfzone, fluid_defsfont_t *defsfont)
1596 {
1597     /* import the generators */
1598     fluid_zone_gen_import_sfont(zone->gen, &zone->range, sfzone);
1599
1600     if((sfzone->instsamp != NULL) && (sfzone->instsamp->data != NULL))
1601     {
1602         SFInst *sfinst = sfzone->instsamp->data;
1603
1604         zone->inst = find_inst_by_idx(defsfont, sfinst->idx);
1605
1606         if(zone->inst == NULL)
1607         {
1608             zone->inst = fluid_inst_import_sfont(zone, sfinst, defsfont);
1609         }
1610
1611         if(zone->inst == NULL)
1612         {
1613             return FLUID_FAILED;
1614         }
1615
1616         if(fluid_preset_zone_create_voice_zones(zone) == FLUID_FAILED)
1617         {
1618             return FLUID_FAILED;
1619         }
1620     }
1621
1622     /* Import the modulators (only SF2.1 and higher) */
1623     return fluid_zone_mod_import_sfont(zone->name, &zone->mod, sfzone);
1624 }
1625
1626 /*
1627  * fluid_preset_zone_get_inst
1628  */
1629 fluid_inst_t *
1630 fluid_preset_zone_get_inst(fluid_preset_zone_t *zone)
1631 {
1632     return zone->inst;
1633 }
1634
1635
1636 /***************************************************************
1637  *
1638  *                           INST
1639  */
1640
1641 /*
1642  * new_fluid_inst
1643  */
1644 fluid_inst_t *
1645 new_fluid_inst()
1646 {
1647     fluid_inst_t *inst = FLUID_NEW(fluid_inst_t);
1648
1649     if(inst == NULL)
1650     {
1651         FLUID_LOG(FLUID_ERR, "Out of memory");
1652         return NULL;
1653     }
1654
1655     inst->name[0] = 0;
1656     inst->global_zone = NULL;
1657     inst->zone = NULL;
1658     return inst;
1659 }
1660
1661 /*
1662  * delete_fluid_inst
1663  */
1664 void
1665 delete_fluid_inst(fluid_inst_t *inst)
1666 {
1667     fluid_inst_zone_t *zone;
1668
1669     fluid_return_if_fail(inst != NULL);
1670
1671     delete_fluid_inst_zone(inst->global_zone);
1672     inst->global_zone = NULL;
1673
1674     zone = inst->zone;
1675
1676     while(zone != NULL)
1677     {
1678         inst->zone = zone->next;
1679         delete_fluid_inst_zone(zone);
1680         zone = inst->zone;
1681     }
1682
1683     FLUID_FREE(inst);
1684 }
1685
1686 /*
1687  * fluid_inst_set_global_zone
1688  */
1689 int
1690 fluid_inst_set_global_zone(fluid_inst_t *inst, fluid_inst_zone_t *zone)
1691 {
1692     inst->global_zone = zone;
1693     return FLUID_OK;
1694 }
1695
1696 /*
1697  * fluid_inst_import_sfont
1698  */
1699 fluid_inst_t *
1700 fluid_inst_import_sfont(fluid_preset_zone_t *preset_zone, SFInst *sfinst, fluid_defsfont_t *defsfont)
1701 {
1702     fluid_list_t *p;
1703     fluid_inst_t *inst;
1704     SFZone *sfzone;
1705     fluid_inst_zone_t *inst_zone;
1706     char zone_name[256];
1707     int count;
1708
1709     inst = (fluid_inst_t *) new_fluid_inst();
1710
1711     if(inst == NULL)
1712     {
1713         FLUID_LOG(FLUID_ERR, "Out of memory");
1714         return NULL;
1715     }
1716
1717     inst->source_idx = sfinst->idx;
1718
1719     p = sfinst->zone;
1720
1721     if(FLUID_STRLEN(sfinst->name) > 0)
1722     {
1723         FLUID_STRCPY(inst->name, sfinst->name);
1724     }
1725     else
1726     {
1727         FLUID_STRCPY(inst->name, "<untitled>");
1728     }
1729
1730     count = 0;
1731
1732     while(p != NULL)
1733     {
1734
1735         sfzone = (SFZone *)fluid_list_get(p);
1736         /* integrates preset zone name in instrument zone name */
1737         FLUID_SNPRINTF(zone_name, sizeof(zone_name), "%s/%s/%d", preset_zone->name,
1738                        inst->name, count);
1739
1740         inst_zone = new_fluid_inst_zone(zone_name);
1741
1742         if(inst_zone == NULL)
1743         {
1744             return NULL;
1745         }
1746
1747         if(fluid_inst_zone_import_sfont(inst_zone, sfzone, defsfont) != FLUID_OK)
1748         {
1749             delete_fluid_inst_zone(inst_zone);
1750             return NULL;
1751         }
1752
1753         if((count == 0) && (fluid_inst_zone_get_sample(inst_zone) == NULL))
1754         {
1755             fluid_inst_set_global_zone(inst, inst_zone);
1756
1757         }
1758         else if(fluid_inst_add_zone(inst, inst_zone) != FLUID_OK)
1759         {
1760             return NULL;
1761         }
1762
1763         p = fluid_list_next(p);
1764         count++;
1765     }
1766
1767     defsfont->inst = fluid_list_append(defsfont->inst, inst);
1768     return inst;
1769 }
1770
1771 /*
1772  * fluid_inst_add_zone
1773  */
1774 int
1775 fluid_inst_add_zone(fluid_inst_t *inst, fluid_inst_zone_t *zone)
1776 {
1777     if(inst->zone == NULL)
1778     {
1779         zone->next = NULL;
1780         inst->zone = zone;
1781     }
1782     else
1783     {
1784         zone->next = inst->zone;
1785         inst->zone = zone;
1786     }
1787
1788     return FLUID_OK;
1789 }
1790
1791 /*
1792  * fluid_inst_get_zone
1793  */
1794 fluid_inst_zone_t *
1795 fluid_inst_get_zone(fluid_inst_t *inst)
1796 {
1797     return inst->zone;
1798 }
1799
1800 /*
1801  * fluid_inst_get_global_zone
1802  */
1803 fluid_inst_zone_t *
1804 fluid_inst_get_global_zone(fluid_inst_t *inst)
1805 {
1806     return inst->global_zone;
1807 }
1808
1809 /***************************************************************
1810  *
1811  *                           INST_ZONE
1812  */
1813
1814 /*
1815  * new_fluid_inst_zone
1816  */
1817 fluid_inst_zone_t *
1818 new_fluid_inst_zone(char *name)
1819 {
1820     fluid_inst_zone_t *zone = NULL;
1821     zone = FLUID_NEW(fluid_inst_zone_t);
1822
1823     if(zone == NULL)
1824     {
1825         FLUID_LOG(FLUID_ERR, "Out of memory");
1826         return NULL;
1827     }
1828
1829     zone->next = NULL;
1830     zone->name = FLUID_STRDUP(name);
1831
1832     if(zone->name == NULL)
1833     {
1834         FLUID_LOG(FLUID_ERR, "Out of memory");
1835         FLUID_FREE(zone);
1836         return NULL;
1837     }
1838
1839     zone->sample = NULL;
1840     zone->range.keylo = 0;
1841     zone->range.keyhi = 128;
1842     zone->range.vello = 0;
1843     zone->range.velhi = 128;
1844     zone->range.ignore = FALSE;
1845     /* Flag the generators as unused.
1846      * This also sets the generator values to default, but they will be overwritten anyway, if used.*/
1847     fluid_gen_set_default_values(&zone->gen[0]);
1848     zone->mod = NULL; /* list of modulators */
1849     return zone;
1850 }
1851
1852 /*
1853  * delete_fluid_inst_zone
1854  */
1855 void
1856 delete_fluid_inst_zone(fluid_inst_zone_t *zone)
1857 {
1858     fluid_return_if_fail(zone != NULL);
1859
1860     delete_fluid_list_mod(zone->mod);
1861
1862     FLUID_FREE(zone->name);
1863     FLUID_FREE(zone);
1864 }
1865
1866 /*
1867  * fluid_inst_zone_next
1868  */
1869 fluid_inst_zone_t *
1870 fluid_inst_zone_next(fluid_inst_zone_t *zone)
1871 {
1872     return zone->next;
1873 }
1874
1875 /*
1876  * fluid_inst_zone_import_sfont
1877  */
1878 int
1879 fluid_inst_zone_import_sfont(fluid_inst_zone_t *inst_zone, SFZone *sfzone, fluid_defsfont_t *defsfont)
1880 {
1881     /* import the generators */
1882     fluid_zone_gen_import_sfont(inst_zone->gen, &inst_zone->range, sfzone);
1883
1884     /* FIXME */
1885     /*    if (zone->gen[GEN_EXCLUSIVECLASS].flags == GEN_SET) { */
1886     /*      FLUID_LOG(FLUID_DBG, "ExclusiveClass=%d\n", (int) zone->gen[GEN_EXCLUSIVECLASS].val); */
1887     /*    } */
1888
1889     /* fixup sample pointer */
1890     if((sfzone->instsamp != NULL) && (sfzone->instsamp->data != NULL))
1891     {
1892         inst_zone->sample = ((SFSample *)(sfzone->instsamp->data))->fluid_sample;
1893     }
1894
1895     /* Import the modulators (only SF2.1 and higher) */
1896     return fluid_zone_mod_import_sfont(inst_zone->name, &inst_zone->mod, sfzone);
1897 }
1898
1899 /*
1900  * fluid_inst_zone_get_sample
1901  */
1902 fluid_sample_t *
1903 fluid_inst_zone_get_sample(fluid_inst_zone_t *zone)
1904 {
1905     return zone->sample;
1906 }
1907
1908
1909 int
1910 fluid_zone_inside_range(fluid_zone_range_t *range, int key, int vel)
1911 {
1912     /* ignoreInstrumentZone is set in mono legato playing */
1913     int ignore_zone = range->ignore;
1914
1915     /* Reset the 'ignore' request */
1916     range->ignore = FALSE;
1917
1918     return !ignore_zone && ((range->keylo <= key) &&
1919                             (range->keyhi >= key) &&
1920                             (range->vello <= vel) &&
1921                             (range->velhi >= vel));
1922 }
1923
1924 /***************************************************************
1925  *
1926  *                           SAMPLE
1927  */
1928
1929 /*
1930  * fluid_sample_in_rom
1931  */
1932 int
1933 fluid_sample_in_rom(fluid_sample_t *sample)
1934 {
1935     return (sample->sampletype & FLUID_SAMPLETYPE_ROM);
1936 }
1937
1938
1939 /*
1940  * fluid_sample_import_sfont
1941  */
1942 int
1943 fluid_sample_import_sfont(fluid_sample_t *sample, SFSample *sfsample, fluid_defsfont_t *defsfont)
1944 {
1945     FLUID_STRCPY(sample->name, sfsample->name);
1946
1947     sample->source_start = sfsample->start;
1948     sample->source_end = (sfsample->end > 0) ? sfsample->end - 1 : 0; /* marks last sample, contrary to SF spec. */
1949     sample->source_loopstart = sfsample->loopstart;
1950     sample->source_loopend = sfsample->loopend;
1951
1952     sample->start = sample->source_start;
1953     sample->end = sample->source_end;
1954     sample->loopstart = sample->source_loopstart;
1955     sample->loopend = sample->source_loopend;
1956     sample->samplerate = sfsample->samplerate;
1957     sample->origpitch = sfsample->origpitch;
1958     sample->pitchadj = sfsample->pitchadj;
1959     sample->sampletype = sfsample->sampletype;
1960
1961     if(defsfont->dynamic_samples)
1962     {
1963         sample->notify = dynamic_samples_sample_notify;
1964     }
1965
1966     if(fluid_sample_validate(sample, defsfont->samplesize) == FLUID_FAILED)
1967     {
1968         return FLUID_FAILED;
1969     }
1970
1971     return FLUID_OK;
1972 }
1973
1974 /* Called if a sample is no longer used by a voice. Used by dynamic sample loading
1975  * to unload a sample that is not used by any loaded presets anymore but couldn't
1976  * be unloaded straight away because it was still in use by a voice. */
1977 static int dynamic_samples_sample_notify(fluid_sample_t *sample, int reason)
1978 {
1979     if(reason == FLUID_SAMPLE_DONE && sample->preset_count == 0)
1980     {
1981         unload_sample(sample);
1982     }
1983
1984     return FLUID_OK;
1985 }
1986
1987 /* Called if a preset has been selected for or unselected from a channel. Used by
1988  * dynamic sample loading to load and unload samples on demand. */
1989 static int dynamic_samples_preset_notify(fluid_preset_t *preset, int reason, int chan)
1990 {
1991     fluid_defsfont_t *defsfont;
1992
1993     if(reason == FLUID_PRESET_SELECTED)
1994     {
1995         FLUID_LOG(FLUID_DBG, "Selected preset '%s' on channel %d", fluid_preset_get_name(preset), chan);
1996         defsfont = fluid_sfont_get_data(preset->sfont);
1997         load_preset_samples(defsfont, preset);
1998     }
1999     else if(reason == FLUID_PRESET_UNSELECTED)
2000     {
2001         FLUID_LOG(FLUID_DBG, "Deselected preset '%s' from channel %d", fluid_preset_get_name(preset), chan);
2002         defsfont = fluid_sfont_get_data(preset->sfont);
2003         unload_preset_samples(defsfont, preset);
2004     }
2005
2006     return FLUID_OK;
2007 }
2008
2009
2010 /* Walk through all samples used by the passed in preset and make sure that the
2011  * sample data is loaded for each sample. Used by dynamic sample loading. */
2012 static int load_preset_samples(fluid_defsfont_t *defsfont, fluid_preset_t *preset)
2013 {
2014     fluid_defpreset_t *defpreset;
2015     fluid_preset_zone_t *preset_zone;
2016     fluid_inst_t *inst;
2017     fluid_inst_zone_t *inst_zone;
2018     fluid_sample_t *sample;
2019     SFData *sffile = NULL;
2020
2021     defpreset = fluid_preset_get_data(preset);
2022     preset_zone = fluid_defpreset_get_zone(defpreset);
2023
2024     while(preset_zone != NULL)
2025     {
2026         inst = fluid_preset_zone_get_inst(preset_zone);
2027         inst_zone = fluid_inst_get_zone(inst);
2028
2029         while(inst_zone != NULL)
2030         {
2031             sample = fluid_inst_zone_get_sample(inst_zone);
2032
2033             if((sample != NULL) && (sample->start != sample->end))
2034             {
2035                 sample->preset_count++;
2036
2037                 /* If this is the first time this sample has been selected,
2038                  * load the sampledata */
2039                 if(sample->preset_count == 1)
2040                 {
2041                     /* Make sure we have an open Soundfont file. Do this here
2042                      * to avoid having to open the file if no loading is necessary
2043                      * for a preset */
2044                     if(sffile == NULL)
2045                     {
2046                         sffile = fluid_sffile_open(defsfont->filename, defsfont->fcbs);
2047
2048                         if(sffile == NULL)
2049                         {
2050                             FLUID_LOG(FLUID_ERR, "Unable to open Soundfont file");
2051                             return FLUID_FAILED;
2052                         }
2053                     }
2054
2055                     if(fluid_defsfont_load_sampledata(defsfont, sffile, sample) == FLUID_OK)
2056                     {
2057                         fluid_sample_sanitize_loop(sample, (sample->end + 1) * sizeof(short));
2058                         fluid_voice_optimize_sample(sample);
2059                     }
2060                     else
2061                     {
2062                         FLUID_LOG(FLUID_ERR, "Unable to load sample '%s', disabling", sample->name);
2063                         sample->start = sample->end = 0;
2064                     }
2065                 }
2066             }
2067
2068             inst_zone = fluid_inst_zone_next(inst_zone);
2069         }
2070
2071         preset_zone = fluid_preset_zone_next(preset_zone);
2072     }
2073
2074     if(sffile != NULL)
2075     {
2076         fluid_sffile_close(sffile);
2077     }
2078
2079     return FLUID_OK;
2080 }
2081
2082 /* Walk through all samples used by the passed in preset and unload the sample data
2083  * of each sample that is not used by any selected preset anymore. Used by dynamic
2084  * sample loading. */
2085 static int unload_preset_samples(fluid_defsfont_t *defsfont, fluid_preset_t *preset)
2086 {
2087     fluid_defpreset_t *defpreset;
2088     fluid_preset_zone_t *preset_zone;
2089     fluid_inst_t *inst;
2090     fluid_inst_zone_t *inst_zone;
2091     fluid_sample_t *sample;
2092
2093     defpreset = fluid_preset_get_data(preset);
2094     preset_zone = fluid_defpreset_get_zone(defpreset);
2095
2096     while(preset_zone != NULL)
2097     {
2098         inst = fluid_preset_zone_get_inst(preset_zone);
2099         inst_zone = fluid_inst_get_zone(inst);
2100
2101         while(inst_zone != NULL)
2102         {
2103             sample = fluid_inst_zone_get_sample(inst_zone);
2104
2105             if((sample != NULL) && (sample->preset_count > 0))
2106             {
2107                 sample->preset_count--;
2108
2109                 /* If the sample is not used by any preset or used by a
2110                  * sounding voice, unload it from the sample cache. If it's
2111                  * still in use by a voice, dynamic_samples_sample_notify will
2112                  * take care of unloading the sample as soon as the voice is
2113                  * finished with it (but only on the next API call). */
2114                 if(sample->preset_count == 0 && sample->refcount == 0)
2115                 {
2116                     unload_sample(sample);
2117                 }
2118             }
2119
2120             inst_zone = fluid_inst_zone_next(inst_zone);
2121         }
2122
2123         preset_zone = fluid_preset_zone_next(preset_zone);
2124     }
2125
2126     return FLUID_OK;
2127 }
2128
2129 /* Unload an unused sample from the samplecache */
2130 static void unload_sample(fluid_sample_t *sample)
2131 {
2132     fluid_return_if_fail(sample != NULL);
2133     fluid_return_if_fail(sample->data != NULL);
2134     fluid_return_if_fail(sample->preset_count == 0);
2135     fluid_return_if_fail(sample->refcount == 0);
2136
2137     FLUID_LOG(FLUID_DBG, "Unloading sample '%s'", sample->name);
2138
2139     if(fluid_samplecache_unload(sample->data) == FLUID_FAILED)
2140     {
2141         FLUID_LOG(FLUID_ERR, "Unable to unload sample '%s'", sample->name);
2142     }
2143     else
2144     {
2145         sample->data = NULL;
2146         sample->data24 = NULL;
2147     }
2148 }
2149
2150 static fluid_inst_t *find_inst_by_idx(fluid_defsfont_t *defsfont, int idx)
2151 {
2152     fluid_list_t *list;
2153     fluid_inst_t *inst;
2154
2155     for(list = defsfont->inst; list != NULL; list = fluid_list_next(list))
2156     {
2157         inst = fluid_list_get(list);
2158
2159         if(inst->source_idx == idx)
2160         {
2161             return inst;
2162         }
2163     }
2164
2165     return NULL;
2166 }