Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't fill notes that overlap slightly #7569

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/PianoRoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ protected slots:

void clearGhostClip();
void glueNotes();
void fitNoteLengths(bool fill);
void constrainNoteLengths(bool constrainMax);
void cutOverlappingNotes();
void fillGapsBetweenNotes();

void changeSnapMode();

Expand Down
83 changes: 80 additions & 3 deletions src/gui/editors/PianoRoll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 +711,12 @@ void PianoRoll::glueNotes()
}
}

void PianoRoll::fitNoteLengths(bool fill)
void PianoRoll::cutOverlappingNotes()
{
// TODO remove the code for fill=true
// it has been replaced by fillGapsBetweenNotes()
bool fill = false;

if (!hasValidMidiClip()) { return; }
m_midiClip->addJournalCheckPoint();
m_midiClip->rearrangeAllNotes();
Expand Down Expand Up @@ -766,6 +770,79 @@ void PianoRoll::fitNoteLengths(bool fill)
}



bool fillGapsBetweenNotesInVector(
const NoteVector& notes,
bool onlyEditSelected)
{
bool success = false;

// Keep track of where the clip ends
TimePos lastEndPos;

for (auto currentNote = notes.begin(); currentNote != notes.end(); ++currentNote)
{
if (onlyEditSelected && !(*currentNote)->selected()) { continue; }

/* Note to self:
* Reset the nextNote iterator to currentNote each time. We don't know how far it iterated
* on the previous note since note lengths differ. Also, never start on currentNote + 1
* because that would not set lastEndPos in case the clip only has a single note.
*/
auto nextNote = currentNote;

for (; nextNote != notes.end(); ++nextNote)
{
lastEndPos = std::max(lastEndPos, (*nextNote)->endPos());

// Break when the overlap between currentNote and nextNote is less than
// 50% of the shortest note's length
int lengthOfShortestNote = std::min((*currentNote)->length(), (*nextNote)->length());
int overlap = (*currentNote)->endPos() - (*nextNote)->pos();
if (overlap < (lengthOfShortestNote / 2)) { break; }
}

// If there are no more notes after currentNote, extend it to the end of the bar
if (nextNote == notes.end())
{
TimePos endOfLastBar = lastEndPos.nextFullBar() * TimePos::ticksPerBar();
if ((*currentNote)->endPos() < endOfLastBar)
{
success = true;
(*currentNote)->setLength(endOfLastBar - (*currentNote)->pos());
}
}

// If there's a gap between currentNote and nextNote, extend currentNote
else if ((*currentNote)->endPos() < (*nextNote)->pos())
{
success = true;
(*currentNote)->setLength((*nextNote)->pos() - (*currentNote)->pos());
}
}

return success;
}



void PianoRoll::fillGapsBetweenNotes()
{
if (!hasValidMidiClip()) { return; }
m_midiClip->addJournalCheckPoint();

// Make sure notes are sorted by start position
m_midiClip->rearrangeAllNotes();

bool hasSelection = !getSelectedNotes().empty();

fillGapsBetweenNotesInVector(
m_midiClip->notes(),
/*onlyEditSelected*/ hasSelection);
}



void PianoRoll::constrainNoteLengths(bool constrainMax)
{
if (!hasValidMidiClip()) { return; }
Expand Down Expand Up @@ -4856,11 +4933,11 @@ PianoRollWindow::PianoRollWindow() :
knifeAction->setShortcut( Qt::SHIFT | Qt::Key_K );

auto fillAction = new QAction(embed::getIconPixmap("fill"), tr("Fill"), noteToolsButton);
connect(fillAction, &QAction::triggered, [this](){ m_editor->fitNoteLengths(true); });
connect(fillAction, &QAction::triggered, m_editor, &PianoRoll::fillGapsBetweenNotes);
fillAction->setShortcut(Qt::SHIFT | Qt::Key_F);

auto cutOverlapsAction = new QAction(embed::getIconPixmap("cut_overlaps"), tr("Cut overlaps"), noteToolsButton);
connect(cutOverlapsAction, &QAction::triggered, [this](){ m_editor->fitNoteLengths(false); });
connect(cutOverlapsAction, &QAction::triggered, m_editor, &PianoRoll::cutOverlappingNotes);
cutOverlapsAction->setShortcut(Qt::SHIFT | Qt::Key_C);

auto minLengthAction = new QAction(embed::getIconPixmap("min_length"), tr("Min length as last"), noteToolsButton);
Expand Down
Loading