fluid_voice.h
上传用户:tjmskj2
上传日期:2020-08-17
资源大小:577k
文件大小:10k
源码类别:

midi

开发平台:

C/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.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  18.  * 02111-1307, USA
  19.  */
  20. #ifndef _FLUID_VOICE_H
  21. #define _FLUID_VOICE_H
  22. #include "fluid_phase.h"
  23. #include "fluid_gen.h"
  24. #include "fluid_mod.h"
  25. #define NO_CHANNEL             0xff
  26. enum fluid_voice_status
  27. {
  28. FLUID_VOICE_CLEAN,
  29. FLUID_VOICE_ON,
  30. FLUID_VOICE_SUSTAINED,
  31. FLUID_VOICE_OFF
  32. };
  33. /*
  34.  * envelope data
  35.  */
  36. struct _fluid_env_data_t {
  37. unsigned int count;
  38. fluid_real_t coeff;
  39. fluid_real_t incr;
  40. fluid_real_t min;
  41. fluid_real_t max;
  42. };
  43. /* Indices for envelope tables */
  44. enum fluid_voice_envelope_index_t{
  45. FLUID_VOICE_ENVDELAY,
  46. FLUID_VOICE_ENVATTACK,
  47. FLUID_VOICE_ENVHOLD,
  48. FLUID_VOICE_ENVDECAY,
  49. FLUID_VOICE_ENVSUSTAIN,
  50. FLUID_VOICE_ENVRELEASE,
  51. FLUID_VOICE_ENVFINISHED,
  52. FLUID_VOICE_ENVLAST
  53. };
  54. /*
  55.  * fluid_voice_t
  56.  */
  57. struct _fluid_voice_t
  58. {
  59. unsigned int id;                /* the id is incremented for every new noteon.
  60.    it's used for noteoff's  */
  61. unsigned char status;
  62. unsigned char chan;             /* the channel number, quick access for channel messages */
  63. unsigned char key;              /* the key, quick acces for noteoff */
  64. unsigned char vel;              /* the velocity */
  65. fluid_channel_t* channel;
  66. fluid_gen_t gen[GEN_LAST];
  67. fluid_mod_t mod[FLUID_NUM_MOD];
  68. int mod_count;
  69. int has_looped;                 /* Flag that is set as soon as the first loop is completed. */
  70. fluid_sample_t* sample;
  71. int check_sample_sanity_flag;   /* Flag that initiates, that sample-related parameters
  72.    have to be checked. */
  73. #if 0
  74. /* Instead of keeping a pointer to a fluid_sample_t structure,
  75.  * I think it would be better to copy the sample data in the
  76.  * voice structure. SoundFont loader then do not have to
  77.  * allocate and maintain the fluid_sample_t structure. [PH]
  78.  *
  79.  * The notify callback may be used also for streaming samples.
  80.  */
  81. short* sample_data;             /* pointer to the sample data */
  82. int sample_data_offset;         /* the offset of data[0] in the whole sample */
  83. int sample_data_length;         /* the length of the data array */
  84. unsigned int sample_start;
  85. unsigned int sample_end;
  86. unsigned int sample_loopstart;
  87. unsigned int sample_loopend;
  88. unsigned int sample_rate;
  89. int sample_origpitch;
  90. int sample_pitchadj;
  91. int sample_type;
  92. int (*sample_notify)(fluid_voice_t* voice, int reason);
  93. void* sample_userdata;
  94. #endif
  95. /* basic parameters */
  96. fluid_real_t output_rate;        /* the sample rate of the synthesizer */
  97. unsigned int start_time;
  98. unsigned int ticks;
  99. unsigned int noteoff_ticks;      /* Delay note-off until this tick */
  100. fluid_real_t amp;                /* current linear amplitude */
  101. fluid_phase_t phase;             /* the phase of the sample wave */
  102. /* Temporary variables used in fluid_voice_write() */
  103. fluid_real_t phase_incr; /* the phase increment for the next 64 samples */
  104. fluid_real_t amp_incr; /* amplitude increment value */
  105. fluid_real_t *dsp_buf; /* buffer to store interpolated sample data to */
  106. int dsp_buf_count;              /* Number of audio samples in dsp_buf */
  107. /* End temporary variables */
  108. /* basic parameters */
  109. fluid_real_t pitch;              /* the pitch in midicents */
  110. fluid_real_t attenuation;        /* the attenuation in centibels */
  111. fluid_real_t min_attenuation_cB; /* Estimate on the smallest possible attenuation
  112.   * during the lifetime of the voice */
  113. fluid_real_t root_pitch, root_pitch_hz;
  114. /* sample and loop start and end points (offset in sample memory).  */
  115. int start;
  116. int end;
  117. int loopstart;
  118. int loopend; /* Note: first point following the loop (superimposed on loopstart) */
  119. /* master gain */
  120. fluid_real_t synth_gain;
  121. /* vol env */
  122. fluid_env_data_t volenv_data[FLUID_VOICE_ENVLAST];
  123. unsigned int volenv_count;
  124. int volenv_section;
  125. fluid_real_t volenv_val;
  126. fluid_real_t amplitude_that_reaches_noise_floor_nonloop;
  127. fluid_real_t amplitude_that_reaches_noise_floor_loop;
  128. /* mod env */
  129. fluid_env_data_t modenv_data[FLUID_VOICE_ENVLAST];
  130. unsigned int modenv_count;
  131. int modenv_section;
  132. fluid_real_t modenv_val;         /* the value of the modulation envelope */
  133. fluid_real_t modenv_to_fc;
  134. fluid_real_t modenv_to_pitch;
  135. /* mod lfo */
  136. fluid_real_t modlfo_val;          /* the value of the modulation LFO */
  137. unsigned int modlfo_delay;       /* the delay of the lfo in samples */
  138. fluid_real_t modlfo_incr;         /* the lfo frequency is converted to a per-buffer increment */
  139. fluid_real_t modlfo_to_fc;
  140. fluid_real_t modlfo_to_pitch;
  141. fluid_real_t modlfo_to_vol;
  142. /* vib lfo */
  143. fluid_real_t viblfo_val;        /* the value of the vibrato LFO */
  144. unsigned int viblfo_delay;      /* the delay of the lfo in samples */
  145. fluid_real_t viblfo_incr;       /* the lfo frequency is converted to a per-buffer increment */
  146. fluid_real_t viblfo_to_pitch;
  147. /* resonant filter */
  148. fluid_real_t fres;              /* the resonance frequency, in cents (not absolute cents) */
  149. fluid_real_t last_fres;         /* Current resonance frequency of the IIR filter */
  150. /* Serves as a flag: A deviation between fres and last_fres */
  151. /* indicates, that the filter has to be recalculated. */
  152. fluid_real_t q_lin;             /* the q-factor on a linear scale */
  153. fluid_real_t filter_gain;       /* Gain correction factor, depends on q */
  154. fluid_real_t hist1, hist2;      /* Sample history for the IIR filter */
  155. int filter_startup;             /* Flag: If set, the filter will be set directly.
  156.    Else it changes smoothly. */
  157. /* filter coefficients */
  158. /* The coefficients are normalized to a0. */
  159. /* b0 and b2 are identical => b02 */
  160. fluid_real_t b02;              /* b0 / a0 */
  161. fluid_real_t b1;              /* b1 / a0 */
  162. fluid_real_t a1;              /* a0 / a0 */
  163. fluid_real_t a2;              /* a1 / a0 */
  164. fluid_real_t b02_incr;
  165. fluid_real_t b1_incr;
  166. fluid_real_t a1_incr;
  167. fluid_real_t a2_incr;
  168. int filter_coeff_incr_count;
  169. /* pan */
  170. fluid_real_t pan;
  171. fluid_real_t amp_left;
  172. fluid_real_t amp_right;
  173. /* reverb */
  174. fluid_real_t reverb_send;
  175. fluid_real_t amp_reverb;
  176. /* chorus */
  177. fluid_real_t chorus_send;
  178. fluid_real_t amp_chorus;
  179. /* interpolation method, as in fluid_interp in fluidsynth.h */
  180. int interp_method;
  181. /* for debugging */
  182. int debug;
  183. double ref;
  184. };
  185. fluid_voice_t* new_fluid_voice(fluid_real_t output_rate);
  186. int delete_fluid_voice(fluid_voice_t* voice);
  187. void fluid_voice_start(fluid_voice_t* voice);
  188. void  fluid_voice_calculate_gen_pitch(fluid_voice_t* voice);
  189. int fluid_voice_write (fluid_voice_t* voice, fluid_real_t *dsp_buf);
  190. int fluid_voice_init(fluid_voice_t* voice, fluid_sample_t* sample,
  191.      fluid_channel_t* channel, int key, int vel,
  192.      unsigned int id, unsigned int time, fluid_real_t gain);
  193. int fluid_voice_modulate(fluid_voice_t* voice, int cc, int ctrl);
  194. int fluid_voice_modulate_all(fluid_voice_t* voice);
  195. /** Set the NRPN value of a generator. */
  196. int fluid_voice_set_param(fluid_voice_t* voice, int gen, fluid_real_t value, int abs);
  197. /** Set the gain. */
  198. int fluid_voice_set_gain(fluid_voice_t* voice, fluid_real_t gain);
  199. /** Update all the synthesis parameters, which depend on generator
  200.     'gen'. This is only necessary after changing a generator of an
  201.     already operating voice.  Most applications will not need this
  202.     function.*/
  203. void fluid_voice_update_param(fluid_voice_t* voice, int gen);
  204. int fluid_voice_noteoff(fluid_voice_t* voice);
  205. int fluid_voice_off(fluid_voice_t* voice);
  206. void fluid_voice_mix (fluid_voice_t *voice,
  207.                       fluid_real_t* left_buf, fluid_real_t* right_buf,
  208.                       fluid_real_t* reverb_buf, fluid_real_t* chorus_buf);
  209. int fluid_voice_kill_excl(fluid_voice_t* voice);
  210. #define fluid_voice_get_channel(voice)  ((voice)->channel)
  211. #define fluid_voice_set_id(_voice, _id)  { (_voice)->id = (_id); }
  212. #define fluid_voice_get_chan(_voice)     (_voice)->chan
  213. #define _PLAYING(voice)  (((voice)->status == FLUID_VOICE_ON) || ((voice)->status == FLUID_VOICE_SUSTAINED))
  214. /* A voice is 'ON', if it has not yet received a noteoff
  215.  * event. Sending a noteoff event will advance the envelopes to
  216.  * section 5 (release). */
  217. #define _ON(voice)  ((voice)->status == FLUID_VOICE_ON && (voice)->volenv_section < FLUID_VOICE_ENVRELEASE)
  218. #define _SUSTAINED(voice)  ((voice)->status == FLUID_VOICE_SUSTAINED)
  219. #define _AVAILABLE(voice)  (((voice)->status == FLUID_VOICE_CLEAN) || ((voice)->status == FLUID_VOICE_OFF))
  220. #define _RELEASED(voice)  ((voice)->chan == NO_CHANNEL)
  221. #define _SAMPLEMODE(voice) ((int)(voice)->gen[GEN_SAMPLEMODE].val)
  222. /* FIXME - This doesn't seem to be used anywhere - JG */
  223. fluid_real_t fluid_voice_gen_value(fluid_voice_t* voice, int num);
  224. #define _GEN(_voice, _n) 
  225.   ((fluid_real_t)(_voice)->gen[_n].val 
  226.    + (fluid_real_t)(_voice)->gen[_n].mod 
  227.    + (fluid_real_t)(_voice)->gen[_n].nrpn)
  228. #define FLUID_SAMPLESANITY_CHECK (1 << 0)
  229. #define FLUID_SAMPLESANITY_STARTUP (1 << 1)
  230. /* defined in fluid_dsp_float.c */
  231. void fluid_dsp_float_config (void);
  232. int fluid_dsp_float_interpolate_none (fluid_voice_t *voice);
  233. int fluid_dsp_float_interpolate_linear (fluid_voice_t *voice);
  234. int fluid_dsp_float_interpolate_4th_order (fluid_voice_t *voice);
  235. int fluid_dsp_float_interpolate_7th_order (fluid_voice_t *voice);
  236. #endif /* _FLUID_VOICE_H */