sfxr.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135
  1. /*
  2. * sfxr.cpp - port of sfxr to LMMS
  3. * Originally written by Tomas Pettersson. For the original license,
  4. * please read readme.txt in this directory
  5. *
  6. * Copyright (c) 2014 Wong Cho Ching
  7. *
  8. * This file is part of LMMS - https://lmms.io
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program (see COPYING); if not, write to the
  22. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. * Boston, MA 02110-1301 USA.
  24. */
  25. #include <cstdlib>
  26. #include <time.h>
  27. #define rnd(n) (rand()%(n+1))
  28. #define PI 3.14159265f
  29. float frnd(float range)
  30. {
  31. return (float)rnd(10000)/10000*range;
  32. }
  33. #include <cmath>
  34. #include <QDomElement>
  35. #include "sfxr.h"
  36. #include "Engine.h"
  37. #include "InstrumentTrack.h"
  38. #include "Knob.h"
  39. #include "NotePlayHandle.h"
  40. #include "PixmapButton.h"
  41. #include "ToolTip.h"
  42. #include "Song.h"
  43. #include "MidiEvent.h"
  44. #include "MidiTime.h"
  45. #include "Mixer.h"
  46. #include "embed.h"
  47. #include "plugin_export.h"
  48. extern "C"
  49. {
  50. Plugin::Descriptor PLUGIN_EXPORT sfxr_plugin_descriptor =
  51. {
  52. STRINGIFY( PLUGIN_NAME ),
  53. "sfxr",
  54. QT_TRANSLATE_NOOP( "pluginBrowser",
  55. "LMMS port of sfxr" ),
  56. "Wong Cho Ching",
  57. 0x0100,
  58. Plugin::Instrument,
  59. new PluginPixmapLoader( "logo" ),
  60. NULL,
  61. NULL
  62. } ;
  63. }
  64. SfxrSynth::SfxrSynth( const sfxrInstrument * s ):
  65. s(s),
  66. playing_sample( true )
  67. {
  68. resetSample( false );
  69. }
  70. SfxrSynth::~SfxrSynth()
  71. {
  72. }
  73. void SfxrSynth::resetSample( bool restart )
  74. {
  75. if(!restart)
  76. {
  77. phase=0;
  78. }
  79. fperiod=100.0/(s->m_startFreqModel.value()*s->m_startFreqModel.value()+0.001);
  80. period=(int)fperiod;
  81. fmaxperiod=100.0/(s->m_minFreqModel.value()*s->m_minFreqModel.value()+0.001);
  82. fslide=1.0-pow((double)s->m_slideModel.value(), 3.0)*0.01;
  83. fdslide=-pow((double)s->m_dSlideModel.value(), 3.0)*0.000001;
  84. square_duty=0.5f-s->m_sqrDutyModel.value()*0.5f;
  85. square_slide=-s->m_sqrSweepModel.value()*0.00005f;
  86. if(s->m_changeAmtModel.value()>=0.0f)
  87. arp_mod=1.0-pow((double)s->m_changeAmtModel.value(), 2.0)*0.9;
  88. else
  89. arp_mod=1.0+pow((double)s->m_changeAmtModel.value(), 2.0)*10.0;
  90. arp_time=0;
  91. arp_limit=(int)(pow(1.0f-s->m_changeSpeedModel.value(), 2.0f)*20000+32);
  92. if(s->m_changeSpeedModel.value()==1.0f)
  93. arp_limit=0;
  94. if(!restart)
  95. {
  96. // reset filter
  97. fltp=0.0f;
  98. fltdp=0.0f;
  99. fltw=pow(s->m_lpFilCutModel.value(), 3.0f)*0.1f;
  100. fltw_d=1.0f+s->m_lpFilCutSweepModel.value()*0.0001f;
  101. fltdmp=5.0f/(1.0f+pow(s->m_lpFilResoModel.value(), 2.0f)*20.0f)*(0.01f+fltw);
  102. if(fltdmp>0.8f) fltdmp=0.8f;
  103. fltphp=0.0f;
  104. flthp=pow(s->m_hpFilCutModel.value(), 2.0f)*0.1f;
  105. flthp_d=1.0+s->m_hpFilCutSweepModel.value()*0.0003f;
  106. // reset vibrato
  107. vib_phase=0.0f;
  108. vib_speed=pow(s->m_vibSpeedModel.value(), 2.0f)*0.01f;
  109. vib_amp=s->m_vibDepthModel.value()*0.5f;
  110. // reset envelope
  111. env_vol=0.0f;
  112. env_stage=0;
  113. env_time=0;
  114. env_length[0]=(int)(s->m_attModel.value()*s->m_attModel.value()*99999.0f)+1;
  115. env_length[1]=(int)(s->m_holdModel.value()*s->m_holdModel.value()*99999.0f)+1;
  116. env_length[2]=(int)(s->m_decModel.value()*s->m_decModel.value()*99999.0f)+1;
  117. fphase=pow(s->m_phaserOffsetModel.value(), 2.0f)*1020.0f;
  118. if(s->m_phaserOffsetModel.value()<0.0f) fphase=-fphase;
  119. fdphase=pow(s->m_phaserSweepModel.value(), 2.0f)*1.0f;
  120. if(s->m_phaserSweepModel.value()<0.0f) fdphase=-fdphase;
  121. iphase=abs((int)fphase);
  122. ipp=0;
  123. for(int i=0;i<1024;i++)
  124. phaser_buffer[i]=0.0f;
  125. for(int i=0;i<32;i++)
  126. noise_buffer[i]=frnd(2.0f)-1.0f;
  127. rep_time=0;
  128. rep_limit=(int)(pow(1.0f-s->m_repeatSpeedModel.value(), 2.0f)*20000+32);
  129. if(s->m_repeatSpeedModel.value()==0.0f)
  130. rep_limit=0;
  131. }
  132. }
  133. void SfxrSynth::update( sampleFrame * buffer, const int32_t frameNum )
  134. {
  135. for(int i=0;i<frameNum;i++)
  136. {
  137. if(!playing_sample)
  138. {
  139. for( ch_cnt_t j=0; j < DEFAULT_CHANNELS; j++ )
  140. {
  141. buffer[i][j]=0.0f;
  142. }
  143. }
  144. rep_time++;
  145. if(rep_limit!=0 && rep_time>=rep_limit)
  146. {
  147. rep_limit=0;
  148. resetSample(true);
  149. }
  150. // frequency envelopes/arpeggios
  151. arp_time++;
  152. if(arp_limit!=0 && arp_time>=arp_limit)
  153. {
  154. arp_limit=0;
  155. fperiod*=arp_mod;
  156. }
  157. fslide+=fdslide;
  158. fperiod*=fslide;
  159. if(fperiod>fmaxperiod)
  160. {
  161. fperiod=fmaxperiod;
  162. if(s->m_minFreqModel.value()>0.0f)
  163. playing_sample=false;
  164. }
  165. float rfperiod=fperiod;
  166. if(vib_amp>0.0f)
  167. {
  168. vib_phase+=vib_speed;
  169. rfperiod=fperiod*(1.0+sin(vib_phase)*vib_amp);
  170. }
  171. period=(int)rfperiod;
  172. if(period<8) period=8;
  173. square_duty+=square_slide;
  174. if(square_duty<0.0f) square_duty=0.0f;
  175. if(square_duty>0.5f) square_duty=0.5f;
  176. // volume envelope
  177. env_time++;
  178. if(env_time>env_length[env_stage])
  179. {
  180. env_time=0;
  181. env_stage++;
  182. if(env_stage==3)
  183. playing_sample=false;
  184. }
  185. if(env_stage==0)
  186. env_vol=(float)env_time/env_length[0];
  187. if(env_stage==1)
  188. env_vol=1.0f+pow(1.0f-(float)env_time/env_length[1], 1.0f)*2.0f*s->m_susModel.value();
  189. if(env_stage==2)
  190. env_vol=1.0f-(float)env_time/env_length[2];
  191. // phaser step
  192. fphase+=fdphase;
  193. iphase=abs((int)fphase);
  194. if(iphase>1023) iphase=1023;
  195. if(flthp_d!=0.0f)
  196. {
  197. flthp*=flthp_d;
  198. if(flthp<0.00001f) flthp=0.00001f;
  199. if(flthp>0.1f) flthp=0.1f;
  200. }
  201. float ssample=0.0f;
  202. for(int si=0;si<8;si++) // 8x supersampling
  203. {
  204. float sample=0.0f;
  205. phase++;
  206. if(phase>=period)
  207. {
  208. // phase=0;
  209. phase%=period;
  210. if(s->m_waveFormModel.value()==3)
  211. for(int i=0;i<32;i++)
  212. noise_buffer[i]=frnd(2.0f)-1.0f;
  213. }
  214. // base waveform
  215. float fp=(float)phase/period;
  216. switch(s->m_waveFormModel.value())
  217. {
  218. case 0: // square
  219. if(fp<square_duty)
  220. sample=0.5f;
  221. else
  222. sample=-0.5f;
  223. break;
  224. case 1: // sawtooth
  225. sample=1.0f-fp*2;
  226. break;
  227. case 2: // sine
  228. sample=(float)sin(fp*2*PI);
  229. break;
  230. case 3: // noise
  231. sample=noise_buffer[phase*32/period];
  232. break;
  233. }
  234. // lp filter
  235. float pp=fltp;
  236. fltw*=fltw_d;
  237. if(fltw<0.0f) fltw=0.0f;
  238. if(fltw>0.1f) fltw=0.1f;
  239. if(s->m_lpFilCutModel.value()!=1.0f)
  240. {
  241. fltdp+=(sample-fltp)*fltw;
  242. fltdp-=fltdp*fltdmp;
  243. }
  244. else
  245. {
  246. fltp=sample;
  247. fltdp=0.0f;
  248. }
  249. fltp+=fltdp;
  250. // hp filter
  251. fltphp+=fltp-pp;
  252. fltphp-=fltphp*flthp;
  253. sample=fltphp;
  254. // phaser
  255. phaser_buffer[ipp&1023]=sample;
  256. sample+=phaser_buffer[(ipp-iphase+1024)&1023];
  257. ipp=(ipp+1)&1023;
  258. // final accumulation and envelope application
  259. ssample+=sample*env_vol;
  260. }
  261. //ssample=ssample/8*master_vol;
  262. //ssample*=2.0f*sound_vol;
  263. ssample*=0.025f;
  264. if(buffer!=NULL)
  265. {
  266. if(ssample>1.0f) ssample=1.0f;
  267. if(ssample<-1.0f) ssample=-1.0f;
  268. for( ch_cnt_t j=0; j<DEFAULT_CHANNELS; j++ )
  269. {
  270. buffer[i][j]=ssample;
  271. }
  272. }
  273. }
  274. }
  275. bool SfxrSynth::isPlaying() const
  276. {
  277. return playing_sample;
  278. }
  279. sfxrInstrument::sfxrInstrument( InstrumentTrack * _instrument_track ) :
  280. Instrument( _instrument_track, &sfxr_plugin_descriptor ),
  281. m_attModel(0.0f, this, "Attack Time"),
  282. m_holdModel(0.3f, this, "Sustain Time"),
  283. m_susModel(0.0f, this, "Sustain Punch"),
  284. m_decModel(0.4f, this, "Decay Time"),
  285. m_startFreqModel(0.3f, this, "Start Frequency"),
  286. m_minFreqModel(0.0f, this, "Min Frequency"),
  287. m_slideModel(0.0f, this, "Slide"),
  288. m_dSlideModel(0.0f, this, "Delta Slide"),
  289. m_vibDepthModel(0.0f, this, "Vibrato Depth"),
  290. m_vibSpeedModel(0.0f, this, "Vibrato Speed"),
  291. m_changeAmtModel(0.0f, this, "Change Amount"),
  292. m_changeSpeedModel(0.0f, this, "Change Speed"),
  293. m_sqrDutyModel(0.0f, this, "Square Duty"),
  294. m_sqrSweepModel(0.0f, this, "Duty Sweep"),
  295. m_repeatSpeedModel(0.0f, this, "Repeat Speed"),
  296. m_phaserOffsetModel(0.0f, this, "Phaser Offset"),
  297. m_phaserSweepModel(0.0f, this, "Phaser Sweep"),
  298. m_lpFilCutModel(1.0f, this, "LP Filter Cutoff"),
  299. m_lpFilCutSweepModel(0.0f, this, "LP Filter Cutoff Sweep"),
  300. m_lpFilResoModel(0.0f, this, "LP Filter Resonance"),
  301. m_hpFilCutModel(0.0f, this, "HP Filter Cutoff"),
  302. m_hpFilCutSweepModel(0.0f, this, "HP Filter Cutoff Sweep"),
  303. m_waveFormModel( SQR_WAVE, 0, WAVES_NUM-1, this, tr( "Wave" ) )
  304. {
  305. }
  306. sfxrInstrument::~sfxrInstrument()
  307. {
  308. }
  309. void sfxrInstrument::saveSettings( QDomDocument & _doc, QDomElement & _this )
  310. {
  311. _this.setAttribute( "version", "1" );
  312. m_attModel.saveSettings( _doc, _this, "att" );
  313. m_holdModel.saveSettings( _doc, _this, "hold" );
  314. m_susModel.saveSettings( _doc, _this, "sus" );
  315. m_decModel.saveSettings( _doc, _this, "dec" );
  316. m_startFreqModel.saveSettings( _doc, _this, "startFreq" );
  317. m_minFreqModel.saveSettings( _doc, _this, "minFreq" );
  318. m_slideModel.saveSettings( _doc, _this, "slide" );
  319. m_dSlideModel.saveSettings( _doc, _this, "dSlide" );
  320. m_vibDepthModel.saveSettings( _doc, _this, "vibDepth" );
  321. m_vibSpeedModel.saveSettings( _doc, _this, "vibSpeed" );
  322. m_changeAmtModel.saveSettings( _doc, _this, "changeAmt" );
  323. m_changeSpeedModel.saveSettings( _doc, _this, "changeSpeed" );
  324. m_sqrDutyModel.saveSettings( _doc, _this, "sqrDuty" );
  325. m_sqrSweepModel.saveSettings( _doc, _this, "sqrSweep" );
  326. m_repeatSpeedModel.saveSettings( _doc, _this, "repeatSpeed" );
  327. m_phaserOffsetModel.saveSettings( _doc, _this, "phaserOffset" );
  328. m_phaserSweepModel.saveSettings( _doc, _this, "phaserSweep" );
  329. m_lpFilCutModel.saveSettings( _doc, _this, "lpFilCut" );
  330. m_lpFilCutSweepModel.saveSettings( _doc, _this, "lpFilCutSweep" );
  331. m_lpFilResoModel.saveSettings( _doc, _this, "lpFilReso" );
  332. m_hpFilCutModel.saveSettings( _doc, _this, "hpFilCut" );
  333. m_hpFilCutSweepModel.saveSettings( _doc, _this, "hpFilCutSweep" );
  334. m_waveFormModel.saveSettings( _doc, _this, "waveForm" );
  335. }
  336. void sfxrInstrument::loadSettings( const QDomElement & _this )
  337. {
  338. m_attModel.loadSettings(_this, "att" );
  339. m_holdModel.loadSettings( _this, "hold" );
  340. m_susModel.loadSettings( _this, "sus" );
  341. m_decModel.loadSettings( _this, "dec" );
  342. m_startFreqModel.loadSettings( _this, "startFreq" );
  343. m_minFreqModel.loadSettings( _this, "minFreq" );
  344. m_slideModel.loadSettings( _this, "slide" );
  345. m_dSlideModel.loadSettings( _this, "dSlide" );
  346. m_vibDepthModel.loadSettings( _this, "vibDepth" );
  347. m_vibSpeedModel.loadSettings( _this, "vibSpeed" );
  348. m_changeAmtModel.loadSettings( _this, "changeAmt" );
  349. m_changeSpeedModel.loadSettings( _this, "changeSpeed" );
  350. m_sqrDutyModel.loadSettings( _this, "sqrDuty" );
  351. m_sqrSweepModel.loadSettings( _this, "sqrSweep" );
  352. m_repeatSpeedModel.loadSettings( _this, "repeatSpeed" );
  353. m_phaserOffsetModel.loadSettings( _this, "phaserOffset" );
  354. m_phaserSweepModel.loadSettings( _this, "phaserSweep" );
  355. m_lpFilCutModel.loadSettings( _this, "lpFilCut" );
  356. m_lpFilCutSweepModel.loadSettings( _this, "lpFilCutSweep" );
  357. m_lpFilResoModel.loadSettings( _this, "lpFilReso" );
  358. m_hpFilCutModel.loadSettings( _this, "hpFilCut" );
  359. m_hpFilCutSweepModel.loadSettings( _this, "hpFilCutSweep" );
  360. m_waveFormModel.loadSettings( _this, "waveForm" );
  361. }
  362. QString sfxrInstrument::nodeName() const
  363. {
  364. return( sfxr_plugin_descriptor.name );
  365. }
  366. void sfxrInstrument::playNote( NotePlayHandle * _n, sampleFrame * _working_buffer )
  367. {
  368. float currentSampleRate = Engine::mixer()->processingSampleRate();
  369. fpp_t frameNum = _n->framesLeftForCurrentPeriod();
  370. const f_cnt_t offset = _n->noteOffset();
  371. if ( _n->totalFramesPlayed() == 0 || _n->m_pluginData == NULL )
  372. {
  373. _n->m_pluginData = new SfxrSynth( this );
  374. }
  375. else if( static_cast<SfxrSynth*>(_n->m_pluginData)->isPlaying() == false )
  376. {
  377. memset(_working_buffer + offset, 0, sizeof(sampleFrame) * frameNum);
  378. _n->noteOff();
  379. return;
  380. }
  381. int32_t pitchedFrameNum = (_n->frequency()/BaseFreq)*frameNum;
  382. pitchedFrameNum /= ( currentSampleRate / 44100 );
  383. // debug code
  384. // qDebug( "pFN %d", pitchedFrameNum );
  385. sampleFrame * pitchedBuffer = new sampleFrame[pitchedFrameNum];
  386. static_cast<SfxrSynth*>(_n->m_pluginData)->update( pitchedBuffer, pitchedFrameNum );
  387. for( fpp_t i=0; i<frameNum; i++ )
  388. {
  389. for( ch_cnt_t j=0; j<DEFAULT_CHANNELS; j++ )
  390. {
  391. _working_buffer[i+offset][j] = pitchedBuffer[i*pitchedFrameNum/frameNum][j];
  392. }
  393. }
  394. delete[] pitchedBuffer;
  395. applyRelease( _working_buffer, _n );
  396. instrumentTrack()->processAudioBuffer( _working_buffer, frameNum + offset, _n );
  397. }
  398. void sfxrInstrument::deleteNotePluginData( NotePlayHandle * _n )
  399. {
  400. delete static_cast<SfxrSynth *>( _n->m_pluginData );
  401. }
  402. PluginView * sfxrInstrument::instantiateView( QWidget * _parent )
  403. {
  404. return( new sfxrInstrumentView( this, _parent ) );
  405. }
  406. void sfxrInstrument::resetModels()
  407. {
  408. m_attModel.reset();
  409. m_holdModel.reset();
  410. m_susModel.reset();
  411. m_decModel.reset();
  412. m_startFreqModel.reset();
  413. m_minFreqModel.reset();
  414. m_slideModel.reset();
  415. m_dSlideModel.reset();
  416. m_vibDepthModel.reset();
  417. m_vibSpeedModel.reset();
  418. m_changeAmtModel.reset();
  419. m_changeSpeedModel.reset();
  420. m_sqrDutyModel.reset();
  421. m_sqrSweepModel.reset();
  422. m_repeatSpeedModel.reset();
  423. m_phaserOffsetModel.reset();
  424. m_phaserSweepModel.reset();
  425. m_lpFilCutModel.reset();
  426. m_lpFilCutSweepModel.reset();
  427. m_lpFilResoModel.reset();
  428. m_hpFilCutModel.reset();
  429. m_hpFilCutSweepModel.reset();
  430. m_waveFormModel.reset();
  431. }
  432. class sfxrKnob : public Knob
  433. {
  434. public:
  435. sfxrKnob( QWidget * _parent ) :
  436. Knob( knobStyled, _parent )
  437. {
  438. setFixedSize( 20, 20 );
  439. setCenterPointX( 10.0 );
  440. setCenterPointY( 10.0 );
  441. setTotalAngle( 270.0 );
  442. setLineWidth( 1 );
  443. }
  444. };
  445. #define createKnob( _knob, _x, _y, _name )\
  446. _knob = new sfxrKnob( this ); \
  447. _knob->setHintText( tr( _name ":" ), "" ); \
  448. _knob->move( _x, _y ); \
  449. ToolTip::add( _knob, tr( _name ) );
  450. #define createButton( _button, _x, _y, _name, _resName )\
  451. _button = new PixmapButton( this, tr( _name ) );\
  452. _button->move( _x, _y );\
  453. _button->setActiveGraphic( embed::getIconPixmap( _resName "_active" ) );\
  454. _button->setInactiveGraphic( embed::getIconPixmap( _resName "_inactive" ) );\
  455. ToolTip::add( _button, tr( _name ) );
  456. #define createButtonLocalGraphic( _button, _x, _y, _name, _resName )\
  457. _button = new PixmapButton( this, tr( _name ) );\
  458. _button->move( _x, _y );\
  459. _button->setActiveGraphic( PLUGIN_NAME::getIconPixmap( _resName "_active" ) );\
  460. _button->setInactiveGraphic( PLUGIN_NAME::getIconPixmap( _resName "_inactive" ) );\
  461. ToolTip::add( _button, tr( _name ) );
  462. sfxrInstrumentView::sfxrInstrumentView( Instrument * _instrument,
  463. QWidget * _parent ) :
  464. InstrumentViewFixedSize( _instrument, _parent )
  465. {
  466. srand(time(NULL));
  467. setAutoFillBackground( true );
  468. QPalette pal;
  469. pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap( "artwork" ) );
  470. setPalette( pal );
  471. createKnob(m_attKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*0, "Attack Time");
  472. createKnob(m_holdKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*1, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*0, "Sustain Time");
  473. createKnob(m_susKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*2, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*0, "Sustain Punch");
  474. createKnob(m_decKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*0, "Decay Time");
  475. m_attKnob ->setObjectName( "envKnob" );
  476. m_holdKnob ->setObjectName( "envKnob" );
  477. m_susKnob ->setObjectName( "envKnob" );
  478. m_decKnob ->setObjectName( "envKnob" );
  479. createKnob(m_startFreqKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Start Frequency");
  480. createKnob(m_minFreqKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*1, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Min Frequency");
  481. createKnob(m_slideKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*2, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Slide");
  482. createKnob(m_dSlideKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Delta Slide");
  483. createKnob(m_vibDepthKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*4, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Vibrato Depth");
  484. createKnob(m_vibSpeedKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*5, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Vibrato Speed");
  485. m_startFreqKnob ->setObjectName( "freqKnob" );
  486. m_minFreqKnob ->setObjectName( "freqKnob" );
  487. m_slideKnob ->setObjectName( "freqKnob" );
  488. m_dSlideKnob ->setObjectName( "freqKnob" );
  489. m_vibDepthKnob ->setObjectName( "freqKnob" );
  490. m_vibSpeedKnob ->setObjectName( "freqKnob" );
  491. createKnob(m_changeAmtKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*2, "Change Amount");
  492. createKnob(m_changeSpeedKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*1, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*2, "Change Speed");
  493. m_changeAmtKnob ->setObjectName( "changeKnob" );
  494. m_changeSpeedKnob ->setObjectName( "changeKnob" );
  495. createKnob(m_sqrDutyKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*2, "Square Duty (Square wave only)");
  496. createKnob(m_sqrSweepKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*4, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*2, "Duty Sweep (Square wave only)");
  497. m_sqrDutyKnob ->setObjectName( "sqrKnob" );
  498. m_sqrSweepKnob ->setObjectName( "sqrKnob" );
  499. createKnob(m_repeatSpeedKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*3, "Repeat Speed");
  500. m_repeatSpeedKnob ->setObjectName( "repeatKnob" );
  501. createKnob(m_phaserOffsetKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*3, "Phaser Offset");
  502. createKnob(m_phaserSweepKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*4, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*3, "Phaser Sweep");
  503. m_phaserOffsetKnob ->setObjectName( "phaserKnob" );
  504. m_phaserSweepKnob ->setObjectName( "phaserKnob" );
  505. createKnob(m_lpFilCutKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "LP Filter Cutoff");
  506. createKnob(m_lpFilCutSweepKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*1, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "LP Filter Cutoff Sweep");
  507. createKnob(m_lpFilResoKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*2, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "LP Filter Resonance");
  508. createKnob(m_hpFilCutKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "HP Filter Cutoff");
  509. createKnob(m_hpFilCutSweepKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*4, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "HP Filter Cutoff Sweep");
  510. m_lpFilCutKnob ->setObjectName( "filterKnob" );
  511. m_lpFilCutSweepKnob ->setObjectName( "filterKnob" );
  512. m_lpFilResoKnob ->setObjectName( "filterKnob" );
  513. m_hpFilCutKnob ->setObjectName( "filterKnob" );
  514. m_hpFilCutSweepKnob ->setObjectName( "filterKnob" );
  515. createButtonLocalGraphic(m_sqrWaveBtn, KNOBS_BASE_X+WAVEFORM_BUTTON_WIDTH*0, WAVEFORM_BASE_Y, "Square Wave", "sfxr_square_wave");
  516. createButtonLocalGraphic(m_sawWaveBtn, KNOBS_BASE_X+WAVEFORM_BUTTON_WIDTH*1, WAVEFORM_BASE_Y, "Saw Wave", "sfxr_saw_wave");
  517. createButtonLocalGraphic(m_sinWaveBtn, KNOBS_BASE_X+WAVEFORM_BUTTON_WIDTH*2, WAVEFORM_BASE_Y, "Sine Wave", "sfxr_sin_wave");
  518. createButtonLocalGraphic(m_noiseWaveBtn, KNOBS_BASE_X+WAVEFORM_BUTTON_WIDTH*3, WAVEFORM_BASE_Y, "Noise", "sfxr_white_noise_wave");
  519. m_waveBtnGroup = new automatableButtonGroup( this );
  520. m_waveBtnGroup->addButton(m_sqrWaveBtn);
  521. m_waveBtnGroup->addButton(m_sawWaveBtn);
  522. m_waveBtnGroup->addButton(m_sinWaveBtn);
  523. m_waveBtnGroup->addButton(m_noiseWaveBtn);
  524. createButtonLocalGraphic(m_pickupBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*0, GENERATOR_BASE_Y, "Generate pick up/coin sfx", "pickup");
  525. createButtonLocalGraphic(m_laserBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*1, GENERATOR_BASE_Y, "Generate laser/shoot sfx", "laser");
  526. createButtonLocalGraphic(m_explosionBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*2, GENERATOR_BASE_Y, "Generate explosion sfx", "explosion");
  527. createButtonLocalGraphic(m_powerupBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*3, GENERATOR_BASE_Y, "Generate power up sfx", "powerup");
  528. createButtonLocalGraphic(m_hitBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*4, GENERATOR_BASE_Y, "Generate hit/hurt sfx", "hit");
  529. createButtonLocalGraphic(m_jumpBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*5, GENERATOR_BASE_Y, "Generate jump sfx", "jump");
  530. createButtonLocalGraphic(m_blipBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*6, GENERATOR_BASE_Y, "Generate blip/select sfx", "blip");
  531. connect( m_pickupBtn, SIGNAL ( clicked() ), this, SLOT ( genPickup() ) );
  532. connect( m_laserBtn, SIGNAL ( clicked() ), this, SLOT ( genLaser() ) );
  533. connect( m_explosionBtn, SIGNAL ( clicked() ), this, SLOT ( genExplosion() ) );
  534. connect( m_powerupBtn, SIGNAL ( clicked() ), this, SLOT ( genPowerup() ) );
  535. connect( m_hitBtn, SIGNAL ( clicked() ), this, SLOT ( genHit() ) );
  536. connect( m_jumpBtn, SIGNAL ( clicked() ), this, SLOT ( genJump() ) );
  537. connect( m_blipBtn, SIGNAL ( clicked() ), this, SLOT ( genBlip() ) );
  538. createButtonLocalGraphic(m_randomizeBtn, RAND_BUTTON_X, RAND_BUTTON_Y, "Generate random sfx", "randomize");
  539. createButtonLocalGraphic(m_mutateBtn, MUTA_BUTTON_X, MUTA_BUTTON_Y, "Mutate sfx", "mutate");
  540. connect( m_randomizeBtn, SIGNAL ( clicked() ), this, SLOT ( randomize() ) );
  541. connect( m_mutateBtn, SIGNAL ( clicked() ), this, SLOT ( mutate() ) );
  542. //preview sound on generator/random/mutate button clicked
  543. /* // disabled for now
  544. connect( m_pickupBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  545. connect( m_laserBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  546. connect( m_explosionBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  547. connect( m_powerupBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  548. connect( m_hitBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  549. connect( m_jumpBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  550. connect( m_blipBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  551. connect( m_randomizeBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  552. connect( m_mutateBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  553. */
  554. }
  555. void sfxrInstrumentView::modelChanged()
  556. {
  557. sfxrInstrument * s = castModel<sfxrInstrument>();
  558. m_attKnob->setModel( &s->m_attModel );
  559. m_holdKnob->setModel( &s->m_holdModel );
  560. m_susKnob->setModel( &s->m_susModel );
  561. m_decKnob->setModel( &s->m_decModel );
  562. m_startFreqKnob->setModel( &s->m_startFreqModel );
  563. m_minFreqKnob->setModel( &s->m_minFreqModel );
  564. m_slideKnob->setModel( &s->m_slideModel );
  565. m_dSlideKnob->setModel( &s->m_dSlideModel );
  566. m_vibDepthKnob->setModel( &s->m_vibDepthModel );
  567. m_vibSpeedKnob->setModel( &s->m_vibSpeedModel );
  568. m_changeAmtKnob->setModel( &s->m_changeAmtModel );
  569. m_changeSpeedKnob->setModel( &s->m_changeSpeedModel );
  570. m_sqrDutyKnob->setModel( &s->m_sqrDutyModel );
  571. m_sqrSweepKnob->setModel( &s->m_sqrSweepModel );
  572. m_repeatSpeedKnob->setModel( &s->m_repeatSpeedModel );
  573. m_phaserOffsetKnob->setModel( &s->m_phaserOffsetModel );
  574. m_phaserSweepKnob->setModel( &s->m_phaserSweepModel );
  575. m_lpFilCutKnob->setModel( &s->m_lpFilCutModel );
  576. m_lpFilCutSweepKnob->setModel( &s->m_lpFilCutSweepModel );
  577. m_lpFilResoKnob->setModel( &s->m_lpFilResoModel );
  578. m_hpFilCutKnob->setModel( &s->m_hpFilCutModel );
  579. m_hpFilCutSweepKnob->setModel( &s->m_hpFilCutSweepModel );
  580. m_waveBtnGroup->setModel( &s->m_waveFormModel );
  581. }
  582. void sfxrInstrumentView::genPickup()
  583. {
  584. sfxrInstrument * s = castModel<sfxrInstrument>();
  585. s->resetModels();
  586. s->m_startFreqModel.setValue( 0.4f+frnd(0.5f) );
  587. s->m_attModel.setValue( 0.0f );
  588. s->m_holdModel.setValue( frnd(0.1f) );
  589. s->m_decModel.setValue( 0.1f+frnd(0.4f) );
  590. s->m_susModel.setValue( 0.3f+frnd(0.3f) );
  591. if(rnd(1))
  592. {
  593. s->m_changeSpeedModel.setValue( 0.5f+frnd(0.2f) );
  594. s->m_changeAmtModel.setValue( 0.2f+frnd(0.4f) );
  595. }
  596. }
  597. void sfxrInstrumentView::genLaser()
  598. {
  599. sfxrInstrument * s = castModel<sfxrInstrument>();
  600. s->resetModels();
  601. s->m_waveFormModel.setValue( rnd(2) );
  602. if(s->m_waveFormModel.value()==2 && rnd(1))
  603. s->m_waveFormModel.setValue( rnd(1) );
  604. s->m_startFreqModel.setValue( 0.5f+frnd(0.5f) );
  605. s->m_minFreqModel.setValue( s->m_startFreqModel.value()-0.2f-frnd(0.6f) );
  606. if(s->m_minFreqModel.value()<0.2f)
  607. {
  608. s->m_minFreqModel.setValue(0.2f);
  609. }
  610. s->m_slideModel.setValue( -0.15f-frnd(0.2f) );
  611. if(rnd(2)==0)
  612. {
  613. s->m_startFreqModel.setValue( 0.3f+frnd(0.6f) );
  614. s->m_minFreqModel.setValue( frnd(0.1f) );
  615. s->m_slideModel.setValue( -0.35f-frnd(0.3f) );
  616. }
  617. if(rnd(1))
  618. {
  619. s->m_sqrDutyModel.setValue( frnd(0.5f) );
  620. s->m_sqrSweepModel.setValue( 0.2f );
  621. }
  622. else
  623. {
  624. s->m_sqrDutyModel.setValue( 0.4f+frnd(0.5f) );
  625. s->m_sqrSweepModel.setValue( -frnd(0.7f) );
  626. }
  627. s->m_attModel.setValue( 0.0f );
  628. s->m_holdModel.setValue( 0.1f+frnd(0.2f) );
  629. s->m_decModel.setValue( frnd(0.4f) );
  630. if(rnd(1))
  631. {
  632. s->m_susModel.setValue( frnd(0.3f) );
  633. }
  634. if(rnd(2)==0)
  635. {
  636. s->m_phaserOffsetModel.setValue( frnd(0.2f) );
  637. s->m_phaserSweepModel.setValue( -frnd(0.2f) );
  638. }
  639. if(rnd(1))
  640. s->m_hpFilCutModel.setValue( frnd(0.3f) );
  641. }
  642. void sfxrInstrumentView::genExplosion()
  643. {
  644. sfxrInstrument * s = castModel<sfxrInstrument>();
  645. s->resetModels();
  646. s->m_waveFormModel.setValue( 3 );
  647. if(rnd(1))
  648. {
  649. s->m_startFreqModel.setValue( 0.1f+frnd(0.4f) );
  650. s->m_slideModel.setValue( -0.1f+frnd(0.4f) );
  651. }
  652. else
  653. {
  654. s->m_startFreqModel.setValue( 0.2f+frnd(0.7f) );
  655. s->m_slideModel.setValue( -0.2f-frnd(0.2f) );
  656. }
  657. s->m_startFreqModel.setValue( s->m_startFreqModel.value()*s->m_startFreqModel.value() );
  658. if(rnd(4)==0)
  659. {
  660. s->m_slideModel.setValue( 0.0f );
  661. }
  662. if(rnd(2)==0)
  663. {
  664. s->m_repeatSpeedModel.setValue( 0.3f+frnd(0.5f) );
  665. }
  666. s->m_attModel.setValue( 0.0f );
  667. s->m_holdModel.setValue( 0.1f+frnd(0.3f) );
  668. s->m_decModel.setValue( 0.5f );
  669. if(rnd(1)==0)
  670. {
  671. s->m_phaserOffsetModel.setValue( -0.3f+frnd(0.9f) );
  672. s->m_phaserSweepModel.setValue( -frnd(0.3f) );
  673. }
  674. s->m_susModel.setValue( 0.2f+frnd(0.6f) );
  675. if(rnd(1))
  676. {
  677. s->m_vibDepthModel.setValue( frnd(0.7f) );
  678. s->m_vibSpeedModel.setValue( frnd(0.6f) );
  679. }
  680. if(rnd(2)==0)
  681. {
  682. s->m_changeSpeedModel.setValue( 0.6f+frnd(0.3f) );
  683. s->m_changeAmtModel.setValue( 0.8f-frnd(1.6f) );
  684. }
  685. }
  686. void sfxrInstrumentView::genPowerup()
  687. {
  688. sfxrInstrument * s = castModel<sfxrInstrument>();
  689. s->resetModels();
  690. if(rnd(1))
  691. s->m_waveFormModel.setValue( 1 );
  692. else
  693. s->m_sqrDutyModel.setValue( frnd(0.6f) );
  694. if(rnd(1))
  695. {
  696. s->m_startFreqModel.setValue( 0.2f+frnd(0.3f) );
  697. s->m_slideModel.setValue( 0.1f+frnd(0.4f) );
  698. s->m_repeatSpeedModel.setValue( 0.4f+frnd(0.4f) );
  699. }
  700. else
  701. {
  702. s->m_startFreqModel.setValue( 0.2f+frnd(0.3f) );
  703. s->m_slideModel.setValue( 0.05f+frnd(0.2f) );
  704. if(rnd(1))
  705. {
  706. s->m_vibDepthModel.setValue( frnd(0.7f) );
  707. s->m_vibSpeedModel.setValue( frnd(0.6f) );
  708. }
  709. }
  710. s->m_attModel.setValue( 0.0f );
  711. s->m_holdModel.setValue( frnd(0.4f) );
  712. s->m_decModel.setValue( 0.1f+frnd(0.4f) );
  713. }
  714. void sfxrInstrumentView::genHit()
  715. {
  716. sfxrInstrument * s = castModel<sfxrInstrument>();
  717. s->resetModels();
  718. s->m_waveFormModel.setValue( rnd(2) );
  719. if(s->m_waveFormModel.value()==2)
  720. {
  721. s->m_waveFormModel.setValue( 3 );
  722. }
  723. if(s->m_waveFormModel.value()==0)
  724. {
  725. s->m_sqrDutyModel.setValue( frnd(0.6f) );
  726. }
  727. s->m_startFreqModel.setValue( 0.2f+frnd(0.6f) );
  728. s->m_slideModel.setValue( -0.3f-frnd(0.4f) );
  729. s->m_attModel.setValue( 0.0f );
  730. s->m_holdModel.setValue( frnd(0.1f) );
  731. s->m_decModel.setValue( 0.1f+frnd(0.2f) );
  732. if(rnd(1))
  733. {
  734. s->m_hpFilCutModel.setValue( frnd(0.3f) );
  735. }
  736. }
  737. void sfxrInstrumentView::genJump()
  738. {
  739. sfxrInstrument * s = castModel<sfxrInstrument>();
  740. s->resetModels();
  741. s->m_waveFormModel.setValue( 0 );
  742. s->m_sqrDutyModel.setValue( frnd(0.6f) );
  743. s->m_startFreqModel.setValue( 0.3f+frnd(0.3f) );
  744. s->m_slideModel.setValue( 0.1f+frnd(0.2f) );
  745. s->m_attModel.setValue( 0.0f );
  746. s->m_holdModel.setValue( 0.1f+frnd(0.3f) );
  747. s->m_decModel.setValue( 0.1f+frnd(0.2f) );
  748. if(rnd(1))
  749. {
  750. s->m_hpFilCutModel.setValue( frnd(0.3f) );
  751. }
  752. if(rnd(1))
  753. {
  754. s->m_lpFilCutModel.setValue( 1.0f-frnd(0.6f) );
  755. }
  756. }
  757. void sfxrInstrumentView::genBlip()
  758. {
  759. sfxrInstrument * s = castModel<sfxrInstrument>();
  760. s->resetModels();
  761. s->m_waveFormModel.setValue( rnd(1) );
  762. if( s->m_waveFormModel.value()==0 )
  763. {
  764. s->m_sqrDutyModel.setValue( frnd(0.6f) );
  765. }
  766. s->m_startFreqModel.setValue( 0.2f+frnd(0.4f) );
  767. s->m_attModel.setValue( 0.0f );
  768. s->m_holdModel.setValue( 0.1f+frnd(0.1f) );
  769. s->m_decModel.setValue( frnd(0.2f) );
  770. s->m_hpFilCutModel.setValue( 0.1f );
  771. }
  772. void sfxrInstrumentView::randomize()
  773. {
  774. sfxrInstrument * s = castModel<sfxrInstrument>();
  775. s->m_startFreqModel.setValue( pow(frnd(2.0f)-1.0f, 2.0f) );
  776. if(rnd(1))
  777. {
  778. s->m_startFreqModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f)+0.5f );
  779. }
  780. s->m_minFreqModel.setValue( 0.0f );
  781. s->m_slideModel.setValue( pow(frnd(2.0f)-1.0f, 5.0f) );
  782. if( s->m_startFreqModel.value()>0.7f && s->m_slideModel.value()>0.2f )
  783. {
  784. s->m_slideModel.setValue( -s->m_slideModel.value() );
  785. }
  786. if( s->m_startFreqModel.value()<0.2f && s->m_slideModel.value()<-0.05f )
  787. {
  788. s->m_slideModel.setValue( -s->m_slideModel.value() );
  789. }
  790. s->m_dSlideModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  791. s->m_sqrDutyModel.setValue( frnd(2.0f)-1.0f );
  792. s->m_sqrSweepModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  793. s->m_vibDepthModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  794. s->m_vibSpeedModel.setValue( frnd(2.0f)-1.0f );
  795. //s->m_vibDelayModel.setValue( frnd(2.0f)-1.0f );
  796. s->m_attModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  797. s->m_holdModel.setValue( pow(frnd(2.0f)-1.0f, 2.0f) );
  798. s->m_decModel.setValue( frnd(2.0f)-1.0f );
  799. s->m_susModel.setValue( pow(frnd(0.8f), 2.0f) );
  800. if(s->m_attModel.value()+s->m_holdModel.value()+s->m_decModel.value()<0.2f)
  801. {
  802. s->m_holdModel.setValue( s->m_holdModel.value()+0.2f+frnd(0.3f) );
  803. s->m_decModel.setValue( s->m_decModel.value()+0.2f+frnd(0.3f) );
  804. }
  805. s->m_lpFilResoModel.setValue( frnd(2.0f)-1.0f );
  806. s->m_lpFilCutModel.setValue( 1.0f-pow(frnd(1.0f), 3.0f) );
  807. s->m_lpFilCutSweepModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  808. if(s->m_lpFilCutModel.value()<0.1f && s->m_lpFilCutSweepModel.value()<-0.05f)
  809. {
  810. s->m_lpFilCutSweepModel.setValue( -s->m_lpFilCutSweepModel.value() );
  811. }
  812. s->m_hpFilCutModel.setValue( pow(frnd(1.0f), 5.0f) );
  813. s->m_hpFilCutSweepModel.setValue( pow(frnd(2.0f)-1.0f, 5.0f) );
  814. s->m_phaserOffsetModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  815. s->m_phaserSweepModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  816. s->m_repeatSpeedModel.setValue( frnd(2.0f)-1.0f );
  817. s->m_changeSpeedModel.setValue( frnd(2.0f)-1.0f );
  818. s->m_changeAmtModel.setValue( frnd(2.0f)-1.0f );
  819. }
  820. void sfxrInstrumentView::mutate()
  821. {
  822. sfxrInstrument * s = castModel<sfxrInstrument>();
  823. if(rnd(1)) s->m_startFreqModel.setValue( s->m_startFreqModel.value()+frnd(0.1f)-0.05f );
  824. // if(rnd(1)) s->m_minFreqModel.setValue( s->m_minFreqModel.value()+frnd(0.1f)-0.05f );
  825. if(rnd(1)) s->m_slideModel.setValue( s->m_slideModel.value()+frnd(0.1f)-0.05f );
  826. if(rnd(1)) s->m_dSlideModel.setValue( s->m_dSlideModel.value()+frnd(0.1f)-0.05f );
  827. if(rnd(1)) s->m_sqrDutyModel.setValue( s->m_sqrDutyModel.value()+frnd(0.1f)-0.05f );
  828. if(rnd(1)) s->m_sqrSweepModel.setValue( s->m_sqrSweepModel.value()+frnd(0.1f)-0.05f );
  829. if(rnd(1)) s->m_vibDepthModel.setValue( s->m_vibDepthModel.value()+frnd(0.1f)-0.05f );
  830. if(rnd(1)) s->m_vibSpeedModel.setValue( s->m_vibSpeedModel.value()+frnd(0.1f)-0.05f );
  831. // if(rnd(1)) s->m_vibDelayModel.setValue( s->m_vibDelayModel.value()+frnd(0.1f)-0.05f );
  832. if(rnd(1)) s->m_attModel.setValue( s->m_attModel.value()+frnd(0.1f)-0.05f );
  833. if(rnd(1)) s->m_holdModel.setValue( s->m_holdModel.value()+frnd(0.1f)-0.05f );
  834. if(rnd(1)) s->m_decModel.setValue( s->m_decModel.value()+frnd(0.1f)-0.05f );
  835. if(rnd(1)) s->m_susModel.setValue( s->m_susModel.value()+frnd(0.1f)-0.05f );
  836. if(rnd(1)) s->m_lpFilResoModel.setValue( s->m_lpFilResoModel.value()+frnd(0.1f)-0.05f );
  837. if(rnd(1)) s->m_lpFilCutModel.setValue( s->m_lpFilCutModel.value()+frnd(0.1f)-0.05f );
  838. if(rnd(1)) s->m_lpFilCutSweepModel.setValue( s->m_lpFilCutSweepModel.value()+frnd(0.1f)-0.05f );
  839. if(rnd(1)) s->m_hpFilCutModel.setValue( s->m_hpFilCutModel.value()+frnd(0.1f)-0.05f );
  840. if(rnd(1)) s->m_hpFilCutSweepModel.setValue( s->m_hpFilCutSweepModel.value()+frnd(0.1f)-0.05f );
  841. if(rnd(1)) s->m_phaserOffsetModel.setValue( s->m_phaserOffsetModel.value()+frnd(0.1f)-0.05f );
  842. if(rnd(1)) s->m_phaserSweepModel.setValue( s->m_phaserSweepModel.value()+frnd(0.1f)-0.05f );
  843. if(rnd(1)) s->m_repeatSpeedModel.setValue( s->m_repeatSpeedModel.value()+frnd(0.1f)-0.05f );
  844. if(rnd(1)) s->m_changeSpeedModel.setValue( s->m_changeSpeedModel.value()+frnd(0.1f)-0.05f );
  845. if(rnd(1)) s->m_changeAmtModel.setValue( s->m_changeAmtModel.value()+frnd(0.1f)-0.05f );
  846. }
  847. void sfxrInstrumentView::previewSound()
  848. {
  849. sfxrInstrument* s = castModel<sfxrInstrument>();
  850. InstrumentTrack* it = s->instrumentTrack();
  851. it->silenceAllNotes();
  852. it->processInEvent( MidiEvent( MidiNoteOn, 0, it->baseNoteModel()->value(), MidiDefaultVelocity ) );
  853. }
  854. extern "C"
  855. {
  856. // necessary for getting instance out of shared lib
  857. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* m, void* )
  858. {
  859. return new sfxrInstrument( static_cast<InstrumentTrack *>( m ) );
  860. }
  861. }