Skip to content

Commit

Permalink
Better cleanup of Windows MIDI input plugin
Browse files Browse the repository at this point in the history
related to #30 and also #19 / #20
  • Loading branch information
pedrolcl committed Dec 15, 2024
1 parent 5e55ded commit d449688
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 46 deletions.
55 changes: 25 additions & 30 deletions library/rt-backends/win-in/winmidiinput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include <QString>
#include <QMap>
#include <QDebug>
#include <windows.h>
#include <mmsystem.h>

Expand Down Expand Up @@ -75,7 +74,6 @@ namespace rt {

~WinMIDIInputPrivate()
{
qDebug() << Q_FUNC_INFO << m_publicName;
close();
}

Expand All @@ -92,32 +90,29 @@ namespace rt {
if (res != MMSYSERR_NOERROR) {
logError("midiInOpen()",res);
} else {
/* Double buffering configuration, to manage Sysexes */
/* Store pointer to our input buffer for System Exclusive messages in MIDIHDR */
inMidiHeader.lpData = (LPSTR)&inSysexBuf[0];

/* Double buffering configuration, to manage Sysexes */
/* Store pointer to our input buffer for System Exclusive messages in MIDIHDR */
inMidiHeader.lpData = (LPSTR)&inSysexBuf[0];
/* Store its size in the MIDIHDR */
inMidiHeader.dwBufferLength = SYSEX_BUF_SIZE;

/* Store its size in the MIDIHDR */
inMidiHeader.dwBufferLength = SYSEX_BUF_SIZE;
/* Flags must be set to 0 */
inMidiHeader.dwFlags = 0;

/* Flags must be set to 0 */
inMidiHeader.dwFlags = 0;

res = midiInPrepareHeader(m_handle, &inMidiHeader, sizeof(MIDIHDR));
if (res != MMSYSERR_NOERROR){
qDebug() << "Error preparing MIDI input header. result=" << res;
}

res = midiInAddBuffer(m_handle, &inMidiHeader, sizeof(MIDIHDR));
if (res != MMSYSERR_NOERROR){
qDebug() << "Error adding MIDI input buffer. result=" << res;
}
/*** ***/
res = midiInPrepareHeader(m_handle, &inMidiHeader, sizeof(MIDIHDR));
if (res != MMSYSERR_NOERROR){
logError("midiInPrepareHeader()", res);
}

res = midiInAddBuffer(m_handle, &inMidiHeader, sizeof(MIDIHDR));
if (res != MMSYSERR_NOERROR){
logError("midiInAddBuffer()", res);
}

res = midiInStart(m_handle);
if (res != MMSYSERR_NOERROR) {
logError("midiInStart()",res);
logError("midiInStart()", res);
}
}
m_currentInput = conn;
Expand All @@ -127,23 +122,22 @@ namespace rt {
}

void close() {
qDebug() << Q_FUNC_INFO;
MMRESULT res;
m_status = false;
m_diagnostics.clear();
if (m_handle != nullptr) {
res = midiInUnprepareHeader(m_handle, &inMidiHeader, sizeof(MIDIHDR));
if (res != MMSYSERR_NOERROR)
logError("midiInUnprepareHeader()", res);
res = midiInStop(m_handle);
if (res != MMSYSERR_NOERROR)
logError("midiInStop()", res);
qDebug() << "midiInStop() OK";
res = midiInReset( m_handle );
if (res != MMSYSERR_NOERROR)
logError("midiInReset()", res);
qDebug() << "midiInReset() OK";
res = midiInClose( m_handle );
if (res != MMSYSERR_NOERROR)
logError("midiInClose()", res);
qDebug() << "midiInClose() OK";
m_handle = nullptr;
}
m_currentInput = MIDIConnection();
Expand Down Expand Up @@ -281,7 +275,7 @@ namespace rt {
object->m_diagnostics << "Errors input";
break;
case MIM_LONGDATA:
{
{
object->m_diagnostics << "Sysex data input";
// Extract MIDI SysEx message information
MIDIHDR* pMidiHdr = reinterpret_cast<MIDIHDR*>(dwParam1);
Expand All @@ -292,9 +286,11 @@ namespace rt {
QByteArray sysExByteArray(reinterpret_cast<const char*>(pMidiHdr->lpData), static_cast<int>(pMidiHdr->dwBytesRecorded));
object->emitSysex(sysExByteArray);
}

/* Queue the MIDIHDR for more input */
midiInAddBuffer(hMidiIn, pMidiHdr, sizeof(MIDIHDR));

if (object->m_status) {
/* Queue the MIDIHDR for more input */
midiInAddBuffer(hMidiIn, pMidiHdr, sizeof(MIDIHDR));
}
break;
}
case MIM_DATA:
Expand All @@ -318,7 +314,6 @@ namespace rt {

WinMIDIInput::~WinMIDIInput()
{
qDebug() << Q_FUNC_INFO;
delete d;
}

Expand Down
2 changes: 0 additions & 2 deletions library/rt-backends/win-out/winmidioutput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ namespace rt {

~WinMIDIOutputPrivate()
{
qDebug() << Q_FUNC_INFO;
close();
}

Expand Down Expand Up @@ -252,7 +251,6 @@ namespace rt {

WinMIDIOutput::~WinMIDIOutput()
{
qDebug() << Q_FUNC_INFO;
delete d;
}

Expand Down
20 changes: 10 additions & 10 deletions library/rt/backendmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,11 @@ namespace drumstick { namespace rt {

void clearLists()
{
qDebug() << Q_FUNC_INFO << "loaders:" << m_loaders.count()
<< "inputs:" << m_inputsList.count() << "outputs:" << m_outputsList.count();
// qDebug() << Q_FUNC_INFO << "loaders:" << m_loaders.count()
// << "inputs:" << m_inputsList.count() << "outputs:" << m_outputsList.count();
while (!m_loaders.empty()) {
QPluginLoader* pluginLoader = m_loaders.takeFirst();
qDebug() << "unloading:" << pluginLoader->fileName();
//qDebug() << "unloading:" << pluginLoader->fileName();
pluginLoader->unload();
delete pluginLoader;
}
Expand Down Expand Up @@ -112,7 +112,7 @@ namespace drumstick { namespace rt {
BackendManager::BackendManager()
: d{new BackendManagerPrivate}
{
qDebug() << Q_FUNC_INFO;
//qDebug() << Q_FUNC_INFO;
QVariantMap defaultSettings {
{ QSTR_DRUMSTICKRT_PUBLICNAMEIN, QStringLiteral("MIDI In")},
{ QSTR_DRUMSTICKRT_PUBLICNAMEOUT, QStringLiteral("MIDI Out")}
Expand All @@ -125,7 +125,7 @@ namespace drumstick { namespace rt {
*/
BackendManager::~BackendManager()
{
qDebug() << Q_FUNC_INFO;
//qDebug() << Q_FUNC_INFO;
}

/**
Expand Down Expand Up @@ -181,7 +181,7 @@ namespace drumstick { namespace rt {
*/
void BackendManager::refresh(QSettings *settings)
{
qDebug() << Q_FUNC_INFO;
//qDebug() << Q_FUNC_INFO;
QVariantMap tmpMap;
settings->beginGroup(QSTR_DRUMSTICKRT_GROUP);
const QStringList allKeys = settings->allKeys();
Expand Down Expand Up @@ -213,7 +213,7 @@ namespace drumstick { namespace rt {
names << (name_out.isEmpty() ? QStringLiteral("MIDI Out") : name_out);
paths << defaultPaths();

qDebug() << Q_FUNC_INFO << "names:" << names << "paths:" << paths;
//qDebug() << Q_FUNC_INFO << "names:" << names << "paths:" << paths;

// Dynamic backends
foreach(const QString& dir, paths) {
Expand All @@ -222,13 +222,13 @@ namespace drumstick { namespace rt {
auto absolutePath = pluginsDir.absoluteFilePath(fileName);
if (QLibrary::isLibrary(absolutePath) && d->isLoaderNeeded(absolutePath)) {
QPluginLoader *loader = new QPluginLoader(absolutePath);
qDebug() << "plugin loader created:" << loader->fileName();
//qDebug() << "plugin loader created:" << loader->fileName();
d->m_loaders << loader;
QObject *obj = loader->instance();
if (obj != nullptr) {
MIDIInput *input = qobject_cast<MIDIInput *>(obj);
if (input != nullptr && !d->m_inputsList.contains(input)) {
qDebug() << "input plugin instantiated:" << name_in;
//qDebug() << "input plugin instantiated:" << name_in;
if (!name_in.isEmpty()) {
input->setPublicName(name_in);
}
Expand All @@ -237,7 +237,7 @@ namespace drumstick { namespace rt {
} else {
MIDIOutput *output = qobject_cast<MIDIOutput *>(obj);
if (output != nullptr && !d->m_outputsList.contains(output)) {
qDebug() << "output plugin instantiated:" << name_out;
//qDebug() << "output plugin instantiated:" << name_out;
if (!name_out.isEmpty()) {
output->setPublicName(name_out);
}
Expand Down
4 changes: 0 additions & 4 deletions utils/vpiano/vpiano.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QFontDialog>
Expand Down Expand Up @@ -107,16 +106,13 @@ VPiano::VPiano(QWidget *parent, Qt::WindowFlags flags)

VPiano::~VPiano()
{
qDebug() << Q_FUNC_INFO;
m_midiIn->close();
m_midiOut->close();
delete m_manager;
}

void VPiano::initialize()
{
qDebug() << Q_FUNC_INFO;

readSettings();

m_manager->refresh(VPianoSettings::instance()->settingsMap());
Expand Down

0 comments on commit d449688

Please sign in to comment.