Skip to content

Commit

Permalink
Automation editor: try match track selection by instrument (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
peterrudenko committed Mar 28, 2024
1 parent ac61de3 commit fcf2ac0
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -588,13 +588,18 @@ void AutomationEditor::setEditableClip(Optional<Clip> clip)

void AutomationEditor::setEditableClip(const Clip &selectedClip, const EventFilter &filter)
{
const auto *selectedSequence = selectedClip.getPattern()->getTrack()->getSequence();
jassert(selectedClip.isValid());
const auto *selectedTrack = selectedClip.getPattern()->getTrack();
const auto selectedRange = Range<float>(
selectedSequence->getFirstBeat() + selectedClip.getBeat(),
selectedSequence->getLastBeat() + selectedClip.getBeat());
selectedTrack->getSequence()->getFirstBeat() + selectedClip.getBeat(),
selectedTrack->getSequence()->getLastBeat() + selectedClip.getBeat());

Clip matchByRange;
float maxIntersection = -1.f;

Clip matchByRangeAndInstrument;
float maxIntersectionForInstrument = -1.f;

Clip matchingClip;
float maxIntersectionLength = -1.f;
for (const auto &map : this->patternMap)
{
const auto *track = map.first.getPattern()->getTrack();
Expand All @@ -611,17 +616,29 @@ void AutomationEditor::setEditableClip(const Clip &selectedClip, const EventFilt
const auto intersectionLength = selectedRange
.getIntersectionWith(matchingRange).getLength();

if (intersectionLength > maxIntersectionLength)
if (track->getTrackChannel() == selectedTrack->getTrackChannel() &&
track->getTrackInstrumentId() == selectedTrack->getTrackInstrumentId() &&
intersectionLength > maxIntersectionForInstrument)
{
maxIntersectionLength = intersectionLength;
matchingClip = map.first;
maxIntersectionForInstrument = intersectionLength;
matchByRangeAndInstrument = map.first;
}

if (intersectionLength > maxIntersection)
{
maxIntersection = intersectionLength;
matchByRange = map.first;
}
}

jassert(matchingClip.isValid());
if (matchingClip.isValid())
jassert(matchByRange.isValid());
if (matchByRangeAndInstrument.isValid())
{
this->setEditableClip(matchByRangeAndInstrument);
}
else if (matchByRange.isValid())
{
this->setEditableClip(matchingClip);
this->setEditableClip(matchByRange);
}
}

Expand Down
10 changes: 5 additions & 5 deletions Source/UI/Sequencer/EditorPanels/EditorPanelsScroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void EditorPanelsScroller::switchToRoll(SafePointer<RollBase> roll)

// in piano roll, allow manually switching between velocity and various automation tracks
this->editorPanelSelectionMode = dynamic_cast<PianoRoll *>(roll.getComponent()) != nullptr ?
EditorPanelSelectionMode::Manual : EditorPanelSelectionMode::Automatic;
EditorPanelSelectionMode::PianoRoll : EditorPanelSelectionMode::PatternRoll;

auto *editor = this->showEditorPanel(this->selectedEditorPanelIndex);
if (this->activeClip.isValid())
Expand Down Expand Up @@ -298,7 +298,7 @@ void EditorPanelsScroller::onChangeClip(const Clip &clip, const Clip &newClip)
}
}

// Only called when the piano roll is switched to another clip;
// only called when the piano roll is switched to another clip:
void EditorPanelsScroller::onChangeViewEditableScope(MidiTrack *const, const Clip &clip, bool)
{
this->activeClip = clip;
Expand All @@ -311,7 +311,7 @@ void EditorPanelsScroller::onChangeViewEditableScope(MidiTrack *const, const Cli
}
}

if (this->editorPanelSelectionMode == EditorPanelSelectionMode::Automatic)
if (this->editorPanelSelectionMode == EditorPanelSelectionMode::PatternRoll)
{
jassertfalse; // piano roll is intended to use manual switching mode
auto *editor = this->showEditorPanelForClip(clip);
Expand All @@ -324,7 +324,7 @@ void EditorPanelsScroller::onChangeViewEditableScope(MidiTrack *const, const Cli
}
}

// Can be called by both the piano roll and the pattern roll
// can be called by both the piano roll and the pattern roll:
void EditorPanelsScroller::changeListenerCallback(ChangeBroadcaster *source)
{
jassert(dynamic_cast<Lasso *>(source));
Expand All @@ -343,7 +343,7 @@ void EditorPanelsScroller::changeListenerCallback(ChangeBroadcaster *source)
{
auto *cc = selection->getFirstAs<ClipComponent>();

if (this->editorPanelSelectionMode == EditorPanelSelectionMode::Automatic)
if (this->editorPanelSelectionMode == EditorPanelSelectionMode::PatternRoll)
{
auto *editor = this->showEditorPanelForClip(cc->getClip());
editor->setEditableClip(cc->getClip());
Expand Down
6 changes: 3 additions & 3 deletions Source/UI/Sequencer/EditorPanels/EditorPanelsScroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,12 @@ class EditorPanelsScroller final :

enum class EditorPanelSelectionMode
{
Manual, // let user decide what to edit, e.g. velocity, tempo, pedals
Automatic // based on the which clip is currently selected
PianoRoll, // let user decide what to edit, e.g. velocity, tempo, pedals
PatternRoll // based on the which clip is currently selected
};

EditorPanelSelectionMode editorPanelSelectionMode =
EditorPanelSelectionMode::Manual;
EditorPanelSelectionMode::PianoRoll;

int selectedEditorPanelIndex = 0;
EditorPanelBase::EventFilter selectedEventFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ void VelocityEditor::setEditableClip(Optional<Clip> clip)

void VelocityEditor::setEditableClip(const Clip &selectedClip, const EventFilter &)
{
this->setEditableClip(selectedClip); // event filters not supported here
this->setEditableClip(selectedClip); // event filters aren't supported here
}

void VelocityEditor::setEditableSelection(WeakReference<Lasso> selection)
Expand Down

0 comments on commit fcf2ac0

Please sign in to comment.