kicker.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. /*
  2. * kicker.cpp - drum synthesizer
  3. *
  4. * Copyright (c) 2006-2009 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. * Copyright (c) 2014 grejppi <grejppi/at/gmail.com>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #include <QDomDocument>
  26. #include <QPainter>
  27. #include "kicker.h"
  28. #include "Engine.h"
  29. #include "InstrumentTrack.h"
  30. #include "Knob.h"
  31. #include "Mixer.h"
  32. #include "NotePlayHandle.h"
  33. #include "KickerOsc.h"
  34. #include "embed.cpp"
  35. extern "C"
  36. {
  37. Plugin::Descriptor PLUGIN_EXPORT kicker_plugin_descriptor =
  38. {
  39. STRINGIFY( PLUGIN_NAME ),
  40. "Kicker",
  41. QT_TRANSLATE_NOOP( "pluginBrowser",
  42. "Versatile drum synthesizer" ),
  43. "Tobias Doerffel <tobydox/at/users.sf.net>",
  44. 0x0100,
  45. Plugin::Instrument,
  46. new PluginPixmapLoader( "logo" ),
  47. NULL,
  48. NULL
  49. } ;
  50. }
  51. kickerInstrument::kickerInstrument( InstrumentTrack * _instrument_track ) :
  52. Instrument( _instrument_track, &kicker_plugin_descriptor ),
  53. m_startFreqModel( 150.0f, 5.0f, 1000.0f, 1.0f, this, tr( "Start frequency" ) ),
  54. m_endFreqModel( 40.0f, 5.0f, 1000.0f, 1.0f, this, tr( "End frequency" ) ),
  55. m_decayModel( 440.0f, 5.0f, 5000.0f, 1.0f, 5000.0f, this, tr( "Length" ) ),
  56. m_distModel( 0.8f, 0.0f, 100.0f, 0.1f, this, tr( "Distortion Start" ) ),
  57. m_distEndModel( 0.8f, 0.0f, 100.0f, 0.1f, this, tr( "Distortion End" ) ),
  58. m_gainModel( 1.0f, 0.1f, 5.0f, 0.05f, this, tr( "Gain" ) ),
  59. m_envModel( 0.163f, 0.01f, 1.0f, 0.001f, this, tr( "Envelope Slope" ) ),
  60. m_noiseModel( 0.0f, 0.0f, 1.0f, 0.01f, this, tr( "Noise" ) ),
  61. m_clickModel( 0.4f, 0.0f, 1.0f, 0.05f, this, tr( "Click" ) ),
  62. m_slopeModel( 0.06f, 0.001f, 1.0f, 0.001f, this, tr( "Frequency Slope" ) ),
  63. m_startNoteModel( true, this, tr( "Start from note" ) ),
  64. m_endNoteModel( false, this, tr( "End to note" ) ),
  65. m_versionModel( KICKER_PRESET_VERSION, 0, KICKER_PRESET_VERSION, this, "" )
  66. {
  67. }
  68. kickerInstrument::~kickerInstrument()
  69. {
  70. }
  71. void kickerInstrument::saveSettings( QDomDocument & _doc,
  72. QDomElement & _this )
  73. {
  74. m_startFreqModel.saveSettings( _doc, _this, "startfreq" );
  75. m_endFreqModel.saveSettings( _doc, _this, "endfreq" );
  76. m_decayModel.saveSettings( _doc, _this, "decay" );
  77. m_distModel.saveSettings( _doc, _this, "dist" );
  78. m_distEndModel.saveSettings( _doc, _this, "distend" );
  79. m_gainModel.saveSettings( _doc, _this, "gain" );
  80. m_envModel.saveSettings( _doc, _this, "env" );
  81. m_noiseModel.saveSettings( _doc, _this, "noise" );
  82. m_clickModel.saveSettings( _doc, _this, "click" );
  83. m_slopeModel.saveSettings( _doc, _this, "slope" );
  84. m_startNoteModel.saveSettings( _doc, _this, "startnote" );
  85. m_endNoteModel.saveSettings( _doc, _this, "endnote" );
  86. m_versionModel.saveSettings( _doc, _this, "version" );
  87. }
  88. void kickerInstrument::loadSettings( const QDomElement & _this )
  89. {
  90. m_versionModel.loadSettings( _this, "version" );
  91. m_startFreqModel.loadSettings( _this, "startfreq" );
  92. m_endFreqModel.loadSettings( _this, "endfreq" );
  93. m_decayModel.loadSettings( _this, "decay" );
  94. m_distModel.loadSettings( _this, "dist" );
  95. if( _this.hasAttribute( "distend" ) )
  96. {
  97. m_distEndModel.loadSettings( _this, "distend" );
  98. }
  99. else
  100. {
  101. m_distEndModel.setValue( m_distModel.value() );
  102. }
  103. m_gainModel.loadSettings( _this, "gain" );
  104. m_envModel.loadSettings( _this, "env" );
  105. m_noiseModel.loadSettings( _this, "noise" );
  106. m_clickModel.loadSettings( _this, "click" );
  107. m_slopeModel.loadSettings( _this, "slope" );
  108. m_startNoteModel.loadSettings( _this, "startnote" );
  109. if( m_versionModel.value() < 1 )
  110. {
  111. m_startNoteModel.setValue( false );
  112. }
  113. m_endNoteModel.loadSettings( _this, "endnote" );
  114. // Try to maintain backwards compatibility
  115. if( !_this.hasAttribute( "version" ) )
  116. {
  117. m_startNoteModel.setValue( false );
  118. m_decayModel.setValue( m_decayModel.value() * 1.33f );
  119. m_envModel.setValue( 1.0f );
  120. m_slopeModel.setValue( 1.0f );
  121. m_clickModel.setValue( 0.0f );
  122. }
  123. m_versionModel.setValue( KICKER_PRESET_VERSION );
  124. }
  125. QString kickerInstrument::nodeName() const
  126. {
  127. return kicker_plugin_descriptor.name;
  128. }
  129. typedef DspEffectLibrary::Distortion DistFX;
  130. typedef KickerOsc<DspEffectLibrary::MonoToStereoAdaptor<DistFX> > SweepOsc;
  131. void kickerInstrument::playNote( NotePlayHandle * _n,
  132. sampleFrame * _working_buffer )
  133. {
  134. const fpp_t frames = _n->framesLeftForCurrentPeriod();
  135. const f_cnt_t offset = _n->noteOffset();
  136. const float decfr = m_decayModel.value() *
  137. Engine::mixer()->processingSampleRate() / 1000.0f;
  138. const f_cnt_t tfp = _n->totalFramesPlayed();
  139. if ( tfp == 0 )
  140. {
  141. _n->m_pluginData = new SweepOsc(
  142. DistFX( m_distModel.value(),
  143. m_gainModel.value() ),
  144. m_startNoteModel.value() ? _n->frequency() : m_startFreqModel.value(),
  145. m_endNoteModel.value() ? _n->frequency() : m_endFreqModel.value(),
  146. m_noiseModel.value() * m_noiseModel.value(),
  147. m_clickModel.value() * 0.25f,
  148. m_slopeModel.value(),
  149. m_envModel.value(),
  150. m_distModel.value(),
  151. m_distEndModel.value(),
  152. decfr );
  153. }
  154. else if( tfp > decfr && !_n->isReleased() )
  155. {
  156. _n->noteOff();
  157. }
  158. SweepOsc * so = static_cast<SweepOsc *>( _n->m_pluginData );
  159. so->update( _working_buffer + offset, frames, Engine::mixer()->processingSampleRate() );
  160. if( _n->isReleased() )
  161. {
  162. const float done = _n->releaseFramesDone();
  163. const float desired = desiredReleaseFrames();
  164. for( fpp_t f = 0; f < frames; ++f )
  165. {
  166. const float fac = ( done+f < desired ) ? ( 1.0f - ( ( done+f ) / desired ) ) : 0;
  167. _working_buffer[f+offset][0] *= fac;
  168. _working_buffer[f+offset][1] *= fac;
  169. }
  170. }
  171. instrumentTrack()->processAudioBuffer( _working_buffer, frames + offset, _n );
  172. }
  173. void kickerInstrument::deleteNotePluginData( NotePlayHandle * _n )
  174. {
  175. delete static_cast<SweepOsc *>( _n->m_pluginData );
  176. }
  177. PluginView * kickerInstrument::instantiateView( QWidget * _parent )
  178. {
  179. return new kickerInstrumentView( this, _parent );
  180. }
  181. class kickerKnob : public Knob
  182. {
  183. public:
  184. kickerKnob( QWidget * _parent ) :
  185. Knob( knobStyled, _parent )
  186. {
  187. setFixedSize( 29, 29 );
  188. setObjectName( "smallKnob" );
  189. }
  190. };
  191. class kickerEnvKnob : public TempoSyncKnob
  192. {
  193. public:
  194. kickerEnvKnob( QWidget * _parent ) :
  195. TempoSyncKnob( knobStyled, _parent )
  196. {
  197. setFixedSize( 29, 29 );
  198. setObjectName( "smallKnob" );
  199. }
  200. };
  201. class kickerLargeKnob : public Knob
  202. {
  203. public:
  204. kickerLargeKnob( QWidget * _parent ) :
  205. Knob( knobStyled, _parent )
  206. {
  207. setFixedSize( 34, 34 );
  208. setObjectName( "largeKnob" );
  209. }
  210. };
  211. kickerInstrumentView::kickerInstrumentView( Instrument * _instrument,
  212. QWidget * _parent ) :
  213. InstrumentView( _instrument, _parent )
  214. {
  215. const int ROW1 = 14;
  216. const int ROW2 = ROW1 + 56;
  217. const int ROW3 = ROW2 + 56;
  218. const int LED_ROW = 63;
  219. const int COL1 = 14;
  220. const int COL2 = COL1 + 56;
  221. const int COL3 = COL2 + 56;
  222. const int COL4 = COL3 + 41;
  223. const int COL5 = COL4 + 41;
  224. const int END_COL = COL1 + 48;
  225. m_startFreqKnob = new kickerLargeKnob( this );
  226. m_startFreqKnob->setHintText( tr( "Start frequency:" ), "Hz" );
  227. m_startFreqKnob->move( COL1, ROW1 );
  228. m_endFreqKnob = new kickerLargeKnob( this );
  229. m_endFreqKnob->setHintText( tr( "End frequency:" ), "Hz" );
  230. m_endFreqKnob->move( END_COL, ROW1 );
  231. m_slopeKnob = new kickerKnob( this );
  232. m_slopeKnob->setHintText( tr( "Frequency Slope:" ), "" );
  233. m_slopeKnob->move( COL3, ROW1 );
  234. m_gainKnob = new kickerKnob( this );
  235. m_gainKnob->setHintText( tr( "Gain:" ), "" );
  236. m_gainKnob->move( COL1, ROW3 );
  237. m_decayKnob = new kickerEnvKnob( this );
  238. m_decayKnob->setHintText( tr( "Envelope Length:" ), "ms" );
  239. m_decayKnob->move( COL2, ROW3 );
  240. m_envKnob = new kickerKnob( this );
  241. m_envKnob->setHintText( tr( "Envelope Slope:" ), "" );
  242. m_envKnob->move( COL3, ROW3 );
  243. m_clickKnob = new kickerKnob( this );
  244. m_clickKnob->setHintText( tr( "Click:" ), "" );
  245. m_clickKnob->move( COL5, ROW1 );
  246. m_noiseKnob = new kickerKnob( this );
  247. m_noiseKnob->setHintText( tr( "Noise:" ), "" );
  248. m_noiseKnob->move( COL5, ROW3 );
  249. m_distKnob = new kickerKnob( this );
  250. m_distKnob->setHintText( tr( "Distortion Start:" ), "" );
  251. m_distKnob->move( COL4, ROW2 );
  252. m_distEndKnob = new kickerKnob( this );
  253. m_distEndKnob->setHintText( tr( "Distortion End:" ), "" );
  254. m_distEndKnob->move( COL5, ROW2 );
  255. m_startNoteToggle = new LedCheckBox( "", this, "", LedCheckBox::Green );
  256. m_startNoteToggle->move( COL1 + 8, LED_ROW );
  257. m_endNoteToggle = new LedCheckBox( "", this, "", LedCheckBox::Green );
  258. m_endNoteToggle->move( END_COL + 8, LED_ROW );
  259. setAutoFillBackground( true );
  260. QPalette pal;
  261. pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap( "artwork" ) );
  262. setPalette( pal );
  263. }
  264. kickerInstrumentView::~kickerInstrumentView()
  265. {
  266. }
  267. void kickerInstrumentView::modelChanged()
  268. {
  269. kickerInstrument * k = castModel<kickerInstrument>();
  270. m_startFreqKnob->setModel( &k->m_startFreqModel );
  271. m_endFreqKnob->setModel( &k->m_endFreqModel );
  272. m_decayKnob->setModel( &k->m_decayModel );
  273. m_distKnob->setModel( &k->m_distModel );
  274. m_distEndKnob->setModel( &k->m_distEndModel );
  275. m_gainKnob->setModel( &k->m_gainModel );
  276. m_envKnob->setModel( &k->m_envModel );
  277. m_noiseKnob->setModel( &k->m_noiseModel );
  278. m_clickKnob->setModel( &k->m_clickModel );
  279. m_slopeKnob->setModel( &k->m_slopeModel );
  280. m_startNoteToggle->setModel( &k->m_startNoteModel );
  281. m_endNoteToggle->setModel( &k->m_endNoteModel );
  282. }
  283. extern "C"
  284. {
  285. // necessary for getting instance out of shared lib
  286. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model *, void * _data )
  287. {
  288. return new kickerInstrument( static_cast<InstrumentTrack *>( _data ) );
  289. }
  290. }