Skip to content

Commit

Permalink
fixed vpiano
Browse files Browse the repository at this point in the history
fixed backendmanager usage in vpiano.
optization in BackendManager::refresh() to keep plugins loader.
correction of commit 1befe4f
preparation for GH issue #30
  • Loading branch information
pedrolcl committed Dec 14, 2024
1 parent 1befe4f commit 5e55ded
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 39 deletions.
64 changes: 39 additions & 25 deletions library/rt/backendmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,56 +49,70 @@ namespace drumstick { namespace rt {

class BackendManager::BackendManagerPrivate {
public:
QList<MIDIInput*> m_inputsList;
QList<MIDIOutput*> m_outputsList;
QList<QPluginLoader*> m_loaders;
QList<QPluginLoader *> m_loaders;
QList<MIDIInput *> m_inputsList;
QList<MIDIOutput *> m_outputsList;

QString m_inputBackend{QLatin1String("Network")};
#if defined(Q_OS_LINUX)
#if defined(Q_OS_LINUX)
QStringList m_outputBackends{QLatin1String("SonivoxEAS"),QLatin1String("FluidSynth"),QLatin1String("ALSA")};
#elif defined(Q_OS_DARWIN)
#elif defined(Q_OS_DARWIN)
QStringList m_outputBackends{QLatin1String("DLS Synth"),QLatin1String("FluidSynth"),QLatin1String("CoreMIDI")};
#elif defined(Q_OS_WINDOWS)
#elif defined(Q_OS_WINDOWS)
QStringList m_outputBackends{QLatin1String("Windows MM"),QLatin1String("FluidSynth")};
#elif defined(Q_OS_UNIX)
#elif defined(Q_OS_UNIX)
QStringList m_outputBackends{QLatin1String("FluidSynth"),QLatin1String("OSS")};
#else
#else
QStringList m_outputBackends{m_inputBackend};
#endif
#endif

~BackendManagerPrivate()
{
clearLists();
}

void clearLists()
{
qDebug() << Q_FUNC_INFO << "loaders:" << m_loaders.count();
while(!m_loaders.empty()) {
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();
pluginLoader->unload();
delete pluginLoader;
}
qDebug() << "inputs:" << m_inputsList.count() << "outputs:" << m_outputsList.count();
m_inputsList.clear();
m_outputsList.clear();
m_loaders.clear();
}
void appendDir(const QString& candidate, QStringList& result)

void appendDir(const QString &candidate, QStringList &result)
{
QDir checked(candidate.trimmed());
//qDebug() << Q_FUNC_INFO << candidate << "exists:" << checked.exists();
if (checked.exists() && !result.contains(checked.absolutePath())) {
result << checked.absolutePath();
}
}

bool isLoaderNeeded(const QString &fileName)
{
auto it = std::find_if(m_loaders.constBegin(),
m_loaders.constEnd(),
[=](QPluginLoader *loader) {
return loader->fileName() == fileName;
});
return it == m_loaders.constEnd();
}
};

/**
* @brief Constructor
*/
BackendManager::BackendManager(): d(new BackendManagerPrivate)
BackendManager::BackendManager()
: d{new BackendManagerPrivate}
{
qDebug() << Q_FUNC_INFO;
QVariantMap defaultSettings {
{ QSTR_DRUMSTICKRT_PUBLICNAMEIN, QStringLiteral("MIDI In")},
{ QSTR_DRUMSTICKRT_PUBLICNAMEOUT, QStringLiteral("MIDI Out")}
Expand All @@ -110,7 +124,9 @@ namespace drumstick { namespace rt {
* @brief Destructor
*/
BackendManager::~BackendManager()
{ }
{
qDebug() << Q_FUNC_INFO;
}

/**
* @brief returns the paths where backends are searched
Expand Down Expand Up @@ -165,6 +181,7 @@ namespace drumstick { namespace rt {
*/
void BackendManager::refresh(QSettings *settings)
{
qDebug() << Q_FUNC_INFO;
QVariantMap tmpMap;
settings->beginGroup(QSTR_DRUMSTICKRT_GROUP);
const QStringList allKeys = settings->allKeys();
Expand All @@ -188,31 +205,28 @@ namespace drumstick { namespace rt {
QStringList names;
QStringList paths;

qDebug() << Q_FUNC_INFO;

d->appendDir(map.value(QSTR_DRUMSTICKRT_PATH).toString(), paths);
name_in = map.value(QSTR_DRUMSTICKRT_PUBLICNAMEIN).toString();
name_out = map.value(QSTR_DRUMSTICKRT_PUBLICNAMEOUT).toString();
names << map.value(QSTR_DRUMSTICKRT_EXCLUDED).toStringList();
names << (name_in.isEmpty() ? QStringLiteral("MIDI In") : name_in);
names << (name_out.isEmpty() ? QStringLiteral("MIDI Out") : name_out);
paths << defaultPaths();
//qDebug() << Q_FUNC_INFO << "names:" << names;
//qDebug() << Q_FUNC_INFO << "paths:" << paths;

d->clearLists();
qDebug() << Q_FUNC_INFO << "names:" << names << "paths:" << paths;

// Dynamic backends
foreach(const QString& dir, paths) {
QDir pluginsDir(dir);
foreach (QString fileName, pluginsDir.entryList(QDir::Files)) {
if (QLibrary::isLibrary(fileName)) {
QPluginLoader *loader = new QPluginLoader(pluginsDir.absoluteFilePath(fileName));
qDebug() << "plugin loader created for" << fileName;
auto absolutePath = pluginsDir.absoluteFilePath(fileName);
if (QLibrary::isLibrary(absolutePath) && d->isLoaderNeeded(absolutePath)) {
QPluginLoader *loader = new QPluginLoader(absolutePath);
qDebug() << "plugin loader created:" << loader->fileName();
d->m_loaders << loader;
QObject *obj = loader->instance();
if (obj != nullptr) {
MIDIInput *input = qobject_cast<MIDIInput*>(obj);
MIDIInput *input = qobject_cast<MIDIInput *>(obj);
if (input != nullptr && !d->m_inputsList.contains(input)) {
qDebug() << "input plugin instantiated:" << name_in;
if (!name_in.isEmpty()) {
Expand All @@ -221,7 +235,7 @@ namespace drumstick { namespace rt {
input->setExcludedConnections(names);
d->m_inputsList << input;
} else {
MIDIOutput *output = qobject_cast<MIDIOutput*>(obj);
MIDIOutput *output = qobject_cast<MIDIOutput *>(obj);
if (output != nullptr && !d->m_outputsList.contains(output)) {
qDebug() << "output plugin instantiated:" << name_out;
if (!name_out.isEmpty()) {
Expand Down
30 changes: 17 additions & 13 deletions utils/vpiano/vpiano.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@
#if defined(Q_OS_MACOS)
#include <CoreFoundation/CoreFoundation.h>
#endif

#include <drumstick/pianokeybd.h>
#include <drumstick/settingsfactory.h>

#include "connections.h"
#include "preferences.h"
#include "vpiano.h"
#include "vpianoabout.h"
#include "vpianosettings.h"
#include <drumstick/backendmanager.h>
#include <drumstick/pianokeybd.h>
#include <drumstick/settingsfactory.h>

using namespace drumstick::rt;
using namespace drumstick::widgets;

VPiano::VPiano( QWidget * parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags),
m_midiIn(nullptr),
m_midiOut(nullptr)
VPiano::VPiano(QWidget *parent, Qt::WindowFlags flags)
: QMainWindow(parent, flags)
, m_manager{new BackendManager}
, m_midiIn{nullptr}
, m_midiOut{nullptr}
{
ui.setupUi(this);

Expand Down Expand Up @@ -108,23 +110,25 @@ VPiano::~VPiano()
qDebug() << Q_FUNC_INFO;
m_midiIn->close();
m_midiOut->close();
delete m_manager;
}

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

readSettings();

BackendManager man;
man.refresh(VPianoSettings::instance()->settingsMap());
m_inputs = man.availableInputs();
m_outputs = man.availableOutputs();
m_manager->refresh(VPianoSettings::instance()->settingsMap());
m_inputs = m_manager->availableInputs();
m_outputs = m_manager->availableOutputs();

m_midiIn = man.findInput(VPianoSettings::instance()->lastInputBackend());
m_midiIn = m_manager->findInput(VPianoSettings::instance()->lastInputBackend());
if (m_midiIn == nullptr) {
qFatal("Unable to find a suitable input backend.");
}

m_midiOut = man.findOutput(VPianoSettings::instance()->lastOutputBackend());
m_midiOut = m_manager->findOutput(VPianoSettings::instance()->lastOutputBackend());
if (m_midiOut == nullptr) {
qFatal("Unable to find a suitable output backend. You may need to set the DRUMSTICKRT environment variable.");
}
Expand Down
6 changes: 5 additions & 1 deletion utils/vpiano/vpiano.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
#ifndef VPIANO_H
#define VPIANO_H

#include <QMainWindow>
#include <QCloseEvent>
#include <QMainWindow>

#include <drumstick/backendmanager.h>
#include <drumstick/rtmidiinput.h>
#include <drumstick/rtmidioutput.h>

#include "ui_vpiano.h"

class VPiano : public QMainWindow
Expand Down Expand Up @@ -67,6 +70,7 @@ public Q_SLOTS:
void initialize();
void useCustomNoteNames();

drumstick::rt::BackendManager *m_manager;
QList<drumstick::rt::MIDIInput*> m_inputs;
QList<drumstick::rt::MIDIOutput*> m_outputs;
drumstick::rt::MIDIInput * m_midiIn;
Expand Down

0 comments on commit 5e55ded

Please sign in to comment.