kicker.cpp 10.0 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.h"
  35. #include "plugin_export.h"
  36. extern "C"
  37. {
  38. Plugin::Descriptor PLUGIN_EXPORT kicker_plugin_descriptor =
  39. {
  40. STRINGIFY( PLUGIN_NAME ),
  41. "Kicker",
  42. QT_TRANSLATE_NOOP( "pluginBrowser",
  43. "Versatile drum synthesizer" ),
  44. "Tobias Doerffel <tobydox/at/users.sf.net>",
  45. 0x0100,
  46. Plugin::Instrument,
  47. new PluginPixmapLoader( "logo" ),
  48. NULL,
  49. NULL
  50. } ;
  51. }
  52. kickerInstrument::kickerInstrument( InstrumentTrack * _instrument_track ) :
  53. Instrument( _instrument_track, &kicker_plugin_descriptor ),
  54. m_startFreqModel( 150.0f, 5.0f, 1000.0f, 1.0f, this, tr( "Start frequency" ) ),
  55. m_endFreqModel( 40.0f, 5.0f, 1000.0f, 1.0f, this, tr( "End frequency" ) ),
  56. m_decayModel( 440.0f, 5.0f, 5000.0f, 1.0f, 5000.0f, this, tr( "Length" ) ),
  57. m_distModel( 0.8f, 0.0f, 100.0f, 0.1f, this, tr( "Start distortion" ) ),
  58. m_distEndModel( 0.8f, 0.0f, 100.0f, 0.1f, this, tr( "End distortion" ) ),
  59. m_gainModel( 1.0f, 0.1f, 5.0f, 0.05f, this, tr( "Gain" ) ),
  60. m_envModel( 0.163f, 0.01f, 1.0f, 0.001f, this, tr( "Envelope slope" ) ),
  61. m_noiseModel( 0.0f, 0.0f, 1.0f, 0.01f, this, tr( "Noise" ) ),
  62. m_clickModel( 0.4f, 0.0f, 1.0f, 0.05f, this, tr( "Click" ) ),
  63. m_slopeModel( 0.06f, 0.001f, 1.0f, 0.001f, this, tr( "Frequency slope" ) ),
  64. m_startNoteModel( true, this, tr( "Start from note" ) ),
  65. m_endNoteModel( false, this, tr( "End to note" ) ),
  66. m_versionModel( KICKER_PRESET_VERSION, 0, KICKER_PRESET_VERSION, this, "" )
  67. {
  68. }
  69. kickerInstrument::~kickerInstrument()
  70. {
  71. }
  72. void kickerInstrument::saveSettings( QDomDocument & _doc,
  73. QDomElement & _this )
  74. {
  75. m_startFreqModel.saveSettings( _doc, _this, "startfreq" );
  76. m_endFreqModel.saveSettings( _doc, _this, "endfreq" );
  77. m_decayModel.saveSettings( _doc, _this, "decay" );
  78. m_distModel.saveSettings( _doc, _this, "dist" );
  79. m_distEndModel.saveSettings( _doc, _this, "distend" );
  80. m_gainModel.saveSettings( _doc, _this, "gain" );
  81. m_envModel.saveSettings( _doc, _this, "env" );
  82. m_noiseModel.saveSettings( _doc, _this, "noise" );
  83. m_clickModel.saveSettings( _doc, _this, "click" );
  84. m_slopeModel.saveSettings( _doc, _this, "slope" );
  85. m_startNoteModel.saveSettings( _doc, _this, "startnote" );
  86. m_endNoteModel.saveSettings( _doc, _this, "endnote" );
  87. m_versionModel.saveSettings( _doc, _this, "version" );
  88. }
  89. void kickerInstrument::loadSettings( const QDomElement & _this )
  90. {
  91. m_versionModel.loadSettings( _this, "version" );
  92. m_startFreqModel.loadSettings( _this, "startfreq" );
  93. m_endFreqModel.loadSettings( _this, "endfreq" );
  94. m_decayModel.loadSettings( _this, "decay" );
  95. m_distModel.loadSettings( _this, "dist" );
  96. if( _this.hasAttribute( "distend" ) )
  97. {
  98. m_distEndModel.loadSettings( _this, "distend" );
  99. }
  100. else
  101. {
  102. m_distEndModel.setValue( m_distModel.value() );
  103. }
  104. m_gainModel.loadSettings( _this, "gain" );
  105. m_envModel.loadSettings( _this, "env" );
  106. m_noiseModel.loadSettings( _this, "noise" );
  107. m_clickModel.loadSettings( _this, "click" );
  108. m_slopeModel.loadSettings( _this, "slope" );
  109. m_startNoteModel.loadSettings( _this, "startnote" );
  110. if( m_versionModel.value() < 1 )
  111. {
  112. m_startNoteModel.setValue( false );
  113. }
  114. m_endNoteModel.loadSettings( _this, "endnote" );
  115. // Try to maintain backwards compatibility
  116. if( !_this.hasAttribute( "version" ) )
  117. {
  118. m_startNoteModel.setValue( false );
  119. m_decayModel.setValue( m_decayModel.value() * 1.33f );
  120. m_envModel.setValue( 1.0f );
  121. m_slopeModel.setValue( 1.0f );
  122. m_clickModel.setValue( 0.0f );
  123. }
  124. m_versionModel.setValue( KICKER_PRESET_VERSION );
  125. }
  126. QString kickerInstrument::nodeName() const
  127. {
  128. return kicker_plugin_descriptor.name;
  129. }
  130. typedef DspEffectLibrary::Distortion DistFX;
  131. typedef KickerOsc<DspEffectLibrary::MonoToStereoAdaptor<DistFX> > SweepOsc;
  132. void kickerInstrument::playNote( NotePlayHandle * _n,
  133. sampleFrame * _working_buffer )
  134. {
  135. const fpp_t frames = _n->framesLeftForCurrentPeriod();
  136. const f_cnt_t offset = _n->noteOffset();
  137. const float decfr = m_decayModel.value() *
  138. Engine::mixer()->processingSampleRate() / 1000.0f;
  139. const f_cnt_t tfp = _n->totalFramesPlayed();
  140. if ( tfp == 0 )
  141. {
  142. _n->m_pluginData = new SweepOsc(
  143. DistFX( m_distModel.value(),
  144. m_gainModel.value() ),
  145. m_startNoteModel.value() ? _n->frequency() : m_startFreqModel.value(),
  146. m_endNoteModel.value() ? _n->frequency() : m_endFreqModel.value(),
  147. m_noiseModel.value() * m_noiseModel.value(),
  148. m_clickModel.value() * 0.25f,
  149. m_slopeModel.value(),
  150. m_envModel.value(),
  151. m_distModel.value(),
  152. m_distEndModel.value(),
  153. decfr );
  154. }
  155. else if( tfp > decfr && !_n->isReleased() )
  156. {
  157. _n->noteOff();
  158. }
  159. SweepOsc * so = static_cast<SweepOsc *>( _n->m_pluginData );
  160. so->update( _working_buffer + offset, frames, Engine::mixer()->processingSampleRate() );
  161. if( _n->isReleased() )
  162. {
  163. const float done = _n->releaseFramesDone();
  164. const float desired = desiredReleaseFrames();
  165. for( fpp_t f = 0; f < frames; ++f )
  166. {
  167. const float fac = ( done+f < desired ) ? ( 1.0f - ( ( done+f ) / desired ) ) : 0;
  168. _working_buffer[f+offset][0] *= fac;
  169. _working_buffer[f+offset][1] *= fac;
  170. }
  171. }
  172. instrumentTrack()->processAudioBuffer( _working_buffer, frames + offset, _n );
  173. }
  174. void kickerInstrument::deleteNotePluginData( NotePlayHandle * _n )
  175. {
  176. delete static_cast<SweepOsc *>( _n->m_pluginData );
  177. }
  178. PluginView * kickerInstrument::instantiateView( QWidget * _parent )
  179. {
  180. return new kickerInstrumentView( this, _parent );
  181. }
  182. class kickerKnob : public Knob
  183. {
  184. public:
  185. kickerKnob( QWidget * _parent ) :
  186. Knob( knobStyled, _parent )
  187. {
  188. setFixedSize( 29, 29 );
  189. setObjectName( "smallKnob" );
  190. }
  191. };
  192. class kickerEnvKnob : public TempoSyncKnob
  193. {
  194. public:
  195. kickerEnvKnob( QWidget * _parent ) :
  196. TempoSyncKnob( knobStyled, _parent )
  197. {
  198. setFixedSize( 29, 29 );
  199. setObjectName( "smallKnob" );
  200. }
  201. };
  202. class kickerLargeKnob : public Knob
  203. {
  204. public:
  205. kickerLargeKnob( QWidget * _parent ) :
  206. Knob( knobStyled, _parent )
  207. {
  208. setFixedSize( 34, 34 );
  209. setObjectName( "largeKnob" );
  210. }
  211. };
  212. kickerInstrumentView::kickerInstrumentView( Instrument * _instrument,
  213. QWidget * _parent ) :
  214. InstrumentViewFixedSize( _instrument, _parent )
  215. {
  216. const int ROW1 = 14;
  217. const int ROW2 = ROW1 + 56;
  218. const int ROW3 = ROW2 + 56;
  219. const int LED_ROW = 63;
  220. const int COL1 = 14;
  221. const int COL2 = COL1 + 56;
  222. const int COL3 = COL2 + 56;
  223. const int COL4 = COL3 + 41;
  224. const int COL5 = COL4 + 41;
  225. const int END_COL = COL1 + 48;
  226. m_startFreqKnob = new kickerLargeKnob( this );
  227. m_startFreqKnob->setHintText( tr( "Start frequency:" ), "Hz" );
  228. m_startFreqKnob->move( COL1, ROW1 );
  229. m_endFreqKnob = new kickerLargeKnob( this );
  230. m_endFreqKnob->setHintText( tr( "End frequency:" ), "Hz" );
  231. m_endFreqKnob->move( END_COL, ROW1 );
  232. m_slopeKnob = new kickerKnob( this );
  233. m_slopeKnob->setHintText( tr( "Frequency slope:" ), "" );
  234. m_slopeKnob->move( COL3, ROW1 );
  235. m_gainKnob = new kickerKnob( this );
  236. m_gainKnob->setHintText( tr( "Gain:" ), "" );
  237. m_gainKnob->move( COL1, ROW3 );
  238. m_decayKnob = new kickerEnvKnob( this );
  239. m_decayKnob->setHintText( tr( "Envelope length:" ), "ms" );
  240. m_decayKnob->move( COL2, ROW3 );
  241. m_envKnob = new kickerKnob( this );
  242. m_envKnob->setHintText( tr( "Envelope slope:" ), "" );
  243. m_envKnob->move( COL3, ROW3 );
  244. m_clickKnob = new kickerKnob( this );
  245. m_clickKnob->setHintText( tr( "Click:" ), "" );
  246. m_clickKnob->move( COL5, ROW1 );
  247. m_noiseKnob = new kickerKnob( this );
  248. m_noiseKnob->setHintText( tr( "Noise:" ), "" );
  249. m_noiseKnob->move( COL5, ROW3 );
  250. m_distKnob = new kickerKnob( this );
  251. m_distKnob->setHintText( tr( "Start distortion:" ), "" );
  252. m_distKnob->move( COL4, ROW2 );
  253. m_distEndKnob = new kickerKnob( this );
  254. m_distEndKnob->setHintText( tr( "End distortion:" ), "" );
  255. m_distEndKnob->move( COL5, ROW2 );
  256. m_startNoteToggle = new LedCheckBox( "", this, "", LedCheckBox::Green );
  257. m_startNoteToggle->move( COL1 + 8, LED_ROW );
  258. m_endNoteToggle = new LedCheckBox( "", this, "", LedCheckBox::Green );
  259. m_endNoteToggle->move( END_COL + 8, LED_ROW );
  260. setAutoFillBackground( true );
  261. QPalette pal;
  262. pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap( "artwork" ) );
  263. setPalette( pal );
  264. }
  265. kickerInstrumentView::~kickerInstrumentView()
  266. {
  267. }
  268. void kickerInstrumentView::modelChanged()
  269. {
  270. kickerInstrument * k = castModel<kickerInstrument>();
  271. m_startFreqKnob->setModel( &k->m_startFreqModel );
  272. m_endFreqKnob->setModel( &k->m_endFreqModel );
  273. m_decayKnob->setModel( &k->m_decayModel );
  274. m_distKnob->setModel( &k->m_distModel );
  275. m_distEndKnob->setModel( &k->m_distEndModel );
  276. m_gainKnob->setModel( &k->m_gainModel );
  277. m_envKnob->setModel( &k->m_envModel );
  278. m_noiseKnob->setModel( &k->m_noiseModel );
  279. m_clickKnob->setModel( &k->m_clickModel );
  280. m_slopeKnob->setModel( &k->m_slopeModel );
  281. m_startNoteToggle->setModel( &k->m_startNoteModel );
  282. m_endNoteToggle->setModel( &k->m_endNoteModel );
  283. }
  284. extern "C"
  285. {
  286. // necessary for getting instance out of shared lib
  287. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model * m, void * )
  288. {
  289. return new kickerInstrument( static_cast<InstrumentTrack *>( m ) );
  290. }
  291. }