From b4de7c9fc16ea11d2b11af6699740f2c22dc475f Mon Sep 17 00:00:00 2001 From: Andrew Belt Date: Wed, 2 Nov 2022 17:58:35 -0400 Subject: [PATCH] Use 16 MIDI loopback devices instead of 1. --- include/midiloopback.hpp | 3 ++- src/midiloopback.cpp | 37 +++++++++++++++++++++++++++---------- 2 files changed, 29 insertions(+), 11 deletions(-) diff --git a/include/midiloopback.hpp b/include/midiloopback.hpp index 7a474c64..185f3bdd 100644 --- a/include/midiloopback.hpp +++ b/include/midiloopback.hpp @@ -1,4 +1,5 @@ #pragma once +#include #include #include @@ -11,7 +12,7 @@ struct Device; struct Context { - Device* devices[1] = {}; + std::vector devices; Context(); ~Context(); diff --git a/src/midiloopback.cpp b/src/midiloopback.cpp index 77bfeed3..7310ba17 100644 --- a/src/midiloopback.cpp +++ b/src/midiloopback.cpp @@ -1,5 +1,6 @@ -#include #include +#include +#include namespace rack { @@ -7,11 +8,14 @@ namespace midiloopback { static const int DRIVER_ID = -12; +static const size_t NUM_DEVICES = 16; struct Device : midi::InputDevice, midi::OutputDevice { + int id = -1; + std::string getName() override { - return "Loopback"; + return string::f("Loopback %d", id + 1); } void sendMessage(const midi::Message& message) override { @@ -27,7 +31,11 @@ struct Driver : midi::Driver { // Input methods std::vector getInputDeviceIds() override { - return {0}; + std::vector deviceIds; + for (size_t i = 0; i < NUM_DEVICES; i++) { + deviceIds.push_back(i); + } + return deviceIds; } int getDefaultInputDeviceId() override { return 0; @@ -51,13 +59,14 @@ struct Driver : midi::Driver { // Output methods std::vector getOutputDeviceIds() override { - return {0}; + // Output IDs match input IDs + return getInputDeviceIds(); } int getDefaultOutputDeviceId() override { - return 0; + return getDefaultInputDeviceId(); } std::string getOutputDeviceName(int deviceId) override { - return getDevice(deviceId)->getName(); + return getInputDeviceName(deviceId); } midi::OutputDevice* subscribeOutput(int deviceId, midi::Output* output) override { midi::OutputDevice* outputDevice = getDevice(deviceId); @@ -77,19 +86,27 @@ struct Driver : midi::Driver { Device* getDevice(int deviceId) { if (!APP->midiLoopbackContext) return NULL; - if (deviceId != 0) + if (!(0 <= deviceId && (size_t) deviceId < NUM_DEVICES)) return NULL; - return APP->midiLoopbackContext->devices[deviceId]; + Context* context = APP->midiLoopbackContext; + return context->devices[deviceId]; } }; Context::Context() { - devices[0] = new Device; + for (size_t i = 0; i < NUM_DEVICES; i++) { + Device* device = new Device; + device->id = i; + devices.push_back(device); + } } Context::~Context() { - delete devices[0]; + for (Device* device : devices) { + delete device; + } + devices.clear(); }