MuseScore  3.4
Music composition and notation
voice.h
Go to the documentation of this file.
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  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18  * 02111-1307, USA
19  */
20 
21 #ifndef _FLUID_VOICE_H
22 #define _FLUID_VOICE_H
23 
24 #include "fluid.h"
25 #include "gen.h"
26 
27 namespace FluidS {
28 
29 #define NO_CHANNEL 0xff
30 
35  };
36 
37 /*
38  * envelope data
39  */
41  unsigned int count; // sample count
42  float coeff;
43  float incr;
44  float min;
45  float max;
46  };
47 
48 /* Indices for envelope tables */
58  };
59 
60 //---------------------------------------------------------
61 // Voice
62 //---------------------------------------------------------
63 
64 class Voice
65  {
66  static float interp_coeff_linear[FLUID_INTERP_MAX][2];
67  static float interp_coeff[FLUID_INTERP_MAX][4];
68  static float sinc_table7[FLUID_INTERP_MAX][7];
69 
71  double _noteTuning; // +/- in midicent
72 
73  //keeps number of frames that are now in cache
74  //Cached frames are the frames that are calculated in terms of DSP (digital sound processing) and interpolated.
75  unsigned _cachedFrames = 0;
76  //Keeps number of the initially cached frames. It's used to calculate actual shift in cache arrays for setting cache to output stream.
77  unsigned _initialCacheFrames = 0;
78  //Cache arrays keep actual calculated data after applying effects. Its size is twice bigger than framesBuffer (2 channels).
79  std::vector<float> _cacheOut;
80  std::vector<float> _cacheReverb;
81  std::vector<float> _cacheChorus;
82 
83  /*
84  / Applies effects to the calculated interpolation and put frames to output containers.
85  / @startBufIdx is used to specify start index of interpolation data array that is used to put data to frames array.
86  / @count specifies how many frames should be calculated. Note, size of output arrays must be twice bigger, than @count.
87  */
88  void effects(int startBufIdx, int count, float* out, float* effect1, float* effect2);
89 
90  /*
91  / Generates DSP data required for sound interpolation. @frames defines number of available frames to generate sound envelope.
92  / Note, the minimal viable number of frames is NUM_FRAMES_DELAY. That is why we need all that stuff with caching. Using bluetooth or some specific hardware leads to less number of frames and silence.
93  */
94  bool generateDataForDSPChain(unsigned frames);
95 
96  /*
97  / Generates sound data from calculated DSP data. Resulting data is stored in @dsp_buf.
98  / The data is generated for @n frames. Note, number of generated frames can be less than @n.
99  */
100  std::tuple<unsigned, bool> interpolateGeneratedDSPData(unsigned n);
101 
102 public:
103  unsigned int id; // the id is incremented for every new noteon.
104  // it's used for noteoff's
105  unsigned char status;
106  unsigned char chan; // the channel number, quick access for channel messages
107  unsigned char key; // the key, quick access for noteoff
108  unsigned char vel; // the velocity
109 
113 
115  bool has_looped; /* Flag that is set as soon as the first loop is completed. */
117  int check_sample_sanity_flag; /* Flag that initiates, that sample-related parameters
118  have to be checked. */
119  unsigned int ticks;
120 
121  float amp; /* the linear amplitude */
122  Phase phase; // the phase of the sample wave
123 
124  // Temporary variables used in write()
125  float phase_incr; /* the phase increment for the next 64 samples */
126  qreal amp_incr; /* amplitude increment value */
127  std::vector<float> dsp_buf; /* buffer to store interpolated sample data to */
128 
129  /* basic parameters */
130  float pitch; /* the pitch in midicents */
131  float attenuation; /* the attenuation in centibels */
132  float min_attenuation_cB; /* Estimate on the smallest possible attenuation
133  * during the lifetime of the voice */
134  float root_pitch, root_pitch_hz;
135 
136  /* sample and loop start and end points (offset in sample memory). */
137  int start;
138  int end;
140  int loopend;
141 
142  /* vol env */
144  unsigned int volenv_count;
146  std::map<int, qreal> Sample2AmpInc;
147  float volenv_val;
150  int positionToTurnOff; // this is the sample accurate position where the sample reaches the noise floor
151 
152  /* mod env */
154  unsigned int modenv_count;
156  float modenv_val; /* the value of the modulation envelope */
159 
160  /* mod lfo */
161  float modlfo_val; /* the value of the modulation LFO */
162  unsigned int modlfo_delay; /* the delay of the lfo in samples */
163  unsigned int modlfo_pos;
164  unsigned int modlfo_dur; // duration in samples
168 
169  /* vib lfo */
170  float viblfo_val; /* the value of the vibrato LFO */
171  unsigned int viblfo_delay; /* the delay of the lfo in samples */
172  float viblfo_incr; /* the lfo frequency is converted to a per-buffer increment */
174 
175  /* resonant filter */
176  float fres; /* the resonance frequency, in cents (not absolute cents) */
177  float last_fres; /* Current resonance frequency of the IIR filter */
178  /* Serves as a flag: A deviation between fres and last_fres */
179  /* indicates, that the filter has to be recalculated. */
180  float q_lin; /* the q-factor on a linear scale */
181  float filter_gain; /* Gain correction factor, depends on q */
182  float hist1, hist2; /* Sample history for the IIR filter */
183  int filter_startup; /* Flag: If set, the filter will be set directly.
184  Else it changes smoothly. */
185 
186  /* filter coefficients */
187  /* The coefficients are normalized to a0. */
188  /* b0 and b2 are identical => b02 */
189  float b02; /* b0 / a0 */
190  float b1; /* b1 / a0 */
191  float a1; /* a0 / a0 */
192  float a2; /* a1 / a0 */
193 
194  float b02_incr;
195  float b1_incr;
196  float a1_incr;
197  float a2_incr;
199 
200  /* pan */
201  float pan;
202  float amp_left;
203  float amp_right;
204 
205  /* reverb */
206  float reverb_send;
207  float amp_reverb;
208 
209  /* chorus */
210  float chorus_send;
211  float amp_chorus;
212 
213  /* interpolation method, as in fluid_interp in fluidsynth.h */
215 
216  /* for debugging */
217  int debug;
218  double ref;
219 
220  public:
221  Voice(Fluid*);
222  Channel* get_channel() const { return channel; }
223  void voice_start();
224  void off();
225  void init(Sample*, Channel*, int key, int vel, unsigned id, double tuning);
226  void gen_incr(int i, float val);
227  void gen_set(int i, float val);
228  float gen_get(int gen);
229  unsigned int get_id() const { return id; }
230  bool isPlaying() { return ((status == FLUID_VOICE_ON) || (status == FLUID_VOICE_SUSTAINED)); }
231  void set_param(int gen, float nrpn_value, int abs);
232 
233  // Update all the synthesis parameters, which depend on generator
234  // 'gen'. This is only necessary after changing a generator of an
235  // already operating voice. Most applications will not need this
236  // function.
237 
238  void update_param(int gen);
239 
240  double GEN(int n) { return gen[n].val + gen[n].mod + gen[n].nrpn; }
241 
242  void modulate_all();
243  void modulate(bool _cc, int _ctrl);
244  float get_lower_boundary_for_attenuation();
245  void check_sample_sanity();
246  void noteoff();
247  void kill_excl();
248  int calculate_hold_decay_frames(int gen_base, int gen_key2base, int is_decay);
249  void calculate_gen_pitch();
250 
251  /* A voice is 'ON', if it has not yet received a noteoff
252  * event. Sending a noteoff event will advance the envelopes to
253  * section 5 (release).
254  */
255  bool RELEASED() const { return chan == NO_CHANNEL; }
256  bool SUSTAINED() const { return status == FLUID_VOICE_SUSTAINED; }
257  bool PLAYING() const { return (status == FLUID_VOICE_ON) || (status == FLUID_VOICE_SUSTAINED); }
258  bool ON() const { return (status == FLUID_VOICE_ON) && (volenv_section < FLUID_VOICE_ENVRELEASE); }
259  int SAMPLEMODE() const { return ((int)gen[GEN_SAMPLEMODE].val); }
260 
261  void calcVolEnv(int n, fluid_env_data_t *env_data);
262  void write(unsigned n, float* out, float* reverb, float* chorus);
263  void add_mod(const Mod* mod, int mode);
264 
265  static void dsp_float_config();
266  bool updateAmpInc(unsigned int &nextNewAmpInc, std::map<int, qreal>::iterator &curSample2AmpInc, qreal &dsp_amp_incr, unsigned int &dsp_i);
267  int dsp_float_interpolate_none(unsigned);
268  int dsp_float_interpolate_linear(unsigned);
269  int dsp_float_interpolate_4th_order(unsigned);
270  int dsp_float_interpolate_7th_order(unsigned);
271  };
272 }
273 
274 
275 #endif /* _FLUID_VOICE_H */
bool RELEASED() const
Definition: voice.h:255
Definition: sfont.h:132
float amp
Definition: voice.h:121
Definition: voice.h:32
double GEN(int n)
Definition: voice.h:240
float pitch
Definition: voice.h:130
unsigned char vel
Definition: voice.h:108
float modenv_val
Definition: voice.h:156
float attenuation
Definition: voice.h:131
Definition: voice.h:34
float amp_right
Definition: voice.h:203
float root_pitch_hz
Definition: voice.h:134
float modlfo_to_vol
Definition: voice.h:167
Definition: fluid.h:216
bool ON() const
Definition: voice.h:258
Definition: voice.h:52
Definition: voice.h:54
float amp_left
Definition: voice.h:202
float min
Definition: voice.h:44
float modlfo_to_fc
Definition: voice.h:165
Phase phase
Definition: voice.h:122
bool isPlaying()
Definition: voice.h:230
float amp_reverb
Definition: voice.h:207
int modenv_section
Definition: voice.h:155
float viblfo_to_pitch
Definition: voice.h:173
float last_fres
Definition: voice.h:177
fluid_voice_status
Definition: voice.h:31
Definition: voice.h:56
#define NO_CHANNEL
Definition: voice.h:29
float volenv_val
Definition: voice.h:147
unsigned int modenv_count
Definition: voice.h:154
float filter_gain
Definition: voice.h:181
double _noteTuning
Definition: voice.h:71
Definition: voice.h:53
int positionToTurnOff
Definition: voice.h:150
unsigned int id
Definition: voice.h:103
float coeff
Definition: voice.h:42
std::vector< float > _cacheReverb
Definition: voice.h:80
double ref
Definition: voice.h:218
int debug
Definition: voice.h:217
Definition: fluid.h:297
unsigned int count
Definition: voice.h:41
float b02
Definition: voice.h:189
float modlfo_to_pitch
Definition: voice.h:166
float hist2
Definition: voice.h:182
Definition: voice.h:40
float amplitude_that_reaches_noise_floor_loop
Definition: voice.h:149
float reverb_send
Definition: voice.h:206
int loopend
Definition: voice.h:140
unsigned char key
Definition: voice.h:107
unsigned char status
Definition: voice.h:105
float b1_incr
Definition: voice.h:195
std::vector< float > dsp_buf
Definition: voice.h:127
Value defines the count of generators (fluid_gen_type)
Definition: fluid.h:209
Definition: fluid.h:565
float max
Definition: voice.h:45
float chorus_send
Definition: voice.h:210
float b02_incr
Definition: voice.h:194
int loopstart
Definition: voice.h:139
unsigned int ticks
Definition: voice.h:119
float a1
Definition: voice.h:191
Definition: voice.h:33
Definition: voice.h:55
Definition: voice.h:51
fluid_voice_envelope_index_t
Definition: voice.h:49
unsigned int modlfo_delay
Definition: voice.h:162
unsigned int viblfo_delay
Definition: voice.h:171
float phase_incr
Definition: voice.h:125
Definition: voice.h:57
std::vector< float > _cacheOut
Definition: voice.h:79
float incr
Definition: voice.h:43
float modenv_to_pitch
Definition: voice.h:158
float modlfo_val
Definition: voice.h:161
int check_sample_sanity_flag
Definition: voice.h:117
int end
Definition: voice.h:138
float a2_incr
Definition: voice.h:197
float pan
Definition: voice.h:201
int volenv_section
Definition: voice.h:145
float a2
Definition: voice.h:192
float modenv_to_fc
Definition: voice.h:157
int start
Definition: voice.h:137
Channel * channel
Definition: voice.h:110
float fres
Definition: voice.h:176
float b1
Definition: voice.h:190
int mod_count
Definition: voice.h:114
int filter_startup
Definition: voice.h:183
float min_attenuation_cB
Definition: voice.h:132
unsigned int modlfo_dur
Definition: voice.h:164
#define FLUID_INTERP_MAX
Definition: fluid.h:629
#define FLUID_NUM_MOD
Definition: fluid.h:493
Definition: voice.h:50
unsigned char chan
Definition: voice.h:106
float a1_incr
Definition: voice.h:196
std::map< int, qreal > Sample2AmpInc
Definition: voice.h:146
qreal amp_incr
Definition: voice.h:126
SoundFont generator structure.
Definition: fluid.h:498
Sample * sample
Definition: voice.h:116
Definition: chan.cpp:25
Definition: fluid.h:649
Fluid * _fluid
Definition: voice.h:70
float viblfo_val
Definition: voice.h:170
float amplitude_that_reaches_noise_floor_nonloop
Definition: voice.h:148
float viblfo_incr
Definition: voice.h:172
int SAMPLEMODE() const
Definition: voice.h:259
std::vector< float > _cacheChorus
Definition: voice.h:81
bool PLAYING() const
Definition: voice.h:257
Channel * get_channel() const
Definition: voice.h:222
bool has_looped
Definition: voice.h:115
Sample mode flags.
Definition: fluid.h:198
Definition: voice.h:64
int interp_method
Definition: voice.h:214
bool SUSTAINED() const
Definition: voice.h:256
float q_lin
Definition: voice.h:180
int filter_coeff_incr_count
Definition: voice.h:198
float amp_chorus
Definition: voice.h:211
unsigned int volenv_count
Definition: voice.h:144
unsigned int modlfo_pos
Definition: voice.h:163
unsigned int get_id() const
Definition: voice.h:229