4566cb1de334e9f4a5a6a7100a5c19c0e85ab8c7
[ardour.git] / libs / fluidsynth / src / fluid_rvoice.h
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 #ifndef _FLUID_RVOICE_H
23 #define _FLUID_RVOICE_H
24
25 #include "fluidsynth_priv.h"
26 #include "fluid_iir_filter.h"
27 #include "fluid_adsr_env.h"
28 #include "fluid_lfo.h"
29 #include "fluid_phase.h"
30 #include "fluid_sfont.h"
31
32 typedef struct _fluid_rvoice_envlfo_t fluid_rvoice_envlfo_t;
33 typedef struct _fluid_rvoice_dsp_t fluid_rvoice_dsp_t;
34 typedef struct _fluid_rvoice_buffers_t fluid_rvoice_buffers_t;
35 typedef struct _fluid_rvoice_t fluid_rvoice_t;
36
37 /* Smallest amplitude that can be perceived (full scale is +/- 0.5)
38  * 16 bits => 96+4=100 dB dynamic range => 0.00001
39  * 0.00001 * 2 is approximately 0.00003 :)
40  */
41 #define FLUID_NOISE_FLOOR 0.00003
42
43
44 enum fluid_loop {
45   FLUID_UNLOOPED = 0,
46   FLUID_LOOP_DURING_RELEASE = 1,
47   FLUID_NOTUSED = 2,
48   FLUID_LOOP_UNTIL_RELEASE = 3
49 };
50
51 /**
52  * rvoice ticks-based parameters
53  * These parameters must be updated even if the voice is currently quiet.
54  */
55 struct _fluid_rvoice_envlfo_t
56 {
57         /* Note-off minimum length */
58         unsigned int ticks;
59         unsigned int noteoff_ticks;      
60
61         /* vol env */
62         fluid_adsr_env_t volenv;
63
64         /* mod env */
65         fluid_adsr_env_t modenv;
66         fluid_real_t modenv_to_fc;
67         fluid_real_t modenv_to_pitch;
68
69         /* mod lfo */
70         fluid_lfo_t modlfo;
71         fluid_real_t modlfo_to_fc;
72         fluid_real_t modlfo_to_pitch;
73         fluid_real_t modlfo_to_vol;
74
75         /* vib lfo */
76         fluid_lfo_t viblfo;
77         fluid_real_t viblfo_to_pitch;
78 };
79
80 /**
81  * rvoice parameters needed for dsp interpolation
82  */
83 struct _fluid_rvoice_dsp_t
84 {
85         /* interpolation method, as in fluid_interp in fluidsynth.h */
86         int interp_method;
87         fluid_sample_t* sample;
88         int check_sample_sanity_flag;   /* Flag that initiates, that sample-related parameters
89                                            have to be checked. */
90
91         /* sample and loop start and end points (offset in sample memory).  */
92         int start;
93         int end;
94         int loopstart;
95         int loopend;    /* Note: first point following the loop (superimposed on loopstart) */
96         enum fluid_loop samplemode;
97
98         /* Stuff needed for phase calculations */
99
100         fluid_real_t pitch;              /* the pitch in midicents */
101         fluid_real_t root_pitch_hz;
102         fluid_real_t output_rate;
103
104         /* Stuff needed for amplitude calculations */
105
106         int has_looped;                 /* Flag that is set as soon as the first loop is completed. */
107         fluid_real_t attenuation;        /* the attenuation in centibels */
108         fluid_real_t min_attenuation_cB; /* Estimate on the smallest possible attenuation
109                                           * during the lifetime of the voice */
110         fluid_real_t amplitude_that_reaches_noise_floor_nonloop;
111         fluid_real_t amplitude_that_reaches_noise_floor_loop;
112         fluid_real_t synth_gain;        /* master gain */
113
114
115         /* Dynamic input to the interpolator below */
116
117         fluid_real_t *dsp_buf;          /* buffer to store interpolated sample data to */
118
119         fluid_real_t amp;                /* current linear amplitude */
120         fluid_real_t amp_incr;          /* amplitude increment value for the next FLUID_BUFSIZE samples */
121
122         fluid_phase_t phase;             /* the phase (current sample offset) of the sample wave */
123         fluid_real_t phase_incr;        /* the phase increment for the next FLUID_BUFSIZE samples */
124         int is_looping;
125
126 };
127
128 /* Currently left, right, reverb, chorus. To be changed if we
129    ever add surround positioning, or stereo reverb/chorus */
130 #define FLUID_RVOICE_MAX_BUFS (4)
131
132 /**
133  * rvoice mixer-related parameters
134  */
135 struct _fluid_rvoice_buffers_t
136 {
137         unsigned int count; /* Number of records in "bufs" */
138         struct {
139                 fluid_real_t amp;
140                 int mapping; /* Mapping to mixdown buffer index */
141         } bufs[FLUID_RVOICE_MAX_BUFS];
142 };
143
144
145 /**
146  * Parameters needed to synthesize a voice
147  */
148 struct _fluid_rvoice_t
149 {
150         fluid_rvoice_envlfo_t envlfo;
151         fluid_rvoice_dsp_t dsp; 
152         fluid_iir_filter_t resonant_filter; /* IIR resonant dsp filter */
153         fluid_rvoice_buffers_t buffers;
154 };
155
156
157 int fluid_rvoice_write(fluid_rvoice_t* voice, fluid_real_t *dsp_buf);
158
159 void fluid_rvoice_buffers_mix(fluid_rvoice_buffers_t* buffers, 
160                               fluid_real_t* dsp_buf, int samplecount, 
161                               fluid_real_t** dest_bufs, int dest_bufcount);
162 void fluid_rvoice_buffers_set_amp(fluid_rvoice_buffers_t* buffers, 
163                                   unsigned int bufnum, fluid_real_t value);
164 void fluid_rvoice_buffers_set_mapping(fluid_rvoice_buffers_t* buffers,
165                                       unsigned int bufnum, int mapping);
166
167 /* Dynamic update functions */
168
169 void fluid_rvoice_noteoff(fluid_rvoice_t* voice, unsigned int min_ticks);
170 void fluid_rvoice_voiceoff(fluid_rvoice_t* voice);
171 void fluid_rvoice_reset(fluid_rvoice_t* voice);
172 void fluid_rvoice_set_output_rate(fluid_rvoice_t* voice, fluid_real_t output_rate);
173 void fluid_rvoice_set_interp_method(fluid_rvoice_t* voice, int interp_method);
174 void fluid_rvoice_set_root_pitch_hz(fluid_rvoice_t* voice, fluid_real_t root_pitch_hz);
175 void fluid_rvoice_set_pitch(fluid_rvoice_t* voice, fluid_real_t value);
176 void fluid_rvoice_set_synth_gain(fluid_rvoice_t* voice, fluid_real_t value);
177 void fluid_rvoice_set_attenuation(fluid_rvoice_t* voice, fluid_real_t value);
178 void fluid_rvoice_set_min_attenuation_cB(fluid_rvoice_t* voice, fluid_real_t value);
179 void fluid_rvoice_set_viblfo_to_pitch(fluid_rvoice_t* voice, fluid_real_t value);
180 void fluid_rvoice_set_modlfo_to_pitch(fluid_rvoice_t* voice, fluid_real_t value);
181 void fluid_rvoice_set_modlfo_to_vol(fluid_rvoice_t* voice, fluid_real_t value);
182 void fluid_rvoice_set_modlfo_to_fc(fluid_rvoice_t* voice, fluid_real_t value);
183 void fluid_rvoice_set_modenv_to_fc(fluid_rvoice_t* voice, fluid_real_t value);
184 void fluid_rvoice_set_modenv_to_pitch(fluid_rvoice_t* voice, fluid_real_t value);
185 void fluid_rvoice_set_start(fluid_rvoice_t* voice, int value);
186 void fluid_rvoice_set_end(fluid_rvoice_t* voice, int value);
187 void fluid_rvoice_set_loopstart(fluid_rvoice_t* voice, int value);
188 void fluid_rvoice_set_loopend(fluid_rvoice_t* voice, int value);
189 void fluid_rvoice_set_sample(fluid_rvoice_t* voice, fluid_sample_t* value);
190 void fluid_rvoice_set_samplemode(fluid_rvoice_t* voice, enum fluid_loop value);
191
192 /* defined in fluid_rvoice_dsp.c */
193
194 void fluid_rvoice_dsp_config (void);
195 int fluid_rvoice_dsp_interpolate_none (fluid_rvoice_dsp_t *voice);
196 int fluid_rvoice_dsp_interpolate_linear (fluid_rvoice_dsp_t *voice);
197 int fluid_rvoice_dsp_interpolate_4th_order (fluid_rvoice_dsp_t *voice);
198 int fluid_rvoice_dsp_interpolate_7th_order (fluid_rvoice_dsp_t *voice);
199
200 #endif