-
Notifications
You must be signed in to change notification settings - Fork 26
/
ElectraOneRouter.cpp
59 lines (52 loc) · 1.96 KB
/
ElectraOneRouter.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
Copyright (c) 2021 Christof Ruch. All rights reserved.
Dual licensed: Distributed under Affero GPL license by default, an MIT license is available for purchase
*/
#include "ElectraOneRouter.h"
#include "UIModel.h"
#include "Logger.h"
ElectraOneRouter::ElectraOneRouter() : enabled_(false)
{
}
ElectraOneRouter::~ElectraOneRouter()
{
// Delete message handler, in case it was created
if (!routerCallback_.isNull()) {
midikraft::MidiController::instance()->removeMessageHandler(routerCallback_);
}
}
void ElectraOneRouter::enable(bool enabled)
{
if (enabled) {
enabled_ = true;
if (routerCallback_.isNull()) {
// Install handler
routerCallback_ = midikraft::MidiController::makeOneHandle();
midikraft::MidiController::instance()->addMessageHandler(routerCallback_, [](MidiInput *source, MidiMessage const &message) {
if (source->getName() == "Electra Controller") {
auto toWhichSynthToForward = UIModel::currentSynth();
if (toWhichSynthToForward) {
auto location = midikraft::Capability::hasCapability<midikraft::MidiLocationCapability>(toWhichSynthToForward);
if (location) {
// Check if this is a channel message, and if yes, re-channel to the current synth
MidiMessage channelMessage = message;
if (message.getChannel() != 0) {
channelMessage.setChannel(location->channel().toOneBasedInt());
}
midikraft::MidiController::instance()->getMidiOutput(location->midiOutput())->sendMessageNow(channelMessage);
}
}
}
});
}
// Make sure to listen to the Electra One if found!
//midikraft::MidiController::instance()->enableMidiInput("Electra Controller");
//SimpleLogger::instance()->postMessage("Listening to messages from USB input Electra Controller");
}
else {
// Stop listening!
enabled_ = false;
//midikraft::MidiController::instance()->disableMidiInput("Electra Controller");
//SimpleLogger::instance()->postMessage("Turning off USB input Electra Controller");
}
}