Skip to content

Commit

Permalink
Implement Note Types (LMMS#5902)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>
  • Loading branch information
IanCaio and messmerd committed Nov 18, 2023
1 parent 0255704 commit 17c9198
Show file tree
Hide file tree
Showing 15 changed files with 170 additions and 103 deletions.
1 change: 1 addition & 0 deletions data/themes/classic/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
1 change: 1 addition & 0 deletions data/themes/default/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion include/DataFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,9 @@ class LMMS_EXPORT DataFile : public QDomDocument
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<UpgradeMethod> UPGRADE_METHODS;
Expand Down
12 changes: 12 additions & 0 deletions include/Note.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ class LMMS_EXPORT Note : public SerializingObject
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; }
Expand Down Expand Up @@ -253,6 +263,8 @@ class LMMS_EXPORT Note : public SerializingObject
TimePos m_length;
TimePos m_pos;
DetuningHelper * m_detuning;

Type m_type = Type::Regular;
};

using NoteVector = std::vector<Note*>;
Expand Down
2 changes: 2 additions & 0 deletions include/PianoRoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -466,6 +467,7 @@ protected slots:
QColor m_lineColor;
QColor m_noteModeColor;
QColor m_noteColor;
QColor m_stepNoteColor;
QColor m_noteTextColor;
QColor m_ghostNoteColor;
QColor m_ghostNoteTextColor;
Expand Down
7 changes: 5 additions & 2 deletions plugins/MidiExport/MidiExport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include "MidiExport.h"

#include "Engine.h"
#include "TrackContainer.h"
#include "DataFile.h"
#include "InstrumentTrack.h"
Expand Down Expand Up @@ -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::Type>(note.attribute("type", "0").toInt());
midiClip.push_back(mnote);
}
}
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions plugins/MidiExport/MidiExport.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

#include "ExportFilter.h"
#include "MidiFile.hpp"
#include "Note.h"

class QDomNode;

Expand All @@ -46,6 +47,7 @@ struct MidiNote
uint8_t pitch;
int duration;
uint8_t volume;
Note::Type type;

inline bool operator<(const MidiNote &b) const
{
Expand All @@ -63,6 +65,16 @@ class MidiExport: public ExportFilter
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;
Expand Down
21 changes: 20 additions & 1 deletion src/core/DataFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -81,7 +82,7 @@ const std::vector<DataFile::UpgradeMethod> 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.
Expand Down Expand Up @@ -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<int>(Note::Type::Step));
}
}
}


/** \brief Note range has been extended to match MIDI specification
*
Expand Down
7 changes: 6 additions & 1 deletion src/core/Note.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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<int>(m_type));

if( m_detuning && m_length )
{
Expand All @@ -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<int>(Type::Regular));
m_type = static_cast<Type>(_this.attribute("type", "0").toInt());

if( _this.hasChildNodes() )
{
Expand Down
2 changes: 1 addition & 1 deletion src/core/NotePlayHandle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down
13 changes: 7 additions & 6 deletions src/core/TimePos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#include "TimePos.h"

#include <cassert>
#include "MeterModel.h"

namespace lmms
Expand Down Expand Up @@ -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<f_cnt_t>( 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<f_cnt_t>(m_ticks * framesPerTick);
}

double TimePos::getTimeInMilliseconds( bpm_t beatsPerMinute ) const
Expand Down Expand Up @@ -221,4 +222,4 @@ double TimePos::ticksToMilliseconds(double ticks, bpm_t beatsPerMinute)
}


} // namespace lmms
} // namespace lmms
Loading

0 comments on commit 17c9198

Please sign in to comment.