bit_invader.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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. static const int wavetableSize = 200;
  42. static const float defaultNormalizationFactor = 1.0f;
  43. extern "C"
  44. {
  45. Plugin::Descriptor PLUGIN_EXPORT bitinvader_plugin_descriptor =
  46. {
  47. STRINGIFY( PLUGIN_NAME ),
  48. "BitInvader",
  49. QT_TRANSLATE_NOOP( "PluginBrowser",
  50. "Customizable wavetable synthesizer" ),
  51. "Andreas Brandmaier <andreas/at/brandmaier/dot/de>",
  52. 0x0100,
  53. Plugin::Instrument,
  54. new PluginPixmapLoader( "logo" ),
  55. NULL,
  56. NULL
  57. } ;
  58. }
  59. bSynth::bSynth( float * _shape, NotePlayHandle * _nph, bool _interpolation,
  60. float _factor, const sample_rate_t _sample_rate ) :
  61. sample_index( 0 ),
  62. sample_realindex( 0 ),
  63. nph( _nph ),
  64. sample_rate( _sample_rate ),
  65. interpolation( _interpolation)
  66. {
  67. sample_shape = new float[wavetableSize];
  68. for (int i=0; i < wavetableSize; ++i)
  69. {
  70. float buf = _shape[i] * _factor;
  71. /* Double check that normalization has been performed correctly,
  72. i.e., the absolute value of all samples is <= 1.0 if _factor
  73. is different to the default normalization factor. If there is
  74. a value > 1.0, clip the sample to 1.0 to limit the range. */
  75. if ((_factor != defaultNormalizationFactor) && (fabsf(buf) > 1.0f))
  76. {
  77. buf = (buf < 0) ? -1.0f : 1.0f;
  78. }
  79. sample_shape[i] = buf;
  80. }
  81. }
  82. bSynth::~bSynth()
  83. {
  84. delete[] sample_shape;
  85. }
  86. sample_t bSynth::nextStringSample( float sample_length )
  87. {
  88. float sample_step =
  89. static_cast<float>( sample_length / ( sample_rate / nph->frequency() ) );
  90. // check overflow
  91. while (sample_realindex >= sample_length) {
  92. sample_realindex -= sample_length;
  93. }
  94. sample_t sample;
  95. if (interpolation) {
  96. // find position in shape
  97. int a = static_cast<int>(sample_realindex);
  98. int b;
  99. if (a < (sample_length-1)) {
  100. b = static_cast<int>(sample_realindex+1);
  101. } else {
  102. b = 0;
  103. }
  104. // Nachkommaanteil
  105. const float frac = fraction( sample_realindex );
  106. sample = linearInterpolate( sample_shape[a], sample_shape[b], frac );
  107. } else {
  108. // No interpolation
  109. sample_index = static_cast<int>(sample_realindex);
  110. sample = sample_shape[sample_index];
  111. }
  112. // progress in shape
  113. sample_realindex += sample_step;
  114. return sample;
  115. }
  116. /***********************************************************************
  117. *
  118. * class BitInvader
  119. *
  120. * lmms - plugin
  121. *
  122. ***********************************************************************/
  123. bitInvader::bitInvader( InstrumentTrack * _instrument_track ) :
  124. Instrument( _instrument_track, &bitinvader_plugin_descriptor ),
  125. m_sampleLength(wavetableSize, 4, wavetableSize, 1, this, tr("Sample length")),
  126. m_graph(-1.0f, 1.0f, wavetableSize, this),
  127. m_interpolation( false, this ),
  128. m_normalize( false, this )
  129. {
  130. m_graph.setWaveToSine();
  131. lengthChanged();
  132. connect( &m_sampleLength, SIGNAL( dataChanged( ) ),
  133. this, SLOT( lengthChanged( ) ), Qt::DirectConnection );
  134. connect( &m_graph, SIGNAL( samplesChanged( int, int ) ),
  135. this, SLOT( samplesChanged( int, int ) ) );
  136. }
  137. bitInvader::~bitInvader()
  138. {
  139. }
  140. void bitInvader::saveSettings( QDomDocument & _doc, QDomElement & _this )
  141. {
  142. // Save plugin version
  143. _this.setAttribute( "version", "0.1" );
  144. // Save sample length
  145. m_sampleLength.saveSettings( _doc, _this, "sampleLength" );
  146. // Save sample shape base64-encoded
  147. QString sampleString;
  148. base64::encode((const char *)m_graph.samples(),
  149. wavetableSize * sizeof(float), sampleString);
  150. _this.setAttribute( "sampleShape", sampleString );
  151. // save LED normalize
  152. m_interpolation.saveSettings( _doc, _this, "interpolation" );
  153. // save LED
  154. m_normalize.saveSettings( _doc, _this, "normalize" );
  155. }
  156. void bitInvader::loadSettings( const QDomElement & _this )
  157. {
  158. // Clear wavetable before loading a new
  159. m_graph.clear();
  160. // Load sample length
  161. m_sampleLength.loadSettings( _this, "sampleLength" );
  162. int sampleLength = (int)m_sampleLength.value();
  163. // Load sample shape
  164. int size = 0;
  165. char * dst = 0;
  166. base64::decode( _this.attribute( "sampleShape"), &dst, &size );
  167. m_graph.setLength(size / sizeof(float));
  168. m_graph.setSamples(reinterpret_cast<float*>(dst));
  169. m_graph.setLength(sampleLength);
  170. delete[] dst;
  171. // Load LED normalize
  172. m_interpolation.loadSettings( _this, "interpolation" );
  173. // Load LED
  174. m_normalize.loadSettings( _this, "normalize" );
  175. }
  176. void bitInvader::lengthChanged()
  177. {
  178. m_graph.setLength( (int) m_sampleLength.value() );
  179. normalize();
  180. }
  181. void bitInvader::samplesChanged( int _begin, int _end )
  182. {
  183. normalize();
  184. //engine::getSongEditor()->setModified();
  185. }
  186. void bitInvader::normalize()
  187. {
  188. // analyze
  189. float max = std::numeric_limits<float>::epsilon();
  190. const float* samples = m_graph.samples();
  191. for(int i=0; i < m_graph.length(); i++)
  192. {
  193. const float f = fabsf( samples[i] );
  194. if (f > max) { max = f; }
  195. }
  196. m_normalizeFactor = 1.0 / max;
  197. }
  198. QString bitInvader::nodeName() const
  199. {
  200. return( bitinvader_plugin_descriptor.name );
  201. }
  202. void bitInvader::playNote( NotePlayHandle * _n,
  203. sampleFrame * _working_buffer )
  204. {
  205. if ( _n->totalFramesPlayed() == 0 || _n->m_pluginData == NULL )
  206. {
  207. float factor;
  208. if( !m_normalize.value() )
  209. {
  210. factor = defaultNormalizationFactor;
  211. }
  212. else
  213. {
  214. factor = m_normalizeFactor;
  215. }
  216. _n->m_pluginData = new bSynth(
  217. const_cast<float*>( m_graph.samples() ),
  218. _n,
  219. m_interpolation.value(), factor,
  220. Engine::mixer()->processingSampleRate() );
  221. }
  222. const fpp_t frames = _n->framesLeftForCurrentPeriod();
  223. const f_cnt_t offset = _n->noteOffset();
  224. bSynth * ps = static_cast<bSynth *>( _n->m_pluginData );
  225. for( fpp_t frame = offset; frame < frames + offset; ++frame )
  226. {
  227. const sample_t cur = ps->nextStringSample( m_graph.length() );
  228. for( ch_cnt_t chnl = 0; chnl < DEFAULT_CHANNELS; ++chnl )
  229. {
  230. _working_buffer[frame][chnl] = cur;
  231. }
  232. }
  233. applyRelease( _working_buffer, _n );
  234. instrumentTrack()->processAudioBuffer( _working_buffer, frames + offset, _n );
  235. }
  236. void bitInvader::deleteNotePluginData( NotePlayHandle * _n )
  237. {
  238. delete static_cast<bSynth *>( _n->m_pluginData );
  239. }
  240. PluginView * bitInvader::instantiateView( QWidget * _parent )
  241. {
  242. return( new bitInvaderView( this, _parent ) );
  243. }
  244. bitInvaderView::bitInvaderView( Instrument * _instrument,
  245. QWidget * _parent ) :
  246. InstrumentViewFixedSize( _instrument, _parent )
  247. {
  248. setAutoFillBackground( true );
  249. QPalette pal;
  250. pal.setBrush( backgroundRole(), PLUGIN_NAME::getIconPixmap(
  251. "artwork" ) );
  252. setPalette( pal );
  253. m_sampleLengthKnob = new Knob( knobDark_28, this );
  254. m_sampleLengthKnob->move( 6, 201 );
  255. m_sampleLengthKnob->setHintText( tr( "Sample length" ), "" );
  256. m_graph = new Graph( this, Graph::NearestStyle, 204, 134 );
  257. m_graph->move(23,59); // 55,120 - 2px border
  258. m_graph->setAutoFillBackground( true );
  259. m_graph->setGraphColor( QColor( 255, 255, 255 ) );
  260. ToolTip::add( m_graph, tr ( "Draw your own waveform here "
  261. "by dragging your mouse on this graph."
  262. ));
  263. pal = QPalette();
  264. pal.setBrush( backgroundRole(),
  265. PLUGIN_NAME::getIconPixmap("wavegraph") );
  266. m_graph->setPalette( pal );
  267. m_sinWaveBtn = new PixmapButton( this, tr( "Sine wave" ) );
  268. m_sinWaveBtn->move( 131, 205 );
  269. m_sinWaveBtn->setActiveGraphic( embed::getIconPixmap(
  270. "sin_wave_active" ) );
  271. m_sinWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  272. "sin_wave_inactive" ) );
  273. ToolTip::add( m_sinWaveBtn,
  274. tr( "Sine wave" ) );
  275. m_triangleWaveBtn = new PixmapButton( this, tr( "Triangle wave" ) );
  276. m_triangleWaveBtn->move( 131 + 14, 205 );
  277. m_triangleWaveBtn->setActiveGraphic(
  278. embed::getIconPixmap( "triangle_wave_active" ) );
  279. m_triangleWaveBtn->setInactiveGraphic(
  280. embed::getIconPixmap( "triangle_wave_inactive" ) );
  281. ToolTip::add( m_triangleWaveBtn,
  282. tr( "Triangle wave" ) );
  283. m_sawWaveBtn = new PixmapButton( this, tr( "Saw wave" ) );
  284. m_sawWaveBtn->move( 131 + 14*2, 205 );
  285. m_sawWaveBtn->setActiveGraphic( embed::getIconPixmap(
  286. "saw_wave_active" ) );
  287. m_sawWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  288. "saw_wave_inactive" ) );
  289. ToolTip::add( m_sawWaveBtn,
  290. tr( "Saw wave" ) );
  291. m_sqrWaveBtn = new PixmapButton( this, tr( "Square wave" ) );
  292. m_sqrWaveBtn->move( 131 + 14*3, 205 );
  293. m_sqrWaveBtn->setActiveGraphic( embed::getIconPixmap(
  294. "square_wave_active" ) );
  295. m_sqrWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  296. "square_wave_inactive" ) );
  297. ToolTip::add( m_sqrWaveBtn,
  298. tr( "Square wave" ) );
  299. m_whiteNoiseWaveBtn = new PixmapButton( this,
  300. tr( "White noise" ) );
  301. m_whiteNoiseWaveBtn->move( 131 + 14*4, 205 );
  302. m_whiteNoiseWaveBtn->setActiveGraphic(
  303. embed::getIconPixmap( "white_noise_wave_active" ) );
  304. m_whiteNoiseWaveBtn->setInactiveGraphic(
  305. embed::getIconPixmap( "white_noise_wave_inactive" ) );
  306. ToolTip::add( m_whiteNoiseWaveBtn,
  307. tr( "White noise" ) );
  308. m_usrWaveBtn = new PixmapButton( this, tr( "User-defined wave" ) );
  309. m_usrWaveBtn->move( 131 + 14*5, 205 );
  310. m_usrWaveBtn->setActiveGraphic( embed::getIconPixmap(
  311. "usr_wave_active" ) );
  312. m_usrWaveBtn->setInactiveGraphic( embed::getIconPixmap(
  313. "usr_wave_inactive" ) );
  314. ToolTip::add( m_usrWaveBtn,
  315. tr( "User-defined wave" ) );
  316. m_smoothBtn = new PixmapButton( this, tr( "Smooth waveform" ) );
  317. m_smoothBtn->move( 131 + 14*6, 205 );
  318. m_smoothBtn->setActiveGraphic( PLUGIN_NAME::getIconPixmap(
  319. "smooth_active" ) );
  320. m_smoothBtn->setInactiveGraphic( PLUGIN_NAME::getIconPixmap(
  321. "smooth_inactive" ) );
  322. ToolTip::add( m_smoothBtn,
  323. tr( "Smooth waveform" ) );
  324. m_interpolationToggle = new LedCheckBox( "Interpolation", this,
  325. tr( "Interpolation" ), LedCheckBox::Yellow );
  326. m_interpolationToggle->move( 131, 221 );
  327. m_normalizeToggle = new LedCheckBox( "Normalize", this,
  328. tr( "Normalize" ), LedCheckBox::Green );
  329. m_normalizeToggle->move( 131, 236 );
  330. connect( m_sinWaveBtn, SIGNAL (clicked () ),
  331. this, SLOT ( sinWaveClicked() ) );
  332. connect( m_triangleWaveBtn, SIGNAL ( clicked () ),
  333. this, SLOT ( triangleWaveClicked() ) );
  334. connect( m_sawWaveBtn, SIGNAL (clicked () ),
  335. this, SLOT ( sawWaveClicked() ) );
  336. connect( m_sqrWaveBtn, SIGNAL ( clicked () ),
  337. this, SLOT ( sqrWaveClicked() ) );
  338. connect( m_whiteNoiseWaveBtn, SIGNAL ( clicked () ),
  339. this, SLOT ( noiseWaveClicked() ) );
  340. connect( m_usrWaveBtn, SIGNAL ( clicked () ),
  341. this, SLOT ( usrWaveClicked() ) );
  342. connect( m_smoothBtn, SIGNAL ( clicked () ),
  343. this, SLOT ( smoothClicked() ) );
  344. connect( m_interpolationToggle, SIGNAL( toggled( bool ) ),
  345. this, SLOT ( interpolationToggled( bool ) ) );
  346. connect( m_normalizeToggle, SIGNAL( toggled( bool ) ),
  347. this, SLOT ( normalizeToggled( bool ) ) );
  348. }
  349. void bitInvaderView::modelChanged()
  350. {
  351. bitInvader * b = castModel<bitInvader>();
  352. m_graph->setModel( &b->m_graph );
  353. m_sampleLengthKnob->setModel( &b->m_sampleLength );
  354. m_interpolationToggle->setModel( &b->m_interpolation );
  355. m_normalizeToggle->setModel( &b->m_normalize );
  356. }
  357. void bitInvaderView::sinWaveClicked()
  358. {
  359. m_graph->model()->clearInvisible();
  360. m_graph->model()->setWaveToSine();
  361. Engine::getSong()->setModified();
  362. }
  363. void bitInvaderView::triangleWaveClicked()
  364. {
  365. m_graph->model()->clearInvisible();
  366. m_graph->model()->setWaveToTriangle();
  367. Engine::getSong()->setModified();
  368. }
  369. void bitInvaderView::sawWaveClicked()
  370. {
  371. m_graph->model()->clearInvisible();
  372. m_graph->model()->setWaveToSaw();
  373. Engine::getSong()->setModified();
  374. }
  375. void bitInvaderView::sqrWaveClicked()
  376. {
  377. m_graph->model()->clearInvisible();
  378. m_graph->model()->setWaveToSquare();
  379. Engine::getSong()->setModified();
  380. }
  381. void bitInvaderView::noiseWaveClicked()
  382. {
  383. m_graph->model()->clearInvisible();
  384. m_graph->model()->setWaveToNoise();
  385. Engine::getSong()->setModified();
  386. }
  387. void bitInvaderView::usrWaveClicked()
  388. {
  389. QString fileName = m_graph->model()->setWaveToUser();
  390. if (!fileName.isEmpty())
  391. {
  392. ToolTip::add(m_usrWaveBtn, fileName);
  393. m_graph->model()->clearInvisible();
  394. Engine::getSong()->setModified();
  395. }
  396. }
  397. void bitInvaderView::smoothClicked()
  398. {
  399. m_graph->model()->smooth();
  400. Engine::getSong()->setModified();
  401. }
  402. void bitInvaderView::interpolationToggled( bool value )
  403. {
  404. m_graph->setGraphStyle( value ? Graph::LinearStyle : Graph::NearestStyle);
  405. Engine::getSong()->setModified();
  406. }
  407. void bitInvaderView::normalizeToggled( bool value )
  408. {
  409. Engine::getSong()->setModified();
  410. }
  411. extern "C"
  412. {
  413. // necessary for getting instance out of shared lib
  414. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model *m, void * )
  415. {
  416. return( new bitInvader( static_cast<InstrumentTrack *>( m ) ) );
  417. }
  418. }