mirror of
https://github.com/LMMS/lmms.git
synced 2025-12-30 01:58:01 -05:00
* 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>
287 lines
5.6 KiB
C++
287 lines
5.6 KiB
C++
/*
|
|
* Note.h - declaration of class note which contains all informations about a
|
|
* note + definitions of several constants and enums
|
|
*
|
|
* Copyright (c) 2004-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
|
|
*
|
|
* This file is part of LMMS - https://lmms.io
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2 of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public
|
|
* License along with this program (see COPYING); if not, write to the
|
|
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
* Boston, MA 02110-1301 USA.
|
|
*
|
|
*/
|
|
|
|
#ifndef LMMS_NOTE_H
|
|
#define LMMS_NOTE_H
|
|
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
#include "volume.h"
|
|
#include "panning.h"
|
|
#include "SerializingObject.h"
|
|
#include "TimePos.h"
|
|
|
|
|
|
namespace lmms
|
|
{
|
|
|
|
|
|
class DetuningHelper;
|
|
|
|
|
|
enum class Key : int
|
|
{
|
|
C = 0,
|
|
Cis = 1, Des = 1,
|
|
D = 2,
|
|
Dis = 3, Es = 3,
|
|
E = 4, Fes = 4,
|
|
F = 5,
|
|
Fis = 6, Ges = 6,
|
|
G = 7,
|
|
Gis = 8, As = 8,
|
|
A = 9,
|
|
Ais = 10, B = 10,
|
|
H = 11
|
|
} ;
|
|
|
|
|
|
enum class Octave : int
|
|
{
|
|
Octave_m1, // MIDI standard starts at C-1
|
|
Octave_0,
|
|
Octave_1,
|
|
Octave_2,
|
|
Octave_3,
|
|
Octave_4,
|
|
Octave_5,
|
|
Octave_6,
|
|
Octave_7,
|
|
Octave_8,
|
|
Octave_9, // incomplete octave, MIDI only goes up to G9
|
|
};
|
|
|
|
const int FirstOctave = -1;
|
|
const int KeysPerOctave = 12;
|
|
|
|
constexpr inline auto operator+(Octave octave, Key key) -> int
|
|
{
|
|
return static_cast<int>(octave) * KeysPerOctave + static_cast<int>(key);
|
|
}
|
|
|
|
constexpr auto DefaultOctave = Octave::Octave_4;
|
|
const int DefaultKey = DefaultOctave + Key::A;
|
|
//! Number of physical keys, limited to MIDI range (valid for both MIDI 1.0 and 2.0)
|
|
const int NumKeys = 128;
|
|
|
|
const int DefaultMiddleKey = Octave::Octave_4 + Key::C;
|
|
const int DefaultBaseKey = Octave::Octave_4 + Key::A;
|
|
const float DefaultBaseFreq = 440.f;
|
|
|
|
const float MaxDetuning = 5 * 12.0f;
|
|
|
|
|
|
|
|
class LMMS_EXPORT Note : public SerializingObject
|
|
{
|
|
public:
|
|
Note( const TimePos & length = TimePos( 0 ),
|
|
const TimePos & pos = TimePos( 0 ),
|
|
int key = DefaultKey,
|
|
volume_t volume = DefaultVolume,
|
|
panning_t panning = DefaultPanning,
|
|
DetuningHelper * detuning = nullptr );
|
|
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; }
|
|
inline void setOldPos( const TimePos & oldPos ) { m_oldPos = oldPos; }
|
|
|
|
inline void setOldLength( const TimePos & oldLength )
|
|
{
|
|
m_oldLength = oldLength;
|
|
}
|
|
inline void setIsPlaying( const bool isPlaying )
|
|
{
|
|
m_isPlaying = isPlaying;
|
|
}
|
|
|
|
|
|
void setLength( const TimePos & length );
|
|
void setPos( const TimePos & pos );
|
|
void setKey( const int key );
|
|
virtual void setVolume( volume_t volume );
|
|
virtual void setPanning( panning_t panning );
|
|
void quantizeLength( const int qGrid );
|
|
void quantizePos( const int qGrid );
|
|
|
|
static inline bool lessThan( const Note * lhs, const Note * rhs )
|
|
{
|
|
// function to compare two notes - must be called explictly when
|
|
// using qSort
|
|
if( (int)( *lhs ).pos() < (int)( *rhs ).pos() )
|
|
{
|
|
return true;
|
|
}
|
|
else if( (int)( *lhs ).pos() > (int)( *rhs ).pos() )
|
|
{
|
|
return false;
|
|
}
|
|
return ( (int)( *lhs ).key() > (int)( *rhs ).key() );
|
|
}
|
|
|
|
inline bool selected() const
|
|
{
|
|
return m_selected;
|
|
}
|
|
|
|
inline int oldKey() const
|
|
{
|
|
return m_oldKey;
|
|
}
|
|
|
|
inline TimePos oldPos() const
|
|
{
|
|
return m_oldPos;
|
|
}
|
|
|
|
inline TimePos oldLength() const
|
|
{
|
|
return m_oldLength;
|
|
}
|
|
|
|
inline bool isPlaying() const
|
|
{
|
|
return m_isPlaying;
|
|
}
|
|
|
|
inline TimePos endPos() const
|
|
{
|
|
const int l = length();
|
|
return pos() + l;
|
|
}
|
|
|
|
inline const TimePos & length() const
|
|
{
|
|
return m_length;
|
|
}
|
|
|
|
inline const TimePos & pos() const
|
|
{
|
|
return m_pos;
|
|
}
|
|
|
|
inline TimePos pos( TimePos basePos ) const
|
|
{
|
|
const int bp = basePos;
|
|
return m_pos - bp;
|
|
}
|
|
|
|
inline int key() const
|
|
{
|
|
return m_key;
|
|
}
|
|
|
|
inline volume_t getVolume() const
|
|
{
|
|
return m_volume;
|
|
}
|
|
|
|
int midiVelocity( int midiBaseVelocity ) const
|
|
{
|
|
return std::min(MidiMaxVelocity, getVolume() * midiBaseVelocity / DefaultVolume);
|
|
}
|
|
|
|
inline panning_t getPanning() const
|
|
{
|
|
return m_panning;
|
|
}
|
|
|
|
static QString classNodeName()
|
|
{
|
|
return "note";
|
|
}
|
|
|
|
inline QString nodeName() const override
|
|
{
|
|
return classNodeName();
|
|
}
|
|
|
|
static TimePos quantized( const TimePos & m, const int qGrid );
|
|
|
|
DetuningHelper * detuning() const
|
|
{
|
|
return m_detuning;
|
|
}
|
|
bool hasDetuningInfo() const;
|
|
bool withinRange(int tickStart, int tickEnd) const;
|
|
|
|
void createDetuning();
|
|
|
|
|
|
protected:
|
|
void saveSettings( QDomDocument & doc, QDomElement & parent ) override;
|
|
void loadSettings( const QDomElement & _this ) override;
|
|
|
|
|
|
private:
|
|
// for piano roll editing
|
|
bool m_selected;
|
|
int m_oldKey;
|
|
TimePos m_oldPos;
|
|
TimePos m_oldLength;
|
|
bool m_isPlaying;
|
|
|
|
int m_key;
|
|
volume_t m_volume;
|
|
panning_t m_panning;
|
|
TimePos m_length;
|
|
TimePos m_pos;
|
|
DetuningHelper * m_detuning;
|
|
|
|
Type m_type = Type::Regular;
|
|
};
|
|
|
|
using NoteVector = std::vector<Note*>;
|
|
|
|
struct NoteBounds
|
|
{
|
|
TimePos start;
|
|
TimePos end;
|
|
int lowest;
|
|
int highest;
|
|
};
|
|
|
|
|
|
std::optional<NoteBounds> boundsForNotes(const NoteVector& notes);
|
|
|
|
|
|
} // namespace lmms
|
|
|
|
#endif // LMMS_NOTE_H
|