Browse Source

Revise way to initialize MIDI drivers

tags/v0.6.1
Andrew Belt 6 years ago
parent
commit
036d1a4b83
11 changed files with 59 additions and 54 deletions
  1. +0
    -1
      include/bridge.hpp
  2. +1
    -4
      include/gamepad.hpp
  3. +1
    -4
      include/keyboard.hpp
  4. +3
    -1
      include/midi.hpp
  5. +1
    -3
      include/rtmidi.hpp
  6. +7
    -6
      src/bridge.cpp
  7. +11
    -7
      src/gamepad.cpp
  8. +14
    -7
      src/keyboard.cpp
  9. +8
    -2
      src/main.cpp
  10. +9
    -10
      src/midi.cpp
  11. +4
    -9
      src/rtmidi.cpp

+ 0
- 1
include/bridge.hpp View File

@@ -26,7 +26,6 @@ void bridgeInit();
void bridgeDestroy();
void bridgeAudioSubscribe(int channel, AudioIO *audio);
void bridgeAudioUnsubscribe(int channel, AudioIO *audio);
BridgeMidiDriver *bridgeGetMidiDriver();


} // namespace rack

+ 1
- 4
include/gamepad.hpp View File

@@ -7,9 +7,6 @@
namespace rack {


const int GAMEPAD_DRIVER = -10;


struct GamepadInputDevice : MidiInputDevice {
int deviceId;
std::vector<uint8_t> ccs;
@@ -30,8 +27,8 @@ struct GamepadDriver : MidiDriver {
};


void gamepadInit();
void gamepadStep();
GamepadDriver *gamepadGetDriver();


} // namespace rack

+ 1
- 4
include/keyboard.hpp View File

@@ -7,9 +7,6 @@
namespace rack {


const int KEYBOARD_DRIVER = -11;


struct KeyboardInputDevice : MidiInputDevice {
int octave = 5;
void processKey(int key, bool released);
@@ -27,9 +24,9 @@ struct KeyboardDriver : MidiDriver {
};


void keyboardInit();
void keyboardPress(int key);
void keyboardRelease(int key);
KeyboardDriver *keyboardGetDriver();


} // namespace rack

+ 3
- 1
include/midi.hpp View File

@@ -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

+ 1
- 3
include/rtmidi.hpp View File

@@ -1,6 +1,5 @@
#pragma once

#include "util/common.hpp"
#include "midi.hpp"
#include <map>

@@ -40,8 +39,7 @@ struct RtMidiDriver : MidiDriver {
};


std::vector<int> rtmidiGetDrivers();
RtMidiDriver *rtmidiCreateDriver(int driverId);
void rtmidiInit();


} // namespace rack

+ 7
- 6
src/bridge.cpp View File

@@ -25,7 +25,7 @@ static BridgeClientConnection *connections[BRIDGE_NUM_PORTS] = {};
static AudioIO *audioListeners[BRIDGE_NUM_PORTS] = {};
static std::thread serverThread;
static bool serverRunning = false;
static BridgeMidiDriver driver;
static BridgeMidiDriver *driver = NULL;


struct BridgeClientConnection {
@@ -206,7 +206,9 @@ struct BridgeClientConnection {
void processMidi(MidiMessage message) {
if (!(0 <= port && port < BRIDGE_NUM_PORTS))
return;
driver.devices[port].onMessage(message);
if (!driver)
return;
driver->devices[port].onMessage(message);
}

void setSampleRate(int sampleRate) {
@@ -402,6 +404,9 @@ void BridgeMidiDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput
void bridgeInit() {
serverRunning = true;
serverThread = std::thread(serverRun);

driver = new BridgeMidiDriver();
midiDriverAdd(BRIDGE_DRIVER, driver);
}

void bridgeDestroy() {
@@ -428,9 +433,5 @@ void bridgeAudioUnsubscribe(int port, AudioIO *audio) {
audioListeners[port] = NULL;
}

BridgeMidiDriver *bridgeGetMidiDriver() {
return &driver;
}


} // namespace rack

+ 11
- 7
src/gamepad.cpp View File

@@ -5,6 +5,10 @@
namespace rack {


static const int GAMEPAD_DRIVER = -10;
static GamepadDriver *driver = NULL;


void GamepadInputDevice::step() {
if (!glfwJoystickPresent(deviceId))
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() {
if (!driver)
return;
for (int i = 0; i < 16; i++) {
if (glfwJoystickPresent(i)) {
driver.devices[i].step();
driver->devices[i].step();
}
}
}

GamepadDriver *gamepadGetDriver() {
return &driver;
}


} // namespace rack

+ 14
- 7
src/keyboard.cpp View File

@@ -5,6 +5,10 @@
namespace rack {


static const int KEYBOARD_DRIVER = -11;
static KeyboardDriver *driver = NULL;


void KeyboardInputDevice::processKey(int key, bool released) {
int note = -1;
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) {
driver.device.processKey(key, false);
if (!driver)
return;
driver->device.processKey(key, false);
}

void keyboardRelease(int key) {
driver.device.processKey(key, true);
}

KeyboardDriver *keyboardGetDriver() {
return &driver;
if (!driver)
return;
driver->device.processKey(key, true);
}




+ 8
- 2
src/main.cpp View File

@@ -7,9 +7,12 @@
#include "asset.hpp"
#include "bridge.hpp"
#include "midi.hpp"
#include "osdialog.h"
#include "rtmidi.hpp"
#include "keyboard.hpp"
#include "gamepad.hpp"
#include "util/color.hpp"

#include "osdialog.h"
#include <unistd.h>


@@ -35,8 +38,10 @@ int main(int argc, char* argv[]) {
// Initialize namespaces
pluginInit();
engineInit();
midiInit();
rtmidiInit();
bridgeInit();
keyboardInit();
gamepadInit();
windowInit();
appInit();
settingsLoad(assetLocal("settings.json"));
@@ -73,6 +78,7 @@ int main(int argc, char* argv[]) {
windowDestroy();
bridgeDestroy();
engineDestroy();
midiDestroy();
pluginDestroy();
loggerDestroy();



+ 9
- 10
src/midi.cpp View File

@@ -192,20 +192,19 @@ void MidiOutput::setDeviceId(int deviceId) {
// 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);
driverIds.push_back(driverId);
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

+ 4
- 9
src/rtmidi.cpp View File

@@ -103,19 +103,14 @@ void RtMidiDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput) {
}


std::vector<int> rtmidiGetDrivers() {
void rtmidiInit() {
std::vector<RtMidi::Api> rtApis;
RtMidi::getCompiledApi(rtApis);

std::vector<int> drivers;
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);
}




Loading…
Cancel
Save