NiCMidi 1.1.0
A MIDI library derived from J.D.Koftinoff jdksmidi
metronome.h
Go to the documentation of this file.
1/*
2 * NiCMidi - A C++ Class Library for MIDI
3 *
4 * Copyright (C) 2021, 2022 Nicola Cassetta
5 * https://github.com/ncassetta/NiCMidi
6 *
7 * This file is part of NiCMidi.
8 *
9 * NiCMidi is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Lesser General Public License as
11 * published by the Free Software Foundation, either version 3 of
12 * the License, or (at your option) any later version.
13 *
14 * NiCMidi is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with NiCMidi. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23
26
27
28#ifndef _NICMIDI_METRONOME_H
29#define _NICMIDI_METRONOME_H
30
31
32#include "midi.h"
33#include "notifier.h"
34#include "tick.h"
35
36
37
47 public:
54 virtual ~Metronome() { Stop(); }
56 virtual void Reset();
58 MIDIClockTime GetCurrentMIDIClockTime() const { return cur_clock; }
60 float GetCurrentTimeMs() const;
63 unsigned int GetCurrentMeasure() const { return cur_measure; }
65 unsigned int GetCurrentBeat() const { return cur_beat; }
67 unsigned int GetTempoScale() const { return new_tempo_scale; }
69 float GetTempoWithoutScale() const { return new_tempobpm; }
71 float GetTempoWithScale() const { return new_tempobpm * new_tempo_scale * 0.01; }
73 unsigned int GetOutPort() const { return new_out_port; }
76 unsigned int GetOutChannel() const { return new_chan; }
78 unsigned char GetMeasNote() const { return new_meas_note; }
80 unsigned char GetBeatNote() const { return new_beat_note; }
82 unsigned char GetSubdNote() const { return new_subd_note; }
84 unsigned char GetSubdType() const { return new_subd_type; }
87 unsigned char GetTimeSigNumerator() const { return timesig_numerator; }
88 // TODO: Actually denominator is not implemented.
89 //unsigned char GetTimeSigDenominator() const { return timesig_denominator; }
92 bool SetTempo(float t);
96 bool SetTempoScale(unsigned int scale);
100 bool SetOutPort(unsigned int port);
104 bool SetOutChannel(unsigned int chan);
107 void SetMeasNote(unsigned char note);
109 void SetBeatNote(unsigned char note);
111 void SetSubdNote(unsigned char note);
114 bool SetSubdType(unsigned char type);
117 void SetTimeSigNumerator(unsigned char n);
118 // TODO: Actually this is not implemented: denominator is assumed 4
119 //void SetTimeSigDenominator(unsigned char d);
120
121 // Inherited from MIDITICK
122
124 virtual void Start();
126 virtual void Stop();
127
128
129 protected:
136 static void StaticTickProc(tMsecs sys_time, void* pt);
138 virtual void TickProc(tMsecs sys_time);
139
141 MIDISequencerGUINotifier* notifier; // The (optional) notifier
142 unsigned int out_port; // The out port id
143 unsigned char chan; // The MIDI channel for sound output
144 unsigned char meas_note; // The MIDI note number for the measure click (1st note of a measure)
145 unsigned char beat_note; // The MIDI note number for the ordinary beat click
146 unsigned char subd_note; // The MIDI note number for subdivision click
147 unsigned char subd_type; // Number of subdivisions (can be 2, 3, 4, 5, 6, 0 = disable subd click)
148 unsigned char timesig_numerator; // The numerator of current time signature (0 = disable measure click)
149 // TODO: actually denominator is assumed 4
150 //unsigned char timesig_denominator;// The denominator of current time signature (can be 2, 4, 8, 16)
151 float tempobpm; // The current tempo in beats per minute
152 unsigned int tempo_scale; // The tempo scale in percentage (100 = true time)
153
154 MIDIClockTime cur_clock; // The current MIDI clock in MIDI ticks from the metronme start
155 float cur_time_ms; // The current clock in milliseconds from the metronoe start
156 unsigned int cur_beat; // The current beat in the measure (1st beat is 0)
157 unsigned int cur_measure; // The current measure (1st measure is 0)
158
159 MIDIClockTime beat_length; // The duration of a beat
160 float msecs_per_beat; // Milliseconds per beat (for internal use)
161 float onoff_time; // Milliseconds between note on and off
162 float next_time_on; // The time of the next Note On message (for internal use)
163 float next_time_off; // The time of the next Note Off message (for internal use)
164
165
166 /* UNUSED ????
167 static int metronome_mode; ///< Flag affecting how metronome beat is calculated
168 MIDITimedMessage msg_beat;
169 */
170
171
172 static const MIDIClockTime QUARTER_LENGTH = DEFAULT_CLKS_PER_BEAT;
173 static const unsigned char DEFAULT_CHAN = 9;
174 static const unsigned char DEFAULT_MEAS_NOTE = 60;
175 static const unsigned char DEFAULT_BEAT_NOTE = 58;
176 static const unsigned char DEFAULT_SUBD_NOTE = 56;
177 static const unsigned char MEAS_NOTE_VEL = 120;
178 static const unsigned char BEAT_NOTE_VEL = 100;
179 static const unsigned char SUBD_NOTE_VEL = 80;
180 static const int MIN_NOTE_LEN = 20; // C++ trouble if you declare it float!
182
183 private:
184 // These are set by our set methods, and are used as buffer values, because real values are updated only at a
185 // metronome (or measure) click
186 unsigned int new_out_port;
187 unsigned char new_chan;
188 unsigned char new_meas_note;
189 unsigned char new_beat_note;
190 unsigned char new_subd_note;
191 unsigned char new_subd_type;
192 unsigned char new_timesig_numerator;
193 unsigned char new_timesig_denominator;
194 float new_tempobpm;
195 unsigned int new_tempo_scale;
196};
197
198
199#endif // METRONOME_H_INCLUDED
A pure virtual class implementing a device that can send MIDISequencerGUIEvent messages to a GUI.
Definition: notifier.h:162
A pure virtual class implementing an object which has a callback procedure to be called at every tick...
Definition: tick.h:65
A MIDITickComponent implementing a metronome.
Definition: metronome.h:46
unsigned char GetTimeSigNumerator() const
Returns the numerator of the current timesig.
Definition: metronome.h:87
bool SetOutPort(unsigned int port)
Sets the MIDI out port for the metronome clicks.
unsigned int GetCurrentBeat() const
Returns current beat in the measure (1st beat is 0).
Definition: metronome.h:65
Metronome(MIDISequencerGUINotifier *n=0)
The constructor.
unsigned int GetOutChannel() const
Returns the number of the MIDI channel assigned to the metronome.
Definition: metronome.h:76
bool SetTempoScale(unsigned int scale)
Sets the global tempo scale.
static void StaticTickProc(tMsecs sys_time, void *pt)
Implements the static method inherited from MIDITickComponent and called at every timer tick.
unsigned char GetSubdNote() const
Returns the MIDI note number for the subdivision click.
Definition: metronome.h:82
virtual void Stop()
Stops the metronome.
float GetTempoWithoutScale() const
Returns current tempo (BPM) without scaling.
Definition: metronome.h:69
void SetSubdNote(unsigned char note)
Sets the MIDI note number for the subdivision click.
unsigned char GetSubdType() const
Returns the subdivision type. It can be 0 (subd clicks disabled), 2, 3, 4, 5, 6.
Definition: metronome.h:84
virtual void Start()
Starts the metronome.
void UpdateValues()
Copies the temp values assigned by the set methods into the effective ones.
virtual void Reset()
Stops the metronome and resets it to its default values.
void SetMeasNote(unsigned char note)
Sets the MIDI note number for the measure click (the 1st beat of the measure).
unsigned char GetBeatNote() const
Returns the MIDI note number for the ordinary beat click.
Definition: metronome.h:80
void SetTimeSigNumerator(unsigned char n)
Sets the numerator of the current timesig.
unsigned int GetOutPort() const
Returns the number of the MIDI out port assigned to the metronome.
Definition: metronome.h:73
virtual ~Metronome()
The destructor. The MIDISequencerGUINotifier is not owned by the Metronome.
Definition: metronome.h:54
unsigned int GetCurrentMeasure() const
Returns current measure (1st measure is 0).
Definition: metronome.h:63
void SetBeatNote(unsigned char note)
Sets the MIDI note number for the ordinary beat click.
virtual void TickProc(tMsecs sys_time)
Implements the pure virtual method inherited from MIDITickComponent (you must not call it directly).
unsigned char GetMeasNote() const
Returns the MIDI note number for the measure click.
Definition: metronome.h:78
bool SetTempo(float t)
Sets the musical tempo.
float GetCurrentTimeMs() const
Returns current time in milliseconds from the start of the metronome.
MIDIClockTime GetCurrentMIDIClockTime() const
Returns current MIDIClockTime in MIDI ticks from the start of the metronome.
Definition: metronome.h:58
unsigned int GetTempoScale() const
Returns current tempo scale in percentage (100 = no scaling, 200 = twice faster, etc....
Definition: metronome.h:67
bool SetOutChannel(unsigned int chan)
Sets the MIDI channel for the metronome clicks.
bool SetSubdType(unsigned char type)
Sets the subdivision type.
float GetTempoWithScale() const
Returns current tempo (BPM) taking into account scaling (this is the true actual tempo).
Definition: metronome.h:71
unsigned long MIDIClockTime
The type of a variable which can hold a time in MIDI ticks.
Definition: midi.h:40
unsigned long long tMsecs
The type of a variable which can hold the elapsed time in milliseconds.
Definition: timer.h:44
const unsigned int DEFAULT_CLKS_PER_BEAT
The default clocks per beat parameter when initializing a MIDIMultiTrack.
Definition: midi.h:51
Contains the MIDI values enumerations (to have readable values instead of hexadecimal values) and som...
Contains the definition of the classes MIDISequencerGUIEvent, MIDISequencerGUINotifier (abstract),...
Contains the definition of the pure virtual class MIDITickComponent.