lb302.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  1. /*
  2. * lb302.cpp - implementation of class lb302 which is a bass synth attempting
  3. * to 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. // Need to include this first to ensure we get M_PI in MinGW with C++11
  31. #define _USE_MATH_DEFINES
  32. #include <math.h>
  33. #include "lb302.h"
  34. #include "AutomatableButton.h"
  35. #include "Engine.h"
  36. #include "InstrumentPlayHandle.h"
  37. #include "InstrumentTrack.h"
  38. #include "Knob.h"
  39. #include "NotePlayHandle.h"
  40. #include "Oscillator.h"
  41. #include "PixmapButton.h"
  42. #include "templates.h"
  43. #include "ToolTip.h"
  44. #include "BandLimitedWave.h"
  45. #include "embed.cpp"
  46. // Envelope Recalculation period
  47. #define ENVINC 64
  48. //
  49. // New config
  50. //
  51. #define LB_24_IGNORE_ENVELOPE
  52. #define LB_FILTERED
  53. //#define LB_DECAY
  54. //#define LB_24_RES_TRICK
  55. #define LB_DIST_RATIO 4.0
  56. #define LB_24_VOL_ADJUST 3.0
  57. //#define LB_DECAY_NOTES
  58. #define LB_DEBUG
  59. #ifdef LB_DEBUG
  60. #include <assert.h>
  61. #endif
  62. //
  63. // Old config
  64. //
  65. //#define engine::mixer()->processingSampleRate() 44100.0f
  66. extern "C"
  67. {
  68. Plugin::Descriptor PLUGIN_EXPORT lb302_plugin_descriptor =
  69. {
  70. STRINGIFY( PLUGIN_NAME ),
  71. "LB302",
  72. QT_TRANSLATE_NOOP( "pluginBrowser",
  73. "Incomplete monophonic imitation tb303" ),
  74. "Paul Giblock <pgib/at/users.sf.net>",
  75. 0x0100,
  76. Plugin::Instrument,
  77. new PluginPixmapLoader( "logo" ),
  78. NULL,
  79. NULL
  80. };
  81. }
  82. //
  83. // lb302Filter
  84. //
  85. lb302Filter::lb302Filter(lb302FilterKnobState* p_fs) :
  86. fs(p_fs),
  87. vcf_c0(0),
  88. vcf_e0(0),
  89. vcf_e1(0)
  90. {
  91. };
  92. void lb302Filter::recalc()
  93. {
  94. vcf_e1 = exp(6.109 + 1.5876*(fs->envmod) + 2.1553*(fs->cutoff) - 1.2*(1.0-(fs->reso)));
  95. vcf_e0 = exp(5.613 - 0.8*(fs->envmod) + 2.1553*(fs->cutoff) - 0.7696*(1.0-(fs->reso)));
  96. vcf_e0*=M_PI/Engine::mixer()->processingSampleRate();
  97. vcf_e1*=M_PI/Engine::mixer()->processingSampleRate();
  98. vcf_e1 -= vcf_e0;
  99. vcf_rescoeff = exp(-1.20 + 3.455*(fs->reso));
  100. };
  101. void lb302Filter::envRecalc()
  102. {
  103. vcf_c0 *= fs->envdecay; // Filter Decay. vcf_decay is adjusted for Hz and ENVINC
  104. // vcf_rescoeff = exp(-1.20 + 3.455*(fs->reso)); moved above
  105. };
  106. void lb302Filter::playNote()
  107. {
  108. vcf_c0 = vcf_e1;
  109. }
  110. //
  111. // lb302FilterIIR2
  112. //
  113. lb302FilterIIR2::lb302FilterIIR2(lb302FilterKnobState* p_fs) :
  114. lb302Filter(p_fs),
  115. vcf_d1(0),
  116. vcf_d2(0),
  117. vcf_a(0),
  118. vcf_b(0),
  119. vcf_c(1)
  120. {
  121. m_dist = new DspEffectLibrary::Distortion( 1.0, 1.0f);
  122. };
  123. lb302FilterIIR2::~lb302FilterIIR2()
  124. {
  125. delete m_dist;
  126. }
  127. void lb302FilterIIR2::recalc()
  128. {
  129. lb302Filter::recalc();
  130. //m_dist->setThreshold(0.5+(fs->dist*2.0));
  131. m_dist->setThreshold(fs->dist*75.0);
  132. };
  133. void lb302FilterIIR2::envRecalc()
  134. {
  135. float k, w;
  136. lb302Filter::envRecalc();
  137. w = vcf_e0 + vcf_c0; // e0 is adjusted for Hz and doesn't need ENVINC
  138. k = exp(-w/vcf_rescoeff); // Does this mean c0 is inheritantly?
  139. vcf_a = 2.0*cos(2.0*w) * k;
  140. vcf_b = -k*k;
  141. vcf_c = 1.0 - vcf_a - vcf_b;
  142. }
  143. float lb302FilterIIR2::process(const float& samp)
  144. {
  145. float ret = vcf_a*vcf_d1 + vcf_b*vcf_d2 + vcf_c*samp;
  146. // Delayed samples for filter
  147. vcf_d2 = vcf_d1;
  148. vcf_d1 = ret;
  149. if(fs->dist > 0)
  150. ret=m_dist->nextSample(ret);
  151. // output = IIR2 + dry
  152. return ret;
  153. }
  154. //
  155. // lb302Filter3Pole
  156. //
  157. lb302Filter3Pole::lb302Filter3Pole(lb302FilterKnobState *p_fs) :
  158. lb302Filter(p_fs),
  159. ay1(0),
  160. ay2(0),
  161. aout(0),
  162. lastin(0)
  163. {
  164. };
  165. void lb302Filter3Pole::recalc()
  166. {
  167. // DO NOT CALL BASE CLASS
  168. vcf_e0 = 0.000001;
  169. vcf_e1 = 1.0;
  170. }
  171. // TODO: Try using k instead of vcf_reso
  172. void lb302Filter3Pole::envRecalc()
  173. {
  174. float w,k;
  175. float kfco;
  176. lb302Filter::envRecalc();
  177. // e0 is adjusted for Hz and doesn't need ENVINC
  178. w = vcf_e0 + vcf_c0;
  179. k = (fs->cutoff > 0.975)?0.975:fs->cutoff;
  180. kfco = 50.f + (k)*((2300.f-1600.f*(fs->envmod))+(w) *
  181. (700.f+1500.f*(k)+(1500.f+(k)*(Engine::mixer()->processingSampleRate()/2.f-6000.f)) *
  182. (fs->envmod)) );
  183. //+iacc*(.3+.7*kfco*kenvmod)*kaccent*kaccurve*2000
  184. #ifdef LB_24_IGNORE_ENVELOPE
  185. // kfcn = fs->cutoff;
  186. kfcn = 2.0 * kfco / Engine::mixer()->processingSampleRate();
  187. #else
  188. kfcn = w;
  189. #endif
  190. kp = ((-2.7528*kfcn + 3.0429)*kfcn + 1.718)*kfcn - 0.9984;
  191. kp1 = kp+1.0;
  192. kp1h = 0.5*kp1;
  193. #ifdef LB_24_RES_TRICK
  194. k = exp(-w/vcf_rescoeff);
  195. kres = (((k))) * (((-2.7079*kp1 + 10.963)*kp1 - 14.934)*kp1 + 8.4974);
  196. #else
  197. kres = (((fs->reso))) * (((-2.7079*kp1 + 10.963)*kp1 - 14.934)*kp1 + 8.4974);
  198. #endif
  199. value = 1.0+( (fs->dist) *(1.5 + 2.0*kres*(1.0-kfcn))); // ENVMOD was DIST
  200. }
  201. float lb302Filter3Pole::process(const float& samp)
  202. {
  203. float ax1 = lastin;
  204. float ay11 = ay1;
  205. float ay31 = ay2;
  206. lastin = (samp) - tanh(kres*aout);
  207. ay1 = kp1h * (lastin+ax1) - kp*ay1;
  208. ay2 = kp1h * (ay1 + ay11) - kp*ay2;
  209. aout = kp1h * (ay2 + ay31) - kp*aout;
  210. return tanh(aout*value)*LB_24_VOL_ADJUST/(1.0+fs->dist);
  211. }
  212. //
  213. // LBSynth
  214. //
  215. lb302Synth::lb302Synth( InstrumentTrack * _instrumentTrack ) :
  216. Instrument( _instrumentTrack, &lb302_plugin_descriptor ),
  217. vcf_cut_knob( 0.75f, 0.0f, 1.5f, 0.005f, this, tr( "VCF Cutoff Frequency" ) ),
  218. vcf_res_knob( 0.75f, 0.0f, 1.25f, 0.005f, this, tr( "VCF Resonance" ) ),
  219. vcf_mod_knob( 0.1f, 0.0f, 1.0f, 0.005f, this, tr( "VCF Envelope Mod" ) ),
  220. vcf_dec_knob( 0.1f, 0.0f, 1.0f, 0.005f, this, tr( "VCF Envelope Decay" ) ),
  221. dist_knob( 0.0f, 0.0f, 1.0f, 0.01f, this, tr( "Distortion" ) ),
  222. wave_shape( 8.0f, 0.0f, 11.0f, this, tr( "Waveform" ) ),
  223. slide_dec_knob( 0.6f, 0.0f, 1.0f, 0.005f, this, tr( "Slide Decay" ) ),
  224. slideToggle( false, this, tr( "Slide" ) ),
  225. accentToggle( false, this, tr( "Accent" ) ),
  226. deadToggle( false, this, tr( "Dead" ) ),
  227. db24Toggle( false, this, tr( "24dB/oct Filter" ) )
  228. {
  229. connect( Engine::mixer(), SIGNAL( sampleRateChanged( ) ),
  230. this, SLOT ( filterChanged( ) ) );
  231. connect( &vcf_cut_knob, SIGNAL( dataChanged( ) ),
  232. this, SLOT ( filterChanged( ) ) );
  233. connect( &vcf_res_knob, SIGNAL( dataChanged( ) ),
  234. this, SLOT ( filterChanged( ) ) );
  235. connect( &vcf_mod_knob, SIGNAL( dataChanged( ) ),
  236. this, SLOT ( filterChanged( ) ) );
  237. connect( &vcf_dec_knob, SIGNAL( dataChanged( ) ),
  238. this, SLOT ( filterChanged( ) ) );
  239. connect( &db24Toggle, SIGNAL( dataChanged( ) ),
  240. this, SLOT ( db24Toggled( ) ) );
  241. connect( &dist_knob, SIGNAL( dataChanged( ) ),
  242. this, SLOT ( filterChanged( )));
  243. // SYNTH
  244. vco_inc = 0.0;
  245. vco_c = 0;
  246. vco_k = 0;
  247. vco_slide = 0; vco_slideinc = 0;
  248. vco_slidebase = 0;
  249. fs.cutoff = 0;
  250. fs.envmod = 0;
  251. fs.reso = 0;
  252. fs.envdecay = 0;
  253. fs.dist = 0;
  254. vcf_envpos = ENVINC;
  255. // Start VCA on an attack.
  256. vca_mode = 3;
  257. vca_a = 0;
  258. //vca_attack = 1.0 - 0.94406088;
  259. vca_attack = 1.0 - 0.96406088;
  260. vca_decay = 0.99897516;
  261. vco_shape = BL_SAWTOOTH;
  262. // Experimenting with a0 between original (0.5) and 1.0
  263. vca_a0 = 0.5;
  264. vca_mode = 3;
  265. vcfs[0] = new lb302FilterIIR2(&fs);
  266. vcfs[1] = new lb302Filter3Pole(&fs);
  267. db24Toggled();
  268. sample_cnt = 0;
  269. release_frame = 0;
  270. catch_frame = 0;
  271. catch_decay = 0;
  272. last_offset = 0;
  273. new_freq = false;
  274. filterChanged();
  275. InstrumentPlayHandle * iph = new InstrumentPlayHandle( this, _instrumentTrack );
  276. Engine::mixer()->addPlayHandle( iph );
  277. }
  278. lb302Synth::~lb302Synth()
  279. {
  280. for (int i=0; i<NUM_FILTERS; ++i) {
  281. delete vcfs[i];
  282. }
  283. }
  284. void lb302Synth::saveSettings( QDomDocument & _doc,
  285. QDomElement & _this )
  286. {
  287. vcf_cut_knob.saveSettings( _doc, _this, "vcf_cut" );
  288. vcf_res_knob.saveSettings( _doc, _this, "vcf_res" );
  289. vcf_mod_knob.saveSettings( _doc, _this, "vcf_mod" );
  290. vcf_dec_knob.saveSettings( _doc, _this, "vcf_dec" );
  291. wave_shape.saveSettings( _doc, _this, "shape");
  292. dist_knob.saveSettings( _doc, _this, "dist");
  293. slide_dec_knob.saveSettings( _doc, _this, "slide_dec");
  294. slideToggle.saveSettings( _doc, _this, "slide");
  295. deadToggle.saveSettings( _doc, _this, "dead");
  296. db24Toggle.saveSettings( _doc, _this, "db24");
  297. }
  298. void lb302Synth::loadSettings( const QDomElement & _this )
  299. {
  300. vcf_cut_knob.loadSettings( _this, "vcf_cut" );
  301. vcf_res_knob.loadSettings( _this, "vcf_res" );
  302. vcf_mod_knob.loadSettings( _this, "vcf_mod" );
  303. vcf_dec_knob.loadSettings( _this, "vcf_dec" );
  304. dist_knob.loadSettings( _this, "dist");
  305. slide_dec_knob.loadSettings( _this, "slide_dec");
  306. wave_shape.loadSettings( _this, "shape");
  307. slideToggle.loadSettings( _this, "slide");
  308. deadToggle.loadSettings( _this, "dead");
  309. db24Toggle.loadSettings( _this, "db24");
  310. db24Toggled();
  311. filterChanged();
  312. }
  313. // TODO: Split into one function per knob. envdecay doesn't require
  314. // recalcFilter.
  315. void lb302Synth::filterChanged()
  316. {
  317. fs.cutoff = vcf_cut_knob.value();
  318. fs.reso = vcf_res_knob.value();
  319. fs.envmod = vcf_mod_knob.value();
  320. fs.dist = LB_DIST_RATIO*dist_knob.value();
  321. float d = 0.2 + (2.3*vcf_dec_knob.value());
  322. d *= Engine::mixer()->processingSampleRate(); // d *= smpl rate
  323. fs.envdecay = pow(0.1, 1.0/d * ENVINC); // decay is 0.1 to the 1/d * ENVINC
  324. // vcf_envdecay is now adjusted for both
  325. // sampling rate and ENVINC
  326. recalcFilter();
  327. }
  328. void lb302Synth::db24Toggled()
  329. {
  330. vcf = vcfs[db24Toggle.value()];
  331. // These recalcFilter calls might suck
  332. recalcFilter();
  333. }
  334. QString lb302Synth::nodeName() const
  335. {
  336. return( lb302_plugin_descriptor.name );
  337. }
  338. // OBSOLETE. Break apart once we get Q_OBJECT to work. >:[
  339. void lb302Synth::recalcFilter()
  340. {
  341. #if QT_VERSION >= 0x050000
  342. vcf.load()->recalc();
  343. #else
  344. vcf->recalc();
  345. #endif
  346. // THIS IS OLD 3pole/24dB code, I may reintegrate it. Don't need it
  347. // right now. Should be toggled by LB_24_RES_TRICK at the moment.
  348. /*kfcn = 2.0 * (((vcf_cutoff*3000))) / engine::mixer()->processingSampleRate();
  349. kp = ((-2.7528*kfcn + 3.0429)*kfcn + 1.718)*kfcn - 0.9984;
  350. kp1 = kp+1.0;
  351. kp1h = 0.5*kp1;
  352. kres = (((vcf_reso))) * (((-2.7079*kp1 + 10.963)*kp1 - 14.934)*kp1 + 8.4974);
  353. value = 1.0+( (((0))) *(1.5 + 2.0*kres*(1.0-kfcn))); // ENVMOD was DIST*/
  354. vcf_envpos = ENVINC; // Trigger filter update in process()
  355. }
  356. inline float GET_INC(float freq) {
  357. return freq/Engine::mixer()->processingSampleRate(); // TODO: Use actual sampling rate.
  358. }
  359. int lb302Synth::process(sampleFrame *outbuf, const int size)
  360. {
  361. const float sampleRatio = 44100.f / Engine::mixer()->processingSampleRate();
  362. float w;
  363. float samp;
  364. // Hold on to the current VCF, and use it throughout this period
  365. #if QT_VERSION >= 0x050000
  366. lb302Filter *filter = vcf.loadAcquire();
  367. #else
  368. lb302Filter *filter = vcf;
  369. #endif
  370. if( release_frame == 0 || ! m_playingNote )
  371. {
  372. vca_mode = 1;
  373. }
  374. if( new_freq )
  375. {
  376. //printf(" playing new note..\n");
  377. lb302Note note;
  378. note.vco_inc = GET_INC( true_freq );
  379. note.dead = deadToggle.value();
  380. initNote(&note);
  381. new_freq = false;
  382. }
  383. // TODO: NORMAL RELEASE
  384. // vca_mode = 1;
  385. for( int i=0; i<size; i++ )
  386. {
  387. // start decay if we're past release
  388. if( i >= release_frame )
  389. {
  390. vca_mode = 1;
  391. }
  392. // update vcf
  393. if(vcf_envpos >= ENVINC) {
  394. filter->envRecalc();
  395. vcf_envpos = 0;
  396. if (vco_slide) {
  397. vco_inc = vco_slidebase - vco_slide;
  398. // Calculate coeff from dec_knob on knob change.
  399. vco_slide -= vco_slide * ( 0.1f - slide_dec_knob.value() * 0.0999f ) * sampleRatio; // TODO: Adjust for ENVINC
  400. }
  401. }
  402. sample_cnt++;
  403. vcf_envpos++;
  404. //int decay_frames = 128;
  405. // update vco
  406. vco_c += vco_inc;
  407. if(vco_c > 0.5)
  408. vco_c -= 1.0;
  409. switch(int(rint(wave_shape.value()))) {
  410. case 0: vco_shape = SAWTOOTH; break;
  411. case 1: vco_shape = TRIANGLE; break;
  412. case 2: vco_shape = SQUARE; break;
  413. case 3: vco_shape = ROUND_SQUARE; break;
  414. case 4: vco_shape = MOOG; break;
  415. case 5: vco_shape = SINE; break;
  416. case 6: vco_shape = EXPONENTIAL; break;
  417. case 7: vco_shape = WHITE_NOISE; break;
  418. case 8: vco_shape = BL_SAWTOOTH; break;
  419. case 9: vco_shape = BL_SQUARE; break;
  420. case 10: vco_shape = BL_TRIANGLE; break;
  421. case 11: vco_shape = BL_MOOG; break;
  422. default: vco_shape = SAWTOOTH; break;
  423. }
  424. // add vco_shape_param the changes the shape of each curve.
  425. // merge sawtooths with triangle and square with round square?
  426. switch (vco_shape) {
  427. case SAWTOOTH: // p0: curviness of line
  428. vco_k = vco_c; // Is this sawtooth backwards?
  429. break;
  430. case TRIANGLE: // p0: duty rev.saw<->triangle<->saw p1: curviness
  431. vco_k = (vco_c*2.0)+0.5;
  432. if (vco_k>0.5)
  433. vco_k = 1.0- vco_k;
  434. break;
  435. case SQUARE: // p0: slope of top
  436. vco_k = (vco_c<0)?0.5:-0.5;
  437. break;
  438. case ROUND_SQUARE: // p0: width of round
  439. vco_k = (vco_c<0)?(sqrtf(1-(vco_c*vco_c*4))-0.5):-0.5;
  440. break;
  441. case MOOG: // Maybe the fall should be exponential/sinsoidal instead of quadric.
  442. // [-0.5, 0]: Rise, [0,0.25]: Slope down, [0.25,0.5]: Low
  443. vco_k = (vco_c*2.0)+0.5;
  444. if (vco_k>1.0) {
  445. vco_k = -0.5 ;
  446. }
  447. else if (vco_k>0.5) {
  448. w = 2.0*(vco_k-0.5)-1.0;
  449. vco_k = 0.5 - sqrtf(1.0-(w*w));
  450. }
  451. vco_k *= 2.0; // MOOG wave gets filtered away
  452. break;
  453. case SINE:
  454. // [-0.5, 0.5] : [-pi, pi]
  455. vco_k = 0.5f * Oscillator::sinSample( vco_c );
  456. break;
  457. case EXPONENTIAL:
  458. vco_k = 0.5 * Oscillator::expSample( vco_c );
  459. break;
  460. case WHITE_NOISE:
  461. vco_k = 0.5 * Oscillator::noiseSample( vco_c );
  462. break;
  463. case BL_SAWTOOTH:
  464. vco_k = BandLimitedWave::oscillate( vco_c + 0.5f, BandLimitedWave::pdToLen( vco_inc ), BandLimitedWave::BLSaw ) * 0.5f;
  465. break;
  466. case BL_SQUARE:
  467. vco_k = BandLimitedWave::oscillate( vco_c + 0.5f, BandLimitedWave::pdToLen( vco_inc ), BandLimitedWave::BLSquare ) * 0.5f;
  468. break;
  469. case BL_TRIANGLE:
  470. vco_k = BandLimitedWave::oscillate( vco_c + 0.5f, BandLimitedWave::pdToLen( vco_inc ), BandLimitedWave::BLTriangle ) * 0.5f;
  471. break;
  472. case BL_MOOG:
  473. vco_k = BandLimitedWave::oscillate( vco_c + 0.5f, BandLimitedWave::pdToLen( vco_inc ), BandLimitedWave::BLMoog );
  474. break;
  475. }
  476. //vca_a = 0.5;
  477. // Write out samples.
  478. #ifdef LB_FILTERED
  479. //samp = vcf->process(vco_k)*2.0*vca_a;
  480. //samp = vcf->process(vco_k)*2.0;
  481. samp = filter->process(vco_k) * vca_a;
  482. //printf("%f %d\n", vco_c, sample_cnt);
  483. //samp = vco_k * vca_a;
  484. if( sample_cnt <= 4 )
  485. {
  486. // vca_a = 0;
  487. }
  488. #else
  489. //samp = vco_k*vca_a;
  490. #endif
  491. /*
  492. float releaseFrames = desiredReleaseFrames();
  493. samp *= (releaseFrames - catch_decay)/releaseFrames;
  494. */
  495. //LB302 samp *= (float)(decay_frames - catch_decay)/(float)decay_frames;
  496. for( int c = 0; c < DEFAULT_CHANNELS; c++ )
  497. {
  498. outbuf[i][c] = samp;
  499. }
  500. // Handle Envelope
  501. if(vca_mode==0) {
  502. vca_a+=(vca_a0-vca_a)*vca_attack;
  503. if(sample_cnt>=0.5*Engine::mixer()->processingSampleRate())
  504. vca_mode = 2;
  505. }
  506. else if(vca_mode == 1) {
  507. vca_a *= vca_decay;
  508. // the following line actually speeds up processing
  509. if(vca_a < (1/65536.0)) {
  510. vca_a = 0;
  511. vca_mode = 3;
  512. }
  513. }
  514. }
  515. return 1;
  516. }
  517. /* Prepares the active LB302 note. I separated this into a function because it
  518. * needs to be called onplayNote() when a new note is started. It also needs
  519. * to be called from process() when a prior edge-to-edge note is done releasing.
  520. */
  521. void lb302Synth::initNote( lb302Note *n)
  522. {
  523. catch_decay = 0;
  524. vco_inc = n->vco_inc;
  525. // Always reset vca on non-dead notes, and
  526. // Only reset vca on decaying(decayed) and never-played
  527. if(n->dead == 0 || (vca_mode==1 || vca_mode==3)) {
  528. //printf(" good\n");
  529. sample_cnt = 0;
  530. vca_mode = 0;
  531. // LB303:
  532. //vca_a = 0;
  533. }
  534. else {
  535. vca_mode = 2;
  536. }
  537. initSlide();
  538. // Slide-from note, save inc for next note
  539. if (slideToggle.value()) {
  540. vco_slideinc = vco_inc; // May need to equal vco_slidebase+vco_slide if last note slid
  541. }
  542. recalcFilter();
  543. if(n->dead ==0){
  544. // Swap next two blocks??
  545. #if QT_VERSION >= 0x050000
  546. vcf.load()->playNote();
  547. #else
  548. vcf->playNote();
  549. #endif
  550. // Ensure envelope is recalculated
  551. vcf_envpos = ENVINC;
  552. // Double Check
  553. //vca_mode = 0;
  554. //vca_a = 0.0;
  555. }
  556. }
  557. void lb302Synth::initSlide()
  558. {
  559. // Initiate Slide
  560. if (vco_slideinc) {
  561. //printf(" sliding\n");
  562. vco_slide = vco_inc-vco_slideinc; // Slide amount
  563. vco_slidebase = vco_inc; // The REAL frequency
  564. vco_slideinc = 0; // reset from-note
  565. }
  566. else {
  567. vco_slide = 0;
  568. }
  569. }
  570. void lb302Synth::playNote( NotePlayHandle * _n, sampleFrame * _working_buffer )
  571. {
  572. if( _n->isMasterNote() || ( _n->hasParent() && _n->isReleased() ) )
  573. {
  574. return;
  575. }
  576. // sort notes: new notes to the end
  577. m_notesMutex.lock();
  578. if( _n->totalFramesPlayed() == 0 )
  579. {
  580. m_notes.append( _n );
  581. }
  582. else
  583. {
  584. m_notes.prepend( _n );
  585. }
  586. m_notesMutex.unlock();
  587. release_frame = qMax( release_frame, _n->framesLeft() + _n->offset() );
  588. }
  589. void lb302Synth::processNote( NotePlayHandle * _n )
  590. {
  591. /// Start a new note.
  592. if( _n->m_pluginData != this )
  593. {
  594. m_playingNote = _n;
  595. new_freq = true;
  596. _n->m_pluginData = this;
  597. }
  598. if( ! m_playingNote && ! _n->isReleased() && release_frame > 0 )
  599. {
  600. m_playingNote = _n;
  601. if ( slideToggle.value() )
  602. {
  603. vco_slideinc = GET_INC( _n->frequency() );
  604. }
  605. }
  606. // Check for slide
  607. if( m_playingNote == _n )
  608. {
  609. true_freq = _n->frequency();
  610. if( slideToggle.value() ) {
  611. vco_slidebase = GET_INC( true_freq ); // The REAL frequency
  612. }
  613. else {
  614. vco_inc = GET_INC( true_freq );
  615. }
  616. }
  617. }
  618. void lb302Synth::play( sampleFrame * _working_buffer )
  619. {
  620. m_notesMutex.lock();
  621. while( ! m_notes.isEmpty() )
  622. {
  623. processNote( m_notes.takeFirst() );
  624. };
  625. m_notesMutex.unlock();
  626. const fpp_t frames = Engine::mixer()->framesPerPeriod();
  627. process( _working_buffer, frames );
  628. instrumentTrack()->processAudioBuffer( _working_buffer, frames, NULL );
  629. // release_frame = 0; //removed for issue # 1432
  630. }
  631. void lb302Synth::deleteNotePluginData( NotePlayHandle * _n )
  632. {
  633. //printf("GONE\n");
  634. if( m_playingNote == _n )
  635. {
  636. m_playingNote = NULL;
  637. }
  638. }
  639. PluginView * lb302Synth::instantiateView( QWidget * _parent )
  640. {
  641. return( new lb302SynthView( this, _parent ) );
  642. }
  643. lb302SynthView::lb302SynthView( Instrument * _instrument, QWidget * _parent ) :
  644. InstrumentView( _instrument, _parent )
  645. {
  646. // GUI
  647. m_vcfCutKnob = new Knob( knobBright_26, this );
  648. m_vcfCutKnob->move( 75, 130 );
  649. m_vcfCutKnob->setHintText( tr( "Cutoff Freq:" ), "" );
  650. m_vcfCutKnob->setLabel( "" );
  651. m_vcfResKnob = new Knob( knobBright_26, this );
  652. m_vcfResKnob->move( 120, 130 );
  653. m_vcfResKnob->setHintText( tr( "Resonance:" ), "" );
  654. m_vcfResKnob->setLabel( "" );
  655. m_vcfModKnob = new Knob( knobBright_26, this );
  656. m_vcfModKnob->move( 165, 130 );
  657. m_vcfModKnob->setHintText( tr( "Env Mod:" ), "" );
  658. m_vcfModKnob->setLabel( "" );
  659. m_vcfDecKnob = new Knob( knobBright_26, this );
  660. m_vcfDecKnob->move( 210, 130 );
  661. m_vcfDecKnob->setHintText( tr( "Decay:" ), "" );
  662. m_vcfDecKnob->setLabel( "" );
  663. m_slideToggle = new LedCheckBox( "", this );
  664. m_slideToggle->move( 10, 180 );
  665. /* m_accentToggle = new LedCheckBox( "", this );
  666. m_accentToggle->move( 10, 200 );
  667. m_accentToggle->setDisabled(true);*/ // accent removed pending real implementation - no need for non-functional buttons
  668. m_deadToggle = new LedCheckBox( "", this );
  669. m_deadToggle->move( 10, 200 );
  670. m_db24Toggle = new LedCheckBox( "", this );
  671. m_db24Toggle->setWhatsThis(
  672. tr( "303-es-que, 24dB/octave, 3 pole filter" ) );
  673. m_db24Toggle->move( 10, 150);
  674. m_slideDecKnob = new Knob( knobBright_26, this );
  675. m_slideDecKnob->move( 210, 75 );
  676. m_slideDecKnob->setHintText( tr( "Slide Decay:" ), "" );
  677. m_slideDecKnob->setLabel( "");
  678. m_distKnob = new Knob( knobBright_26, this );
  679. m_distKnob->move( 210, 190 );
  680. m_distKnob->setHintText( tr( "DIST:" ), "" );
  681. m_distKnob->setLabel( tr( ""));
  682. // Shapes
  683. // move to 120,75
  684. const int waveBtnX = 10;
  685. const int waveBtnY = 96;
  686. PixmapButton * sawWaveBtn = new PixmapButton( this, tr( "Saw wave" ) );
  687. sawWaveBtn->move( waveBtnX, waveBtnY );
  688. sawWaveBtn->setActiveGraphic( embed::getIconPixmap(
  689. "saw_wave_active" ) );
  690. sawWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  691. "saw_wave_inactive" ) );
  692. ToolTip::add( sawWaveBtn,
  693. tr( "Click here for a saw-wave." ) );
  694. PixmapButton * triangleWaveBtn =
  695. new PixmapButton( this, tr( "Triangle wave" ) );
  696. triangleWaveBtn->move( waveBtnX+(16*1), waveBtnY );
  697. triangleWaveBtn->setActiveGraphic(
  698. embed::getIconPixmap( "triangle_wave_active" ) );
  699. triangleWaveBtn->setInactiveGraphic(
  700. embed::getIconPixmap( "triangle_wave_inactive" ) );
  701. ToolTip::add( triangleWaveBtn,
  702. tr( "Click here for a triangle-wave." ) );
  703. PixmapButton * sqrWaveBtn = new PixmapButton( this, tr( "Square wave" ) );
  704. sqrWaveBtn->move( waveBtnX+(16*2), waveBtnY );
  705. sqrWaveBtn->setActiveGraphic( embed::getIconPixmap(
  706. "square_wave_active" ) );
  707. sqrWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  708. "square_wave_inactive" ) );
  709. ToolTip::add( sqrWaveBtn,
  710. tr( "Click here for a square-wave." ) );
  711. PixmapButton * roundSqrWaveBtn =
  712. new PixmapButton( this, tr( "Rounded square wave" ) );
  713. roundSqrWaveBtn->move( waveBtnX+(16*3), waveBtnY );
  714. roundSqrWaveBtn->setActiveGraphic( embed::getIconPixmap(
  715. "round_square_wave_active" ) );
  716. roundSqrWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  717. "round_square_wave_inactive" ) );
  718. ToolTip::add( roundSqrWaveBtn,
  719. tr( "Click here for a square-wave with a rounded end." ) );
  720. PixmapButton * moogWaveBtn =
  721. new PixmapButton( this, tr( "Moog wave" ) );
  722. moogWaveBtn->move( waveBtnX+(16*4), waveBtnY );
  723. moogWaveBtn->setActiveGraphic(
  724. embed::getIconPixmap( "moog_saw_wave_active" ) );
  725. moogWaveBtn->setInactiveGraphic(
  726. embed::getIconPixmap( "moog_saw_wave_inactive" ) );
  727. ToolTip::add( moogWaveBtn,
  728. tr( "Click here for a moog-like wave." ) );
  729. PixmapButton * sinWaveBtn = new PixmapButton( this, tr( "Sine wave" ) );
  730. sinWaveBtn->move( waveBtnX+(16*5), waveBtnY );
  731. sinWaveBtn->setActiveGraphic( embed::getIconPixmap(
  732. "sin_wave_active" ) );
  733. sinWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  734. "sin_wave_inactive" ) );
  735. ToolTip::add( sinWaveBtn,
  736. tr( "Click for a sine-wave." ) );
  737. PixmapButton * exponentialWaveBtn =
  738. new PixmapButton( this, tr( "White noise wave" ) );
  739. exponentialWaveBtn->move( waveBtnX+(16*6), waveBtnY );
  740. exponentialWaveBtn->setActiveGraphic(
  741. embed::getIconPixmap( "exp_wave_active" ) );
  742. exponentialWaveBtn->setInactiveGraphic(
  743. embed::getIconPixmap( "exp_wave_inactive" ) );
  744. ToolTip::add( exponentialWaveBtn,
  745. tr( "Click here for an exponential wave." ) );
  746. PixmapButton * whiteNoiseWaveBtn =
  747. new PixmapButton( this, tr( "White noise wave" ) );
  748. whiteNoiseWaveBtn->move( waveBtnX+(16*7), waveBtnY );
  749. whiteNoiseWaveBtn->setActiveGraphic(
  750. embed::getIconPixmap( "white_noise_wave_active" ) );
  751. whiteNoiseWaveBtn->setInactiveGraphic(
  752. embed::getIconPixmap( "white_noise_wave_inactive" ) );
  753. ToolTip::add( whiteNoiseWaveBtn,
  754. tr( "Click here for white-noise." ) );
  755. PixmapButton * blSawWaveBtn =
  756. new PixmapButton( this, tr( "Bandlimited saw wave" ) );
  757. blSawWaveBtn->move( waveBtnX+(16*9)-8, waveBtnY );
  758. blSawWaveBtn->setActiveGraphic(
  759. embed::getIconPixmap( "saw_wave_active" ) );
  760. blSawWaveBtn->setInactiveGraphic(
  761. embed::getIconPixmap( "saw_wave_inactive" ) );
  762. ToolTip::add( blSawWaveBtn,
  763. tr( "Click here for bandlimited saw wave." ) );
  764. PixmapButton * blSquareWaveBtn =
  765. new PixmapButton( this, tr( "Bandlimited square wave" ) );
  766. blSquareWaveBtn->move( waveBtnX+(16*10)-8, waveBtnY );
  767. blSquareWaveBtn->setActiveGraphic(
  768. embed::getIconPixmap( "square_wave_active" ) );
  769. blSquareWaveBtn->setInactiveGraphic(
  770. embed::getIconPixmap( "square_wave_inactive" ) );
  771. ToolTip::add( blSquareWaveBtn,
  772. tr( "Click here for bandlimited square wave." ) );
  773. PixmapButton * blTriangleWaveBtn =
  774. new PixmapButton( this, tr( "Bandlimited triangle wave" ) );
  775. blTriangleWaveBtn->move( waveBtnX+(16*11)-8, waveBtnY );
  776. blTriangleWaveBtn->setActiveGraphic(
  777. embed::getIconPixmap( "triangle_wave_active" ) );
  778. blTriangleWaveBtn->setInactiveGraphic(
  779. embed::getIconPixmap( "triangle_wave_inactive" ) );
  780. ToolTip::add( blTriangleWaveBtn,
  781. tr( "Click here for bandlimited triangle wave." ) );
  782. PixmapButton * blMoogWaveBtn =
  783. new PixmapButton( this, tr( "Bandlimited moog saw wave" ) );
  784. blMoogWaveBtn->move( waveBtnX+(16*12)-8, waveBtnY );
  785. blMoogWaveBtn->setActiveGraphic(
  786. embed::getIconPixmap( "moog_saw_wave_active" ) );
  787. blMoogWaveBtn->setInactiveGraphic(
  788. embed::getIconPixmap( "moog_saw_wave_inactive" ) );
  789. ToolTip::add( blMoogWaveBtn,
  790. tr( "Click here for bandlimited moog saw wave." ) );
  791. m_waveBtnGrp = new automatableButtonGroup( this );
  792. m_waveBtnGrp->addButton( sawWaveBtn );
  793. m_waveBtnGrp->addButton( triangleWaveBtn );
  794. m_waveBtnGrp->addButton( sqrWaveBtn );
  795. m_waveBtnGrp->addButton( roundSqrWaveBtn );
  796. m_waveBtnGrp->addButton( moogWaveBtn );
  797. m_waveBtnGrp->addButton( sinWaveBtn );
  798. m_waveBtnGrp->addButton( exponentialWaveBtn );
  799. m_waveBtnGrp->addButton( whiteNoiseWaveBtn );
  800. m_waveBtnGrp->addButton( blSawWaveBtn );
  801. m_waveBtnGrp->addButton( blSquareWaveBtn );
  802. m_waveBtnGrp->addButton( blTriangleWaveBtn );
  803. m_waveBtnGrp->addButton( blMoogWaveBtn );
  804. setAutoFillBackground( true );
  805. QPalette pal;
  806. pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap(
  807. "artwork" ) );
  808. setPalette( pal );
  809. }
  810. lb302SynthView::~lb302SynthView()
  811. {
  812. }
  813. void lb302SynthView::modelChanged()
  814. {
  815. lb302Synth * syn = castModel<lb302Synth>();
  816. m_vcfCutKnob->setModel( &syn->vcf_cut_knob );
  817. m_vcfResKnob->setModel( &syn->vcf_res_knob );
  818. m_vcfDecKnob->setModel( &syn->vcf_dec_knob );
  819. m_vcfModKnob->setModel( &syn->vcf_mod_knob );
  820. m_slideDecKnob->setModel( &syn->slide_dec_knob );
  821. m_distKnob->setModel( &syn->dist_knob );
  822. m_waveBtnGrp->setModel( &syn->wave_shape );
  823. m_slideToggle->setModel( &syn->slideToggle );
  824. /*m_accentToggle->setModel( &syn->accentToggle );*/
  825. m_deadToggle->setModel( &syn->deadToggle );
  826. m_db24Toggle->setModel( &syn->db24Toggle );
  827. }
  828. extern "C"
  829. {
  830. // necessary for getting instance out of shared lib
  831. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model *, void * _data )
  832. {
  833. return( new lb302Synth(
  834. static_cast<InstrumentTrack *>( _data ) ) );
  835. }
  836. }