sfxr.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  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 "AudioEngine.h"
  37. #include "Engine.h"
  38. #include "InstrumentTrack.h"
  39. #include "Knob.h"
  40. #include "lmms_constants.h"
  41. #include "NotePlayHandle.h"
  42. #include "PixmapButton.h"
  43. #include "ToolTip.h"
  44. #include "Song.h"
  45. #include "MidiEvent.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. nullptr,
  61. nullptr,
  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!=nullptr)
  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::audioEngine()->processingSampleRate();
  369. fpp_t frameNum = _n->framesLeftForCurrentPeriod();
  370. const f_cnt_t offset = _n->noteOffset();
  371. if ( _n->totalFramesPlayed() == 0 || _n->m_pluginData == nullptr )
  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. const auto baseFreq = instrumentTrack()->baseFreq();
  382. int32_t pitchedFrameNum = (_n->frequency() / baseFreq) * frameNum;
  383. pitchedFrameNum /= ( currentSampleRate / 44100 );
  384. // debug code
  385. // qDebug( "pFN %d", pitchedFrameNum );
  386. sampleFrame * pitchedBuffer = new sampleFrame[pitchedFrameNum];
  387. static_cast<SfxrSynth*>(_n->m_pluginData)->update( pitchedBuffer, pitchedFrameNum );
  388. for( fpp_t i=0; i<frameNum; i++ )
  389. {
  390. for( ch_cnt_t j=0; j<DEFAULT_CHANNELS; j++ )
  391. {
  392. _working_buffer[i+offset][j] = pitchedBuffer[i*pitchedFrameNum/frameNum][j];
  393. }
  394. }
  395. delete[] pitchedBuffer;
  396. applyRelease( _working_buffer, _n );
  397. instrumentTrack()->processAudioBuffer( _working_buffer, frameNum + offset, _n );
  398. }
  399. void sfxrInstrument::deleteNotePluginData( NotePlayHandle * _n )
  400. {
  401. delete static_cast<SfxrSynth *>( _n->m_pluginData );
  402. }
  403. PluginView * sfxrInstrument::instantiateView( QWidget * _parent )
  404. {
  405. return( new sfxrInstrumentView( this, _parent ) );
  406. }
  407. void sfxrInstrument::resetModels()
  408. {
  409. m_attModel.reset();
  410. m_holdModel.reset();
  411. m_susModel.reset();
  412. m_decModel.reset();
  413. m_startFreqModel.reset();
  414. m_minFreqModel.reset();
  415. m_slideModel.reset();
  416. m_dSlideModel.reset();
  417. m_vibDepthModel.reset();
  418. m_vibSpeedModel.reset();
  419. m_changeAmtModel.reset();
  420. m_changeSpeedModel.reset();
  421. m_sqrDutyModel.reset();
  422. m_sqrSweepModel.reset();
  423. m_repeatSpeedModel.reset();
  424. m_phaserOffsetModel.reset();
  425. m_phaserSweepModel.reset();
  426. m_lpFilCutModel.reset();
  427. m_lpFilCutSweepModel.reset();
  428. m_lpFilResoModel.reset();
  429. m_hpFilCutModel.reset();
  430. m_hpFilCutSweepModel.reset();
  431. m_waveFormModel.reset();
  432. }
  433. class sfxrKnob : public Knob
  434. {
  435. public:
  436. sfxrKnob( QWidget * _parent ) :
  437. Knob( knobStyled, _parent )
  438. {
  439. setFixedSize( 20, 20 );
  440. setCenterPointX( 10.0 );
  441. setCenterPointY( 10.0 );
  442. setTotalAngle( 270.0 );
  443. setLineWidth( 1 );
  444. }
  445. };
  446. #define createKnob( _knob, _x, _y, _name )\
  447. _knob = new sfxrKnob( this ); \
  448. _knob->setHintText( tr( _name ":" ), "" ); \
  449. _knob->move( _x, _y ); \
  450. ToolTip::add( _knob, tr( _name ) );
  451. #define createButton( _button, _x, _y, _name, _resName )\
  452. _button = new PixmapButton( this, tr( _name ) );\
  453. _button->move( _x, _y );\
  454. _button->setActiveGraphic( embed::getIconPixmap( _resName "_active" ) );\
  455. _button->setInactiveGraphic( embed::getIconPixmap( _resName "_inactive" ) );\
  456. ToolTip::add( _button, tr( _name ) );
  457. #define createButtonLocalGraphic( _button, _x, _y, _name, _resName )\
  458. _button = new PixmapButton( this, tr( _name ) );\
  459. _button->move( _x, _y );\
  460. _button->setActiveGraphic( PLUGIN_NAME::getIconPixmap( _resName "_active" ) );\
  461. _button->setInactiveGraphic( PLUGIN_NAME::getIconPixmap( _resName "_inactive" ) );\
  462. ToolTip::add( _button, tr( _name ) );
  463. sfxrInstrumentView::sfxrInstrumentView( Instrument * _instrument,
  464. QWidget * _parent ) :
  465. InstrumentViewFixedSize( _instrument, _parent )
  466. {
  467. srand(time(nullptr));
  468. setAutoFillBackground( true );
  469. QPalette pal;
  470. pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap( "artwork" ) );
  471. setPalette( pal );
  472. createKnob(m_attKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*0, "Attack Time");
  473. createKnob(m_holdKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*1, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*0, "Sustain Time");
  474. createKnob(m_susKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*2, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*0, "Sustain Punch");
  475. createKnob(m_decKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*0, "Decay Time");
  476. m_attKnob ->setObjectName( "envKnob" );
  477. m_holdKnob ->setObjectName( "envKnob" );
  478. m_susKnob ->setObjectName( "envKnob" );
  479. m_decKnob ->setObjectName( "envKnob" );
  480. createKnob(m_startFreqKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Start Frequency");
  481. createKnob(m_minFreqKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*1, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Min Frequency");
  482. createKnob(m_slideKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*2, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Slide");
  483. createKnob(m_dSlideKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Delta Slide");
  484. createKnob(m_vibDepthKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*4, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Vibrato Depth");
  485. createKnob(m_vibSpeedKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*5, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*1, "Vibrato Speed");
  486. m_startFreqKnob ->setObjectName( "freqKnob" );
  487. m_minFreqKnob ->setObjectName( "freqKnob" );
  488. m_slideKnob ->setObjectName( "freqKnob" );
  489. m_dSlideKnob ->setObjectName( "freqKnob" );
  490. m_vibDepthKnob ->setObjectName( "freqKnob" );
  491. m_vibSpeedKnob ->setObjectName( "freqKnob" );
  492. createKnob(m_changeAmtKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*2, "Change Amount");
  493. createKnob(m_changeSpeedKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*1, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*2, "Change Speed");
  494. m_changeAmtKnob ->setObjectName( "changeKnob" );
  495. m_changeSpeedKnob ->setObjectName( "changeKnob" );
  496. createKnob(m_sqrDutyKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*2, "Square Duty (Square wave only)");
  497. createKnob(m_sqrSweepKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*4, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*2, "Duty Sweep (Square wave only)");
  498. m_sqrDutyKnob ->setObjectName( "sqrKnob" );
  499. m_sqrSweepKnob ->setObjectName( "sqrKnob" );
  500. createKnob(m_repeatSpeedKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*3, "Repeat Speed");
  501. m_repeatSpeedKnob ->setObjectName( "repeatKnob" );
  502. createKnob(m_phaserOffsetKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*3, "Phaser Offset");
  503. createKnob(m_phaserSweepKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*4, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*3, "Phaser Sweep");
  504. m_phaserOffsetKnob ->setObjectName( "phaserKnob" );
  505. m_phaserSweepKnob ->setObjectName( "phaserKnob" );
  506. createKnob(m_lpFilCutKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*0, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "LP Filter Cutoff");
  507. createKnob(m_lpFilCutSweepKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*1, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "LP Filter Cutoff Sweep");
  508. createKnob(m_lpFilResoKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*2, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "LP Filter Resonance");
  509. createKnob(m_hpFilCutKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*3, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "HP Filter Cutoff");
  510. createKnob(m_hpFilCutSweepKnob, KNOBS_BASE_X+KNOB_BLOCK_SIZE_X*4, KNOBS_BASE_Y+KNOB_BLOCK_SIZE_Y*4, "HP Filter Cutoff Sweep");
  511. m_lpFilCutKnob ->setObjectName( "filterKnob" );
  512. m_lpFilCutSweepKnob ->setObjectName( "filterKnob" );
  513. m_lpFilResoKnob ->setObjectName( "filterKnob" );
  514. m_hpFilCutKnob ->setObjectName( "filterKnob" );
  515. m_hpFilCutSweepKnob ->setObjectName( "filterKnob" );
  516. createButtonLocalGraphic(m_sqrWaveBtn, KNOBS_BASE_X+WAVEFORM_BUTTON_WIDTH*0, WAVEFORM_BASE_Y, "Square Wave", "sfxr_square_wave");
  517. createButtonLocalGraphic(m_sawWaveBtn, KNOBS_BASE_X+WAVEFORM_BUTTON_WIDTH*1, WAVEFORM_BASE_Y, "Saw Wave", "sfxr_saw_wave");
  518. createButtonLocalGraphic(m_sinWaveBtn, KNOBS_BASE_X+WAVEFORM_BUTTON_WIDTH*2, WAVEFORM_BASE_Y, "Sine Wave", "sfxr_sin_wave");
  519. createButtonLocalGraphic(m_noiseWaveBtn, KNOBS_BASE_X+WAVEFORM_BUTTON_WIDTH*3, WAVEFORM_BASE_Y, "Noise", "sfxr_white_noise_wave");
  520. m_waveBtnGroup = new automatableButtonGroup( this );
  521. m_waveBtnGroup->addButton(m_sqrWaveBtn);
  522. m_waveBtnGroup->addButton(m_sawWaveBtn);
  523. m_waveBtnGroup->addButton(m_sinWaveBtn);
  524. m_waveBtnGroup->addButton(m_noiseWaveBtn);
  525. createButtonLocalGraphic(m_pickupBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*0, GENERATOR_BASE_Y, "Generate pick up/coin sfx", "pickup");
  526. createButtonLocalGraphic(m_laserBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*1, GENERATOR_BASE_Y, "Generate laser/shoot sfx", "laser");
  527. createButtonLocalGraphic(m_explosionBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*2, GENERATOR_BASE_Y, "Generate explosion sfx", "explosion");
  528. createButtonLocalGraphic(m_powerupBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*3, GENERATOR_BASE_Y, "Generate power up sfx", "powerup");
  529. createButtonLocalGraphic(m_hitBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*4, GENERATOR_BASE_Y, "Generate hit/hurt sfx", "hit");
  530. createButtonLocalGraphic(m_jumpBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*5, GENERATOR_BASE_Y, "Generate jump sfx", "jump");
  531. createButtonLocalGraphic(m_blipBtn, GENERATOR_BASE_X+GENERATOR_BUTTON_WIDTH*6, GENERATOR_BASE_Y, "Generate blip/select sfx", "blip");
  532. connect( m_pickupBtn, SIGNAL ( clicked() ), this, SLOT ( genPickup() ) );
  533. connect( m_laserBtn, SIGNAL ( clicked() ), this, SLOT ( genLaser() ) );
  534. connect( m_explosionBtn, SIGNAL ( clicked() ), this, SLOT ( genExplosion() ) );
  535. connect( m_powerupBtn, SIGNAL ( clicked() ), this, SLOT ( genPowerup() ) );
  536. connect( m_hitBtn, SIGNAL ( clicked() ), this, SLOT ( genHit() ) );
  537. connect( m_jumpBtn, SIGNAL ( clicked() ), this, SLOT ( genJump() ) );
  538. connect( m_blipBtn, SIGNAL ( clicked() ), this, SLOT ( genBlip() ) );
  539. createButtonLocalGraphic(m_randomizeBtn, RAND_BUTTON_X, RAND_BUTTON_Y, "Generate random sfx", "randomize");
  540. createButtonLocalGraphic(m_mutateBtn, MUTA_BUTTON_X, MUTA_BUTTON_Y, "Mutate sfx", "mutate");
  541. connect( m_randomizeBtn, SIGNAL ( clicked() ), this, SLOT ( randomize() ) );
  542. connect( m_mutateBtn, SIGNAL ( clicked() ), this, SLOT ( mutate() ) );
  543. //preview sound on generator/random/mutate button clicked
  544. /* // disabled for now
  545. connect( m_pickupBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  546. connect( m_laserBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  547. connect( m_explosionBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  548. connect( m_powerupBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  549. connect( m_hitBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  550. connect( m_jumpBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  551. connect( m_blipBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  552. connect( m_randomizeBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  553. connect( m_mutateBtn, SIGNAL ( clicked() ), this, SLOT ( previewSound() ) );
  554. */
  555. }
  556. void sfxrInstrumentView::modelChanged()
  557. {
  558. sfxrInstrument * s = castModel<sfxrInstrument>();
  559. m_attKnob->setModel( &s->m_attModel );
  560. m_holdKnob->setModel( &s->m_holdModel );
  561. m_susKnob->setModel( &s->m_susModel );
  562. m_decKnob->setModel( &s->m_decModel );
  563. m_startFreqKnob->setModel( &s->m_startFreqModel );
  564. m_minFreqKnob->setModel( &s->m_minFreqModel );
  565. m_slideKnob->setModel( &s->m_slideModel );
  566. m_dSlideKnob->setModel( &s->m_dSlideModel );
  567. m_vibDepthKnob->setModel( &s->m_vibDepthModel );
  568. m_vibSpeedKnob->setModel( &s->m_vibSpeedModel );
  569. m_changeAmtKnob->setModel( &s->m_changeAmtModel );
  570. m_changeSpeedKnob->setModel( &s->m_changeSpeedModel );
  571. m_sqrDutyKnob->setModel( &s->m_sqrDutyModel );
  572. m_sqrSweepKnob->setModel( &s->m_sqrSweepModel );
  573. m_repeatSpeedKnob->setModel( &s->m_repeatSpeedModel );
  574. m_phaserOffsetKnob->setModel( &s->m_phaserOffsetModel );
  575. m_phaserSweepKnob->setModel( &s->m_phaserSweepModel );
  576. m_lpFilCutKnob->setModel( &s->m_lpFilCutModel );
  577. m_lpFilCutSweepKnob->setModel( &s->m_lpFilCutSweepModel );
  578. m_lpFilResoKnob->setModel( &s->m_lpFilResoModel );
  579. m_hpFilCutKnob->setModel( &s->m_hpFilCutModel );
  580. m_hpFilCutSweepKnob->setModel( &s->m_hpFilCutSweepModel );
  581. m_waveBtnGroup->setModel( &s->m_waveFormModel );
  582. }
  583. void sfxrInstrumentView::genPickup()
  584. {
  585. sfxrInstrument * s = castModel<sfxrInstrument>();
  586. s->resetModels();
  587. s->m_startFreqModel.setValue( 0.4f+frnd(0.5f) );
  588. s->m_attModel.setValue( 0.0f );
  589. s->m_holdModel.setValue( frnd(0.1f) );
  590. s->m_decModel.setValue( 0.1f+frnd(0.4f) );
  591. s->m_susModel.setValue( 0.3f+frnd(0.3f) );
  592. if(rnd(1))
  593. {
  594. s->m_changeSpeedModel.setValue( 0.5f+frnd(0.2f) );
  595. s->m_changeAmtModel.setValue( 0.2f+frnd(0.4f) );
  596. }
  597. }
  598. void sfxrInstrumentView::genLaser()
  599. {
  600. sfxrInstrument * s = castModel<sfxrInstrument>();
  601. s->resetModels();
  602. s->m_waveFormModel.setValue( rnd(2) );
  603. if(s->m_waveFormModel.value()==2 && rnd(1))
  604. s->m_waveFormModel.setValue( rnd(1) );
  605. s->m_startFreqModel.setValue( 0.5f+frnd(0.5f) );
  606. s->m_minFreqModel.setValue( s->m_startFreqModel.value()-0.2f-frnd(0.6f) );
  607. if(s->m_minFreqModel.value()<0.2f)
  608. {
  609. s->m_minFreqModel.setValue(0.2f);
  610. }
  611. s->m_slideModel.setValue( -0.15f-frnd(0.2f) );
  612. if(rnd(2)==0)
  613. {
  614. s->m_startFreqModel.setValue( 0.3f+frnd(0.6f) );
  615. s->m_minFreqModel.setValue( frnd(0.1f) );
  616. s->m_slideModel.setValue( -0.35f-frnd(0.3f) );
  617. }
  618. if(rnd(1))
  619. {
  620. s->m_sqrDutyModel.setValue( frnd(0.5f) );
  621. s->m_sqrSweepModel.setValue( 0.2f );
  622. }
  623. else
  624. {
  625. s->m_sqrDutyModel.setValue( 0.4f+frnd(0.5f) );
  626. s->m_sqrSweepModel.setValue( -frnd(0.7f) );
  627. }
  628. s->m_attModel.setValue( 0.0f );
  629. s->m_holdModel.setValue( 0.1f+frnd(0.2f) );
  630. s->m_decModel.setValue( frnd(0.4f) );
  631. if(rnd(1))
  632. {
  633. s->m_susModel.setValue( frnd(0.3f) );
  634. }
  635. if(rnd(2)==0)
  636. {
  637. s->m_phaserOffsetModel.setValue( frnd(0.2f) );
  638. s->m_phaserSweepModel.setValue( -frnd(0.2f) );
  639. }
  640. if(rnd(1))
  641. s->m_hpFilCutModel.setValue( frnd(0.3f) );
  642. }
  643. void sfxrInstrumentView::genExplosion()
  644. {
  645. sfxrInstrument * s = castModel<sfxrInstrument>();
  646. s->resetModels();
  647. s->m_waveFormModel.setValue( 3 );
  648. if(rnd(1))
  649. {
  650. s->m_startFreqModel.setValue( 0.1f+frnd(0.4f) );
  651. s->m_slideModel.setValue( -0.1f+frnd(0.4f) );
  652. }
  653. else
  654. {
  655. s->m_startFreqModel.setValue( 0.2f+frnd(0.7f) );
  656. s->m_slideModel.setValue( -0.2f-frnd(0.2f) );
  657. }
  658. s->m_startFreqModel.setValue( s->m_startFreqModel.value()*s->m_startFreqModel.value() );
  659. if(rnd(4)==0)
  660. {
  661. s->m_slideModel.setValue( 0.0f );
  662. }
  663. if(rnd(2)==0)
  664. {
  665. s->m_repeatSpeedModel.setValue( 0.3f+frnd(0.5f) );
  666. }
  667. s->m_attModel.setValue( 0.0f );
  668. s->m_holdModel.setValue( 0.1f+frnd(0.3f) );
  669. s->m_decModel.setValue( 0.5f );
  670. if(rnd(1)==0)
  671. {
  672. s->m_phaserOffsetModel.setValue( -0.3f+frnd(0.9f) );
  673. s->m_phaserSweepModel.setValue( -frnd(0.3f) );
  674. }
  675. s->m_susModel.setValue( 0.2f+frnd(0.6f) );
  676. if(rnd(1))
  677. {
  678. s->m_vibDepthModel.setValue( frnd(0.7f) );
  679. s->m_vibSpeedModel.setValue( frnd(0.6f) );
  680. }
  681. if(rnd(2)==0)
  682. {
  683. s->m_changeSpeedModel.setValue( 0.6f+frnd(0.3f) );
  684. s->m_changeAmtModel.setValue( 0.8f-frnd(1.6f) );
  685. }
  686. }
  687. void sfxrInstrumentView::genPowerup()
  688. {
  689. sfxrInstrument * s = castModel<sfxrInstrument>();
  690. s->resetModels();
  691. if(rnd(1))
  692. s->m_waveFormModel.setValue( 1 );
  693. else
  694. s->m_sqrDutyModel.setValue( frnd(0.6f) );
  695. if(rnd(1))
  696. {
  697. s->m_startFreqModel.setValue( 0.2f+frnd(0.3f) );
  698. s->m_slideModel.setValue( 0.1f+frnd(0.4f) );
  699. s->m_repeatSpeedModel.setValue( 0.4f+frnd(0.4f) );
  700. }
  701. else
  702. {
  703. s->m_startFreqModel.setValue( 0.2f+frnd(0.3f) );
  704. s->m_slideModel.setValue( 0.05f+frnd(0.2f) );
  705. if(rnd(1))
  706. {
  707. s->m_vibDepthModel.setValue( frnd(0.7f) );
  708. s->m_vibSpeedModel.setValue( frnd(0.6f) );
  709. }
  710. }
  711. s->m_attModel.setValue( 0.0f );
  712. s->m_holdModel.setValue( frnd(0.4f) );
  713. s->m_decModel.setValue( 0.1f+frnd(0.4f) );
  714. }
  715. void sfxrInstrumentView::genHit()
  716. {
  717. sfxrInstrument * s = castModel<sfxrInstrument>();
  718. s->resetModels();
  719. s->m_waveFormModel.setValue( rnd(2) );
  720. if(s->m_waveFormModel.value()==2)
  721. {
  722. s->m_waveFormModel.setValue( 3 );
  723. }
  724. if(s->m_waveFormModel.value()==0)
  725. {
  726. s->m_sqrDutyModel.setValue( frnd(0.6f) );
  727. }
  728. s->m_startFreqModel.setValue( 0.2f+frnd(0.6f) );
  729. s->m_slideModel.setValue( -0.3f-frnd(0.4f) );
  730. s->m_attModel.setValue( 0.0f );
  731. s->m_holdModel.setValue( frnd(0.1f) );
  732. s->m_decModel.setValue( 0.1f+frnd(0.2f) );
  733. if(rnd(1))
  734. {
  735. s->m_hpFilCutModel.setValue( frnd(0.3f) );
  736. }
  737. }
  738. void sfxrInstrumentView::genJump()
  739. {
  740. sfxrInstrument * s = castModel<sfxrInstrument>();
  741. s->resetModels();
  742. s->m_waveFormModel.setValue( 0 );
  743. s->m_sqrDutyModel.setValue( frnd(0.6f) );
  744. s->m_startFreqModel.setValue( 0.3f+frnd(0.3f) );
  745. s->m_slideModel.setValue( 0.1f+frnd(0.2f) );
  746. s->m_attModel.setValue( 0.0f );
  747. s->m_holdModel.setValue( 0.1f+frnd(0.3f) );
  748. s->m_decModel.setValue( 0.1f+frnd(0.2f) );
  749. if(rnd(1))
  750. {
  751. s->m_hpFilCutModel.setValue( frnd(0.3f) );
  752. }
  753. if(rnd(1))
  754. {
  755. s->m_lpFilCutModel.setValue( 1.0f-frnd(0.6f) );
  756. }
  757. }
  758. void sfxrInstrumentView::genBlip()
  759. {
  760. sfxrInstrument * s = castModel<sfxrInstrument>();
  761. s->resetModels();
  762. s->m_waveFormModel.setValue( rnd(1) );
  763. if( s->m_waveFormModel.value()==0 )
  764. {
  765. s->m_sqrDutyModel.setValue( frnd(0.6f) );
  766. }
  767. s->m_startFreqModel.setValue( 0.2f+frnd(0.4f) );
  768. s->m_attModel.setValue( 0.0f );
  769. s->m_holdModel.setValue( 0.1f+frnd(0.1f) );
  770. s->m_decModel.setValue( frnd(0.2f) );
  771. s->m_hpFilCutModel.setValue( 0.1f );
  772. }
  773. void sfxrInstrumentView::randomize()
  774. {
  775. sfxrInstrument * s = castModel<sfxrInstrument>();
  776. s->m_startFreqModel.setValue( pow(frnd(2.0f)-1.0f, 2.0f) );
  777. if(rnd(1))
  778. {
  779. s->m_startFreqModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f)+0.5f );
  780. }
  781. s->m_minFreqModel.setValue( 0.0f );
  782. s->m_slideModel.setValue( pow(frnd(2.0f)-1.0f, 5.0f) );
  783. if( s->m_startFreqModel.value()>0.7f && s->m_slideModel.value()>0.2f )
  784. {
  785. s->m_slideModel.setValue( -s->m_slideModel.value() );
  786. }
  787. if( s->m_startFreqModel.value()<0.2f && s->m_slideModel.value()<-0.05f )
  788. {
  789. s->m_slideModel.setValue( -s->m_slideModel.value() );
  790. }
  791. s->m_dSlideModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  792. s->m_sqrDutyModel.setValue( frnd(2.0f)-1.0f );
  793. s->m_sqrSweepModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  794. s->m_vibDepthModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  795. s->m_vibSpeedModel.setValue( frnd(2.0f)-1.0f );
  796. //s->m_vibDelayModel.setValue( frnd(2.0f)-1.0f );
  797. s->m_attModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  798. s->m_holdModel.setValue( pow(frnd(2.0f)-1.0f, 2.0f) );
  799. s->m_decModel.setValue( frnd(2.0f)-1.0f );
  800. s->m_susModel.setValue( pow(frnd(0.8f), 2.0f) );
  801. if(s->m_attModel.value()+s->m_holdModel.value()+s->m_decModel.value()<0.2f)
  802. {
  803. s->m_holdModel.setValue( s->m_holdModel.value()+0.2f+frnd(0.3f) );
  804. s->m_decModel.setValue( s->m_decModel.value()+0.2f+frnd(0.3f) );
  805. }
  806. s->m_lpFilResoModel.setValue( frnd(2.0f)-1.0f );
  807. s->m_lpFilCutModel.setValue( 1.0f-pow(frnd(1.0f), 3.0f) );
  808. s->m_lpFilCutSweepModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  809. if(s->m_lpFilCutModel.value()<0.1f && s->m_lpFilCutSweepModel.value()<-0.05f)
  810. {
  811. s->m_lpFilCutSweepModel.setValue( -s->m_lpFilCutSweepModel.value() );
  812. }
  813. s->m_hpFilCutModel.setValue( pow(frnd(1.0f), 5.0f) );
  814. s->m_hpFilCutSweepModel.setValue( pow(frnd(2.0f)-1.0f, 5.0f) );
  815. s->m_phaserOffsetModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  816. s->m_phaserSweepModel.setValue( pow(frnd(2.0f)-1.0f, 3.0f) );
  817. s->m_repeatSpeedModel.setValue( frnd(2.0f)-1.0f );
  818. s->m_changeSpeedModel.setValue( frnd(2.0f)-1.0f );
  819. s->m_changeAmtModel.setValue( frnd(2.0f)-1.0f );
  820. }
  821. void sfxrInstrumentView::mutate()
  822. {
  823. sfxrInstrument * s = castModel<sfxrInstrument>();
  824. if(rnd(1)) s->m_startFreqModel.setValue( s->m_startFreqModel.value()+frnd(0.1f)-0.05f );
  825. // if(rnd(1)) s->m_minFreqModel.setValue( s->m_minFreqModel.value()+frnd(0.1f)-0.05f );
  826. if(rnd(1)) s->m_slideModel.setValue( s->m_slideModel.value()+frnd(0.1f)-0.05f );
  827. if(rnd(1)) s->m_dSlideModel.setValue( s->m_dSlideModel.value()+frnd(0.1f)-0.05f );
  828. if(rnd(1)) s->m_sqrDutyModel.setValue( s->m_sqrDutyModel.value()+frnd(0.1f)-0.05f );
  829. if(rnd(1)) s->m_sqrSweepModel.setValue( s->m_sqrSweepModel.value()+frnd(0.1f)-0.05f );
  830. if(rnd(1)) s->m_vibDepthModel.setValue( s->m_vibDepthModel.value()+frnd(0.1f)-0.05f );
  831. if(rnd(1)) s->m_vibSpeedModel.setValue( s->m_vibSpeedModel.value()+frnd(0.1f)-0.05f );
  832. // if(rnd(1)) s->m_vibDelayModel.setValue( s->m_vibDelayModel.value()+frnd(0.1f)-0.05f );
  833. if(rnd(1)) s->m_attModel.setValue( s->m_attModel.value()+frnd(0.1f)-0.05f );
  834. if(rnd(1)) s->m_holdModel.setValue( s->m_holdModel.value()+frnd(0.1f)-0.05f );
  835. if(rnd(1)) s->m_decModel.setValue( s->m_decModel.value()+frnd(0.1f)-0.05f );
  836. if(rnd(1)) s->m_susModel.setValue( s->m_susModel.value()+frnd(0.1f)-0.05f );
  837. if(rnd(1)) s->m_lpFilResoModel.setValue( s->m_lpFilResoModel.value()+frnd(0.1f)-0.05f );
  838. if(rnd(1)) s->m_lpFilCutModel.setValue( s->m_lpFilCutModel.value()+frnd(0.1f)-0.05f );
  839. if(rnd(1)) s->m_lpFilCutSweepModel.setValue( s->m_lpFilCutSweepModel.value()+frnd(0.1f)-0.05f );
  840. if(rnd(1)) s->m_hpFilCutModel.setValue( s->m_hpFilCutModel.value()+frnd(0.1f)-0.05f );
  841. if(rnd(1)) s->m_hpFilCutSweepModel.setValue( s->m_hpFilCutSweepModel.value()+frnd(0.1f)-0.05f );
  842. if(rnd(1)) s->m_phaserOffsetModel.setValue( s->m_phaserOffsetModel.value()+frnd(0.1f)-0.05f );
  843. if(rnd(1)) s->m_phaserSweepModel.setValue( s->m_phaserSweepModel.value()+frnd(0.1f)-0.05f );
  844. if(rnd(1)) s->m_repeatSpeedModel.setValue( s->m_repeatSpeedModel.value()+frnd(0.1f)-0.05f );
  845. if(rnd(1)) s->m_changeSpeedModel.setValue( s->m_changeSpeedModel.value()+frnd(0.1f)-0.05f );
  846. if(rnd(1)) s->m_changeAmtModel.setValue( s->m_changeAmtModel.value()+frnd(0.1f)-0.05f );
  847. }
  848. void sfxrInstrumentView::previewSound()
  849. {
  850. sfxrInstrument* s = castModel<sfxrInstrument>();
  851. InstrumentTrack* it = s->instrumentTrack();
  852. it->silenceAllNotes();
  853. it->processInEvent( MidiEvent( MidiNoteOn, 0, it->baseNoteModel()->value(), MidiDefaultVelocity ) );
  854. }
  855. extern "C"
  856. {
  857. // necessary for getting instance out of shared lib
  858. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* m, void* )
  859. {
  860. return new sfxrInstrument( static_cast<InstrumentTrack *>( m ) );
  861. }
  862. }