Simple command line MIDI metronome.
#include "metronome.h"
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDebug>
#include <QReadLocker>
#include <QStringList>
#include <QTextStream>
#include <QIODevice>
#include <QWriteLocker>
#include <QtAlgorithms>
#include <csignal>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
#define right Qt::right
#define left Qt::left
#define endl Qt::endl
#endif
QTextStream cout(stdout, QIODevice::WriteOnly);
QTextStream cerr(stderr, QIODevice::WriteOnly);
m_weak_note(METRONOME_WEAK_NOTE),
m_strong_note(METRONOME_STRONG_NOTE),
m_weak_velocity(METRONOME_VELOCITY),
m_strong_velocity(METRONOME_VELOCITY),
m_program(METRONOME_PROGRAM),
m_channel(METRONOME_CHANNEL),
m_volume(METRONOME_VOLUME),
m_pan(METRONOME_PAN),
m_resolution(METRONOME_RESOLUTION),
m_bpm(TEMPO_DEFAULT),
m_ts_num(RHYTHM_TS_NUM),
m_ts_div(RHYTHM_TS_DEN),
m_noteDuration(NOTE_DURATION),
m_portId(-1),
m_queueId(-1),
m_clientId(-1),
m_Stopped(true)
{
QString name{QStringLiteral("Metronome")};
m_Client->open();
m_Client->setClientName(name);
m_Client->setHandler(this);
m_Port->attach( m_Client );
m_Port->setPortName(name);
m_Port->setCapability( SND_SEQ_PORT_CAP_READ |
SND_SEQ_PORT_CAP_SUBS_READ |
SND_SEQ_PORT_CAP_WRITE );
m_Port->setPortType( SND_SEQ_PORT_TYPE_MIDI_GENERIC |
SND_SEQ_PORT_TYPE_APPLICATION );
m_Queue = m_Client->createQueue(name);
m_clientId = m_Client->getClientId();
m_queueId = m_Queue->getId();
m_portId = m_Port->getPortId();
m_Port->setTimestamping(true);
m_Port->setTimestampQueue(m_queueId);
TimerId best = Timer::bestGlobalTimerId();
m_Queue->setTimer(qtimer);
m_Client->setRealTimeInput(false);
m_Client->startSequencerInput();
}
Metronome::~Metronome()
{
m_Port->detach();
m_Client->close();
}
{
metronome_pattern(
static_cast<int>(ev->
getTick()) + m_patternDuration);
delete ev;
}
{
ev->
setSource(
static_cast<unsigned char>(m_portId));
m_Client->outputDirect(ev);
}
void Metronome::sendControlChange(int cc, int value)
{
metronome_event_output(&ev);
}
void Metronome::sendInitialControls()
{
metronome_set_program();
metronome_set_controls();
metronome_set_tempo();
}
void Metronome::metronome_set_program()
{
metronome_event_output(&ev);
}
void Metronome::metronome_schedule_event(
SequencerEvent* ev,
int tick,
bool lb)
{
ev->
setSource(
static_cast<unsigned char>(m_portId));
if (lb)
ev->
setDestination(
static_cast<unsigned char>(m_clientId),
static_cast<unsigned char>(m_portId));
else
m_Client->outputDirect(ev);
}
void Metronome::metronome_note(int note, int tick)
{
NoteEvent ev(m_channel, note, METRONOME_VELOCITY, m_noteDuration);
metronome_schedule_event(&ev, tick, false);
}
void Metronome::metronome_echo(int tick)
{
metronome_schedule_event(&ev, tick, true);
}
void Metronome::metronome_pattern(int tick)
{
int j, t, duration;
t = tick;
duration = m_resolution * 4 / m_ts_div;
for (j = 0; j < m_ts_num; j++) {
metronome_note(j ? m_weak_note : m_strong_note, t);
t += duration;
}
metronome_echo(t);
}
void Metronome::metronome_set_tempo()
{
m_Queue->setTempo(t);
m_Client->drainOutput();
}
void Metronome::metronome_set_controls()
{
sendControlChange(MIDI_CTL_MSB_MAIN_VOLUME, m_volume);
sendControlChange(MIDI_CTL_MSB_PAN, m_pan);
}
void Metronome::subscribe(const QString& portName)
{
m_Port->subscribeTo(portName);
}
bool Metronome::stopped()
{
QReadLocker locker(&m_mutex);
return m_Stopped;
}
void Metronome::stop()
{
QWriteLocker locker(&m_mutex);
m_Stopped = true;
m_Client->dropOutput();
}
void Metronome::shutupSound()
{
sendControlChange( MIDI_CTL_ALL_NOTES_OFF, 0 );
sendControlChange( MIDI_CTL_ALL_SOUNDS_OFF, 0 );
}
void Metronome::play(QString tempo)
{
bool ok;
m_Stopped = false;
m_patternDuration = m_resolution * 4 / m_ts_div * m_ts_num;
m_bpm = tempo.toInt(&ok);
if (!ok) m_bpm = TEMPO_DEFAULT;
cout << "Metronome playing. " << m_bpm << " bpm" << endl;
cout << "Press Ctrl+C to exit" << endl;
try {
sendInitialControls();
m_Queue->start();
metronome_pattern(0);
metronome_pattern(m_patternDuration);
while (!stopped())
sleep(1);
cerr <<
"SequencerError exception. Error code: " << err.
code()
cerr <<
"Location: " << err.
location() << endl;
}
}
static Metronome* metronome = nullptr;
void signalHandler(int sig)
{
if (sig == SIGINT)
qDebug() << "Caught a SIGINT. Exiting";
else if (sig == SIGTERM)
qDebug() << "Caught a SIGTERM. Exiting";
if (metronome != nullptr) {
metronome->stop();
metronome->shutupSound();
}
}
int main(int argc, char **argv)
{
const QString PGM_NAME = QStringLiteral("drumstick-metronome");
const QString PGM_DESCRIPTION = QStringLiteral("ALSA based command line metronome");
const QString ERRORSTR = QStringLiteral("Fatal error from the ALSA sequencer. "
"This usually happens when the kernel doesn't have ALSA support, "
"or the device node (/dev/snd/seq) doesn't exists, "
"or the kernel module (snd_seq) is not loaded. "
"Please check your ALSA/MIDI configuration.");
signal(SIGINT, signalHandler);
signal(SIGTERM, signalHandler);
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName(PGM_NAME);
QCoreApplication::setApplicationVersion(QStringLiteral(QT_STRINGIFY(VERSION)));
QCommandLineParser parser;
parser.setApplicationDescription(PGM_DESCRIPTION);
auto helpOption = parser.addHelpOption();
auto versionOption = parser.addVersionOption();
QCommandLineOption portOption({"p","port"}, "Destination, MIDI port identifier.", "client:port");
parser.addOption(portOption);
QCommandLineOption bpmOption({"b","bpm"}, "Tempo, in beats per minute (default=120).", "BPM", "120");
parser.addOption(bpmOption);
parser.process(app);
if (parser.isSet(versionOption) || parser.isSet(helpOption)) {
return 0;
}
try {
metronome = new Metronome();
if (parser.isSet(portOption)) {
QString port = parser.value(portOption);
metronome->subscribe(port);
} else {
cerr << "Destination Port is mandatory" << endl;
parser.showHelp();
}
QString bpm("120");
if (parser.isSet(bpmOption)) {
bpm = parser.value(bpmOption);
}
metronome->play(bpm);
cerr << ERRORSTR <<
" Returned error was: " << ex.
qstrError() << endl;
} catch (...) {
cerr << ERRORSTR << endl;
}
delete metronome;
return 0;
}
Classes managing ALSA Timers.
Event representing a MIDI control change event.
Class representing a note event with duration.
Event representing a MIDI program change event.
void setPPQ(int value)
Sets the queue resolution in parts per quarter note.
unsigned int getTempo()
Gets the queue's tempo in microseconds per beat.
void setNominalBPM(float value)
Sets the queue's nominal tempo in BPM (beats per minute).
void setId(snd_timer_id_t *value)
Sets the timer identifier record.
Exception class for ALSA Sequencer errors.
void scheduleTick(const int queue, const int tick, const bool relative)
Sets the event to be scheduled in musical time (ticks) units.
void setDirect()
Sets the event to be immediately delivered, not queued/scheduled.
snd_seq_tick_time_t getTick() const
Gets the tick time of the event.
void setSubscribers()
Sets the event's destination to be all the subscribers of the source port.
void setDestination(const unsigned char client, const unsigned char port)
Sets the client:port destination of the event.
void setSource(const unsigned char port)
Sets the event's source port ID.
snd_seq_event_type_t getSequencerType() const
Gets the sequencer event type.
ALSA Timer identifier container.
QString qstrError() const
Gets the human readable error message from the error code.
int code() const
Gets the numeric error code.
const QString & location() const
Gets the location of the error code as provided in the constructor.
Drumstick ALSA library wrapper.
SequencerError Exception class.