Import libfluidsynth into the Ardour codebase
[ardour.git] / libs / fluidsynth / src / fluid_gen.c
1 /* FluidSynth - A Software Synthesizer
2  *
3  * Copyright (C) 2003  Peter Hanappe and others.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public License
7  * as published by the Free Software Foundation; either version 2 of
8  * the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the Free
17  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA
19  */
20
21
22 #include "fluid_gen.h"
23 #include "fluid_chan.h"
24
25
26 /* See SFSpec21 $8.1.3 */
27 fluid_gen_info_t fluid_gen_info[] = {
28         /* number/name             init  scale         min        max         def */
29         { GEN_STARTADDROFS,           1,     1,       0.0f,     1e10f,       0.0f },
30         { GEN_ENDADDROFS,             1,     1,     -1e10f,      0.0f,       0.0f },
31         { GEN_STARTLOOPADDROFS,       1,     1,     -1e10f,     1e10f,       0.0f },
32         { GEN_ENDLOOPADDROFS,         1,     1,     -1e10f,     1e10f,       0.0f },
33         { GEN_STARTADDRCOARSEOFS,     0,     1,       0.0f,     1e10f,       0.0f },
34         { GEN_MODLFOTOPITCH,          1,     2,  -12000.0f,  12000.0f,       0.0f },
35         { GEN_VIBLFOTOPITCH,          1,     2,  -12000.0f,  12000.0f,       0.0f },
36         { GEN_MODENVTOPITCH,          1,     2,  -12000.0f,  12000.0f,       0.0f },
37         { GEN_FILTERFC,               1,     2,    1500.0f,  13500.0f,   13500.0f },
38         { GEN_FILTERQ,                1,     1,       0.0f,    960.0f,       0.0f },
39         { GEN_MODLFOTOFILTERFC,       1,     2,  -12000.0f,  12000.0f,       0.0f },
40         { GEN_MODENVTOFILTERFC,       1,     2,  -12000.0f,  12000.0f,       0.0f },
41         { GEN_ENDADDRCOARSEOFS,       0,     1,     -1e10f,      0.0f,       0.0f },
42         { GEN_MODLFOTOVOL,            1,     1,    -960.0f,    960.0f,       0.0f },
43         { GEN_UNUSED1,                0,     0,       0.0f,      0.0f,       0.0f },
44         { GEN_CHORUSSEND,             1,     1,       0.0f,   1000.0f,       0.0f },
45         { GEN_REVERBSEND,             1,     1,       0.0f,   1000.0f,       0.0f },
46         { GEN_PAN,                    1,     1,    -500.0f,    500.0f,       0.0f },
47         { GEN_UNUSED2,                0,     0,       0.0f,      0.0f,       0.0f },
48         { GEN_UNUSED3,                0,     0,       0.0f,      0.0f,       0.0f },
49         { GEN_UNUSED4,                0,     0,       0.0f,      0.0f,       0.0f },
50         { GEN_MODLFODELAY,            1,     2,  -12000.0f,   5000.0f,  -12000.0f },
51         { GEN_MODLFOFREQ,             1,     4,  -16000.0f,   4500.0f,       0.0f },
52         { GEN_VIBLFODELAY,            1,     2,  -12000.0f,   5000.0f,  -12000.0f },
53         { GEN_VIBLFOFREQ,             1,     4,  -16000.0f,   4500.0f,       0.0f },
54         { GEN_MODENVDELAY,            1,     2,  -12000.0f,   5000.0f,  -12000.0f },
55         { GEN_MODENVATTACK,           1,     2,  -12000.0f,   8000.0f,  -12000.0f },
56         { GEN_MODENVHOLD,             1,     2,  -12000.0f,   5000.0f,  -12000.0f },
57         { GEN_MODENVDECAY,            1,     2,  -12000.0f,   8000.0f,  -12000.0f },
58         { GEN_MODENVSUSTAIN,          0,     1,       0.0f,   1000.0f,       0.0f },
59         { GEN_MODENVRELEASE,          1,     2,  -12000.0f,   8000.0f,  -12000.0f },
60         { GEN_KEYTOMODENVHOLD,        0,     1,   -1200.0f,   1200.0f,       0.0f },
61         { GEN_KEYTOMODENVDECAY,       0,     1,   -1200.0f,   1200.0f,       0.0f },
62         { GEN_VOLENVDELAY,            1,     2,  -12000.0f,   5000.0f,  -12000.0f },
63         { GEN_VOLENVATTACK,           1,     2,  -12000.0f,   8000.0f,  -12000.0f },
64         { GEN_VOLENVHOLD,             1,     2,  -12000.0f,   5000.0f,  -12000.0f },
65         { GEN_VOLENVDECAY,            1,     2,  -12000.0f,   8000.0f,  -12000.0f },
66         { GEN_VOLENVSUSTAIN,          0,     1,       0.0f,   1440.0f,       0.0f },
67         { GEN_VOLENVRELEASE,          1,     2,  -12000.0f,   8000.0f,  -12000.0f },
68         { GEN_KEYTOVOLENVHOLD,        0,     1,   -1200.0f,   1200.0f,       0.0f },
69         { GEN_KEYTOVOLENVDECAY,       0,     1,   -1200.0f,   1200.0f,       0.0f },
70         { GEN_INSTRUMENT,             0,     0,       0.0f,      0.0f,       0.0f },
71         { GEN_RESERVED1,              0,     0,       0.0f,      0.0f,       0.0f },
72         { GEN_KEYRANGE,               0,     0,       0.0f,    127.0f,       0.0f },
73         { GEN_VELRANGE,               0,     0,       0.0f,    127.0f,       0.0f },
74         { GEN_STARTLOOPADDRCOARSEOFS, 0,     1,     -1e10f,     1e10f,       0.0f },
75         { GEN_KEYNUM,                 1,     0,       0.0f,    127.0f,      -1.0f },
76         { GEN_VELOCITY,               1,     1,       0.0f,    127.0f,      -1.0f },
77         { GEN_ATTENUATION,            1,     1,       0.0f,   1440.0f,       0.0f },
78         { GEN_RESERVED2,              0,     0,       0.0f,      0.0f,       0.0f },
79         { GEN_ENDLOOPADDRCOARSEOFS,   0,     1,     -1e10f,     1e10f,       0.0f },
80         { GEN_COARSETUNE,             0,     1,    -120.0f,    120.0f,       0.0f },
81         { GEN_FINETUNE,               0,     1,     -99.0f,     99.0f,       0.0f },
82         { GEN_SAMPLEID,               0,     0,       0.0f,      0.0f,       0.0f },
83         { GEN_SAMPLEMODE,             0,     0,       0.0f,      0.0f,       0.0f },
84         { GEN_RESERVED3,              0,     0,       0.0f,      0.0f,       0.0f },
85         { GEN_SCALETUNE,              0,     1,       0.0f,   1200.0f,     100.0f },
86         { GEN_EXCLUSIVECLASS,         0,     0,       0.0f,      0.0f,       0.0f },
87         { GEN_OVERRIDEROOTKEY,        1,     0,       0.0f,    127.0f,      -1.0f },
88         { GEN_PITCH,                  1,     0,       0.0f,    127.0f,       0.0f }
89 };
90
91
92 /**
93  * Set an array of generators to their default values.
94  * @param gen Array of generators (should be #GEN_LAST in size).
95  * @return Always returns 0
96  */
97 int
98 fluid_gen_set_default_values(fluid_gen_t* gen)
99 {
100         int i;
101
102         for (i = 0; i < GEN_LAST; i++) {
103                 gen[i].flags = GEN_UNUSED;
104                 gen[i].mod = 0.0;
105                 gen[i].nrpn = 0.0;
106                 gen[i].val = fluid_gen_info[i].def;
107         }
108
109         return FLUID_OK;
110 }
111
112
113 /* fluid_gen_init
114  *
115  * Set an array of generators to their initial value
116  */
117 int
118 fluid_gen_init(fluid_gen_t* gen, fluid_channel_t* channel)
119 {
120         int i;
121
122         fluid_gen_set_default_values(gen);
123
124         for (i = 0; i < GEN_LAST; i++) {
125                 gen[i].nrpn = fluid_channel_get_gen(channel, i);
126
127                 /* This is an extension to the SoundFont standard. More
128                  * documentation is available at the fluid_synth_set_gen2()
129                  * function. */
130                 if (fluid_channel_get_gen_abs(channel, i)) {
131                         gen[i].flags = GEN_ABS_NRPN;
132                 }
133         }
134
135         return FLUID_OK;
136 }
137
138 fluid_real_t fluid_gen_scale(int gen, float value)
139 {
140         return (fluid_gen_info[gen].min
141                 + value * (fluid_gen_info[gen].max - fluid_gen_info[gen].min));
142 }
143
144 fluid_real_t fluid_gen_scale_nrpn(int gen, int data)
145 {
146         fluid_real_t value = (float) data - 8192.0f;
147         fluid_clip(value, -8192, 8192);
148         return value * (float) fluid_gen_info[gen].nrpn_scale;
149 }