From 925122545ec2b975809fd0ba48202e862de71265 Mon Sep 17 00:00:00 2001 From: "Raine M. Ekman" Date: Sat, 10 Jan 2015 21:23:14 +0200 Subject: [PATCH] Fix issue #1292, beautify code a bit, and revert an initialization change I can't grasp the reason for. --- plugins/opl2/opl2instrument.cpp | 53 +++++++++++++++++++-------------- plugins/opl2/opl2instrument.h | 7 +++-- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/plugins/opl2/opl2instrument.cpp b/plugins/opl2/opl2instrument.cpp index d1613a1aa..0b8084349 100644 --- a/plugins/opl2/opl2instrument.cpp +++ b/plugins/opl2/opl2instrument.cpp @@ -56,6 +56,7 @@ #include "embed.cpp" #include "math.h" +#include "debug.h" #include "Knob.h" #include "LcdSpinBox.h" @@ -92,7 +93,7 @@ Plugin * PLUGIN_EXPORT lmms_plugin_main( Model *, void * _data ) QMutex opl2instrument::emulatorMutex; // Weird ordering of voice parameters -const unsigned int adlib_opadd[9] = {0x00, 0x01, 0x02, 0x08, 0x09, 0x0A, 0x10, 0x11, 0x12}; +const unsigned int adlib_opadd[OPL2_VOICES] = {0x00, 0x01, 0x02, 0x08, 0x09, 0x0A, 0x10, 0x11, 0x12}; opl2instrument::opl2instrument( InstrumentTrack * _instrument_track ) : Instrument( _instrument_track, &OPL2_plugin_descriptor ), @@ -141,9 +142,6 @@ opl2instrument::opl2instrument( InstrumentTrack * _instrument_track ) : InstrumentPlayHandle * iph = new InstrumentPlayHandle( this, _instrument_track ); Engine::mixer()->addPlayHandle( iph ); - // Voices are laid out in a funny way... - // adlib_opadd = {0x00, 0x01, 0x02, 0x08, 0x09, 0x0A, 0x10, 0x11, 0x12}; - // Create an emulator - samplerate, 16 bit, mono emulatorMutex.lock(); theEmulator = new CTemuopl(Engine::mixer()->processingSampleRate(), true, false); @@ -153,9 +151,9 @@ opl2instrument::opl2instrument( InstrumentTrack * _instrument_track ) : emulatorMutex.unlock(); //Initialize voice values - voiceNote[0] = 0; - voiceLRU[0] = 0; - for(int i=1; i<9; ++i) { + // voiceNote[0] = 0; + // voiceLRU[0] = 0; + for(int i=0; iinit(); theEmulator->write(0x01,0x20); emulatorMutex.unlock(); - for(int i=1; i<9; ++i) { + for(int i=0; i0; --i) { if( voiceLRU[i-1] != OPL2_NO_VOICE ) { break; } } voiceLRU[i] = v; +#ifdef false + printf("%d %d %d %d %d %d %d %d %d <-- \n", voiceLRU[0],voiceLRU[1],voiceLRU[2], + voiceLRU[3],voiceLRU[4],voiceLRU[5],voiceLRU[6],voiceLRU[7],voiceLRU[8]); +#endif return i; } @@ -307,11 +314,11 @@ bool opl2instrument::handleMidiEvent( const MidiEvent& event, const MidiTime& ti break; case MidiNoteOff: key = event.key() +12; - for(voice=0; voice<9; ++voice) { + for(voice=0; voicewrite(0xA0+voice, fnums[key] & 0xff); theEmulator->write(0xB0+voice, (fnums[key] & 0x1f00) >> 8 ); - voiceNote[voice] = OPL2_VOICE_FREE; + voiceNote[voice] |= OPL2_VOICE_FREE; pushVoice(voice); } } @@ -323,7 +330,7 @@ bool opl2instrument::handleMidiEvent( const MidiEvent& event, const MidiTime& ti if( velocities[key] != 0) { velocities[key] = vel; } - for(voice=0; voice<9; ++voice) { + for(voice=0; voicewrite(0xA0+v, fnums[voiceNote[v] ] & 0xff); - theEmulator->write(0xB0+v, 32 + ((fnums[voiceNote[v]] & 0x1f00) >> 8) ); - } + // Update pitch of all voices (also released ones) + for( int v=0; vwrite(0xA0+v, fnums[vn] & 0xff); + theEmulator->write(0xB0+v, (playing ? 32 : 0) + ((fnums[vn] & 0x1f00) >> 8) ); } break; case MidiControlChange: @@ -363,7 +370,9 @@ bool opl2instrument::handleMidiEvent( const MidiEvent& event, const MidiTime& ti } break; default: +#ifdef LMMS_DEBUG printf("Midi CC %02x %02x\n", event.controllerNumber(), event.controllerValue() ); +#endif break; } break; @@ -476,7 +485,7 @@ void opl2instrument::loadSettings( const QDomElement & _this ) } // Load a patch into the emulator -void opl2instrument::loadPatch(unsigned char inst[14]) { +void opl2instrument::loadPatch(const unsigned char inst[14]) { emulatorMutex.lock(); for(int v=0; v<9; ++v) { theEmulator->write(0x20+adlib_opadd[v],inst[0]); // op1 AM/VIB/EG/KSR/Multiplier @@ -526,7 +535,7 @@ void opl2instrument::loadGMPatch() { // Update patch from the models to the chip emulation void opl2instrument::updatePatch() { - unsigned char *inst = midi_fm_instruments[0]; + unsigned char inst[14] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; inst[0] = ( op1_trem_mdl.value() ? 128 : 0 ) + ( op1_vib_mdl.value() ? 64 : 0 ) + ( op1_perc_mdl.value() ? 0 : 32 ) + // NB. This envelope mode is "perc", not "sus" @@ -564,7 +573,7 @@ void opl2instrument::updatePatch() { // have to do this, as the level knobs might've changed for( int voice = 0; voice < 9 ; ++voice) { - if(voiceNote[voice]!=OPL2_VOICE_FREE) { + if(voiceNote[voice] && OPL2_VOICE_FREE == 0) { setVoiceVelocity(voice, velocities[voiceNote[voice]] ); } } diff --git a/plugins/opl2/opl2instrument.h b/plugins/opl2/opl2instrument.h index 7cd644942..f42ff7352 100644 --- a/plugins/opl2/opl2instrument.h +++ b/plugins/opl2/opl2instrument.h @@ -33,8 +33,11 @@ #include "Knob.h" #include "PixmapButton.h" -#define OPL2_VOICE_FREE 255 +// This one is a flag, MIDI notes take 7 low bits +#define OPL2_VOICE_FREE 128 #define OPL2_NO_VOICE 255 +#define OPL2_VOICES 9 + // The "normal" range for LMMS pitchbends #define DEFAULT_BEND_CENTS 100 @@ -58,7 +61,7 @@ public: void saveSettings( QDomDocument & _doc, QDomElement & _this ); void loadSettings( const QDomElement & _this ); - void loadPatch(unsigned char inst[14]); + void loadPatch(const unsigned char inst[14]); void tuneEqual(int center, float Hz); IntModel m_patchModel;