sfxr.cpp 32 KB

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