libdrmconf  0.11.3
A library to program DMR radios.
melody.hh
1 #ifndef MELODY_HH
2 #define MELODY_HH
3 
4 #include "configobject.hh"
5 #include <QPair>
6 
19 class 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 
28 public:
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 
75 public:
77  Melody(unsigned int bpm=100, QObject *parent = nullptr);
78 
79  ConfigItem *clone() const;
80  bool copy(const ConfigItem &other);
81 
83  iterator begin();
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 
118 protected:
120  static unsigned int quantizationTimingError(
121  const QVector<QPair<double, unsigned int>> &tones, unsigned int bpm);
122 
123 protected:
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
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
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
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
const Note & operator[](size_t index) const
Element access.
Definition: melody.cc:63
bool infer(const QVector< QPair< double, unsigned int >> &tones)
Infer melody from a vector of frequeny-duration pairs.
Definition: melody.cc:121
unsigned int bpm
The BPM of the melody.
Definition: melody.hh:24
QVector< Note >::const_iterator const_iterator
Const iterator over notes.
Definition: melody.hh:73
QString melody
The melody in LilyPond notation.
Definition: melody.hh:26
unsigned int _bpm
Holds the beats per minute.
Definition: melody.hh:125
QVector< Note >::iterator iterator
Iterator over notes.
Definition: melody.hh:71
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
QPair< double, unsigned int > toTone(unsigned int bpm) const
Converts the note to a frequency in Hz and duration in seconds.
Definition: melody.cc:261
QString toLilypond(Duration currentDuration) const
Serializes the note in Lilypond notation.
Definition: melody.cc:221
Duration duration
The note duration.
Definition: melody.hh:43
static unsigned int quantizationTimingError(unsigned int ms, unsigned int bpm)
Computes the quantization timing error for the given duration and BPM.
Definition: melody.cc:356
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
Note()
Default constructor.
Definition: melody.cc:158
bool fromLilypond(const QString &note, Duration currentDuration)
Reads a note in Lilypond notation.
Definition: melody.cc:165
Tone
Possible tone values.
Definition: melody.hh:32
unsigned int infer(double frequency, unsigned int ms, unsigned int bpm)
Infers the note from the given frequency and duration in ms.
Definition: melody.cc:302
Tone tone
The note tone.
Definition: melody.hh:41