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