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 Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 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 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser 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 * 24 bits => 144-4 = 140 dB dynamic range => 1.e-7
40 * 1.e-7 * 2 == 2.e-7 :)
42 #define FLUID_NOISE_FLOOR 2.e-7
47 FLUID_LOOP_DURING_RELEASE = 1,
49 FLUID_LOOP_UNTIL_RELEASE = 3
53 * rvoice ticks-based parameters
54 * These parameters must be updated even if the voice is currently quiet.
56 struct _fluid_rvoice_envlfo_t
58 /* Note-off minimum length */
60 unsigned int noteoff_ticks;
63 fluid_adsr_env_t volenv;
66 fluid_adsr_env_t modenv;
67 fluid_real_t modenv_to_fc;
68 fluid_real_t modenv_to_pitch;
72 fluid_real_t modlfo_to_fc;
73 fluid_real_t modlfo_to_pitch;
74 fluid_real_t modlfo_to_vol;
78 fluid_real_t viblfo_to_pitch;
82 * rvoice parameters needed for dsp interpolation
84 struct _fluid_rvoice_dsp_t
86 /* interpolation method, as in fluid_interp in fluidsynth.h */
87 enum fluid_interp interp_method;
88 enum fluid_loop samplemode;
90 /* Flag that is set as soon as the first loop is completed. */
93 /* Flag that initiates, that sample-related parameters have to be checked. */
94 char check_sample_sanity_flag;
96 fluid_sample_t *sample;
98 /* sample and loop start and end points (offset in sample memory). */
102 int loopend; /* Note: first point following the loop (superimposed on loopstart) */
104 /* Stuff needed for portamento calculations */
105 fluid_real_t pitchoffset; /* the portamento range in midicents */
106 fluid_real_t pitchinc; /* the portamento increment in midicents */
108 /* Stuff needed for phase calculations */
110 fluid_real_t pitch; /* the pitch in midicents */
111 fluid_real_t root_pitch_hz;
112 fluid_real_t output_rate;
114 /* Stuff needed for amplitude calculations */
116 fluid_real_t attenuation; /* the attenuation in centibels */
117 fluid_real_t prev_attenuation; /* the previous attenuation in centibels
118 used by fluid_rvoice_multi_retrigger_attack() */
119 fluid_real_t min_attenuation_cB; /* Estimate on the smallest possible attenuation
120 * during the lifetime of the voice */
121 fluid_real_t amplitude_that_reaches_noise_floor_nonloop;
122 fluid_real_t amplitude_that_reaches_noise_floor_loop;
123 fluid_real_t synth_gain; /* master gain */
125 /* Dynamic input to the interpolator below */
127 fluid_real_t amp; /* current linear amplitude */
128 fluid_real_t amp_incr; /* amplitude increment value for the next FLUID_BUFSIZE samples */
130 fluid_phase_t phase; /* the phase (current sample offset) of the sample wave */
131 fluid_real_t phase_incr; /* the phase increment for the next FLUID_BUFSIZE samples */
134 /* Currently left, right, reverb, chorus. To be changed if we
135 ever add surround positioning, or stereo reverb/chorus */
136 #define FLUID_RVOICE_MAX_BUFS (4)
139 * rvoice mixer-related parameters
141 struct _fluid_rvoice_buffers_t
143 unsigned int count; /* Number of records in "bufs" */
147 int mapping; /* Mapping to mixdown buffer index */
148 } bufs[FLUID_RVOICE_MAX_BUFS];
153 * Hard real-time parameters needed to synthesize a voice
155 struct _fluid_rvoice_t
157 fluid_rvoice_envlfo_t envlfo;
158 fluid_rvoice_dsp_t dsp;
159 fluid_iir_filter_t resonant_filter; /* IIR resonant dsp filter */
160 fluid_iir_filter_t resonant_custom_filter; /* optional custom/general-purpose IIR resonant filter */
161 fluid_rvoice_buffers_t buffers;
165 int fluid_rvoice_write(fluid_rvoice_t *voice, fluid_real_t *dsp_buf);
167 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_buffers_set_amp);
168 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_buffers_set_mapping);
170 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_noteoff);
171 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_voiceoff);
172 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_reset);
173 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_multi_retrigger_attack);
174 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_portamento);
175 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_output_rate);
176 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_interp_method);
177 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_root_pitch_hz);
178 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_pitch);
179 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_attenuation);
180 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_min_attenuation_cB);
181 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_viblfo_to_pitch);
182 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modlfo_to_pitch);
183 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modlfo_to_vol);
184 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modlfo_to_fc);
185 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modenv_to_fc);
186 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_modenv_to_pitch);
187 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_synth_gain);
188 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_start);
189 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_end);
190 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_loopstart);
191 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_loopend);
192 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_samplemode);
193 DECLARE_FLUID_RVOICE_FUNCTION(fluid_rvoice_set_sample);
195 /* defined in fluid_rvoice_dsp.c */
196 void fluid_rvoice_dsp_config(void);
197 int fluid_rvoice_dsp_interpolate_none(fluid_rvoice_dsp_t *voice, fluid_real_t *FLUID_RESTRICT dsp_buf, int is_looping);
198 int fluid_rvoice_dsp_interpolate_linear(fluid_rvoice_dsp_t *voice, fluid_real_t *FLUID_RESTRICT dsp_buf, int is_looping);
199 int fluid_rvoice_dsp_interpolate_4th_order(fluid_rvoice_dsp_t *voice, fluid_real_t *FLUID_RESTRICT dsp_buf, int is_looping);
200 int fluid_rvoice_dsp_interpolate_7th_order(fluid_rvoice_dsp_t *voice, fluid_real_t *FLUID_RESTRICT dsp_buf, int is_looping);
204 * Combines the most significant 16 bit part of a sample with a potentially present
205 * least sig. 8 bit part in order to create a 24 bit sample.
207 static FLUID_INLINE int32_t
208 fluid_rvoice_get_sample(const short int *dsp_msb, const char *dsp_lsb, unsigned int idx)
210 /* cast sample to unsigned type, so we can safely shift and bitwise or
211 * without relying on undefined behaviour (should never happen anyway ofc...) */
212 uint32_t msb = (uint32_t)dsp_msb[idx];
215 /* most soundfonts have 16 bit samples, assume that it's unlikely we
216 * experience 24 bit samples here */
217 if(FLUID_UNLIKELY(dsp_lsb != NULL))
219 lsb = (uint8_t)dsp_lsb[idx];
222 return (int32_t)((msb << 8) | lsb);