diff --git a/core/AudioEngine/mtAudioSequencer.cpp b/core/AudioEngine/mtAudioSequencer.cpp index 37af98090..a215d81ae 100644 --- a/core/AudioEngine/mtAudioSequencer.cpp +++ b/core/AudioEngine/mtAudioSequencer.cpp @@ -200,25 +200,17 @@ void playerEngine::fxGlide(uint8_t fx_val, uint8_t fx_n) uint8_t otherFx_n = !fx_n; uint8_t maxFxGlide = sequencer.getFxMax(Sequencer::strFxConsts::FX_TYPE_GLIDE); - uint8_t minFxGlide = sequencer.getFxMin(Sequencer::strFxConsts::FX_TYPE_GLIDE); - uint8_t halfFxGlide = maxFxGlide / 2; if (fx_n == MOST_SIGNIFICANT_FX) { - currentSeqModValues.glide = - (fx_val > halfFxGlide) - ? mapClamped(fx_val, halfFxGlide, maxFxGlide, 1000, GLIDE_MAX) - : mapClamped(fx_val, minFxGlide, halfFxGlide, GLIDE_MIN, 1000); + currentSeqModValues.glide = glideFxValueToMs(fx_val, maxFxGlide); } else if (fx_n == LEAST_SIGNIFICANT_FX) { if (!trackControlParameter[(int)controlType::sequencerMode + otherFx_n] [(int)parameterList::glide]) { - currentSeqModValues.glide = - (fx_val > halfFxGlide) - ? mapClamped(fx_val, halfFxGlide, maxFxGlide, 1000, GLIDE_MAX) - : mapClamped(fx_val, minFxGlide, halfFxGlide, GLIDE_MIN, 1000); + currentSeqModValues.glide = glideFxValueToMs(fx_val, maxFxGlide); } } trackControlParameter[(int)controlType::sequencerMode + fx_n][(int)parameterList::glide] = 1; diff --git a/core/interface/configEditor/configMenu.cpp b/core/interface/configEditor/configMenu.cpp index 30c5a6410..a26add1ff 100644 --- a/core/interface/configEditor/configMenu.cpp +++ b/core/interface/configEditor/configMenu.cpp @@ -204,6 +204,7 @@ const strItemTypeListText programChangeInChannelSetup{ &mtConfig.midi.programCha 17, notesChannel }; const strItemTypeListText middleCSetup{ &mtConfig.midi.middleC, 4, middleC }; +const strItemTypeListText midiGlideModeSetup{ &mtConfig.midi.glideMode, 4, midiGlideMode }; const strItemTypeListTextWithAction slaveClockCompSetup{ &mtConfig.midi.slaveClockComp, std::size(slaveClockComp), slaveClockComp, setSlaveClockCompValue }; cMenuItem melClockIn(menuMidi, "Clock in", nullptr, menuTypeItemListText, &clockInSetup); @@ -225,6 +226,11 @@ cMenuItem melNotesInChannel(menuMidi, menuTypeItemListText, ¬esInChanneletup); cMenuItem melNotsOutMode(menuMidi, "MIDI out", nullptr, menuTypeItemListText, ¬esOutModeSetup); +cMenuItem melMidiGlideMode(menuMidi, + "MIDI Glide", + nullptr, + menuTypeItemListText, + &midiGlideModeSetup); cMenuItem melCCInMode(menuMidi, "CC in", supportedCcInDesc, menuTypeItemListText, &ccInModeSetup); cMenuItem melCCInChannel(menuMidi, "CC in channel", diff --git a/core/interface/configEditor/configMenuDefs.h b/core/interface/configEditor/configMenuDefs.h index a16c98efa..3aa81588d 100644 --- a/core/interface/configEditor/configMenuDefs.h +++ b/core/interface/configEditor/configMenuDefs.h @@ -229,6 +229,13 @@ const char* const notesChannel[17] = { "CH9", "CH10", "CH11", "CH12", "CH13", "CH14", "CH15", "CH16", }; +const char* const midiGlideMode[4] = { + "Off", + "Legato", + "Bend", + "Both", +}; + const char* const slaveClockComp[kExternalClockCompMaxVal + 1] = { "-6", "-5", "-4", "-3", "-2", "-1", "0", "1", "2", "3", "4", "5", "6", }; diff --git a/core/source/mtConfig.cpp b/core/source/mtConfig.cpp index 95068645b..260fec101 100644 --- a/core/source/mtConfig.cpp +++ b/core/source/mtConfig.cpp @@ -338,6 +338,11 @@ void checkConfig() mtConfig.midi.programChangeInChannel = 0; } + if (mtConfig.midi.glideMode > 3) + { + mtConfig.midi.glideMode = 0; + } + if (mtConfig.midi.middleC > 3) { mtConfig.midi.middleC = 2; @@ -533,6 +538,7 @@ void resetConfig() mtConfig.midi.slaveClockComp = kExternalClockCompDefaultVal; mtConfig.midi.programChangeInMode = 0; mtConfig.midi.programChangeInChannel = 0; + mtConfig.midi.glideMode = MIDI_GLIDE_MODE_OFF; // values ---------------------------------------- mtConfig.values.padsLightBack = PADS_LIGHT_BACK_DEFAULT; diff --git a/core/source/mtSequencer.cpp b/core/source/mtSequencer.cpp index 8f8de5abb..4a396f74b 100644 --- a/core/source/mtSequencer.cpp +++ b/core/source/mtSequencer.cpp @@ -94,6 +94,8 @@ AUDIO_CORE_RAM void Sequencer::tick(float intervalSeconds) updateExternalClockTempo((seqtimesec_t)intervalSeconds); + midiGlideStep(intervalSeconds); + if (isSetInternalClock() || player.selectionMode) { // Integer clock, same ticks and offsets as the double accumulator it replaced (internalTickClock.h) @@ -832,14 +834,15 @@ AUDIO_CORE_RAM void Sequencer::play_microStep(int row) { if (playerRow.stepOpen && playerRow.stepToSend.note >= 0) { - // looks weird but keeps logic from commit - // "Send NOTE OFF to held MIDI notes on track when track muted or sample played" - if (isMidiInstrument(playerRow.stepToSend.instrument) || - isSynthInstrument(playerRow.stepToSend.instrument) || - isMidiInstrument(playerRow.stepSent.instrument) || - isSynthInstrument(playerRow.stepSent.instrument)) + if (!midiGlidePrepare(row, &playerRow.stepSent, &playerRow.stepToSend)) { - sendNoteOff(row, &playerRow.stepSent); + if (isMidiInstrument(playerRow.stepToSend.instrument) || + isSynthInstrument(playerRow.stepToSend.instrument) || + isMidiInstrument(playerRow.stepSent.instrument) || + isSynthInstrument(playerRow.stepSent.instrument)) + { + sendNoteOff(row, &playerRow.stepSent); + } } playerRow.stepOpen = 0; @@ -1041,6 +1044,8 @@ AUDIO_CORE_RAM void Sequencer::play_microStep(int row) playerRow.stepSent = playerRow.stepToSend; } + + midiGlideFlushLegatoOff(row); } // ************************** @@ -1622,6 +1627,7 @@ void Sequencer::stop(StopContext context) songTimer.stop(); stopAllOpenNotes(); cancelFxes(); + midiGlideClearAll(); player.isPlay = 0; player.isStop = 1; @@ -2627,11 +2633,6 @@ AUDIO_CORE_RAM void Sequencer::sendNoteOn(uint8_t track, bool toMidi = isMidiInstrument(step->instrument); bool toSynth = isSynthInstrument(step->instrument); - if (toSynth && synthsMuteState == MuteState::kMuted) - { - return; - } - if (toMidi || toSynth) { if ((step->fx[0].type == fx.FX_TYPE_MIDI_CHORD && step->fx[1].type != fx.FX_TYPE_ARP) || @@ -2656,6 +2657,73 @@ AUDIO_CORE_RAM void Sequencer::sendNoteOn(uint8_t track, velocity = clamp(velocity + deltaPerformanceVelocityFx, 0, kMaxMidiVelocity); + if (toMidi) + { + const uint8_t channel = instrumentIdxToMidiChannel(step->instrument); + strMidiGlide& g = midiGlide[channel - 1]; + + if (g.pending) + { + g.pending = false; + + if (g.fromNote >= 0 && (uint8_t)g.fromNote != step->note) + { + uint8_t glideFxValue = 0; + if (step->fx[1].type == fx.FX_TYPE_GLIDE) + { + glideFxValue = step->fx[1].value; + } + else if (step->fx[0].type == fx.FX_TYPE_GLIDE) + { + glideFxValue = step->fx[0].value; + } + + const int32_t glideUs = (int32_t)glideFxValueToMs(glideFxValue) * 1000; + + if (glideUs > 0) + { + int32_t startCents; + if (g.rampNote >= 0 && g.remainingUs > 0) + { + startCents = (int32_t)g.bend * ((int32_t)g.rangeSemitones * 100) / 8192; + } + else + { + startCents = (int32_t)((int32_t)g.fromNote - (int32_t)step->note) * 100; + } + + int interval = (int)g.fromNote - (int)step->note; + if (interval < 0) + { + interval = -interval; + } + + if (interval > g.rangeSemitones) + { + const uint8_t range = (uint8_t)clamp(interval, 2, 24); + sendCC(99, 0, channel, false); + sendCC(98, 0, channel, false); + sendCC(6, range, channel, false); + sendCC(38, 0, channel, false); + sendCC(101, 127, channel, false); + g.rangeSemitones = range; + } + + int32_t startRaw = startCents * 8192 / ((int32_t)g.rangeSemitones * 100); + startRaw = clamp(startRaw, -8192, 8191); + + g.bend = (int16_t)startRaw; + g.startBend = startRaw; + g.totalUs = glideUs; + g.remainingUs = glideUs; + g.rampNote = (int8_t)step->note; + + sendPitchbend((int16_t)startRaw, (int8_t)channel, false); + } + } + } + } + sendMidiNoteOn( step->note, velocity, instrumentIdxToMidiChannel(step->instrument), toSynth); } @@ -2689,8 +2757,22 @@ AUDIO_CORE_RAM void Sequencer::sendNoteOff(uint8_t track, } else { + const uint8_t channel = instrumentIdxToMidiChannel(instrument); - sendMidiNoteOff(step->note, 0, instrumentIdxToMidiChannel(instrument), isSynth); + if (isMidi) + { + strMidiGlide& g = midiGlide[channel - 1]; + + if (g.rampNote >= 0 && (uint8_t)g.rampNote == step->note) + { + g.rampNote = -1; + g.remainingUs = 0; + g.bend = 0; + sendPitchbend(0, (int8_t)channel, false); + } + } + + sendMidiNoteOff(step->note, 0, channel, isSynth); } } else if (isSampleTrack(track)) @@ -2699,6 +2781,153 @@ AUDIO_CORE_RAM void Sequencer::sendNoteOff(uint8_t track, } } +AUDIO_CORE_RAM bool Sequencer::midiGlidePrepare(uint8_t row, + strPlayer::strPlayerTrack::strSendStep* oldStep, + strPlayer::strPlayerTrack::strSendStep* newStep) +{ + const uint8_t mode = mtConfig.midi.glideMode; + if (mode == MIDI_GLIDE_MODE_OFF) + { + return false; + } + + const bool oldIsMidi = isMidiInstrument(oldStep->instrument); + + if (oldStep->note < 0 || + !oldIsMidi || + !isMidiInstrument(newStep->instrument)) + { + return false; + } + + if (oldStep->fx[0].type == fx.FX_TYPE_MIDI_CHORD || oldStep->fx[1].type == fx.FX_TYPE_MIDI_CHORD || + newStep->fx[0].type == fx.FX_TYPE_MIDI_CHORD || newStep->fx[1].type == fx.FX_TYPE_MIDI_CHORD) + { + return false; + } + + uint8_t glideFxValue = 0; + if (newStep->fx[1].type == fx.FX_TYPE_GLIDE) + { + glideFxValue = newStep->fx[1].value; + } + else if (newStep->fx[0].type == fx.FX_TYPE_GLIDE) + { + glideFxValue = newStep->fx[0].value; + } + + if (glideFxValue == 0) + { + return false; + } + + const uint8_t oldChannel = instrumentIdxToMidiChannel(oldStep->instrument); + const uint8_t newChannel = instrumentIdxToMidiChannel(newStep->instrument); + if (oldChannel != newChannel) + { + return false; + } + + if (mode == MIDI_GLIDE_MODE_BEND) + { + strMidiGlide& g = midiGlide[newChannel - 1]; + g.pending = true; + g.fromNote = oldStep->note; + return false; + } + + strPlayer::strPlayerTrack& playerRow = player.track[row]; + playerRow.legatoOffPending = 1; + playerRow.legatoOffNote = oldStep->note; + playerRow.legatoOffChannel = oldChannel; + + if (mode == MIDI_GLIDE_MODE_BOTH) + { + strMidiGlide& g = midiGlide[newChannel - 1]; + g.pending = true; + g.fromNote = oldStep->note; + } + + return true; +} + +AUDIO_CORE_RAM void Sequencer::midiGlideFlushLegatoOff(uint8_t row) +{ + strPlayer::strPlayerTrack& playerRow = player.track[row]; + + if (playerRow.legatoOffPending) + { + playerRow.legatoOffPending = 0; + sendMidiNoteOff(playerRow.legatoOffNote, 0, playerRow.legatoOffChannel, false); + } +} + +AUDIO_CORE_RAM void Sequencer::midiGlideStep(float intervalSeconds) +{ + if (!player.isPlay) + { + return; + } + + const int32_t dtUs = (int32_t)(intervalSeconds * 1000000.0f); + + for (uint8_t idx = 0; idx < kMidiInstrumentsCount; idx++) + { + strMidiGlide& g = midiGlide[idx]; + + if (g.rampNote < 0 || g.remainingUs <= 0) + { + continue; + } + + const int8_t channel = (int8_t)(idx + 1); + + g.remainingUs -= dtUs; + + if (g.remainingUs <= 0) + { + g.rampNote = -1; + g.remainingUs = 0; + g.bend = 0; + sendPitchbend(0, channel, false); + } + else + { + const int32_t raw = (int32_t)(((int64_t)g.startBend * g.remainingUs) / g.totalUs); + if (raw != g.bend) + { + g.bend = (int16_t)raw; + sendPitchbend((int16_t)raw, channel, false); + } + } + } +} + +AUDIO_CORE_RAM void Sequencer::midiGlideClearAll() +{ + for (uint8_t idx = 0; idx < kMidiInstrumentsCount; idx++) + { + strMidiGlide& g = midiGlide[idx]; + + const int8_t channel = (int8_t)(idx + 1); + + if (g.rampNote >= 0 || g.bend != 0) + { + sendPitchbend(0, channel, false); + } + + g.bend = 0; + g.rampNote = -1; + g.pending = false; + g.remainingUs = 0; + } + + for (uint8_t row = 0; row < kAllTracks; row++) + { + player.track[row].legatoOffPending = 0; + } +} + int8_t Sequencer::velocityToVolume(int8_t velo) { if (velo < 0) diff --git a/core/source/mtSequencer.h b/core/source/mtSequencer.h index e3173c1b3..fc4c5ab57 100644 --- a/core/source/mtSequencer.h +++ b/core/source/mtSequencer.h @@ -460,6 +460,10 @@ class Sequencer uint8_t recOpen = 0; uint8_t recNoteOnStepIdx = 0; + bool legatoOffPending = 0; + int8_t legatoOffNote = -1; + uint8_t legatoOffChannel = 0; + int16_t noteSource = -1; uint16_t uStep = 0; // aktualny microstep @@ -1014,6 +1018,13 @@ class Sequencer void sendMidiChordOn(uint8_t track, strPlayer::strPlayerTrack::strSendStep* step); void sendMidiChordOff(uint8_t track, strPlayer::strPlayerTrack::strSendStep* step); + bool midiGlidePrepare(uint8_t row, + strPlayer::strPlayerTrack::strSendStep* oldStep, + strPlayer::strPlayerTrack::strSendStep* newStep); + void midiGlideFlushLegatoOff(uint8_t row); + void midiGlideStep(float intervalSeconds); + void midiGlideClearAll(); + uint16_t getChordBitmask(uint8_t chord); uint16_t getChordBitmaskForArp(uint8_t chord); @@ -1050,6 +1061,8 @@ class Sequencer strPlayer player; + strMidiGlide midiGlide[kMidiInstrumentsCount]; + int externalClockPulses = 0; int externalClocksToPlay = 0; diff --git a/core/source/mtStructs.h b/core/source/mtStructs.h index 53df36097..b26735e6a 100644 --- a/core/source/mtStructs.h +++ b/core/source/mtStructs.h @@ -3,6 +3,7 @@ #include "config.h" #include "mtEnvelopeGenerator.h" +#include "util_math.h" #include //===================================================================== @@ -175,6 +176,34 @@ const uint16_t GLIDE_MAX = 15000; const uint8_t SEND_MIN = 0; const uint8_t SEND_MAX = 100; +inline uint16_t glideFxValueToMs(uint8_t fxVal, uint8_t maxFx = 100) +{ + const uint8_t half = maxFx / 2; + if (half == 0) + { + return 0; + } + return fxVal > half ? mapClamped(fxVal, half, maxFx, 1000, GLIDE_MAX) + : mapClamped(fxVal, 0, half, GLIDE_MIN, 1000); +} + +const uint8_t MIDI_GLIDE_MODE_OFF = 0; +const uint8_t MIDI_GLIDE_MODE_LEGATO = 1; +const uint8_t MIDI_GLIDE_MODE_BEND = 2; +const uint8_t MIDI_GLIDE_MODE_BOTH = 3; + +struct strMidiGlide +{ + int16_t bend = 0; + uint8_t rangeSemitones = 2; + int8_t rampNote = -1; + int32_t startBend = 0; + int32_t remainingUs = 0; + int32_t totalUs = 0; + bool pending = false; + int8_t fromNote = -1; +}; + const uint16_t ATTACK_MAX = 10000; // zastosowane jest dzielenie przez 100 bez floatow wiec wazne // zeby wartosci czasowe max env byly podzielne przez 100 const uint16_t DECAY_MAX = 10000; @@ -1100,6 +1129,7 @@ struct strMtConfig uint8_t slaveClockComp; uint8_t programChangeInMode; uint8_t programChangeInChannel; + uint8_t glideMode; } midi; struct strGlobalValues