@@ -26,7 +26,6 @@ void bridgeInit(); | |||||
void bridgeDestroy(); | void bridgeDestroy(); | ||||
void bridgeAudioSubscribe(int channel, AudioIO *audio); | void bridgeAudioSubscribe(int channel, AudioIO *audio); | ||||
void bridgeAudioUnsubscribe(int channel, AudioIO *audio); | void bridgeAudioUnsubscribe(int channel, AudioIO *audio); | ||||
BridgeMidiDriver *bridgeGetMidiDriver(); | |||||
} // namespace rack | } // namespace rack |
@@ -7,9 +7,6 @@ | |||||
namespace rack { | namespace rack { | ||||
const int GAMEPAD_DRIVER = -10; | |||||
struct GamepadInputDevice : MidiInputDevice { | struct GamepadInputDevice : MidiInputDevice { | ||||
int deviceId; | int deviceId; | ||||
std::vector<uint8_t> ccs; | std::vector<uint8_t> ccs; | ||||
@@ -30,8 +27,8 @@ struct GamepadDriver : MidiDriver { | |||||
}; | }; | ||||
void gamepadInit(); | |||||
void gamepadStep(); | void gamepadStep(); | ||||
GamepadDriver *gamepadGetDriver(); | |||||
} // namespace rack | } // namespace rack |
@@ -7,9 +7,6 @@ | |||||
namespace rack { | namespace rack { | ||||
const int KEYBOARD_DRIVER = -11; | |||||
struct KeyboardInputDevice : MidiInputDevice { | struct KeyboardInputDevice : MidiInputDevice { | ||||
int octave = 5; | int octave = 5; | ||||
void processKey(int key, bool released); | void processKey(int key, bool released); | ||||
@@ -27,9 +24,9 @@ struct KeyboardDriver : MidiDriver { | |||||
}; | }; | ||||
void keyboardInit(); | |||||
void keyboardPress(int key); | void keyboardPress(int key); | ||||
void keyboardRelease(int key); | void keyboardRelease(int key); | ||||
KeyboardDriver *keyboardGetDriver(); | |||||
} // namespace rack | } // namespace rack |
@@ -127,7 +127,9 @@ struct MidiOutput : MidiIO { | |||||
}; | }; | ||||
void midiInit(); | |||||
void midiDestroy(); | |||||
/** Registers a new MIDI driver. Takes pointer ownership. */ | |||||
void midiDriverAdd(int driverId, MidiDriver *driver); | |||||
} // namespace rack | } // namespace rack |
@@ -1,6 +1,5 @@ | |||||
#pragma once | #pragma once | ||||
#include "util/common.hpp" | |||||
#include "midi.hpp" | #include "midi.hpp" | ||||
#include <map> | #include <map> | ||||
@@ -40,8 +39,7 @@ struct RtMidiDriver : MidiDriver { | |||||
}; | }; | ||||
std::vector<int> rtmidiGetDrivers(); | |||||
RtMidiDriver *rtmidiCreateDriver(int driverId); | |||||
void rtmidiInit(); | |||||
} // namespace rack | } // namespace rack |
@@ -25,7 +25,7 @@ static BridgeClientConnection *connections[BRIDGE_NUM_PORTS] = {}; | |||||
static AudioIO *audioListeners[BRIDGE_NUM_PORTS] = {}; | static AudioIO *audioListeners[BRIDGE_NUM_PORTS] = {}; | ||||
static std::thread serverThread; | static std::thread serverThread; | ||||
static bool serverRunning = false; | static bool serverRunning = false; | ||||
static BridgeMidiDriver driver; | |||||
static BridgeMidiDriver *driver = NULL; | |||||
struct BridgeClientConnection { | struct BridgeClientConnection { | ||||
@@ -206,7 +206,9 @@ struct BridgeClientConnection { | |||||
void processMidi(MidiMessage message) { | void processMidi(MidiMessage message) { | ||||
if (!(0 <= port && port < BRIDGE_NUM_PORTS)) | if (!(0 <= port && port < BRIDGE_NUM_PORTS)) | ||||
return; | return; | ||||
driver.devices[port].onMessage(message); | |||||
if (!driver) | |||||
return; | |||||
driver->devices[port].onMessage(message); | |||||
} | } | ||||
void setSampleRate(int sampleRate) { | void setSampleRate(int sampleRate) { | ||||
@@ -402,6 +404,9 @@ void BridgeMidiDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput | |||||
void bridgeInit() { | void bridgeInit() { | ||||
serverRunning = true; | serverRunning = true; | ||||
serverThread = std::thread(serverRun); | serverThread = std::thread(serverRun); | ||||
driver = new BridgeMidiDriver(); | |||||
midiDriverAdd(BRIDGE_DRIVER, driver); | |||||
} | } | ||||
void bridgeDestroy() { | void bridgeDestroy() { | ||||
@@ -428,9 +433,5 @@ void bridgeAudioUnsubscribe(int port, AudioIO *audio) { | |||||
audioListeners[port] = NULL; | audioListeners[port] = NULL; | ||||
} | } | ||||
BridgeMidiDriver *bridgeGetMidiDriver() { | |||||
return &driver; | |||||
} | |||||
} // namespace rack | } // namespace rack |
@@ -5,6 +5,10 @@ | |||||
namespace rack { | namespace rack { | ||||
static const int GAMEPAD_DRIVER = -10; | |||||
static GamepadDriver *driver = NULL; | |||||
void GamepadInputDevice::step() { | void GamepadInputDevice::step() { | ||||
if (!glfwJoystickPresent(deviceId)) | if (!glfwJoystickPresent(deviceId)) | ||||
return; | return; | ||||
@@ -92,20 +96,20 @@ void GamepadDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput) { | |||||
} | } | ||||
static GamepadDriver driver; | |||||
void gamepadInit() { | |||||
driver = new GamepadDriver(); | |||||
midiDriverAdd(GAMEPAD_DRIVER, driver); | |||||
} | |||||
void gamepadStep() { | void gamepadStep() { | ||||
if (!driver) | |||||
return; | |||||
for (int i = 0; i < 16; i++) { | for (int i = 0; i < 16; i++) { | ||||
if (glfwJoystickPresent(i)) { | if (glfwJoystickPresent(i)) { | ||||
driver.devices[i].step(); | |||||
driver->devices[i].step(); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
GamepadDriver *gamepadGetDriver() { | |||||
return &driver; | |||||
} | |||||
} // namespace rack | } // namespace rack |
@@ -5,6 +5,10 @@ | |||||
namespace rack { | namespace rack { | ||||
static const int KEYBOARD_DRIVER = -11; | |||||
static KeyboardDriver *driver = NULL; | |||||
void KeyboardInputDevice::processKey(int key, bool released) { | void KeyboardInputDevice::processKey(int key, bool released) { | ||||
int note = -1; | int note = -1; | ||||
switch (key) { | switch (key) { | ||||
@@ -100,18 +104,21 @@ void KeyboardDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput) | |||||
} | } | ||||
static KeyboardDriver driver; | |||||
void keyboardInit() { | |||||
driver = new KeyboardDriver(); | |||||
midiDriverAdd(KEYBOARD_DRIVER, driver); | |||||
} | |||||
void keyboardPress(int key) { | void keyboardPress(int key) { | ||||
driver.device.processKey(key, false); | |||||
if (!driver) | |||||
return; | |||||
driver->device.processKey(key, false); | |||||
} | } | ||||
void keyboardRelease(int key) { | void keyboardRelease(int key) { | ||||
driver.device.processKey(key, true); | |||||
} | |||||
KeyboardDriver *keyboardGetDriver() { | |||||
return &driver; | |||||
if (!driver) | |||||
return; | |||||
driver->device.processKey(key, true); | |||||
} | } | ||||
@@ -7,9 +7,12 @@ | |||||
#include "asset.hpp" | #include "asset.hpp" | ||||
#include "bridge.hpp" | #include "bridge.hpp" | ||||
#include "midi.hpp" | #include "midi.hpp" | ||||
#include "osdialog.h" | |||||
#include "rtmidi.hpp" | |||||
#include "keyboard.hpp" | |||||
#include "gamepad.hpp" | |||||
#include "util/color.hpp" | #include "util/color.hpp" | ||||
#include "osdialog.h" | |||||
#include <unistd.h> | #include <unistd.h> | ||||
@@ -35,8 +38,10 @@ int main(int argc, char* argv[]) { | |||||
// Initialize namespaces | // Initialize namespaces | ||||
pluginInit(); | pluginInit(); | ||||
engineInit(); | engineInit(); | ||||
midiInit(); | |||||
rtmidiInit(); | |||||
bridgeInit(); | bridgeInit(); | ||||
keyboardInit(); | |||||
gamepadInit(); | |||||
windowInit(); | windowInit(); | ||||
appInit(); | appInit(); | ||||
settingsLoad(assetLocal("settings.json")); | settingsLoad(assetLocal("settings.json")); | ||||
@@ -73,6 +78,7 @@ int main(int argc, char* argv[]) { | |||||
windowDestroy(); | windowDestroy(); | ||||
bridgeDestroy(); | bridgeDestroy(); | ||||
engineDestroy(); | engineDestroy(); | ||||
midiDestroy(); | |||||
pluginDestroy(); | pluginDestroy(); | ||||
loggerDestroy(); | loggerDestroy(); | ||||
@@ -192,20 +192,19 @@ void MidiOutput::setDeviceId(int deviceId) { | |||||
// midi | // midi | ||||
//////////////////// | //////////////////// | ||||
static void midiAddDriver(int driverId, MidiDriver *driver) { | |||||
void midiDestroy() { | |||||
driverIds.clear(); | |||||
for (auto &pair : drivers) { | |||||
delete pair.second; | |||||
} | |||||
drivers.clear(); | |||||
} | |||||
void midiDriverAdd(int driverId, MidiDriver *driver) { | |||||
assert(driver); | assert(driver); | ||||
driverIds.push_back(driverId); | driverIds.push_back(driverId); | ||||
drivers[driverId] = driver; | drivers[driverId] = driver; | ||||
} | } | ||||
void midiInit() { | |||||
for (int driverId : rtmidiGetDrivers()) { | |||||
midiAddDriver(driverId, rtmidiCreateDriver(driverId)); | |||||
} | |||||
midiAddDriver(BRIDGE_DRIVER, bridgeGetMidiDriver()); | |||||
midiAddDriver(KEYBOARD_DRIVER, keyboardGetDriver()); | |||||
midiAddDriver(GAMEPAD_DRIVER, gamepadGetDriver()); | |||||
} | |||||
} // namespace rack | } // namespace rack |
@@ -103,19 +103,14 @@ void RtMidiDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput) { | |||||
} | } | ||||
std::vector<int> rtmidiGetDrivers() { | |||||
void rtmidiInit() { | |||||
std::vector<RtMidi::Api> rtApis; | std::vector<RtMidi::Api> rtApis; | ||||
RtMidi::getCompiledApi(rtApis); | RtMidi::getCompiledApi(rtApis); | ||||
std::vector<int> drivers; | |||||
for (RtMidi::Api api : rtApis) { | for (RtMidi::Api api : rtApis) { | ||||
drivers.push_back((int) api); | |||||
int driverId = (int) api; | |||||
MidiDriver *driver = new RtMidiDriver(driverId); | |||||
midiDriverAdd(driverId, driver); | |||||
} | } | ||||
return drivers; | |||||
} | |||||
RtMidiDriver *rtmidiCreateDriver(int driverId) { | |||||
return new RtMidiDriver(driverId); | |||||
} | } | ||||