Simplify code for dealing with RtMidi

This commit is contained in:
Martchus 2023-10-14 21:11:37 +02:00
parent 81069a98b2
commit 9287b9b7b5
2 changed files with 49 additions and 85 deletions

View File

@ -33,8 +33,6 @@
CMidiDeviceRt::CMidiDeviceRt() CMidiDeviceRt::CMidiDeviceRt()
{ {
m_validConnection = false; m_validConnection = false;
m_midiout = nullptr;
m_midiin = nullptr;
m_midiPorts[0] = -1; m_midiPorts[0] = -1;
m_midiPorts[1] = -1; m_midiPorts[1] = -1;
m_rawDataIndex = 0; m_rawDataIndex = 0;
@ -43,39 +41,25 @@ CMidiDeviceRt::CMidiDeviceRt()
CMidiDeviceRt::~CMidiDeviceRt() CMidiDeviceRt::~CMidiDeviceRt()
{ {
if (m_midiout!=nullptr) { delete m_midiout; }
if (m_midiin!=nullptr) {delete m_midiin; }
} }
void CMidiDeviceRt::init() void CMidiDeviceRt::init()
{ {
if (m_midiin == nullptr || m_midiout == nullptr) { if (m_midiin && m_midiout) {
m_midiPorts[0] = -1; return;
m_midiPorts[1] = -1; }
m_rawDataIndex = 0; m_midiPorts[0] = -1;
if (m_midiout!=nullptr) { m_midiPorts[1] = -1;
delete m_midiout; m_rawDataIndex = 0;
m_midiout = nullptr; try {
} m_midiout = std::make_unique<RtMidiOut>();
try { } catch (RtMidiError &error) {
m_midiout = new RtMidiOut(); error.printMessage();
} }
catch(RtMidiError &error){ try {
error.printMessage(); m_midiin = std::make_unique<RtMidiIn>();
return; } catch (RtMidiError &error) {
} error.printMessage();
if (m_midiin!=nullptr) {
delete m_midiin;
m_midiin = nullptr;
}
try {
m_midiin = new RtMidiIn();
}
catch(RtMidiError &error){
error.printMessage();
return;
}
} }
} }
@ -91,31 +75,20 @@ QString CMidiDeviceRt::addIndexToString(const QString &name, int index)
QStringList CMidiDeviceRt::getMidiPortList(midiType_t type) QStringList CMidiDeviceRt::getMidiPortList(midiType_t type)
{ {
init(); init();
QStringList portNameList; auto portNameList = QStringList();
if (m_midiin == nullptr || m_midiout == nullptr) { if (!m_midiin || !m_midiout) {
return portNameList; return portNameList;
} }
unsigned int nPorts; auto *const midiDevice = type == MIDI_INPUT ? static_cast<RtMidi *>(m_midiin.get()) : static_cast<RtMidi *>(m_midiout.get());
QString name; const auto nPorts = midiDevice->getPortCount();
RtMidi* midiDevice; portNameList.reserve(nPorts);
if (type == MIDI_INPUT) for (unsigned int i = 0; i < nPorts && i < std::numeric_limits<int>::max(); ++i) {
midiDevice = m_midiin;
else
midiDevice = m_midiout;
nPorts = midiDevice->getPortCount();
for(unsigned int i=0; i< nPorts && i < std::numeric_limits<int>::max(); ++i)
{
// kotechnology creating indexed string from the post name // kotechnology creating indexed string from the post name
name = addIndexToString(midiDevice->getPortName(i).c_str(), static_cast<int>(i)); const auto name = addIndexToString(midiDevice->getPortName(i).c_str(), static_cast<int>(i));
if (name.contains("RtMidi Output Client")) if (!name.contains(QLatin1String("RtMidi Output Client")) && !name.contains(QLatin1String("RtMidi Input Client")))
continue; portNameList << name;
if (name.contains("RtMidi Input Client"))
continue;
portNameList << name;
} }
return portNameList; return portNameList;
@ -124,35 +97,24 @@ QStringList CMidiDeviceRt::getMidiPortList(midiType_t type)
bool CMidiDeviceRt::openMidiPort(midiType_t type, const QString &portName) bool CMidiDeviceRt::openMidiPort(midiType_t type, const QString &portName)
{ {
init(); init();
if (m_midiin == nullptr || m_midiout == nullptr) { if (!m_midiin || !m_midiout || portName.isEmpty()) {
return false; return false;
} }
unsigned int nPorts;
QString name;
RtMidi* midiDevice;
if (portName.isEmpty())
return false;
int dev; int dev;
if (type == MIDI_INPUT) RtMidi *midiDevice;
{ if (type == MIDI_INPUT) {
midiDevice = m_midiin; midiDevice = m_midiin.get();
dev = 0; dev = 0;
} } else {
else midiDevice = m_midiout.get();
{
midiDevice = m_midiout;
dev = 1; dev = 1;
} }
nPorts = midiDevice->getPortCount(); const auto nPorts = midiDevice->getPortCount();
for (unsigned int i = 0; i < nPorts && i <= std::numeric_limits<int>::max(); i++) {
for(unsigned int i=0; i< nPorts && i <= std::numeric_limits<int>::max(); i++)
{
// kotechnology creating indexed string from the post name // kotechnology creating indexed string from the post name
name = addIndexToString(midiDevice->getPortName(i).c_str(), static_cast<int>(i)); const auto name = addIndexToString(midiDevice->getPortName(i).c_str(), static_cast<int>(i));
if (name == portName) // Test for a match if (name == portName) // Test for a match
{ {
if (m_midiPorts[dev] >= 0) if (m_midiPorts[dev] >= 0)

View File

@ -30,11 +30,12 @@
#include "MidiDeviceBase.h" #include "MidiDeviceBase.h"
#include "rtmidi/RtMidi.h" #include "rtmidi/RtMidi.h"
class CMidiDeviceRt : public CMidiDeviceBase #include <memory>
{
class CMidiDeviceRt : public CMidiDeviceBase {
virtual void init(); virtual void init();
//! add a midi event to be played immediately //! add a midi event to be played immediately
virtual void playMidiEvent(const CMidiEvent & event); virtual void playMidiEvent(const CMidiEvent &event);
virtual int checkMidiInput(); virtual int checkMidiInput();
virtual CMidiEvent readMidiInput(); virtual CMidiEvent readMidiInput();
@ -43,30 +44,31 @@ class CMidiDeviceRt : public CMidiDeviceBase
virtual bool openMidiPort(midiType_t type, const QString &portName); virtual bool openMidiPort(midiType_t type, const QString &portName);
virtual void closeMidiPort(midiType_t type, int index); virtual void closeMidiPort(midiType_t type, int index);
virtual bool validMidiConnection() {return m_validConnection;} virtual bool validMidiConnection()
{
return m_validConnection;
}
// based on the fluid synth settings // based on the fluid synth settings
virtual int midiSettingsSetStr(const QString &name, const QString &str); virtual int midiSettingsSetStr(const QString &name, const QString &str);
virtual int midiSettingsSetNum(const QString &name, double val); virtual int midiSettingsSetNum(const QString &name, double val);
virtual int midiSettingsSetInt(const QString &name, int val); virtual int midiSettingsSetInt(const QString &name, int val);
virtual QString midiSettingsGetStr(const QString &name); virtual QString midiSettingsGetStr(const QString &name);
virtual double midiSettingsGetNum(const QString &name); virtual double midiSettingsGetNum(const QString &name);
virtual int midiSettingsGetInt(const QString &name); virtual int midiSettingsGetInt(const QString &name);
public: public:
CMidiDeviceRt(); CMidiDeviceRt();
~CMidiDeviceRt(); ~CMidiDeviceRt();
private: private:
std::unique_ptr<RtMidiOut> m_midiout;
RtMidiOut *m_midiout; std::unique_ptr<RtMidiIn> m_midiin;
RtMidiIn *m_midiin;
double m_stamp; double m_stamp;
// 0 for input, 1 for output // 0 for input, 1 for output
int m_midiPorts[2]; // select which MIDI output port to open int m_midiPorts[2]; // select which MIDI output port to open
std::vector<unsigned char> m_inputMessage; std::vector<unsigned char> m_inputMessage;
unsigned char m_savedRawBytes[40]; // Raw data is used for used for a SYSTEM_EVENT unsigned char m_savedRawBytes[40]; // Raw data is used for used for a SYSTEM_EVENT
unsigned int m_rawDataIndex; unsigned int m_rawDataIndex;