123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930 |
- /* Nes.cpp - A NES instrument plugin for LMMS
- *
- * Copyright (c) 2014 Vesa Kivimäki
- * Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
- *
- * 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 "Nes.h"
- #include "Engine.h"
- #include "InstrumentTrack.h"
- #include "ToolTip.h"
- #include "Song.h"
- #include "lmms_math.h"
- #include "interpolation.h"
- #include "Mixer.h"
- #include "Oscillator.h"
- #include "embed.h"
- #include "plugin_export.h"
- extern "C"
- {
- Plugin::Descriptor PLUGIN_EXPORT nes_plugin_descriptor =
- {
- STRINGIFY( PLUGIN_NAME ),
- "Nescaline",
- QT_TRANSLATE_NOOP( "pluginBrowser",
- "A NES-like synthesizer" ),
- "Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>",
- 0x0100,
- Plugin::Instrument,
- new PluginPixmapLoader( "logo" ),
- NULL,
- NULL
- } ;
- }
- NesObject::NesObject( NesInstrument * nes, const sample_rate_t samplerate, NotePlayHandle * nph ) :
- m_parent( nes ),
- m_samplerate( samplerate ),
- m_nph( nph )
- {
- m_pitchUpdateCounter = 0;
- m_pitchUpdateFreq = wavelength( 60.0f );
-
- m_LFSR = LFSR_INIT;
-
- m_ch1Counter = 0;
- m_ch2Counter = 0;
- m_ch3Counter = 0;
- m_ch4Counter = 0;
-
- m_ch1EnvCounter = 0;
- m_ch2EnvCounter = 0;
- m_ch4EnvCounter = 0;
-
- m_ch1EnvValue = 15;
- m_ch2EnvValue = 15;
- m_ch4EnvValue = 15;
-
- m_ch1SweepCounter = 0;
- m_ch2SweepCounter = 0;
- m_ch4SweepCounter = 0;
-
- m_12Last = 0.0f;
- m_34Last = 0.0f;
-
- m_maxWlen = wavelength( MIN_FREQ );
-
- m_nsf = NES_SIMPLE_FILTER * ( m_samplerate / 44100.0 );
-
- m_lastNoteFreq = 0;
- m_lastNoiseFreq = -1.0f; // value that is always different than noisefreq so it gets updated at start
-
- m_vibratoPhase = 0;
-
- updatePitch();
- }
- NesObject::~NesObject()
- {
- }
- void NesObject::renderOutput( sampleFrame * buf, fpp_t frames )
- {
- ////////////////////////////////
- // //
- // variables for processing //
- // //
- ////////////////////////////////
-
- bool ch1Enabled = m_parent->m_ch1Enabled.value();
- bool ch2Enabled = m_parent->m_ch2Enabled.value();
- bool ch3Enabled = m_parent->m_ch3Enabled.value();
- bool ch4Enabled = m_parent->m_ch4Enabled.value();
-
- float ch1DutyCycle = DUTY_CYCLE[ m_parent->m_ch1DutyCycle.value() ];
- int ch1EnvLen = wavelength( floorf( 240.0 / ( m_parent->m_ch1EnvLen.value() + 1 ) ) );
- bool ch1EnvLoop = m_parent->m_ch1EnvLooped.value();
-
- float ch2DutyCycle = DUTY_CYCLE[ m_parent->m_ch2DutyCycle.value() ];
- int ch2EnvLen = wavelength( floorf( 240.0 / ( m_parent->m_ch2EnvLen.value() + 1 ) ) );
- bool ch2EnvLoop = m_parent->m_ch2EnvLooped.value();
-
- int ch4EnvLen = wavelength( floorf( 240.0 / ( m_parent->m_ch4EnvLen.value() + 1 ) ) );
- bool ch4EnvLoop = m_parent->m_ch4EnvLooped.value();
-
- // processing variables for operators
- int ch1;
- int ch2;
- int ch3;
- int ch4;
-
- // levels for generators (used for dc offset compensation)
- int ch1Level;
- int ch2Level;
- int ch3Level;
- int ch4Level;
-
- int ch1SweepRate = wavelength( floorf( 120.0 / ( m_parent->m_ch1SweepRate.value() + 1 ) ) );
- int ch2SweepRate = wavelength( floorf( 120.0 / ( m_parent->m_ch2SweepRate.value() + 1 ) ) );
- int ch4SweepRate = wavelength( floorf( 60.0f / ( 8.0f - qAbs( m_parent->m_ch4Sweep.value() ) ) ) );
- int ch1Sweep = static_cast<int>( m_parent->m_ch1SweepAmt.value() * -1.0 );
- int ch2Sweep = static_cast<int>( m_parent->m_ch2SweepAmt.value() * -1.0 );
- int ch4Sweep = 0;
- if( m_parent->m_ch4Sweep.value() != 0.0f )
- {
- ch4Sweep = m_parent->m_ch4Sweep.value() > 0.0f
- ? -1
- : 1;
- }
- // the amounts are inverted so we correct them here
- if( ch1Sweep > 0 )
- {
- ch1Sweep = 8 - ch1Sweep;
- }
- if( ch1Sweep < 0 )
- {
- ch1Sweep = -8 - ch1Sweep;
- }
- if( ch2Sweep > 0 )
- {
- ch2Sweep = 8 - ch2Sweep;
- }
- if( ch2Sweep < 0 )
- {
- ch2Sweep = -8 - ch2Sweep;
- }
-
-
- // start framebuffer loop
-
- for( f_cnt_t f = 0; f < frames; f++ )
- {
- ////////////////////////////////
- // //
- // pitch update //
- // //
- ////////////////////////////////
-
- m_pitchUpdateCounter++;
- if( m_pitchUpdateCounter >= m_pitchUpdateFreq )
- {
- updatePitch();
- m_pitchUpdateCounter = 0;
- }
- ////////////////////////////////
- // //
- // channel 1 //
- // //
- ////////////////////////////////
- // render pulse wave
- if( m_wlen1 <= m_maxWlen && m_wlen1 >= MIN_WLEN && ch1Enabled )
- {
- ch1Level = m_parent->m_ch1EnvEnabled.value()
- ? static_cast<int>( ( m_parent->m_ch1Volume.value() * m_ch1EnvValue ) / 15.0 )
- : static_cast<int>( m_parent->m_ch1Volume.value() );
- ch1 = m_ch1Counter > m_wlen1 * ch1DutyCycle
- ? 0
- : ch1Level;
- }
- else ch1 = ch1Level = 0;
-
- // update sweep
- m_ch1SweepCounter++;
- if( m_ch1SweepCounter >= ch1SweepRate )
- {
- m_ch1SweepCounter = 0;
- if( m_parent->m_ch1SweepEnabled.value() && m_wlen1 <= m_maxWlen && m_wlen1 >= MIN_WLEN )
- {
- // check if the sweep goes up or down
- if( ch1Sweep > 0 )
- {
- m_wlen1 += m_wlen1 >> qAbs( ch1Sweep );
- }
- if( ch1Sweep < 0 )
- {
- m_wlen1 -= m_wlen1 >> qAbs( ch1Sweep );
- m_wlen1--; // additional minus 1 for ch1 only
- }
- }
- }
-
- // update framecounters
- m_ch1Counter++;
- m_ch1Counter = m_wlen1 ? m_ch1Counter % m_wlen1 : 0;
- m_ch1EnvCounter++;
- if( m_ch1EnvCounter >= ch1EnvLen )
- {
- m_ch1EnvCounter = 0;
- m_ch1EnvValue--;
- if( m_ch1EnvValue < 0 )
- {
- m_ch1EnvValue = ch1EnvLoop ? 15 : 0;
- }
- }
-
- ////////////////////////////////
- // //
- // channel 2 //
- // //
- ////////////////////////////////
- // render pulse wave
- if( m_wlen2 <= m_maxWlen && m_wlen2 >= MIN_WLEN && ch2Enabled )
- {
- ch2Level = m_parent->m_ch2EnvEnabled.value()
- ? static_cast<int>( ( m_parent->m_ch2Volume.value() * m_ch2EnvValue ) / 15.0 )
- : static_cast<int>( m_parent->m_ch2Volume.value() );
- ch2 = m_ch2Counter > m_wlen2 * ch2DutyCycle
- ? 0
- : ch2Level;
- }
- else ch2 = ch2Level = 0;
-
- // update sweep
- m_ch2SweepCounter++;
- if( m_ch2SweepCounter >= ch2SweepRate )
- {
- m_ch2SweepCounter = 0;
- if( m_parent->m_ch2SweepEnabled.value() && m_wlen2 <= m_maxWlen && m_wlen2 >= MIN_WLEN )
- {
- // check if the sweep goes up or down
- if( ch2Sweep > 0 )
- {
- m_wlen2 += m_wlen2 >> qAbs( ch2Sweep );
- }
- if( ch2Sweep < 0 )
- {
- m_wlen2 -= m_wlen2 >> qAbs( ch2Sweep );
- }
- }
- }
-
- // update framecounters
- m_ch2Counter++;
- m_ch2Counter = m_wlen2 ? m_ch2Counter % m_wlen2 : 0;
-
- m_ch2EnvCounter++;
- if( m_ch2EnvCounter >= ch2EnvLen )
- {
- m_ch2EnvCounter = 0;
- m_ch2EnvValue--;
- if( m_ch2EnvValue < 0 )
- {
- m_ch2EnvValue = ch2EnvLoop
- ? 15
- : 0;
- }
- }
-
-
- ////////////////////////////////
- // //
- // channel 3 //
- // //
- ////////////////////////////////
-
- // make sure we don't overflow
- m_ch3Counter = m_wlen3 ? m_ch3Counter % m_wlen3 : 0;
-
- // render triangle wave
- if( m_wlen3 <= m_maxWlen && ch3Enabled )
- {
- ch3Level = static_cast<int>( m_parent->m_ch3Volume.value() );
- ch3 = m_wlen3 ? TRIANGLE_WAVETABLE[ ( m_ch3Counter * 32 ) / m_wlen3 ] : 0;
- ch3 = ( ch3 * ch3Level ) / 15;
- }
- else ch3 = ch3Level = 0;
-
- m_ch3Counter++;
-
-
- ////////////////////////////////
- // //
- // channel 4 //
- // //
- ////////////////////////////////
-
- // render pseudo noise
- if( ch4Enabled )
- {
- ch4Level = m_parent->m_ch4EnvEnabled.value()
- ? ( static_cast<int>( m_parent->m_ch4Volume.value() ) * m_ch4EnvValue ) / 15
- : static_cast<int>( m_parent->m_ch4Volume.value() );
- ch4 = LFSR()
- ? ch4Level
- : 0;
- }
- else ch4 = ch4Level = 0;
-
- // update framecounters
- m_ch4Counter++;
- if( m_ch4Counter >= m_wlen4 )
- {
- m_ch4Counter = 0;
- updateLFSR( m_parent->m_ch4NoiseMode.value() );
- }
- m_ch4EnvCounter++;
- if( m_ch4EnvCounter >= ch4EnvLen )
- {
- m_ch4EnvCounter = 0;
- m_ch4EnvValue--;
- if( m_ch4EnvValue < 0 )
- {
- m_ch4EnvValue = ch4EnvLoop
- ? 15
- : 0;
- }
- }
-
- m_ch4SweepCounter++;
- if( m_ch4SweepCounter >= ch4SweepRate )
- {
- m_ch4SweepCounter = 0;
- if( ch4Sweep != 0 )
- {
- int freqN = nearestNoiseFreq( static_cast<float>( m_samplerate ) / m_wlen4 );
- freqN = qBound( 0, freqN + ch4Sweep, 15 );
- m_wlen4 = wavelength( NOISE_FREQS[ freqN ] );
- if( m_wlen4 == 0 && ch4Sweep == 1 ) // a workaround for sweep getting stuck on 0 wavelength
- {
- while( m_wlen4 == 0 )
- {
- m_wlen4 = wavelength( NOISE_FREQS[ ++freqN ] );
- }
- }
- }
- }
-
- ////////////////////////////////
- // //
- // final stage - mixing //
- // //
- ////////////////////////////////
-
- float pin1 = static_cast<float>( ch1 + ch2 );
- // add dithering noise
- pin1 *= 1.0 + ( Oscillator::noiseSample( 0.0f ) * DITHER_AMP );
- pin1 = pin1 / 30.0f;
-
- pin1 = signedPow( pin1, NES_DIST );
-
- pin1 = pin1 * 2.0f - 1.0f;
-
- // simple first order iir filter, to simulate the frequency response falloff in nes analog audio output
- pin1 = linearInterpolate( pin1, m_12Last, m_nsf );
- m_12Last = pin1;
- // compensate DC offset
- pin1 += 1.0f - signedPow( static_cast<float>( ch1Level + ch2Level ) / 30.0f, NES_DIST );
-
- pin1 *= NES_MIXING_12;
-
- float pin2 = static_cast<float>( ch3 + ch4 );
- // add dithering noise
- pin2 *= 1.0 + ( Oscillator::noiseSample( 0.0f ) * DITHER_AMP );
- pin2 = pin2 / 30.0f;
-
- pin2 = signedPow( pin2, NES_DIST );
-
- pin2 = pin2 * 2.0f - 1.0f;
- // simple first order iir filter, to simulate the frequency response falloff in nes analog audio output
- pin2 = linearInterpolate( pin2, m_34Last, m_nsf );
- m_34Last = pin2;
-
- // compensate DC offset
- pin2 += 1.0f - signedPow( static_cast<float>( ch3Level + ch4Level ) / 30.0f, NES_DIST );
-
- pin2 *= NES_MIXING_34;
-
- const float mixdown = ( pin1 + pin2 ) * NES_MIXING_ALL * m_parent->m_masterVol.value();
- buf[f][0] = mixdown;
- buf[f][1] = mixdown;
-
- } // end framebuffer loop
- }
- void NesObject::updateVibrato( float * freq )
- {
- float vibratoAmt = floorf( m_parent->m_vibrato.value() ) / 15.0f;
- m_vibratoPhase++;
- m_vibratoPhase %= 32;
- float vibratoRatio = 1.0f + ( static_cast<float>( TRIANGLE_WAVETABLE[ m_vibratoPhase ] ) * 0.02f * vibratoAmt );
- *freq *= vibratoRatio;
- }
- void NesObject::updatePitch()
- {
- float freq = m_nph->frequency();
- // if vibrato is active, update vibrato
- if( m_parent->m_vibrato.value() > 0 )
- {
- updateVibrato( &freq );
- }
- // check if frequency has changed, if so, update wavelengths of ch1-3
- if( freq != m_lastNoteFreq )
- {
- m_wlen1 = wavelength( freq * m_parent->m_freq1 );
- m_wlen2 = wavelength( freq * m_parent->m_freq2 );
- m_wlen3 = wavelength( freq * m_parent->m_freq3 );
- }
- // noise channel can use either note freq or preset freqs
- if( m_parent->m_ch4NoiseFreqMode.value() && freq != m_lastNoteFreq )
- {
- float f = freq * 2.0f;
- if( m_parent->m_ch4NoiseQuantize.value() ) // note freq can be quantized to the preset freqs
- {
- f = NOISE_FREQS[ nearestNoiseFreq( f ) ];
- }
- m_wlen4 = wavelength( f );
- }
- if( ! m_parent->m_ch4NoiseFreqMode.value() && m_lastNoiseFreq != m_parent->m_ch4NoiseFreq.value() )
- {
- m_wlen4 = wavelength( NOISE_FREQS[ 15 - static_cast<int>( m_parent->m_ch4NoiseFreq.value() ) ] );
- m_lastNoiseFreq = m_parent->m_ch4NoiseFreq.value();
- }
-
- m_lastNoteFreq = freq;
- }
- NesInstrument::NesInstrument( InstrumentTrack * instrumentTrack ) :
- Instrument( instrumentTrack, &nes_plugin_descriptor ),
- m_ch1Enabled( true, this ),
- m_ch1Crs( 0.f, -24.f, 24.f, 1.f, this, tr( "Channel 1 coarse detune" ) ),
- m_ch1Volume( 15.f, 0.f, 15.f, 1.f, this, tr( "Channel 1 volume" ) ),
-
- m_ch1EnvEnabled( false, this ),
- m_ch1EnvLooped( false, this ),
- m_ch1EnvLen( 0.f, 0.f, 15.f, 1.f, this, tr( "Channel 1 envelope length" ) ),
-
- m_ch1DutyCycle( 0, 0, 3, this, tr( "Channel 1 duty cycle" ) ),
-
- m_ch1SweepEnabled( false, this ),
- m_ch1SweepAmt( 0.f, -7.f, 7.f, 1.f, this, tr( "Channel 1 sweep amount" ) ),
- m_ch1SweepRate( 0.f, 0.f, 7.f, 1.f, this, tr( "Channel 1 sweep rate" ) ),
-
- m_ch2Enabled( true, this ),
- m_ch2Crs( 0.f, -24.f, 24.f, 1.f, this, tr( "Channel 2 Coarse detune" ) ),
- m_ch2Volume( 15.f, 0.f, 15.f, 1.f, this, tr( "Channel 2 Volume" ) ),
-
- m_ch2EnvEnabled( false, this ),
- m_ch2EnvLooped( false, this ),
- m_ch2EnvLen( 0.f, 0.f, 15.f, 1.f, this, tr( "Channel 2 envelope length" ) ),
-
- m_ch2DutyCycle( 2, 0, 3, this, tr( "Channel 2 duty cycle" ) ),
-
- m_ch2SweepEnabled( false, this ),
- m_ch2SweepAmt( 0.f, -7.f, 7.f, 1.f, this, tr( "Channel 2 sweep amount" ) ),
- m_ch2SweepRate( 0.f, 0.f, 7.f, 1.f, this, tr( "Channel 2 sweep rate" ) ),
-
- //channel 3
- m_ch3Enabled( true, this ),
- m_ch3Crs( 0.f, -24.f, 24.f, 1.f, this, tr( "Channel 3 coarse detune" ) ),
- m_ch3Volume( 15.f, 0.f, 15.f, 1.f, this, tr( "Channel 3 volume" ) ),
- //channel 4
- m_ch4Enabled( false, this ),
- m_ch4Volume( 15.f, 0.f, 15.f, 1.f, this, tr( "Channel 4 volume" ) ),
-
- m_ch4EnvEnabled( false, this ),
- m_ch4EnvLooped( false, this ),
- m_ch4EnvLen( 0.f, 0.f, 15.f, 1.f, this, tr( "Channel 4 envelope length" ) ),
-
- m_ch4NoiseMode( false, this ),
- m_ch4NoiseFreqMode( false, this ),
- m_ch4NoiseFreq( 0.f, 0.f, 15.f, 1.f, this, tr( "Channel 4 noise frequency" ) ),
-
- m_ch4Sweep( 0.f, -7.f, 7.f, 1.f, this, tr( "Channel 4 noise frequency sweep" ) ),
- m_ch4NoiseQuantize( true, this ),
-
- //master
- m_masterVol( 1.0f, 0.0f, 2.0f, 0.01f, this, tr( "Master volume" ) ),
- m_vibrato( 0.0f, 0.0f, 15.0f, 1.0f, this, tr( "Vibrato" ) )
- {
- connect( &m_ch1Crs, SIGNAL( dataChanged() ), this, SLOT( updateFreq1() ) );
- connect( &m_ch2Crs, SIGNAL( dataChanged() ), this, SLOT( updateFreq2() ) );
- connect( &m_ch3Crs, SIGNAL( dataChanged() ), this, SLOT( updateFreq3() ) );
-
- updateFreq1();
- updateFreq2();
- updateFreq3();
- }
- NesInstrument::~NesInstrument()
- {
- }
- void NesInstrument::playNote( NotePlayHandle * n, sampleFrame * workingBuffer )
- {
- const fpp_t frames = n->framesLeftForCurrentPeriod();
- const f_cnt_t offset = n->noteOffset();
-
- if ( n->totalFramesPlayed() == 0 || n->m_pluginData == NULL )
- {
- NesObject * nes = new NesObject( this, Engine::mixer()->processingSampleRate(), n );
- n->m_pluginData = nes;
- }
-
- NesObject * nes = static_cast<NesObject *>( n->m_pluginData );
-
- nes->renderOutput( workingBuffer + offset, frames );
-
- applyRelease( workingBuffer, n );
- instrumentTrack()->processAudioBuffer( workingBuffer, frames + offset, n );
- }
- void NesInstrument::deleteNotePluginData( NotePlayHandle * n )
- {
- delete static_cast<NesObject *>( n->m_pluginData );
- }
- void NesInstrument::saveSettings( QDomDocument & doc, QDomElement & element )
- {
- m_ch1Enabled.saveSettings( doc, element, "on1" );
- m_ch1Crs.saveSettings( doc, element, "crs1" );
- m_ch1Volume.saveSettings( doc, element, "vol1" );
-
- m_ch1EnvEnabled.saveSettings( doc, element, "envon1" );
- m_ch1EnvLooped.saveSettings( doc, element, "envloop1" );
- m_ch1EnvLen.saveSettings( doc, element, "envlen1" );
-
- m_ch1DutyCycle.saveSettings( doc, element, "dc1" );
-
- m_ch1SweepEnabled.saveSettings( doc, element, "sweep1" );
- m_ch1SweepAmt.saveSettings( doc, element, "swamt1" );
- m_ch1SweepRate.saveSettings( doc, element, "swrate1" );
-
- // channel 2
- m_ch2Enabled.saveSettings( doc, element, "on2" );
- m_ch2Crs.saveSettings( doc, element, "crs2" );
- m_ch2Volume.saveSettings( doc, element, "vol2" );
-
- m_ch2EnvEnabled.saveSettings( doc, element, "envon2" );
- m_ch2EnvLooped.saveSettings( doc, element, "envloop2" );
- m_ch2EnvLen.saveSettings( doc, element, "envlen2" );
-
- m_ch2DutyCycle.saveSettings( doc, element, "dc2" );
-
- m_ch2SweepEnabled.saveSettings( doc, element, "sweep2" );
- m_ch2SweepAmt.saveSettings( doc, element, "swamt2" );
- m_ch2SweepRate.saveSettings( doc, element, "swrate2" );
-
- //channel 3
- m_ch3Enabled.saveSettings( doc, element, "on3" );
- m_ch3Crs.saveSettings( doc, element, "crs3" );
- m_ch3Volume.saveSettings( doc, element, "vol3" );
- //channel 4
- m_ch4Enabled.saveSettings( doc, element, "on4" );
- m_ch4Volume.saveSettings( doc, element, "vol4" );
-
- m_ch4EnvEnabled.saveSettings( doc, element, "envon4" );
- m_ch4EnvLooped.saveSettings( doc, element, "envloop4" );
- m_ch4EnvLen.saveSettings( doc, element, "envlen4" );
-
- m_ch4NoiseMode.saveSettings( doc, element, "nmode4" );
- m_ch4NoiseFreqMode.saveSettings( doc, element, "nfrqmode4" );
- m_ch4NoiseFreq.saveSettings( doc, element, "nfreq4" );
-
- m_ch4NoiseQuantize.saveSettings( doc, element, "nq4" );
- m_ch4Sweep.saveSettings( doc, element, "nswp4" );
-
- //master
- m_masterVol.saveSettings( doc, element, "vol" );
- m_vibrato.saveSettings( doc, element, "vibr" );
- }
- void NesInstrument::loadSettings( const QDomElement & element )
- {
- m_ch1Enabled.loadSettings( element, "on1" );
- m_ch1Crs.loadSettings( element, "crs1" );
- m_ch1Volume.loadSettings( element, "vol1" );
-
- m_ch1EnvEnabled.loadSettings( element, "envon1" );
- m_ch1EnvLooped.loadSettings( element, "envloop1" );
- m_ch1EnvLen.loadSettings( element, "envlen1" );
-
- m_ch1DutyCycle.loadSettings( element, "dc1" );
-
- m_ch1SweepEnabled.loadSettings( element, "sweep1" );
- m_ch1SweepAmt.loadSettings( element, "swamt1" );
- m_ch1SweepRate.loadSettings( element, "swrate1" );
-
- // channel 2
- m_ch2Enabled.loadSettings( element, "on2" );
- m_ch2Crs.loadSettings( element, "crs2" );
- m_ch2Volume.loadSettings( element, "vol2" );
-
- m_ch2EnvEnabled.loadSettings( element, "envon2" );
- m_ch2EnvLooped.loadSettings( element, "envloop2" );
- m_ch2EnvLen.loadSettings( element, "envlen2" );
-
- m_ch2DutyCycle.loadSettings( element, "dc2" );
-
- m_ch2SweepEnabled.loadSettings( element, "sweep2" );
- m_ch2SweepAmt.loadSettings( element, "swamt2" );
- m_ch2SweepRate.loadSettings( element, "swrate2" );
-
- //channel 3
- m_ch3Enabled.loadSettings( element, "on3" );
- m_ch3Crs.loadSettings( element, "crs3" );
- m_ch3Volume.loadSettings( element, "vol3" );
- //channel 4
- m_ch4Enabled.loadSettings( element, "on4" );
- m_ch4Volume.loadSettings( element, "vol4" );
-
- m_ch4EnvEnabled.loadSettings( element, "envon4" );
- m_ch4EnvLooped.loadSettings( element, "envloop4" );
- m_ch4EnvLen.loadSettings( element, "envlen4" );
-
- m_ch4NoiseMode.loadSettings( element, "nmode4" );
- m_ch4NoiseFreqMode.loadSettings( element, "nfrqmode4" );
- m_ch4NoiseFreq.loadSettings( element, "nfreq4" );
-
- m_ch4NoiseQuantize.loadSettings( element, "nq4" );
- m_ch4Sweep.loadSettings( element, "nswp4" );
-
- //master
- m_masterVol.loadSettings( element, "vol" );
- m_vibrato.loadSettings( element, "vibr" );
- }
- QString NesInstrument::nodeName() const
- {
- return( nes_plugin_descriptor.name );
- }
- PluginView * NesInstrument::instantiateView( QWidget * parent )
- {
- return( new NesInstrumentView( this, parent ) );
- }
- void NesInstrument::updateFreq1()
- {
- m_freq1 = powf( 2, m_ch1Crs.value() / 12.0f );
- }
- void NesInstrument::updateFreq2()
- {
- m_freq2 = powf( 2, m_ch2Crs.value() / 12.0f );
- }
- void NesInstrument::updateFreq3()
- {
- m_freq3 = powf( 2, m_ch3Crs.value() / 12.0f );
- }
- QPixmap * NesInstrumentView::s_artwork = NULL;
- NesInstrumentView::NesInstrumentView( Instrument * instrument, QWidget * parent ) :
- InstrumentViewFixedSize( instrument, parent )
- {
- setAutoFillBackground( true );
- QPalette pal;
- if( s_artwork == NULL )
- {
- s_artwork = new QPixmap( PLUGIN_NAME::getIconPixmap( "artwork" ) );
- }
- pal.setBrush( backgroundRole(), *s_artwork );
- setPalette( pal );
- const int KNOB_Y1 = 24;
- const int KNOB_Y2 = 81;
- const int KNOB_Y3 = 138;
- const int KNOB_Y4 = 195;
-
- const int KNOB_X1 = 12;
- const int KNOB_X2 = 46;
- const int KNOB_X3 = 84;
- const int KNOB_X4 = 117;
- const int KNOB_X5 = 151;
- const int KNOB_X6 = 172;
- const int KNOB_X7 = 206;
-
- // channel 1
-
- makeknob( m_ch1VolumeKnob, KNOB_X1, KNOB_Y1, tr( "Volume" ), "", "" )
- makeknob( m_ch1CrsKnob, KNOB_X2, KNOB_Y1, tr( "Coarse detune" ), "", "" )
- makeknob( m_ch1EnvLenKnob, KNOB_X3, KNOB_Y1, tr( "Envelope length" ), "", "" )
-
- makenesled( m_ch1EnabledBtn, KNOB_X1, KNOB_Y1 - 12, tr( "Enable channel 1" ) )
- makenesled( m_ch1EnvEnabledBtn, KNOB_X3, KNOB_Y1 - 12, tr( "Enable envelope 1" ) )
- makenesled( m_ch1EnvLoopedBtn, 129, KNOB_Y1 - 12, tr( "Enable envelope 1 loop" ) )
- makenesled( m_ch1SweepEnabledBtn, KNOB_X6, KNOB_Y1 - 12, tr( "Enable sweep 1" ) )
- makeknob( m_ch1SweepAmtKnob, KNOB_X6, KNOB_Y1, tr( "Sweep amount" ), "", "" )
- makeknob( m_ch1SweepRateKnob, KNOB_X7, KNOB_Y1, tr( "Sweep rate" ), "", "" )
- int dcx = 117;
- makedcled( ch1_dc1, dcx, 42, tr( "12.5% Duty cycle" ), "nesdc1_on" )
- dcx += 13;
- makedcled( ch1_dc2, dcx, 42, tr( "25% Duty cycle" ), "nesdc2_on" )
- dcx += 13;
- makedcled( ch1_dc3, dcx, 42, tr( "50% Duty cycle" ), "nesdc3_on" )
- dcx += 13;
- makedcled( ch1_dc4, dcx, 42, tr( "75% Duty cycle" ), "nesdc4_on" )
-
- m_ch1DutyCycleGrp = new automatableButtonGroup( this );
- m_ch1DutyCycleGrp -> addButton( ch1_dc1 );
- m_ch1DutyCycleGrp -> addButton( ch1_dc2 );
- m_ch1DutyCycleGrp -> addButton( ch1_dc3 );
- m_ch1DutyCycleGrp -> addButton( ch1_dc4 );
-
-
- // channel 2
-
- makeknob( m_ch2VolumeKnob, KNOB_X1, KNOB_Y2, tr( "Volume" ), "", "" )
- makeknob( m_ch2CrsKnob, KNOB_X2, KNOB_Y2, tr( "Coarse detune" ), "", "" )
- makeknob( m_ch2EnvLenKnob, KNOB_X3, KNOB_Y2, tr( "Envelope length" ), "", "" )
-
- makenesled( m_ch2EnabledBtn, KNOB_X1, KNOB_Y2 - 12, tr( "Enable channel 2" ) )
- makenesled( m_ch2EnvEnabledBtn, KNOB_X3, KNOB_Y2 - 12, tr( "Enable envelope 2" ) )
- makenesled( m_ch2EnvLoopedBtn, 129, KNOB_Y2 - 12, tr( "Enable envelope 2 loop" ) )
- makenesled( m_ch2SweepEnabledBtn, KNOB_X6, KNOB_Y2 - 12, tr( "Enable sweep 2" ) )
- makeknob( m_ch2SweepAmtKnob, KNOB_X6, KNOB_Y2, tr( "Sweep amount" ), "", "" )
- makeknob( m_ch2SweepRateKnob, KNOB_X7, KNOB_Y2, tr( "Sweep rate" ), "", "" )
- dcx = 117;
- makedcled( ch2_dc1, dcx, 99, tr( "12.5% Duty cycle" ), "nesdc1_on" )
- dcx += 13;
- makedcled( ch2_dc2, dcx, 99, tr( "25% Duty cycle" ), "nesdc2_on" )
- dcx += 13;
- makedcled( ch2_dc3, dcx, 99, tr( "50% Duty cycle" ), "nesdc3_on" )
- dcx += 13;
- makedcled( ch2_dc4, dcx, 99, tr( "75% Duty cycle" ), "nesdc4_on" )
-
- m_ch2DutyCycleGrp = new automatableButtonGroup( this );
- m_ch2DutyCycleGrp -> addButton( ch2_dc1 );
- m_ch2DutyCycleGrp -> addButton( ch2_dc2 );
- m_ch2DutyCycleGrp -> addButton( ch2_dc3 );
- m_ch2DutyCycleGrp -> addButton( ch2_dc4 );
-
- //channel 3
- makenesled( m_ch3EnabledBtn, KNOB_X1, KNOB_Y3 - 12, tr( "Enable channel 3" ) )
- makeknob( m_ch3VolumeKnob, KNOB_X1, KNOB_Y3, tr( "Volume" ), "", "" )
- makeknob( m_ch3CrsKnob, KNOB_X2, KNOB_Y3, tr( "Coarse detune" ), "", "" )
-
- //channel 4
- makeknob( m_ch4VolumeKnob, KNOB_X1, KNOB_Y4, tr( "Volume" ), "", "" )
- makeknob( m_ch4NoiseFreqKnob, KNOB_X2, KNOB_Y4, tr( "Noise Frequency" ), "", "" )
- makeknob( m_ch4EnvLenKnob, KNOB_X3, KNOB_Y4, tr( "Envelope length" ), "", "" )
- makeknob( m_ch4SweepKnob, KNOB_X4, KNOB_Y4, tr( "Frequency sweep" ), "", "" )
-
- makenesled( m_ch4EnabledBtn, KNOB_X1, KNOB_Y4 - 12, tr( "Enable channel 4" ) )
- makenesled( m_ch4EnvEnabledBtn, KNOB_X3, KNOB_Y4 - 12, tr( "Enable envelope 4" ) )
- makenesled( m_ch4EnvLoopedBtn, 129, KNOB_Y4 - 12, tr( "Enable envelope 4 loop" ) )
- makenesled( m_ch4NoiseQuantizeBtn, 162, KNOB_Y4 - 12, tr( "Quantize noise frequency when using note frequency" ) )
-
- makenesled( m_ch4NoiseFreqModeBtn, 148, 203, tr( "Use note frequency for noise" ) )
- makenesled( m_ch4NoiseModeBtn, 148, 224, tr( "Noise mode" ) )
-
- //master
- makeknob( m_masterVolKnob, KNOB_X4, KNOB_Y3, tr( "Master volume" ), "", "" )
- makeknob( m_vibratoKnob, KNOB_X5, KNOB_Y3, tr( "Vibrato" ), "", "" )
- }
- NesInstrumentView::~NesInstrumentView()
- {
- }
- void NesInstrumentView::modelChanged()
- {
- NesInstrument * nes = castModel<NesInstrument>();
- m_ch1EnabledBtn->setModel( &nes->m_ch1Enabled );
- m_ch1CrsKnob->setModel( &nes->m_ch1Crs );
- m_ch1VolumeKnob->setModel( &nes->m_ch1Volume );
- m_ch1EnvEnabledBtn->setModel( &nes->m_ch1EnvEnabled );
- m_ch1EnvLoopedBtn->setModel( &nes->m_ch1EnvLooped );
- m_ch1EnvLenKnob->setModel( &nes->m_ch1EnvLen );
- m_ch1DutyCycleGrp->setModel( &nes->m_ch1DutyCycle );
- m_ch1SweepEnabledBtn->setModel( &nes->m_ch1SweepEnabled );
- m_ch1SweepAmtKnob->setModel( &nes->m_ch1SweepAmt );
- m_ch1SweepRateKnob->setModel( &nes->m_ch1SweepRate );
- // channel 2
- m_ch2EnabledBtn->setModel( &nes->m_ch2Enabled );
- m_ch2CrsKnob->setModel( &nes->m_ch2Crs );
- m_ch2VolumeKnob->setModel( &nes->m_ch2Volume );
- m_ch2EnvEnabledBtn->setModel( &nes->m_ch2EnvEnabled );
- m_ch2EnvLoopedBtn->setModel( &nes->m_ch2EnvLooped );
- m_ch2EnvLenKnob->setModel( &nes->m_ch2EnvLen );
- m_ch2DutyCycleGrp->setModel( &nes->m_ch2DutyCycle );
- m_ch2SweepEnabledBtn->setModel( &nes->m_ch2SweepEnabled );
- m_ch2SweepAmtKnob->setModel( &nes->m_ch2SweepAmt );
- m_ch2SweepRateKnob->setModel( &nes->m_ch2SweepRate );
- //channel 3
- m_ch3EnabledBtn->setModel( &nes->m_ch3Enabled );
- m_ch3CrsKnob->setModel( &nes->m_ch3Crs );
- m_ch3VolumeKnob->setModel( &nes->m_ch3Volume );
- //channel 4
- m_ch4EnabledBtn->setModel( &nes->m_ch4Enabled );
- m_ch4VolumeKnob->setModel( &nes->m_ch4Volume );
- m_ch4EnvEnabledBtn->setModel( &nes->m_ch4EnvEnabled );
- m_ch4EnvLoopedBtn->setModel( &nes->m_ch4EnvLooped );
- m_ch4EnvLenKnob->setModel( &nes->m_ch4EnvLen );
- m_ch4NoiseModeBtn->setModel( &nes->m_ch4NoiseMode );
- m_ch4NoiseFreqModeBtn->setModel( &nes->m_ch4NoiseFreqMode );
- m_ch4NoiseFreqKnob->setModel( &nes->m_ch4NoiseFreq );
- m_ch4SweepKnob->setModel( &nes->m_ch4Sweep );
- m_ch4NoiseQuantizeBtn->setModel( &nes->m_ch4NoiseQuantize );
- //master
- m_masterVolKnob->setModel( &nes->m_masterVol );
- m_vibratoKnob->setModel( &nes->m_vibrato );
- }
- extern "C"
- {
- // necessary for getting instance out of shared lib
- PLUGIN_EXPORT Plugin * lmms_plugin_main( Model *m, void * _data )
- {
- return( new NesInstrument( static_cast<InstrumentTrack *>( m ) ) );
- }
- }
|