From 17c919879ff5f9e0451fa8c1f4d5e345e4a576e3 Mon Sep 17 00:00:00 2001 From: IanCaio Date: Sat, 18 Nov 2023 19:14:27 -0300 Subject: [PATCH] Implement Note Types (#5902) * Initial Commit Starts implementing Note Types. The two available types are RegularNote and StepNote. PianoRoll now paints the color with a different color for StepNotes. Pattern::addStep now sets the type of the note to StepNote. Negative size is still used to signal a step note. * Update Pattern.cpp to account for the Note::Type Updates the methods noteAtStep(), addStepNote() and checkType() from Pattern.cpp to account for the note type and not the note length. * Update PatternView::paintEvent to draw step notes PatternView::paintEvent now draws the pattern if the pattern type is BeatPattern and TCOs aren't fixed (Song Editor). Color used is still the BeatPattern color (grey) and the conditional doesn't look very nice and can be improved. Pattern::beatPatternLength was also updated so it accounts for the note type not note length. Review this method, as it looks a bit weird (particularly the second conditional). * Implements StepNotes setting a NPH with 0 frames Now, instead of TimePos returning 0 for negative lengths, we create a NotePlayHandle with 0 frames when the note type is StepNote on InstrumentTrack::play. * Improves PatternView::paintEvent conditional Improves a conditional inside PatternView::paintEvent by reversing the order in which they are executed. * Adds upgrade method for backwards compatibility Adds an upgrade method that converts notes with negative length to StepNotes, so old projects can be loaded properly. Explicitly set the Note::RegularNote value as 0. Make the default "type" value "0", so notes without a type are loaded as RegularNotes. * Addresses Veratil's review - Changes "addStepNote" so "checkType" isn't called twice in a row. - Changes style on a one line conditional. * Uses ternary expression on statement Reduces number of lines by using ternary expression. * Addresses PR review (sakertooth) - Changes class setter to inline - Uses enum class instead of enum - Uses auto and const where appropriate * Finished changes from review (sakertooth) - Used std::max instead of qMax - Fixed style on lines changed in the PR * Uses std::find_if to save codelines As suggested by sakertooth, by using std::find_if we are able to simplify the checkType method to two lines. * Addresses review from sakertooth - Reverts m_detuning in-class initialization - Removes testing warning - Removes unnecessary comment * Addresses DomClark's review - Rename the Note Types enum to avoid redundancy - Uses std::all_of instead of std::find_if on MidiClip checkType - Rewrites addStepNote so it sets the note type before adding it to the clip, avoiding having to manually change the type of the clip after adding the note * Updates MidiExport to use Note Types - Now MidiExport is updated to use note types instead of relying on negative length notes. - For that change it was necessary to find a way of letting MidiExport know how long step notes should be. The solution found was to add an attribute to the Instrument XML called "beatlen", which would hold the number of frames of the instrument's beat. That would be converted to ticks, so we could calculate how long the MIDI notes would have to be to play the whole step note. If the attribute was not found, the default value of 16 ticks would be used as a length of step notes, as a fallback. * Fixes ambiguity on enum usage Due to changes in the name of enum classes, there was an ambiguity caused in NotePlayHandle.cpp. That was fixed. * Addresses new code reviews - Addresses code review from PhysSong and Messmerd * Fixes note drawing on Song Editor - Notes were not being draw on the song editor for BeatClips. This commit fixes this. * Adds cassert header to TimePos.cpp - Adds header to use assert() on TimePos.cpp * Apply suggestions from code review Fixes style on some lines Co-authored-by: Dalton Messmer <33463986+messmerd@users.noreply.github.com> * Reverts some changes on MidiExport - Some changes were reverted on MidiExport and InstrumentTrack. We were storing the beat length on the XML of Instrument Tracks, but in reality the beat length is a per note attribute, and some instruments could run into a segmentation fault when calling beat length without a NotePlayHandle (i.e.: AFP). Because of that I reverted this change, so the beat length is not stored on the XML anymore, and instead we have a magic number on the MidiExport class that holds a default beat length which is actually an upper limit for the MIDI notes of step notes. In the future we can improve this by finding a way to store the beat length on the note class to use it instead. The MidiExport logic is not worsened at all because previously the beat length wasn't even considered during export (it was actually improved making the exported notes extend until the next one instead of cutting shorter). * Fix the order of included files --------- Co-authored-by: Dalton Messmer <33463986+messmerd@users.noreply.github.com> --- data/themes/classic/style.css | 1 + data/themes/default/style.css | 1 + include/DataFile.h | 3 +- include/Note.h | 12 +++ include/PianoRoll.h | 2 + plugins/MidiExport/MidiExport.cpp | 7 +- plugins/MidiExport/MidiExport.h | 12 +++ src/core/DataFile.cpp | 21 ++++- src/core/Note.cpp | 7 +- src/core/NotePlayHandle.cpp | 2 +- src/core/TimePos.cpp | 13 +-- src/gui/clips/MidiClipView.cpp | 138 ++++++++++++++++-------------- src/gui/editors/PianoRoll.cpp | 10 ++- src/tracks/InstrumentTrack.cpp | 7 +- src/tracks/MidiClip.cpp | 37 ++++---- 15 files changed, 170 insertions(+), 103 deletions(-) diff --git a/data/themes/classic/style.css b/data/themes/classic/style.css index b378c4b8e..9b50851a3 100644 --- a/data/themes/classic/style.css +++ b/data/themes/classic/style.css @@ -143,6 +143,7 @@ lmms--gui--PianoRoll { qproperty-backgroundShade: rgba( 255, 255, 255, 10 ); qproperty-noteModeColor: rgb( 255, 255, 255 ); qproperty-noteColor: rgb( 119, 199, 216 ); + qproperty-stepNoteColor: #9b1313; qproperty-noteTextColor: rgb( 255, 255, 255 ); qproperty-noteOpacity: 128; qproperty-noteBorders: true; /* boolean property, set false to have borderless notes */ diff --git a/data/themes/default/style.css b/data/themes/default/style.css index 323f6d03d..172a67d8e 100644 --- a/data/themes/default/style.css +++ b/data/themes/default/style.css @@ -175,6 +175,7 @@ lmms--gui--PianoRoll { qproperty-backgroundShade: rgba(255, 255, 255, 10); qproperty-noteModeColor: #0bd556; qproperty-noteColor: #0bd556; + qproperty-stepNoteColor: #9b1313; qproperty-noteTextColor: #ffffff; qproperty-noteOpacity: 165; qproperty-noteBorders: false; /* boolean property, set false to have borderless notes */ diff --git a/include/DataFile.h b/include/DataFile.h index ceda9b829..3f1706229 100644 --- a/include/DataFile.h +++ b/include/DataFile.h @@ -127,8 +127,9 @@ private: void upgrade_mixerRename(); void upgrade_bbTcoRename(); void upgrade_sampleAndHold(); - void upgrade_midiCCIndexing(); + void upgrade_midiCCIndexing(); void upgrade_loopsRename(); + void upgrade_noteTypes(); // List of all upgrade methods static const std::vector UPGRADE_METHODS; diff --git a/include/Note.h b/include/Note.h index 2df196af2..08cbce3db 100644 --- a/include/Note.h +++ b/include/Note.h @@ -107,6 +107,16 @@ public: Note( const Note & note ); ~Note() override; + // Note types + enum class Type + { + Regular = 0, + Step + }; + + Type type() const { return m_type; } + inline void setType(Type t) { m_type = t; } + // used by GUI inline void setSelected( const bool selected ) { m_selected = selected; } inline void setOldKey( const int oldKey ) { m_oldKey = oldKey; } @@ -253,6 +263,8 @@ private: TimePos m_length; TimePos m_pos; DetuningHelper * m_detuning; + + Type m_type = Type::Regular; }; using NoteVector = std::vector; diff --git a/include/PianoRoll.h b/include/PianoRoll.h index 38788180f..bcaea8637 100644 --- a/include/PianoRoll.h +++ b/include/PianoRoll.h @@ -73,6 +73,7 @@ class PianoRoll : public QWidget Q_PROPERTY(QColor lineColor MEMBER m_lineColor) Q_PROPERTY(QColor noteModeColor MEMBER m_noteModeColor) Q_PROPERTY(QColor noteColor MEMBER m_noteColor) + Q_PROPERTY(QColor stepNoteColor MEMBER m_stepNoteColor) Q_PROPERTY(QColor ghostNoteColor MEMBER m_ghostNoteColor) Q_PROPERTY(QColor noteTextColor MEMBER m_noteTextColor) Q_PROPERTY(QColor ghostNoteTextColor MEMBER m_ghostNoteTextColor) @@ -466,6 +467,7 @@ private: QColor m_lineColor; QColor m_noteModeColor; QColor m_noteColor; + QColor m_stepNoteColor; QColor m_noteTextColor; QColor m_ghostNoteColor; QColor m_ghostNoteTextColor; diff --git a/plugins/MidiExport/MidiExport.cpp b/plugins/MidiExport/MidiExport.cpp index df968e36a..2600a40f2 100644 --- a/plugins/MidiExport/MidiExport.cpp +++ b/plugins/MidiExport/MidiExport.cpp @@ -27,6 +27,7 @@ #include "MidiExport.h" +#include "Engine.h" #include "TrackContainer.h" #include "DataFile.h" #include "InstrumentTrack.h" @@ -279,6 +280,7 @@ void MidiExport::writeMidiClip(MidiNoteVector &midiClip, const QDomNode& n, mnote.volume = qMin(qRound(base_volume * LocaleHelper::toDouble(note.attribute("vol", "100")) * (127.0 / 200.0)), 127); mnote.time = base_time + note.attribute("pos", "0").toInt(); mnote.duration = note.attribute("len", "0").toInt(); + mnote.type = static_cast(note.attribute("type", "0").toInt()); midiClip.push_back(mnote); } } @@ -311,6 +313,7 @@ void MidiExport::writePatternClip(MidiNoteVector& src, MidiNoteVector& dst, note.pitch = srcNote.pitch; note.time = base + time; note.volume = srcNote.volume; + note.type = srcNote.type; dst.push_back(note); } } @@ -329,9 +332,9 @@ void MidiExport::processPatternNotes(MidiNoteVector& nv, int cutPos) next = cur; cur = it->time; } - if (it->duration < 0) + if (it->type == Note::Type::Step) { - it->duration = qMin(qMin(-it->duration, next - cur), cutPos - it->time); + it->duration = qMin(qMin(DefaultBeatLength, next - cur), cutPos - it->time); } } } diff --git a/plugins/MidiExport/MidiExport.h b/plugins/MidiExport/MidiExport.h index 1e355e45a..7c77c7af2 100644 --- a/plugins/MidiExport/MidiExport.h +++ b/plugins/MidiExport/MidiExport.h @@ -30,6 +30,7 @@ #include "ExportFilter.h" #include "MidiFile.hpp" +#include "Note.h" class QDomNode; @@ -46,6 +47,7 @@ struct MidiNote uint8_t pitch; int duration; uint8_t volume; + Note::Type type; inline bool operator<(const MidiNote &b) const { @@ -63,6 +65,16 @@ public: MidiExport(); ~MidiExport() override = default; + // Default Beat Length in ticks for step notes + // TODO: The beat length actually varies per note, however the method that + // calculates it (InstrumentTrack::beatLen) requires a NotePlayHandle to do + // so. While we don't figure out a way to hold the beat length of each note + // on its member variables, we will use a default value as a beat length that + // will be used as an upper limit of the midi note length. This doesn't worsen + // the current logic used for MidiExport because right now the beat length is + // not even considered during the generation of the MIDI. + static constexpr int DefaultBeatLength = 1500; + gui::PluginView* instantiateView(QWidget *) override { return nullptr; diff --git a/src/core/DataFile.cpp b/src/core/DataFile.cpp index b83e1bebb..a520e6bc5 100644 --- a/src/core/DataFile.cpp +++ b/src/core/DataFile.cpp @@ -43,6 +43,7 @@ #include "embed.h" #include "GuiApplication.h" #include "LocaleHelper.h" +#include "Note.h" #include "PluginFactory.h" #include "ProjectVersion.h" #include "SongEditor.h" @@ -81,7 +82,7 @@ const std::vector DataFile::UPGRADE_METHODS = { &DataFile::upgrade_defaultTripleOscillatorHQ, &DataFile::upgrade_mixerRename , &DataFile::upgrade_bbTcoRename, &DataFile::upgrade_sampleAndHold , &DataFile::upgrade_midiCCIndexing, - &DataFile::upgrade_loopsRename + &DataFile::upgrade_loopsRename , &DataFile::upgrade_noteTypes }; // Vector of all versions that have upgrade routines. @@ -1666,6 +1667,24 @@ void DataFile::upgrade_automationNodes() } } +// Convert the negative length notes to StepNotes +void DataFile::upgrade_noteTypes() +{ + const auto notes = elementsByTagName("note"); + + for (int i = 0; i < notes.size(); ++i) + { + auto note = notes.item(i).toElement(); + + const auto noteSize = note.attribute("len").toInt(); + if (noteSize < 0) + { + note.setAttribute("len", DefaultTicksPerBar / 16); + note.setAttribute("type", static_cast(Note::Type::Step)); + } + } +} + /** \brief Note range has been extended to match MIDI specification * diff --git a/src/core/Note.cpp b/src/core/Note.cpp index a4ad61412..ed3a00f10 100644 --- a/src/core/Note.cpp +++ b/src/core/Note.cpp @@ -74,7 +74,8 @@ Note::Note( const Note & note ) : m_panning( note.m_panning ), m_length( note.m_length ), m_pos( note.m_pos ), - m_detuning( nullptr ) + m_detuning(nullptr), + m_type(note.m_type) { if( note.m_detuning ) { @@ -179,6 +180,7 @@ void Note::saveSettings( QDomDocument & doc, QDomElement & parent ) parent.setAttribute( "pan", m_panning ); parent.setAttribute( "len", m_length ); parent.setAttribute( "pos", m_pos ); + parent.setAttribute("type", static_cast(m_type)); if( m_detuning && m_length ) { @@ -197,6 +199,9 @@ void Note::loadSettings( const QDomElement & _this ) m_panning = _this.attribute( "pan" ).toInt(); m_length = _this.attribute( "len" ).toInt(); m_pos = _this.attribute( "pos" ).toInt(); + // Default m_type value is 0, which corresponds to RegularNote + static_assert(0 == static_cast(Type::Regular)); + m_type = static_cast(_this.attribute("type", "0").toInt()); if( _this.hasChildNodes() ) { diff --git a/src/core/NotePlayHandle.cpp b/src/core/NotePlayHandle.cpp index eb9c7ddbf..712b64e89 100644 --- a/src/core/NotePlayHandle.cpp +++ b/src/core/NotePlayHandle.cpp @@ -53,7 +53,7 @@ NotePlayHandle::NotePlayHandle( InstrumentTrack* instrumentTrack, NotePlayHandle *parent, int midiEventChannel, Origin origin ) : - PlayHandle( Type::NotePlayHandle, _offset ), + PlayHandle( PlayHandle::Type::NotePlayHandle, _offset ), Note( n.length(), n.pos(), n.key(), n.getVolume(), n.getPanning(), n.detuning() ), m_pluginData( nullptr ), m_instrumentTrack( instrumentTrack ), diff --git a/src/core/TimePos.cpp b/src/core/TimePos.cpp index 86a65f103..09c1019bc 100644 --- a/src/core/TimePos.cpp +++ b/src/core/TimePos.cpp @@ -25,6 +25,7 @@ #include "TimePos.h" +#include #include "MeterModel.h" namespace lmms @@ -161,11 +162,11 @@ tick_t TimePos::getTickWithinBeat( const TimeSig &sig ) const f_cnt_t TimePos::frames( const float framesPerTick ) const { - if( m_ticks >= 0 ) - { - return static_cast( m_ticks * framesPerTick ); - } - return 0; + // Before, step notes used to have negative length. This + // assert is a safeguard against negative length being + // introduced again (now using Note Types instead #5902) + assert(m_ticks >= 0); + return static_cast(m_ticks * framesPerTick); } double TimePos::getTimeInMilliseconds( bpm_t beatsPerMinute ) const @@ -221,4 +222,4 @@ double TimePos::ticksToMilliseconds(double ticks, bpm_t beatsPerMinute) } -} // namespace lmms \ No newline at end of file +} // namespace lmms diff --git a/src/gui/clips/MidiClipView.cpp b/src/gui/clips/MidiClipView.cpp index 151df8d3c..b13d6e003 100644 --- a/src/gui/clips/MidiClipView.cpp +++ b/src/gui/clips/MidiClipView.cpp @@ -25,6 +25,7 @@ #include "MidiClipView.h" +#include #include #include #include @@ -458,9 +459,78 @@ void MidiClipView::paintEvent( QPaintEvent * ) const int x_base = BORDER_WIDTH; bool displayPattern = fixedClips() || (pixelsPerBar >= 96 && m_legacySEPattern); - // melody clip paint event NoteVector const & noteCollection = m_clip->m_notes; - if( m_clip->m_clipType == MidiClip::Type::MelodyClip && !noteCollection.empty() ) + + // Beat clip paint event (on BB Editor) + if (beatClip && displayPattern) + { + QPixmap stepon0; + QPixmap stepon200; + QPixmap stepoff; + QPixmap stepoffl; + const int steps = std::max(1, m_clip->m_steps); + const int w = width() - 2 * BORDER_WIDTH; + + // scale step graphics to fit the beat clip length + stepon0 = s_stepBtnOn0->scaled(w / steps, + s_stepBtnOn0->height(), + Qt::IgnoreAspectRatio, + Qt::SmoothTransformation); + stepon200 = s_stepBtnOn200->scaled(w / steps, + s_stepBtnOn200->height(), + Qt::IgnoreAspectRatio, + Qt::SmoothTransformation); + stepoff = s_stepBtnOff->scaled(w / steps, + s_stepBtnOff->height(), + Qt::IgnoreAspectRatio, + Qt::SmoothTransformation); + stepoffl = s_stepBtnOffLight->scaled(w / steps, + s_stepBtnOffLight->height(), + Qt::IgnoreAspectRatio, + Qt::SmoothTransformation); + + for (int it = 0; it < steps; it++) // go through all the steps in the beat clip + { + Note* n = m_clip->noteAtStep(it); + + // figure out x and y coordinates for step graphic + const int x = BORDER_WIDTH + static_cast(it * w / steps); + const int y = height() - s_stepBtnOff->height() - 1; + + if (n) + { + const int vol = n->getVolume(); + p.drawPixmap(x, y, stepoffl); + p.drawPixmap(x, y, stepon0); + p.setOpacity(std::sqrt(vol / 200.0)); + p.drawPixmap(x, y, stepon200); + p.setOpacity(1); + } + else if ((it / 4) % 2) + { + p.drawPixmap(x, y, stepoffl); + } + else + { + p.drawPixmap(x, y, stepoff); + } + } // end for loop + + // draw a transparent rectangle over muted clips + if (muted) + { + p.setBrush(mutedBackgroundColor()); + p.setOpacity(0.5); + p.drawRect(0, 0, width(), height()); + } + } + // Melody clip and Beat clip (on Song Editor) paint event + else if + ( + !noteCollection.empty() && + (m_clip->m_clipType == MidiClip::Type::MelodyClip || + m_clip->m_clipType == MidiClip::Type::BeatClip) + ) { // Compute the minimum and maximum key in the clip // so that we know how much there is to draw. @@ -574,70 +644,6 @@ void MidiClipView::paintEvent( QPaintEvent * ) p.restore(); } - // beat clip paint event - else if (beatClip && displayPattern) - { - QPixmap stepon0; - QPixmap stepon200; - QPixmap stepoff; - QPixmap stepoffl; - const int steps = qMax( 1, - m_clip->m_steps ); - const int w = width() - 2 * BORDER_WIDTH; - - // scale step graphics to fit the beat clip length - stepon0 = s_stepBtnOn0->scaled( w / steps, - s_stepBtnOn0->height(), - Qt::IgnoreAspectRatio, - Qt::SmoothTransformation ); - stepon200 = s_stepBtnOn200->scaled( w / steps, - s_stepBtnOn200->height(), - Qt::IgnoreAspectRatio, - Qt::SmoothTransformation ); - stepoff = s_stepBtnOff->scaled( w / steps, - s_stepBtnOff->height(), - Qt::IgnoreAspectRatio, - Qt::SmoothTransformation ); - stepoffl = s_stepBtnOffLight->scaled( w / steps, - s_stepBtnOffLight->height(), - Qt::IgnoreAspectRatio, - Qt::SmoothTransformation ); - - for( int it = 0; it < steps; it++ ) // go through all the steps in the beat clip - { - Note * n = m_clip->noteAtStep( it ); - - // figure out x and y coordinates for step graphic - const int x = BORDER_WIDTH + static_cast( it * w / steps ); - const int y = height() - s_stepBtnOff->height() - 1; - - if( n ) - { - const int vol = n->getVolume(); - p.drawPixmap( x, y, stepoffl ); - p.drawPixmap( x, y, stepon0 ); - p.setOpacity( sqrt( vol / 200.0 ) ); - p.drawPixmap( x, y, stepon200 ); - p.setOpacity( 1 ); - } - else if( ( it / 4 ) % 2 ) - { - p.drawPixmap( x, y, stepoffl ); - } - else - { - p.drawPixmap( x, y, stepoff ); - } - } // end for loop - - // draw a transparent rectangle over muted clips - if ( muted ) - { - p.setBrush( mutedBackgroundColor() ); - p.setOpacity( 0.5 ); - p.drawRect( 0, 0, width(), height() ); - } - } // bar lines const int lineSize = 3; diff --git a/src/gui/editors/PianoRoll.cpp b/src/gui/editors/PianoRoll.cpp index 6bf1b5daf..2d8f9cbc2 100644 --- a/src/gui/editors/PianoRoll.cpp +++ b/src/gui/editors/PianoRoll.cpp @@ -3498,11 +3498,15 @@ void PianoRoll::paintEvent(QPaintEvent * pe ) // is the note in visible area? if (note->key() > bottomKey && note->key() <= topKey) { - // we've done and checked all, let's draw the note + // We've done and checked all, let's draw the note with + // the appropriate color + const auto fillColor = note->type() == Note::Type::Regular ? m_noteColor : m_stepNoteColor; + drawNoteRect( p, x + m_whiteKeyWidth, noteYPos(note->key()), note_width, - note, m_noteColor, m_noteTextColor, m_selectedNoteColor, - m_noteOpacity, m_noteBorders, drawNoteNames); + note, fillColor, m_noteTextColor, m_selectedNoteColor, + m_noteOpacity, m_noteBorders, drawNoteNames + ); } // draw note editing stuff diff --git a/src/tracks/InstrumentTrack.cpp b/src/tracks/InstrumentTrack.cpp index 8804833ee..4b00e0d79 100644 --- a/src/tracks/InstrumentTrack.cpp +++ b/src/tracks/InstrumentTrack.cpp @@ -776,8 +776,11 @@ bool InstrumentTrack::play( const TimePos & _start, const fpp_t _frames, while( nit != notes.end() && ( cur_note = *nit )->pos() == cur_start ) { - const f_cnt_t note_frames = - cur_note->length().frames( frames_per_tick ); + // If the note is a Step Note, frames will be 0 so the NotePlayHandle + // plays for the whole length of the sample + const auto note_frames = cur_note->type() == Note::Type::Step + ? 0 + : cur_note->length().frames(frames_per_tick); NotePlayHandle* notePlayHandle = NotePlayHandleManager::acquire( this, _offset, note_frames, *cur_note ); notePlayHandle->setPatternTrack(pattern_track); diff --git a/src/tracks/MidiClip.cpp b/src/tracks/MidiClip.cpp index 490f6e6d0..087079dc8 100644 --- a/src/tracks/MidiClip.cpp +++ b/src/tracks/MidiClip.cpp @@ -25,6 +25,7 @@ #include "MidiClip.h" +#include #include #include "GuiApplication.h" @@ -174,19 +175,18 @@ TimePos MidiClip::beatClipLength() const for (const auto& note : m_notes) { - if (note->length() < 0) + if (note->type() == Note::Type::Step) { max_length = std::max(max_length, note->pos() + 1); } } - if( m_steps != TimePos::stepsPerBar() ) + if (m_steps != TimePos::stepsPerBar()) { - max_length = m_steps * TimePos::ticksPerBar() / - TimePos::stepsPerBar(); + max_length = m_steps * TimePos::ticksPerBar() / TimePos::stepsPerBar(); } - return TimePos( max_length ).nextFullBar() * TimePos::ticksPerBar(); + return TimePos{max_length}.nextFullBar() * TimePos::ticksPerBar(); } @@ -235,13 +235,13 @@ void MidiClip::removeNote( Note * _note_to_del ) } -// returns a pointer to the note at specified step, or NULL if note doesn't exist - -Note * MidiClip::noteAtStep( int _step ) +// Returns a pointer to the note at specified step, or nullptr if note doesn't exist +Note * MidiClip::noteAtStep(int step) { for (const auto& note : m_notes) { - if (note->pos() == TimePos::stepPosition(_step) && note->length() < 0) + if (note->pos() == TimePos::stepPosition(step) + && note->type() == Note::Type::Step) { return note; } @@ -278,8 +278,10 @@ void MidiClip::clearNotes() Note * MidiClip::addStepNote( int step ) { - return addNote( Note( TimePos( -DefaultTicksPerBar ), - TimePos::stepPosition( step ) ), false ); + Note stepNote = Note(TimePos(DefaultTicksPerBar / 16), TimePos::stepPosition(step)); + stepNote.setType(Note::Type::Step); + + return addNote(stepNote, false); } @@ -351,15 +353,10 @@ void MidiClip::setType( Type _new_clip_type ) void MidiClip::checkType() { - for (auto& note : m_notes) - { - if (note->length() > 0) - { - setType(Type::MelodyClip); - return; - } - } - setType( Type::BeatClip ); + // If all notes are StepNotes, we have a BeatClip + const auto beatClip = std::all_of(m_notes.begin(), m_notes.end(), [](auto note) { return note->type() == Note::Type::Step; }); + + setType(beatClip ? Type::BeatClip : Type::MelodyClip); }