Browse Source

MIDI Interface remembers port and channel

tags/v0.3.0
Andrew Belt 7 years ago
parent
commit
9bbbe0c42d
1 changed files with 40 additions and 5 deletions
  1. +40
    -5
      src/core/MidiInterface.cpp

+ 40
- 5
src/core/MidiInterface.cpp View File

@@ -58,10 +58,45 @@ struct MidiInterface : Module {
int getPortCount(); int getPortCount();
std::string getPortName(int portId); std::string getPortName(int portId);
// -1 will close the port // -1 will close the port
void openPort(int portId);
void setPortId(int portId);
void setChannel(int channel) {
this->channel = channel;
}
void pressNote(int note); void pressNote(int note);
void releaseNote(int note); void releaseNote(int note);
void processMidi(long msg); void processMidi(long msg);

json_t *toJson() {
json_t *rootJ = json_object();
if (portId >= 0) {
std::string portName = getPortName(portId);
json_object_set_new(rootJ, "portName", json_string(portName.c_str()));
json_object_set_new(rootJ, "channel", json_integer(channel));
}
return rootJ;
}

void fromJson(json_t *rootJ) {
json_t *portNameJ = json_object_get(rootJ, "portName");
if (portNameJ) {
std::string portName = json_string_value(portNameJ);
for (int i = 0; i < getPortCount(); i++) {
if (portName == getPortName(i)) {
setPortId(i);
break;
}
}
}

json_t *channelJ = json_object_get(rootJ, "channel");
if (channelJ) {
setChannel(json_integer_value(channelJ));
}
}

void initialize() {
setPortId(-1);
}
}; };




@@ -73,7 +108,7 @@ MidiInterface::MidiInterface() {
} }


MidiInterface::~MidiInterface() { MidiInterface::~MidiInterface() {
openPort(-1);
setPortId(-1);
} }


void MidiInterface::step() { void MidiInterface::step() {
@@ -115,7 +150,7 @@ std::string MidiInterface::getPortName(int portId) {
return stringf("%s: %s (%s)", info->interf, info->name, info->input ? "input" : "output"); return stringf("%s: %s (%s)", info->interf, info->name, info->input ? "input" : "output");
} }


void MidiInterface::openPort(int portId) {
void MidiInterface::setPortId(int portId) {
PmError err; PmError err;


// Close existing port // Close existing port
@@ -215,7 +250,7 @@ struct MidiItem : MenuItem {
MidiInterface *midiInterface; MidiInterface *midiInterface;
int portId; int portId;
void onAction() { void onAction() {
midiInterface->openPort(portId);
midiInterface->setPortId(portId);
} }
}; };


@@ -252,7 +287,7 @@ struct ChannelItem : MenuItem {
MidiInterface *midiInterface; MidiInterface *midiInterface;
int channel; int channel;
void onAction() { void onAction() {
midiInterface->channel = channel;
midiInterface->setChannel(channel);
} }
}; };




Loading…
Cancel
Save