1 /* FluidSynth - A Software Synthesizer
3 * Copyright (C) 2003 Peter Hanappe and others.
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.
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.
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
22 #ifndef _FLUID_RVOICE_H
23 #define _FLUID_RVOICE_H
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"
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;
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 :)
41 #define FLUID_NOISE_FLOOR 0.00003
46 FLUID_LOOP_DURING_RELEASE = 1,
48 FLUID_LOOP_UNTIL_RELEASE = 3
52 * rvoice ticks-based parameters
53 * These parameters must be updated even if the voice is currently quiet.
55 struct _fluid_rvoice_envlfo_t
57 /* Note-off minimum length */
59 unsigned int noteoff_ticks;
62 fluid_adsr_env_t volenv;
65 fluid_adsr_env_t modenv;
66 fluid_real_t modenv_to_fc;
67 fluid_real_t modenv_to_pitch;
71 fluid_real_t modlfo_to_fc;
72 fluid_real_t modlfo_to_pitch;
73 fluid_real_t modlfo_to_vol;
77 fluid_real_t viblfo_to_pitch;
81 * rvoice parameters needed for dsp interpolation
83 struct _fluid_rvoice_dsp_t
85 /* interpolation method, as in fluid_interp in fluidsynth.h */
87 fluid_sample_t* sample;
88 int check_sample_sanity_flag; /* Flag that initiates, that sample-related parameters
89 have to be checked. */
91 /* sample and loop start and end points (offset in sample memory). */
95 int loopend; /* Note: first point following the loop (superimposed on loopstart) */
96 enum fluid_loop samplemode;
98 /* Stuff needed for phase calculations */
100 fluid_real_t pitch; /* the pitch in midicents */
101 fluid_real_t root_pitch_hz;
102 fluid_real_t output_rate;
104 /* Stuff needed for amplitude calculations */
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 */
115 /* Dynamic input to the interpolator below */
117 fluid_real_t *dsp_buf; /* buffer to store interpolated sample data to */
119 fluid_real_t amp; /* current linear amplitude */
120 fluid_real_t amp_incr; /* amplitude increment value for the next FLUID_BUFSIZE samples */
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 */
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)
133 * rvoice mixer-related parameters
135 struct _fluid_rvoice_buffers_t
137 unsigned int count; /* Number of records in "bufs" */
140 int mapping; /* Mapping to mixdown buffer index */
141 } bufs[FLUID_RVOICE_MAX_BUFS];
146 * Parameters needed to synthesize a voice
148 struct _fluid_rvoice_t
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;
157 int fluid_rvoice_write(fluid_rvoice_t* voice, fluid_real_t *dsp_buf);
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);
167 /* Dynamic update functions */
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);
192 /* defined in fluid_rvoice_dsp.c */
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);