return true;
}
+bool save_midi_mapping_to_file(const MIDIMappingProto &mapping_proto, const string &filename)
+{
+ // Save to disk. We use the text format because it's friendlier
+ // for a user to look at and edit.
+ int fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_CREAT, 0666);
+ if (fd == -1) {
+ perror(filename.c_str());
+ return false;
+ }
+ io::FileOutputStream output(fd); // Takes ownership of fd.
+ if (!TextFormat::Print(mapping_proto, &output)) {
+ // TODO: Don't overwrite the old file (if any) on error.
+ output.Close();
+ return false;
+ }
+
+ output.Close();
+ return true;
+}
+
void MIDIMapper::set_midi_mapping(const MIDIMappingProto &new_mapping)
{
+ lock_guard<mutex> lock(mapping_mu);
if (mapping_proto) {
mapping_proto->CopyFrom(new_mapping);
} else {
midi_thread = thread(&MIDIMapper::thread_func, this);
}
+const MIDIMappingProto &MIDIMapper::get_current_mapping() const
+{
+ lock_guard<mutex> lock(mapping_mu);
+ return *mapping_proto;
+}
+
+ControllerReceiver *MIDIMapper::set_receiver(ControllerReceiver *new_receiver)
+{
+ lock_guard<mutex> lock(mapping_mu);
+ swap(receiver, new_receiver);
+ return new_receiver; // Now old receiver.
+}
+
#define RETURN_ON_ERROR(msg, expr) do { \
int err = (expr); \
if (err < 0) { \
void MIDIMapper::handle_event(snd_seq_t *seq, snd_seq_event_t *event)
{
+ lock_guard<mutex> lock(mapping_mu);
switch (event->type) {
case SND_SEQ_EVENT_CONTROLLER: {
printf("Controller %d changed to %d\n", event->data.control.param, event->data.control.value);
const int controller = event->data.control.param;
const float value = map_controller_to_float(event->data.control.value);
+ receiver->controller_changed(controller);
+
// Global controllers.
match_controller(controller, MIDIMappingBusProto::kLocutFieldNumber, MIDIMappingProto::kLocutBankFieldNumber,
value, bind(&ControllerReceiver::set_locut, receiver, _2));
case SND_SEQ_EVENT_NOTEON: {
const int note = event->data.note.note;
+ receiver->note_on(note);
+
printf("Note: %d\n", note);
// Bank change commands. TODO: Highlight the bank change in the UI.