libdrmconf 0.12.1
A library to program DMR radios.
Loading...
Searching...
No Matches
melody.hh
1#ifndef MELODY_HH
2#define MELODY_HH
3
4#include "configobject.hh"
5#include <QPair>
6
19class Melody : public ConfigItem
20{
21 Q_OBJECT
22
24 Q_PROPERTY(unsigned int bpm READ bpm WRITE setBPM)
26 Q_PROPERTY(QString melody READ toLilypond WRITE fromLilypond)
27
28public:
30 struct Note {
32 enum class Tone {
33 Rest, C, Cis, D, Dis, E, F, Fis, G, Gis, A, Ais, B
34 };
36 enum class Duration {
37 Whole, Half, Quarter, Eighth, Sixteenth
38 };
39
45 bool dotted;
47 int octave;
48
50 Note();
51
53 bool fromLilypond(const QString &note, Duration currentDuration);
54
56 QString toLilypond(Duration currentDuration) const;
57
59 QPair<double, unsigned int> toTone(unsigned int bpm) const;
60
64 unsigned int infer(double frequency, unsigned int ms, unsigned int bpm);
65
67 static unsigned int quantizationTimingError(unsigned int ms, unsigned int bpm);
68 };
69
71 typedef QVector<Note>::iterator iterator;
73 typedef QVector<Note>::const_iterator const_iterator;
74
75public:
77 Melody(unsigned int bpm=100, QObject *parent = nullptr);
78
79 ConfigItem *clone() const;
80 bool copy(const ConfigItem &other);
81
85 iterator end();
87 const_iterator begin() const;
89 const_iterator end() const;
90
92 size_t count() const;
94 const Note &operator [](size_t index) const;
96 Note &operator [](size_t index);
97
99 unsigned int bpm() const;
101 void setBPM(unsigned int bpm);
102
109 bool fromLilypond(const QString &melody);
111 QString toLilypond() const;
112
114 QVector<QPair<double, unsigned int>> toTones() const;
116 bool infer(const QVector<QPair<double, unsigned int>> &tones);
117
118protected:
120 static unsigned int quantizationTimingError(
121 const QVector<QPair<double, unsigned int>> &tones, unsigned int bpm);
122
123protected:
125 unsigned int _bpm;
127 QVector<Note> _melody;
128};
129
130#endif // MELODY_HH
Base class for all configuration objects (channels, zones, contacts, etc).
Definition configobject.hh:40
A config item that encodes a melody.
Definition melody.hh:20
static unsigned int quantizationTimingError(const QVector< QPair< double, unsigned int > > &tones, unsigned int bpm)
Computes the absolute quantization timing error over the given melody for the given BPM.
Definition melody.cc:144
QVector< Note > _melody
The actual melody.
Definition melody.hh:127
bool copy(const ConfigItem &other)
Copies the given item into this one.
Definition melody.cc:27
QString toLilypond() const
Serializes the melody into Lilypond notation.
Definition melody.cc:101
iterator end()
Returns an iterator pointing right after the last note.
Definition melody.cc:44
void setBPM(unsigned int bpm)
Sets the BPM of the melody.
Definition melody.cc:77
Melody(unsigned int bpm=100, QObject *parent=nullptr)
Empty constructor.
Definition melody.cc:10
QVector< QPair< double, unsigned int > > toTones() const
Converts the melody to a series of tones in terms of frequency and duration in ms.
Definition melody.cc:112
bool infer(const QVector< QPair< double, unsigned int > > &tones)
Infer melody from a vector of frequeny-duration pairs.
Definition melody.cc:121
iterator begin()
Returns an iterator pointing at the first note.
Definition melody.cc:40
bool fromLilypond(const QString &melody)
Parses the Lilypond notation of the melody.
Definition melody.cc:85
QVector< Note >::const_iterator const_iterator
Const iterator over notes.
Definition melody.hh:73
const Note & operator[](size_t index) const
Element access.
Definition melody.cc:63
unsigned int bpm
The BPM of the melody.
Definition melody.hh:24
QString melody
The melody in LilyPond notation.
Definition melody.hh:26
QVector< Note >::iterator iterator
Iterator over notes.
Definition melody.hh:71
unsigned int _bpm
Holds the beats per minute.
Definition melody.hh:125
size_t count() const
Returns the number of notes (and rests) of this melody.
Definition melody.cc:58
ConfigItem * clone() const
Clones this item.
Definition melody.cc:17
Encodes a note, that is tone and duration.
Definition melody.hh:30
Duration
Note durations as fractions of a bar.
Definition melody.hh:36
Duration duration
The note duration.
Definition melody.hh:43
int octave
The octave of the note, 0 means middle.
Definition melody.hh:47
bool dotted
If true, the note/rest is dottet.
Definition melody.hh:45
Tone
Possible tone values.
Definition melody.hh:32
Tone tone
The note tone.
Definition melody.hh:41