lb302.cpp 26 KB

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