lb302.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * lb302.h - declaration of class lb302 which is a bass synth attempting to
  3. * emulate the Roland TB303 bass synth
  4. *
  5. * Copyright (c) 2006-2008 Paul Giblock <pgib/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * lb302FilterIIR2 is based on the gsyn filter code by Andy Sloane.
  10. *
  11. * lb302Filter3Pole is based on the TB303 instrument written by
  12. * Josep M Comajuncosas for the CSounds library
  13. *
  14. * This program is free software; you can redistribute it and/or
  15. * modify it under the terms of the GNU General Public
  16. * License as published by the Free Software Foundation; either
  17. * version 2 of the License, or (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public
  25. * License along with this program (see COPYING); if not, write to the
  26. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  27. * Boston, MA 02110-1301 USA.
  28. *
  29. */
  30. #ifndef LB302_H_
  31. #define LB302_H_
  32. #include "DspEffectLibrary.h"
  33. #include "Instrument.h"
  34. #include "InstrumentView.h"
  35. #include "LedCheckbox.h"
  36. #include "Knob.h"
  37. #include "NotePlayHandle.h"
  38. #include <QMutex>
  39. static const int NUM_FILTERS = 2;
  40. class lb302SynthView;
  41. class NotePlayHandle;
  42. class lb302FilterKnobState
  43. {
  44. public:
  45. float cutoff;
  46. float reso;
  47. float envmod;
  48. float envdecay;
  49. float dist;
  50. };
  51. class lb302Filter
  52. {
  53. public:
  54. lb302Filter(lb302FilterKnobState* p_fs);
  55. virtual ~lb302Filter() {};
  56. virtual void recalc();
  57. virtual void envRecalc();
  58. virtual float process(const float& samp)=0;
  59. virtual void playNote();
  60. protected:
  61. lb302FilterKnobState *fs;
  62. // Filter Decay
  63. float vcf_c0; // c0=e1 on retrigger; c0*=ed every sample; cutoff=e0+c0
  64. float vcf_e0, // e0 and e1 for interpolation
  65. vcf_e1;
  66. float vcf_rescoeff; // Resonance coefficient [0.30,9.54]
  67. };
  68. class lb302FilterIIR2 : public lb302Filter
  69. {
  70. public:
  71. lb302FilterIIR2(lb302FilterKnobState* p_fs);
  72. virtual ~lb302FilterIIR2();
  73. virtual void recalc();
  74. virtual void envRecalc();
  75. virtual float process(const float& samp);
  76. protected:
  77. float vcf_d1, // d1 and d2 are added back into the sample with
  78. vcf_d2; // vcf_a and b as coefficients. IIR2 resonance
  79. // loop.
  80. // IIR2 Coefficients for mixing dry and delay.
  81. float vcf_a, // Mixing coefficients for the final sound.
  82. vcf_b, //
  83. vcf_c;
  84. DspEffectLibrary::Distortion * m_dist;
  85. };
  86. class lb302Filter3Pole : public lb302Filter
  87. {
  88. public:
  89. lb302Filter3Pole(lb302FilterKnobState* p_fs);
  90. //virtual void recalc();
  91. virtual void envRecalc();
  92. virtual void recalc();
  93. virtual float process(const float& samp);
  94. protected:
  95. float kfcn,
  96. kp,
  97. kp1,
  98. kp1h,
  99. kres;
  100. float ay1,
  101. ay2,
  102. aout,
  103. lastin,
  104. value;
  105. };
  106. class lb302Note
  107. {
  108. public:
  109. float vco_inc;
  110. bool dead;
  111. };
  112. class lb302Synth : public Instrument
  113. {
  114. Q_OBJECT
  115. public:
  116. lb302Synth( InstrumentTrack * _instrument_track );
  117. virtual ~lb302Synth();
  118. virtual void play( sampleFrame * _working_buffer );
  119. virtual void playNote( NotePlayHandle * _n,
  120. sampleFrame * _working_buffer );
  121. virtual void deleteNotePluginData( NotePlayHandle * _n );
  122. virtual void saveSettings( QDomDocument & _doc, QDomElement & _parent );
  123. virtual void loadSettings( const QDomElement & _this );
  124. virtual QString nodeName() const;
  125. virtual Flags flags() const
  126. {
  127. return IsSingleStreamed;
  128. }
  129. virtual f_cnt_t desiredReleaseFrames() const
  130. {
  131. return 0; //4048;
  132. }
  133. virtual PluginView * instantiateView( QWidget * _parent );
  134. private:
  135. void processNote( NotePlayHandle * n );
  136. void initNote(lb302Note *Note);
  137. void initSlide();
  138. private:
  139. FloatModel vcf_cut_knob;
  140. FloatModel vcf_res_knob;
  141. FloatModel vcf_mod_knob;
  142. FloatModel vcf_dec_knob;
  143. FloatModel vco_fine_detune_knob;
  144. FloatModel dist_knob;
  145. IntModel wave_shape;
  146. FloatModel slide_dec_knob;
  147. BoolModel slideToggle;
  148. BoolModel accentToggle;
  149. BoolModel deadToggle;
  150. BoolModel db24Toggle;
  151. public slots:
  152. void filterChanged();
  153. void db24Toggled();
  154. private:
  155. // Oscillator
  156. float vco_inc, // Sample increment for the frequency. Creates Sawtooth.
  157. vco_k, // Raw oscillator sample [-0.5,0.5]
  158. vco_c; // Raw oscillator sample [-0.5,0.5]
  159. float vco_slide, //* Current value of slide exponential curve. Nonzero=sliding
  160. vco_slideinc, //* Slide base to use in next node. Nonzero=slide next note
  161. vco_slidebase; //* The base vco_inc while sliding.
  162. enum vco_shape_t { SAWTOOTH, SQUARE, TRIANGLE, MOOG, ROUND_SQUARE, SINE, EXPONENTIAL, WHITE_NOISE,
  163. BL_SAWTOOTH, BL_SQUARE, BL_TRIANGLE, BL_MOOG };
  164. vco_shape_t vco_shape;
  165. // Filters (just keep both loaded and switch)
  166. lb302Filter* vcfs[NUM_FILTERS];
  167. // User settings
  168. lb302FilterKnobState fs;
  169. QAtomicPointer<lb302Filter> vcf;
  170. int release_frame;
  171. // More States
  172. int vcf_envpos; // Update counter. Updates when >= ENVINC
  173. float vca_attack, // Amp attack
  174. vca_decay, // Amp decay
  175. vca_a0, // Initial amplifier coefficient
  176. vca_a; // Amplifier coefficient.
  177. // Envelope State
  178. enum VCA_Mode
  179. {
  180. attack = 0,
  181. decay = 1,
  182. idle = 2,
  183. never_played = 3
  184. };
  185. VCA_Mode vca_mode;
  186. // My hacks
  187. int sample_cnt;
  188. int last_offset;
  189. int catch_frame;
  190. int catch_decay;
  191. bool new_freq;
  192. float true_freq;
  193. void recalcFilter();
  194. int process(sampleFrame *outbuf, const int size);
  195. friend class lb302SynthView;
  196. NotePlayHandle * m_playingNote;
  197. NotePlayHandleList m_notes;
  198. QMutex m_notesMutex;
  199. } ;
  200. class lb302SynthView : public InstrumentViewFixedSize
  201. {
  202. Q_OBJECT
  203. public:
  204. lb302SynthView( Instrument * _instrument,
  205. QWidget * _parent );
  206. virtual ~lb302SynthView();
  207. private:
  208. virtual void modelChanged();
  209. Knob * m_vcfCutKnob;
  210. Knob * m_vcfResKnob;
  211. Knob * m_vcfDecKnob;
  212. Knob * m_vcfModKnob;
  213. Knob * m_distKnob;
  214. Knob * m_slideDecKnob;
  215. automatableButtonGroup * m_waveBtnGrp;
  216. LedCheckBox * m_slideToggle;
  217. /*LedCheckBox * m_accentToggle;*/ // removed pending accent implementation
  218. LedCheckBox * m_deadToggle;
  219. LedCheckBox * m_db24Toggle;
  220. } ;
  221. #endif