Skip to content

Commit

Permalink
Remove unused noteOffTick and dim pad upon sequenced noteoff
Browse files Browse the repository at this point in the history
  • Loading branch information
izzyreal committed Nov 22, 2022
1 parent af2c8d9 commit e473840
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 69 deletions.
20 changes: 12 additions & 8 deletions src/main/audiomidi/EventHandler.cpp
Expand Up @@ -19,7 +19,6 @@
#include <lcdgui/screens/MixerSetupScreen.hpp>
#include <lcdgui/screens/TransScreen.hpp>
#include <lcdgui/screens/SyncScreen.hpp>
#include <lcdgui/screens/window/CountMetronomeScreen.hpp>
#include <lcdgui/screens/window/VmpcDirectToDiskRecorderScreen.hpp>

#include <midi/core/MidiMessage.hpp>
Expand Down Expand Up @@ -57,8 +56,6 @@ void EventHandler::handle(const std::shared_ptr<Event>& event, Track* track, cha

void EventHandler::handleNoThru(const std::shared_ptr<Event>& event, Track* track, int timeStamp, char drum)
{
auto countMetronomeScreen = mpc.screens->get<CountMetronomeScreen>("count-metronome");

if (sequencer->isCountingIn() && event->getTick() != -1)
{
return;
Expand Down Expand Up @@ -114,21 +111,21 @@ void EventHandler::handleNoThru(const std::shared_ptr<Event>& event, Track* trac
// Each of the 4 DRUMs is routed to their respective 0-based MIDI channel.
// This MIDI channel is part of a MIDI system that is internal to VMPC2000XL's sound player engine.
midiAdapter.process(ne, drumIndex, newVelo);
auto eventFrame = mpc.getAudioMidiServices()->getFrameSequencer()->getEventFrameOffset();

auto frameSeq = mpc.getAudioMidiServices()->getFrameSequencer();
auto eventFrame = frameSeq->getEventFrameOffset();

if (timeStamp != -1)
eventFrame = timeStamp;

int startTick = ne->getNoteOff().lock() ? ne->getTick() : ne->getNoteOnTick();

auto pgmIndex = sampler->getDrumBusProgramIndex(drumIndex + 1);
auto pgm = sampler->getProgram(pgmIndex);
auto voiceOverlap = pgm->getNoteParameters(ne->getNote())->getVoiceOverlap();
auto duration = voiceOverlap == 2 ? ne->getDuration() : -1;
auto audioServer = mpc.getAudioMidiServices()->getAudioServer();
auto durationFrames = (duration == -1 || duration == 0) ? -1 : mpc::sequencer::SeqUtil::ticksToFrames(duration, sequencer->getTempo(), audioServer->getSampleRate());

mpc.getMms()->mpcTransport(midiAdapter.get().lock().get(), 0, ne->getVariationType(), ne->getVariationValue(), eventFrame, startTick, durationFrames);
mpc.getMms()->mpcTransport(midiAdapter.get().lock().get(), 0, ne->getVariationType(), ne->getVariationValue(), eventFrame, ne->getTick(), durationFrames);

if (audioServer->isRealTime())
{
Expand All @@ -147,6 +144,13 @@ void EventHandler::handleNoThru(const std::shared_ptr<Event>& event, Track* trac
notifyVelo = 255;

mpc.getHardware()->getPad(pad)->notifyObservers(notifyVelo);

if (ne->getDuration() > 0)
{
frameSeq->enqueEventAfterNFrames([this, pad]() {
mpc.getHardware()->getPad(pad)->notifyObservers(255);
}, durationFrames);
}
}
}
}
Expand Down Expand Up @@ -197,7 +201,7 @@ void EventHandler::midiOut(const std::shared_ptr<Event>& e, Track* track)
if (candidate != end(transposeCache))
{
auto transposeParameters = *candidate;
auto copy = std::make_shared<NoteEvent>(true);
auto copy = std::make_shared<NoteEvent>(true, 0);
noteEvent->CopyValuesTo(copy);
noteEvent = copy;
noteEvent->setNote(noteEvent->getNote() + transposeParameters.second);
Expand Down
50 changes: 22 additions & 28 deletions src/main/lcdgui/screens/StepEditorScreen.cpp
@@ -1,6 +1,6 @@
#include "StepEditorScreen.hpp"
#include "sequencer/MidiAdapter.hpp"

#include <audiomidi/EventHandler.hpp>
#include <audiomidi/AudioMidiServices.hpp>

#include <hardware/Hardware.hpp>
Expand Down Expand Up @@ -70,7 +70,6 @@ void StepEditorScreen::open()
{
findField("tonote")->setLocation(115, 0);
findLabel("fromnote")->Hide(true);
auto rectangle = findChild<Rectangle>("view-background");

lastRow = 0;

Expand Down Expand Up @@ -715,7 +714,6 @@ void StepEditorScreen::downOrUp(int increment)
auto srcLetter = src.substr(0, 1);
int srcNumber = stoi(src.substr(1, 2));
auto controls = mpc.getControls();
auto destination = srcLetter + std::to_string(srcNumber + increment);

if (srcNumber + increment != -1)
{
Expand Down Expand Up @@ -1353,21 +1351,23 @@ void StepEditorScreen::adhocPlayNoteEvent(const std::shared_ptr<mpc::sequencer::
{
auto tick = noteEvent->getTick();
noteEvent->setTick(-1);
auto eventHandler = mpc.getEventHandler();
auto tr = track.get();

MidiAdapter midiAdapter;

auto mms = mpc.getMms();

midiAdapter.process(noteEvent, tr->getBus() - 1, noteEvent ? noteEvent->getVelocity() : 0);

auto varType = noteEvent->getVariationType();
auto varValue = noteEvent->getVariationValue();

int uniqueEnoughID = playSingleEventCounter++;

if (playSingleEventCounter < 0) playSingleEventCounter = 0;
if (playSingleEventCounter < 0)
{
playSingleEventCounter = 0;
}

auto mms = mpc.getMms();

mms->mpcTransport(midiAdapter.get().lock().get(), 0, varType, varValue, 0, uniqueEnoughID, -1);

Expand All @@ -1376,28 +1376,22 @@ void StepEditorScreen::adhocPlayNoteEvent(const std::shared_ptr<mpc::sequencer::
auto frameSeq = mpc.getAudioMidiServices()->getFrameSequencer();
auto sampleRate = mpc.getAudioMidiServices()->getAudioServer()->getSampleRate();
auto tempo = sequencer->getTempo();
auto &events = frameSeq->eventsAfterNFrames;

for (auto &e: events)
{
if (!e.occupied.load())
{
auto durationInFrames = mpc::sequencer::SeqUtil::ticksToFrames(noteEvent->getDuration(),
tempo, sampleRate);
e.init(durationInFrames, [noteEvent, tr, mms, uniqueEnoughID]() {
auto noteOff = noteEvent->getNoteOff().lock();
auto noteOffTick = noteOff->getTick();
noteOff->setTick(-1);
noteOff->setNote(noteEvent->getNote());
MidiAdapter midiAdapter2;
midiAdapter2.process(noteOff, tr->getBus() - 1, 0);
auto noteOffToSend = midiAdapter2.get();
noteOff->setTick(noteOffTick);
mms->mpcTransport(noteOffToSend.lock().get(), 0, 0, 0, 0, uniqueEnoughID, -1);
});
break;
}
}
auto durationInFrames = SeqUtil::ticksToFrames(noteEvent->getDuration(), tempo, sampleRate);

auto eventAfterNFrames = [noteEvent, tr, mms, uniqueEnoughID]() {
auto noteOff = noteEvent->getNoteOff();
auto noteOffTick = noteOff->getTick();
noteOff->setTick(-1);
noteOff->setNote(noteEvent->getNote());
MidiAdapter midiAdapter2;
midiAdapter2.process(noteOff, tr->getBus() - 1, 0);
auto noteOffToSend = midiAdapter2.get();
noteOff->setTick(noteOffTick);
mms->mpcTransport(noteOffToSend.lock().get(), 0, 0, 0, 0, uniqueEnoughID, -1);
};

frameSeq->enqueEventAfterNFrames(eventAfterNFrames, durationInFrames);
}

void StepEditorScreen::adhocPlayNoteEventsAtCurrentPosition()
Expand Down
16 changes: 14 additions & 2 deletions src/main/sequencer/FrameSeq.cpp
Expand Up @@ -231,8 +231,8 @@ void FrameSeq::repeatPad(int duration)
noteEvent->setVelocity(fullLevel ? 127 : p->getPressure());
noteEvent->setDuration(duration);

noteEvent->getNoteOff().lock()->setTick(tick + duration);
noteEvent->getNoteOff().lock()->setVelocity(0);
noteEvent->getNoteOff()->setTick(tick + duration);
noteEvent->getNoteOff()->setVelocity(0);
auto eventFrame = getEventFrameOffset();

MidiAdapter midiAdapter;
Expand Down Expand Up @@ -565,3 +565,15 @@ void FrameSeq::stopSequencer()
sequencer->stop();
move(0);
}

void FrameSeq::enqueEventAfterNFrames(std::function<void()> event, unsigned long nFrames)
{
for (auto &e : eventsAfterNFrames)
{
if (!e.occupied.load())
{
e.init(nFrames, event);
break;
}
}
}
3 changes: 2 additions & 1 deletion src/main/sequencer/FrameSeq.hpp
Expand Up @@ -61,6 +61,7 @@ namespace mpc::sequencer {
bool wasRunning = false;
std::shared_ptr<ctoot::midi::core::ShortMessage> midiSyncClockMsg;
std::shared_ptr<ctoot::midi::core::ShortMessage> midiSyncStartStopContinueMsg;
std::vector<EventAfterNFrames> eventsAfterNFrames = std::vector<EventAfterNFrames>(100);

void move(int newTickPos);
void repeatPad(int duration);
Expand All @@ -78,7 +79,6 @@ namespace mpc::sequencer {
void stopSequencer();

public:
std::vector<EventAfterNFrames> eventsAfterNFrames = std::vector<EventAfterNFrames>(10);
void start(float sampleRate);
void startMetronome(int sampleRate);
void work(int nFrames) override;
Expand All @@ -89,6 +89,7 @@ namespace mpc::sequencer {
int getTickPosition();

void sendMidiSyncMsg(unsigned char status);
void enqueEventAfterNFrames(std::function<void()>, unsigned long nFrames);

public:
FrameSeq(mpc::Mpc& mpc);
Expand Down
21 changes: 3 additions & 18 deletions src/main/sequencer/NoteEvent.cpp
Expand Up @@ -10,17 +10,16 @@ NoteEvent::NoteEvent()
NoteEvent::NoteEvent(int i)
{
number = i;
noteOff = std::make_shared<NoteEvent>(false, 0);
noteOff = std::make_shared<NoteEvent>(true, 0);
noteOff->number = number;
}

NoteEvent::NoteEvent(bool /*dummyParameter*/, int _noteOnTick)
NoteEvent::NoteEvent(bool /*dummyParameter*/, int /* noteOnTick */)
{
// noteoff ctor should not create a noteoff
noteOnTick = _noteOnTick;
}

std::weak_ptr<NoteEvent> NoteEvent::getNoteOff()
std::shared_ptr<NoteEvent> NoteEvent::getNoteOff()
{
return noteOff;
}
Expand Down Expand Up @@ -120,17 +119,3 @@ void NoteEvent::CopyValuesTo(std::weak_ptr<Event> dest)
lDest->velocity = velocity;
lDest->setDuration(getDuration());
}

void NoteEvent::setTick(int tick)
{
Event::setTick(tick);

if (noteOff)
{
noteOff->noteOnTick = tick;
}
}

int NoteEvent::getNoteOnTick() {
return noteOnTick;
}
12 changes: 5 additions & 7 deletions src/main/sequencer/NoteEvent.hpp
Expand Up @@ -13,14 +13,13 @@ class NoteEvent
int variationTypeNumber = 0;
int variationValue = 64;
int velocity = 0;
int noteOnTick = -1;


protected:
std::shared_ptr<NoteEvent> noteOff;

public:
std::weak_ptr<NoteEvent> getNoteOff();
int getNoteOnTick();
std::shared_ptr<NoteEvent> getNoteOff();

void setNote(int i);
int getNote();
void setDuration(int i);
Expand All @@ -34,12 +33,11 @@ class NoteEvent
int getVelocity();

void CopyValuesTo(std::weak_ptr<Event> dest) override;
void setTick(int tick) override;


NoteEvent();
NoteEvent(int i);

NoteEvent(bool noteOffTrue, int noteOnTick); // ctor used for noteOffs
NoteEvent(bool noteOffTrue, int /* noteOnTick */); // ctor used for noteOffs
std::string getTypeName() override { return "note"; }

};
Expand Down
15 changes: 10 additions & 5 deletions src/main/sequencer/Track.cpp
Expand Up @@ -347,7 +347,9 @@ int Track::getBus()
void Track::setDeviceIndex(int i)
{
if (i < 0 || i > 32)
return;
{
return;
}

device = i;

Expand Down Expand Up @@ -414,12 +416,16 @@ int Track::getCorrectedTickPos()
shiftedTick += amount;

if (shiftedTick < 0)
{
shiftedTick = 0;
}

auto lastTick = parent->getLastTick();

if (shiftedTick > lastTick)
{
shiftedTick = lastTick;
}

correctedTickPos = shiftedTick;
}
Expand Down Expand Up @@ -448,6 +454,7 @@ void Track::processRealtimeQueuedEvents()
}

for (int noteOnIndex = 0; noteOnIndex < noteOnCount; noteOnIndex++) {

auto noteOn = bulkNoteOns[noteOnIndex];

if (noteOn->getTick() == -2)
Expand All @@ -466,7 +473,6 @@ void Track::processRealtimeQueuedEvents()

for (int noteOffIndex = 0; noteOffIndex < noteOffCount; noteOffIndex++)
{

auto noteOff = bulkNoteOffs[noteOffIndex];

if (noteOff->getNote() == noteOn->getNote())
Expand Down Expand Up @@ -564,7 +570,7 @@ void Track::playNext()
bool oneOrMorePadsArePressed = false;
auto hardware = mpc.getHardware();

for (auto& p : hardware->getPads())
for (auto& p : hardware->getPads())
{
if (p->isPressed())
{
Expand All @@ -573,8 +579,7 @@ void Track::playNext()
}
}

if (!_delete && oneOrMorePadsArePressed &&
hardware->getTopPanel()->isSixteenLevelsEnabled())
if (!_delete && oneOrMorePadsArePressed && hardware->getTopPanel()->isSixteenLevelsEnabled())
{
auto vmpcSettingsScreen = mpc.screens->get<VmpcSettingsScreen>("vmpc-settings");
auto assign16LevelsScreen = mpc.screens->get<Assign16LevelsScreen>("assign-16-levels");
Expand Down

0 comments on commit e473840

Please sign in to comment.