Watsyn.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * Watsyn.h - a 4-oscillator modulating wavetable synth
  3. *
  4. * Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef WATSYN_H
  25. #define WATSYN_H
  26. #include "Instrument.h"
  27. #include "InstrumentView.h"
  28. #include "Graph.h"
  29. #include "AutomatableModel.h"
  30. #include "AutomatableButton.h"
  31. #include "TempoSyncKnob.h"
  32. #include "NotePlayHandle.h"
  33. #include "PixmapButton.h"
  34. #include <samplerate.h>
  35. #include "MemoryManager.h"
  36. #define makeknob( name, x, y, hint, unit, oname ) \
  37. name = new Knob( knobStyled, this ); \
  38. name ->move( x, y ); \
  39. name ->setHintText( hint, unit ); \
  40. name ->setObjectName( oname ); \
  41. name ->setFixedSize( 19, 19 );
  42. #define maketsknob( name, x, y, hint, unit, oname ) \
  43. name = new TempoSyncKnob( knobStyled, this ); \
  44. name ->move( x, y ); \
  45. name ->setHintText( hint, unit ); \
  46. name ->setObjectName( oname ); \
  47. name ->setFixedSize( 19, 19 );
  48. #define A1ROW 26
  49. #define A2ROW 49
  50. #define B1ROW 72
  51. #define B2ROW 95
  52. const int GRAPHLEN = 220; // don't change - must be same as the size of the widget
  53. const int WAVERATIO = 32; // oversampling ratio
  54. const int WAVELEN = GRAPHLEN * WAVERATIO;
  55. const int PMOD_AMT = WAVELEN / 2;
  56. const int MOD_MIX = 0;
  57. const int MOD_AM = 1;
  58. const int MOD_RM = 2;
  59. const int MOD_PM = 3;
  60. const int NUM_MODS = 4;
  61. const int A1_OSC = 0;
  62. const int A2_OSC = 1;
  63. const int B1_OSC = 2;
  64. const int B2_OSC = 3;
  65. const int NUM_OSCS = 4;
  66. class WatsynInstrument;
  67. class WatsynObject
  68. {
  69. MM_OPERATORS
  70. public:
  71. WatsynObject( float * _A1wave, float * _A2wave,
  72. float * _B1wave, float * _B2wave,
  73. int _amod, int _bmod, const sample_rate_t _samplerate, NotePlayHandle * _nph, fpp_t _frames,
  74. WatsynInstrument * _w );
  75. virtual ~WatsynObject();
  76. void renderOutput( fpp_t _frames );
  77. inline sampleFrame * abuf() const
  78. {
  79. return m_abuf;
  80. }
  81. inline sampleFrame * bbuf() const
  82. {
  83. return m_bbuf;
  84. }
  85. inline sample_rate_t samplerate() const
  86. {
  87. return m_samplerate;
  88. }
  89. private:
  90. int m_amod;
  91. int m_bmod;
  92. const sample_rate_t m_samplerate;
  93. NotePlayHandle * m_nph;
  94. fpp_t m_fpp;
  95. WatsynInstrument * m_parent;
  96. sampleFrame * m_abuf;
  97. sampleFrame * m_bbuf;
  98. float m_lphase [NUM_OSCS];
  99. float m_rphase [NUM_OSCS];
  100. float m_A1wave [WAVELEN];
  101. float m_A2wave [WAVELEN];
  102. float m_B1wave [WAVELEN];
  103. float m_B2wave [WAVELEN];
  104. };
  105. class WatsynInstrument : public Instrument
  106. {
  107. Q_OBJECT
  108. public:
  109. WatsynInstrument( InstrumentTrack * _instrument_track );
  110. virtual ~WatsynInstrument();
  111. virtual void playNote( NotePlayHandle * _n,
  112. sampleFrame * _working_buffer );
  113. virtual void deleteNotePluginData( NotePlayHandle * _n );
  114. virtual void saveSettings( QDomDocument & _doc,
  115. QDomElement & _this );
  116. virtual void loadSettings( const QDomElement & _this );
  117. virtual QString nodeName() const;
  118. virtual f_cnt_t desiredReleaseFrames() const
  119. {
  120. return( 64 );
  121. }
  122. virtual PluginView * instantiateView( QWidget * _parent );
  123. public slots:
  124. void updateVolumes();
  125. void updateFreqA1();
  126. void updateFreqA2();
  127. void updateFreqB1();
  128. void updateFreqB2();
  129. void updateWaveA1();
  130. void updateWaveA2();
  131. void updateWaveB1();
  132. void updateWaveB2();
  133. protected:
  134. float m_lvol [NUM_OSCS];
  135. float m_rvol [NUM_OSCS];
  136. float m_lfreq [NUM_OSCS];
  137. float m_rfreq [NUM_OSCS];
  138. private:
  139. inline float leftCh( float _vol, float _pan )
  140. {
  141. return ( _pan <= 0 ? 1.0 : 1.0 - ( _pan / 100.0 ) ) * _vol / 100.0;
  142. }
  143. inline float rightCh( float _vol, float _pan )
  144. {
  145. return ( _pan >= 0 ? 1.0 : 1.0 + ( _pan / 100.0 ) ) * _vol / 100.0;
  146. }
  147. // memcpy utilizing libsamplerate (src) for sinc interpolation
  148. inline void srccpy( float * _dst, float * _src )
  149. {
  150. int err;
  151. const int margin = 64;
  152. // copy to temp array
  153. float tmps [ GRAPHLEN + margin ]; // temp array in stack
  154. float * tmp = &tmps[0];
  155. memcpy( tmp, _src, sizeof( float ) * GRAPHLEN );
  156. memcpy( tmp + GRAPHLEN, _src, sizeof( float ) * margin );
  157. SRC_STATE * src_state = src_new( SRC_SINC_FASTEST, 1, &err );
  158. SRC_DATA src_data;
  159. src_data.data_in = tmp;
  160. src_data.input_frames = GRAPHLEN + margin;
  161. src_data.data_out = _dst;
  162. src_data.output_frames = WAVELEN;
  163. src_data.src_ratio = static_cast<double>( WAVERATIO );
  164. src_data.end_of_input = 0;
  165. err = src_process( src_state, &src_data );
  166. if( err ) { qDebug( "Watsyn SRC error: %s", src_strerror( err ) ); }
  167. src_delete( src_state );
  168. }
  169. // memcpy utilizing cubic interpolation
  170. /* inline void cipcpy( float * _dst, float * _src )
  171. {
  172. // calculate cyclic tangents
  173. float tang[GRAPHLEN];
  174. tang[0] = ( _src[1] - _src[ GRAPHLEN - 1] ) / 2;
  175. tang[ GRAPHLEN - 1 ] = ( _src[0] - _src[ GRAPHLEN - 2 ] ) / 2;
  176. for( int i = 1; i < GRAPHLEN-1; i++ )
  177. {
  178. tang[i] = ( _src[i+1] - _src[i-1] ) / 2;
  179. }
  180. // calculate cspline
  181. for( int i=0; i < WAVELEN; i++ )
  182. {
  183. const float s1 = _src[ i / WAVERATIO ];
  184. const float s2 = _src[ ( i / WAVERATIO + 1 ) % GRAPHLEN ];
  185. const float m1 = tang[ i / WAVERATIO ];
  186. const float m2 = tang[ ( i / WAVERATIO + 1 ) % GRAPHLEN ];
  187. const float x = static_cast<float>( i % WAVERATIO ) / WAVERATIO;
  188. const float x2 = x * x;
  189. const float x3 = x * x * x;
  190. _dst[i] = ( ( x3 * 2.0 - x2 * 3.0 + 1.0 ) * s1 ) +
  191. ( ( x3 * -2.0 + x2 * 3.0 ) * s2 ) +
  192. ( ( x3 - x2 * 2 + x ) * m1 ) +
  193. ( ( x3 - x2 ) * m2 );
  194. }
  195. }*/
  196. FloatModel a1_vol;
  197. FloatModel a2_vol;
  198. FloatModel b1_vol;
  199. FloatModel b2_vol;
  200. FloatModel a1_pan;
  201. FloatModel a2_pan;
  202. FloatModel b1_pan;
  203. FloatModel b2_pan;
  204. FloatModel a1_mult;
  205. FloatModel a2_mult;
  206. FloatModel b1_mult;
  207. FloatModel b2_mult;
  208. FloatModel a1_ltune;
  209. FloatModel a2_ltune;
  210. FloatModel b1_ltune;
  211. FloatModel b2_ltune;
  212. FloatModel a1_rtune;
  213. FloatModel a2_rtune;
  214. FloatModel b1_rtune;
  215. FloatModel b2_rtune;
  216. graphModel a1_graph;
  217. graphModel a2_graph;
  218. graphModel b1_graph;
  219. graphModel b2_graph;
  220. FloatModel m_abmix;
  221. FloatModel m_envAmt;
  222. TempoSyncKnobModel m_envAtt;
  223. TempoSyncKnobModel m_envHold;
  224. TempoSyncKnobModel m_envDec;
  225. FloatModel m_xtalk;
  226. IntModel m_amod;
  227. IntModel m_bmod;
  228. IntModel m_selectedGraph;
  229. float A1_wave [WAVELEN];
  230. float A2_wave [WAVELEN];
  231. float B1_wave [WAVELEN];
  232. float B2_wave [WAVELEN];
  233. friend class WatsynObject;
  234. friend class WatsynView;
  235. };
  236. class WatsynView : public InstrumentViewFixedSize
  237. {
  238. Q_OBJECT
  239. public:
  240. WatsynView( Instrument * _instrument,
  241. QWidget * _parent );
  242. virtual ~WatsynView();
  243. protected slots:
  244. void updateLayout();
  245. void sinWaveClicked();
  246. void triWaveClicked();
  247. void sawWaveClicked();
  248. void sqrWaveClicked();
  249. void smoothClicked();
  250. void normalizeClicked();
  251. void invertClicked();
  252. void phaseLeftClicked();
  253. void phaseRightClicked();
  254. void loadClicked();
  255. private:
  256. virtual void modelChanged();
  257. // knobs
  258. Knob * a1_volKnob;
  259. Knob * a2_volKnob;
  260. Knob * b1_volKnob;
  261. Knob * b2_volKnob;
  262. Knob * a1_panKnob;
  263. Knob * a2_panKnob;
  264. Knob * b1_panKnob;
  265. Knob * b2_panKnob;
  266. Knob * a1_multKnob;
  267. Knob * a2_multKnob;
  268. Knob * b1_multKnob;
  269. Knob * b2_multKnob;
  270. Knob * a1_ltuneKnob;
  271. Knob * a2_ltuneKnob;
  272. Knob * b1_ltuneKnob;
  273. Knob * b2_ltuneKnob;
  274. Knob * a1_rtuneKnob;
  275. Knob * a2_rtuneKnob;
  276. Knob * b1_rtuneKnob;
  277. Knob * b2_rtuneKnob;
  278. Knob * m_abmixKnob;
  279. Knob * m_envAmtKnob;
  280. TempoSyncKnob * m_envAttKnob;
  281. TempoSyncKnob * m_envHoldKnob;
  282. TempoSyncKnob * m_envDecKnob;
  283. Knob * m_xtalkKnob;
  284. automatableButtonGroup * m_selectedGraphGroup;
  285. automatableButtonGroup * m_aModGroup;
  286. automatableButtonGroup * m_bModGroup;
  287. Graph * a1_graph;
  288. Graph * a2_graph;
  289. Graph * b1_graph;
  290. Graph * b2_graph;
  291. PixmapButton * m_sinWaveButton;
  292. PixmapButton * m_triWaveButton;
  293. PixmapButton * m_sawWaveButton;
  294. PixmapButton * m_sqrWaveButton;
  295. PixmapButton * m_normalizeButton;
  296. PixmapButton * m_invertButton;
  297. PixmapButton * m_smoothButton;
  298. PixmapButton * m_phaseLeftButton;
  299. PixmapButton * m_phaseRightButton;
  300. PixmapButton * m_loadButton;
  301. };
  302. #endif