bit_invader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * bit_invader.cpp - instrument which uses a usereditable wavetable
  3. *
  4. * Copyright (c) 2006-2008 Andreas Brandmaier <andy/at/brandmaier/dot/de>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #include <QDomElement>
  25. #include "bit_invader.h"
  26. #include "base64.h"
  27. #include "Engine.h"
  28. #include "Graph.h"
  29. #include "InstrumentTrack.h"
  30. #include "Knob.h"
  31. #include "LedCheckbox.h"
  32. #include "Mixer.h"
  33. #include "NotePlayHandle.h"
  34. #include "Oscillator.h"
  35. #include "PixmapButton.h"
  36. #include "ToolTip.h"
  37. #include "Song.h"
  38. #include "interpolation.h"
  39. #include "embed.h"
  40. #include "plugin_export.h"
  41. extern "C"
  42. {
  43. Plugin::Descriptor PLUGIN_EXPORT bitinvader_plugin_descriptor =
  44. {
  45. STRINGIFY( PLUGIN_NAME ),
  46. "BitInvader",
  47. QT_TRANSLATE_NOOP( "pluginBrowser",
  48. "Customizable wavetable synthesizer" ),
  49. "Andreas Brandmaier <andreas/at/brandmaier/dot/de>",
  50. 0x0100,
  51. Plugin::Instrument,
  52. new PluginPixmapLoader( "logo" ),
  53. NULL,
  54. NULL
  55. } ;
  56. }
  57. bSynth::bSynth( float * _shape, NotePlayHandle * _nph, bool _interpolation,
  58. float _factor, const sample_rate_t _sample_rate ) :
  59. sample_index( 0 ),
  60. sample_realindex( 0 ),
  61. nph( _nph ),
  62. sample_rate( _sample_rate ),
  63. interpolation( _interpolation)
  64. {
  65. sample_shape = new float[200];
  66. for (int i=0; i < 200; ++i)
  67. {
  68. sample_shape[i] = _shape[i] * _factor;
  69. }
  70. }
  71. bSynth::~bSynth()
  72. {
  73. delete[] sample_shape;
  74. }
  75. sample_t bSynth::nextStringSample( float sample_length )
  76. {
  77. float sample_step =
  78. static_cast<float>( sample_length / ( sample_rate / nph->frequency() ) );
  79. // check overflow
  80. while (sample_realindex >= sample_length) {
  81. sample_realindex -= sample_length;
  82. }
  83. sample_t sample;
  84. if (interpolation) {
  85. // find position in shape
  86. int a = static_cast<int>(sample_realindex);
  87. int b;
  88. if (a < (sample_length-1)) {
  89. b = static_cast<int>(sample_realindex+1);
  90. } else {
  91. b = 0;
  92. }
  93. // Nachkommaanteil
  94. const float frac = fraction( sample_realindex );
  95. sample = linearInterpolate( sample_shape[a], sample_shape[b], frac );
  96. } else {
  97. // No interpolation
  98. sample_index = static_cast<int>(sample_realindex);
  99. sample = sample_shape[sample_index];
  100. }
  101. // progress in shape
  102. sample_realindex += sample_step;
  103. return sample;
  104. }
  105. /***********************************************************************
  106. *
  107. * class BitInvader
  108. *
  109. * lmms - plugin
  110. *
  111. ***********************************************************************/
  112. bitInvader::bitInvader( InstrumentTrack * _instrument_track ) :
  113. Instrument( _instrument_track, &bitinvader_plugin_descriptor ),
  114. m_sampleLength( 128, 4, 200, 1, this, tr( "Sample length" ) ),
  115. m_graph( -1.0f, 1.0f, 200, this ),
  116. m_interpolation( false, this ),
  117. m_normalize( false, this )
  118. {
  119. lengthChanged();
  120. m_graph.setWaveToSine();
  121. connect( &m_sampleLength, SIGNAL( dataChanged( ) ),
  122. this, SLOT( lengthChanged( ) ) );
  123. connect( &m_graph, SIGNAL( samplesChanged( int, int ) ),
  124. this, SLOT( samplesChanged( int, int ) ) );
  125. }
  126. bitInvader::~bitInvader()
  127. {
  128. }
  129. void bitInvader::saveSettings( QDomDocument & _doc, QDomElement & _this )
  130. {
  131. // Save plugin version
  132. _this.setAttribute( "version", "0.1" );
  133. // Save sample length
  134. m_sampleLength.saveSettings( _doc, _this, "sampleLength" );
  135. // Save sample shape base64-encoded
  136. QString sampleString;
  137. base64::encode( (const char *)m_graph.samples(),
  138. m_graph.length() * sizeof(float), sampleString );
  139. _this.setAttribute( "sampleShape", sampleString );
  140. // save LED normalize
  141. m_interpolation.saveSettings( _doc, _this, "interpolation" );
  142. // save LED
  143. m_normalize.saveSettings( _doc, _this, "normalize" );
  144. }
  145. void bitInvader::loadSettings( const QDomElement & _this )
  146. {
  147. // Load sample length
  148. m_sampleLength.loadSettings( _this, "sampleLength" );
  149. int sampleLength = (int)m_sampleLength.value();
  150. // Load sample shape
  151. int size = 0;
  152. char * dst = 0;
  153. base64::decode( _this.attribute( "sampleShape"), &dst, &size );
  154. m_graph.setLength( sampleLength );
  155. m_graph.setSamples( (float*) dst );
  156. delete[] dst;
  157. // Load LED normalize
  158. m_interpolation.loadSettings( _this, "interpolation" );
  159. // Load LED
  160. m_normalize.loadSettings( _this, "normalize" );
  161. }
  162. void bitInvader::lengthChanged()
  163. {
  164. m_graph.setLength( (int) m_sampleLength.value() );
  165. normalize();
  166. }
  167. void bitInvader::samplesChanged( int _begin, int _end )
  168. {
  169. normalize();
  170. //engine::getSongEditor()->setModified();
  171. }
  172. void bitInvader::normalize()
  173. {
  174. // analyze
  175. float max = 0;
  176. const float* samples = m_graph.samples();
  177. for(int i=0; i < m_graph.length(); i++)
  178. {
  179. const float f = fabsf( samples[i] );
  180. if (f > max) { max = f; }
  181. }
  182. m_normalizeFactor = 1.0 / max;
  183. }
  184. QString bitInvader::nodeName() const
  185. {
  186. return( bitinvader_plugin_descriptor.name );
  187. }
  188. void bitInvader::playNote( NotePlayHandle * _n,
  189. sampleFrame * _working_buffer )
  190. {
  191. if ( _n->totalFramesPlayed() == 0 || _n->m_pluginData == NULL )
  192. {
  193. float factor;
  194. if( !m_normalize.value() )
  195. {
  196. factor = 1.0f;
  197. }
  198. else
  199. {
  200. factor = m_normalizeFactor;
  201. }
  202. _n->m_pluginData = new bSynth(
  203. const_cast<float*>( m_graph.samples() ),
  204. _n,
  205. m_interpolation.value(), factor,
  206. Engine::mixer()->processingSampleRate() );
  207. }
  208. const fpp_t frames = _n->framesLeftForCurrentPeriod();
  209. const f_cnt_t offset = _n->noteOffset();
  210. bSynth * ps = static_cast<bSynth *>( _n->m_pluginData );
  211. for( fpp_t frame = offset; frame < frames + offset; ++frame )
  212. {
  213. const sample_t cur = ps->nextStringSample( m_graph.length() );
  214. for( ch_cnt_t chnl = 0; chnl < DEFAULT_CHANNELS; ++chnl )
  215. {
  216. _working_buffer[frame][chnl] = cur;
  217. }
  218. }
  219. applyRelease( _working_buffer, _n );
  220. instrumentTrack()->processAudioBuffer( _working_buffer, frames + offset, _n );
  221. }
  222. void bitInvader::deleteNotePluginData( NotePlayHandle * _n )
  223. {
  224. delete static_cast<bSynth *>( _n->m_pluginData );
  225. }
  226. PluginView * bitInvader::instantiateView( QWidget * _parent )
  227. {
  228. return( new bitInvaderView( this, _parent ) );
  229. }
  230. bitInvaderView::bitInvaderView( Instrument * _instrument,
  231. QWidget * _parent ) :
  232. InstrumentViewFixedSize( _instrument, _parent )
  233. {
  234. setAutoFillBackground( true );
  235. QPalette pal;
  236. pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap(
  237. "artwork" ) );
  238. setPalette( pal );
  239. m_sampleLengthKnob = new Knob( knobDark_28, this );
  240. m_sampleLengthKnob->move( 6, 201 );
  241. m_sampleLengthKnob->setHintText( tr( "Sample length" ), "" );
  242. m_graph = new Graph( this, Graph::NearestStyle, 204, 134 );
  243. m_graph->move(23,59); // 55,120 - 2px border
  244. m_graph->setAutoFillBackground( true );
  245. m_graph->setGraphColor( QColor( 255, 255, 255 ) );
  246. ToolTip::add( m_graph, tr ( "Draw your own waveform here "
  247. "by dragging your mouse on this graph."
  248. ));
  249. pal = QPalette();
  250. pal.setBrush( backgroundRole(),
  251. PLUGIN_NAME::getIconPixmap("wavegraph") );
  252. m_graph->setPalette( pal );
  253. m_sinWaveBtn = new PixmapButton( this, tr( "Sine wave" ) );
  254. m_sinWaveBtn->move( 131, 205 );
  255. m_sinWaveBtn->setActiveGraphic( embed::getIconPixmap(
  256. "sin_wave_active" ) );
  257. m_sinWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  258. "sin_wave_inactive" ) );
  259. ToolTip::add( m_sinWaveBtn,
  260. tr( "Sine wave" ) );
  261. m_triangleWaveBtn = new PixmapButton( this, tr( "Triangle wave" ) );
  262. m_triangleWaveBtn->move( 131 + 14, 205 );
  263. m_triangleWaveBtn->setActiveGraphic(
  264. embed::getIconPixmap( "triangle_wave_active" ) );
  265. m_triangleWaveBtn->setInactiveGraphic(
  266. embed::getIconPixmap( "triangle_wave_inactive" ) );
  267. ToolTip::add( m_triangleWaveBtn,
  268. tr( "Triangle wave" ) );
  269. m_sawWaveBtn = new PixmapButton( this, tr( "Saw wave" ) );
  270. m_sawWaveBtn->move( 131 + 14*2, 205 );
  271. m_sawWaveBtn->setActiveGraphic( embed::getIconPixmap(
  272. "saw_wave_active" ) );
  273. m_sawWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  274. "saw_wave_inactive" ) );
  275. ToolTip::add( m_sawWaveBtn,
  276. tr( "Saw wave" ) );
  277. m_sqrWaveBtn = new PixmapButton( this, tr( "Square wave" ) );
  278. m_sqrWaveBtn->move( 131 + 14*3, 205 );
  279. m_sqrWaveBtn->setActiveGraphic( embed::getIconPixmap(
  280. "square_wave_active" ) );
  281. m_sqrWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  282. "square_wave_inactive" ) );
  283. ToolTip::add( m_sqrWaveBtn,
  284. tr( "Square wave" ) );
  285. m_whiteNoiseWaveBtn = new PixmapButton( this,
  286. tr( "White noise" ) );
  287. m_whiteNoiseWaveBtn->move( 131 + 14*4, 205 );
  288. m_whiteNoiseWaveBtn->setActiveGraphic(
  289. embed::getIconPixmap( "white_noise_wave_active" ) );
  290. m_whiteNoiseWaveBtn->setInactiveGraphic(
  291. embed::getIconPixmap( "white_noise_wave_inactive" ) );
  292. ToolTip::add( m_whiteNoiseWaveBtn,
  293. tr( "White noise" ) );
  294. m_usrWaveBtn = new PixmapButton( this, tr( "User-defined wave" ) );
  295. m_usrWaveBtn->move( 131 + 14*5, 205 );
  296. m_usrWaveBtn->setActiveGraphic( embed::getIconPixmap(
  297. "usr_wave_active" ) );
  298. m_usrWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  299. "usr_wave_inactive" ) );
  300. ToolTip::add( m_usrWaveBtn,
  301. tr( "User-defined wave" ) );
  302. m_smoothBtn = new PixmapButton( this, tr( "Smooth waveform" ) );
  303. m_smoothBtn->move( 131 + 14*6, 205 );
  304. m_smoothBtn->setActiveGraphic( PLUGIN_NAME::getIconPixmap(
  305. "smooth_active" ) );
  306. m_smoothBtn->setInactiveGraphic( PLUGIN_NAME::getIconPixmap(
  307. "smooth_inactive" ) );
  308. ToolTip::add( m_smoothBtn,
  309. tr( "Smooth waveform" ) );
  310. m_interpolationToggle = new LedCheckBox( "Interpolation", this,
  311. tr( "Interpolation" ), LedCheckBox::Yellow );
  312. m_interpolationToggle->move( 131, 221 );
  313. m_normalizeToggle = new LedCheckBox( "Normalize", this,
  314. tr( "Normalize" ), LedCheckBox::Green );
  315. m_normalizeToggle->move( 131, 236 );
  316. connect( m_sinWaveBtn, SIGNAL (clicked () ),
  317. this, SLOT ( sinWaveClicked() ) );
  318. connect( m_triangleWaveBtn, SIGNAL ( clicked () ),
  319. this, SLOT ( triangleWaveClicked() ) );
  320. connect( m_sawWaveBtn, SIGNAL (clicked () ),
  321. this, SLOT ( sawWaveClicked() ) );
  322. connect( m_sqrWaveBtn, SIGNAL ( clicked () ),
  323. this, SLOT ( sqrWaveClicked() ) );
  324. connect( m_whiteNoiseWaveBtn, SIGNAL ( clicked () ),
  325. this, SLOT ( noiseWaveClicked() ) );
  326. connect( m_usrWaveBtn, SIGNAL ( clicked () ),
  327. this, SLOT ( usrWaveClicked() ) );
  328. connect( m_smoothBtn, SIGNAL ( clicked () ),
  329. this, SLOT ( smoothClicked() ) );
  330. connect( m_interpolationToggle, SIGNAL( toggled( bool ) ),
  331. this, SLOT ( interpolationToggled( bool ) ) );
  332. connect( m_normalizeToggle, SIGNAL( toggled( bool ) ),
  333. this, SLOT ( normalizeToggled( bool ) ) );
  334. }
  335. void bitInvaderView::modelChanged()
  336. {
  337. bitInvader * b = castModel<bitInvader>();
  338. m_graph->setModel( &b->m_graph );
  339. m_sampleLengthKnob->setModel( &b->m_sampleLength );
  340. m_interpolationToggle->setModel( &b->m_interpolation );
  341. m_normalizeToggle->setModel( &b->m_normalize );
  342. }
  343. void bitInvaderView::sinWaveClicked()
  344. {
  345. m_graph->model()->clearInvisible();
  346. m_graph->model()->setWaveToSine();
  347. Engine::getSong()->setModified();
  348. }
  349. void bitInvaderView::triangleWaveClicked()
  350. {
  351. m_graph->model()->clearInvisible();
  352. m_graph->model()->setWaveToTriangle();
  353. Engine::getSong()->setModified();
  354. }
  355. void bitInvaderView::sawWaveClicked()
  356. {
  357. m_graph->model()->clearInvisible();
  358. m_graph->model()->setWaveToSaw();
  359. Engine::getSong()->setModified();
  360. }
  361. void bitInvaderView::sqrWaveClicked()
  362. {
  363. m_graph->model()->clearInvisible();
  364. m_graph->model()->setWaveToSquare();
  365. Engine::getSong()->setModified();
  366. }
  367. void bitInvaderView::noiseWaveClicked()
  368. {
  369. m_graph->model()->clearInvisible();
  370. m_graph->model()->setWaveToNoise();
  371. Engine::getSong()->setModified();
  372. }
  373. void bitInvaderView::usrWaveClicked()
  374. {
  375. QString fileName = m_graph->model()->setWaveToUser();
  376. if (!fileName.isEmpty())
  377. {
  378. ToolTip::add(m_usrWaveBtn, fileName);
  379. m_graph->model()->clearInvisible();
  380. Engine::getSong()->setModified();
  381. }
  382. }
  383. void bitInvaderView::smoothClicked()
  384. {
  385. m_graph->model()->smooth();
  386. Engine::getSong()->setModified();
  387. }
  388. void bitInvaderView::interpolationToggled( bool value )
  389. {
  390. m_graph->setGraphStyle( value ? Graph::LinearStyle : Graph::NearestStyle);
  391. Engine::getSong()->setModified();
  392. }
  393. void bitInvaderView::normalizeToggled( bool value )
  394. {
  395. Engine::getSong()->setModified();
  396. }
  397. extern "C"
  398. {
  399. // necessary for getting instance out of shared lib
  400. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model *m, void * )
  401. {
  402. return( new bitInvader( static_cast<InstrumentTrack *>( m ) ) );
  403. }
  404. }