]> git.sesse.net Git - nageru/commitdiff
Support non-binary MIDI lights (ie., with an on-velocity different from 1).
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 16 Jan 2019 22:00:53 +0000 (23:00 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 16 Jan 2019 22:01:09 +0000 (23:01 +0100)
No UI support yet. Most likely we'll only have UI support in Futatabi.

futatabi/midi_mapper.cpp
nageru/midi_mapper.cpp
nageru/midi_mapper.h
shared/midi_device.cpp
shared/midi_device.h
shared/midi_mapper_util.h
shared/midi_mapping.proto

index d1cfea245178246031397a423625273a602c0cca..849c72c7e1f4dd399096fc7b372d13283c37ebcf 100644 (file)
@@ -16,6 +16,7 @@
 #include <unistd.h>
 #include <algorithm>
 #include <functional>
+#include <map>
 #include <thread>
 
 #include "defs.h"
@@ -205,7 +206,7 @@ void MIDIMapper::refresh_lights()
 
 void MIDIMapper::update_lights_lock_held()
 {
-       set<unsigned> active_lights;  // Desired state.
+       map<unsigned, uint8_t> active_lights;  // Desired state.
        if (current_controller_bank == 0) {
                activate_mapped_light(*mapping_proto, MIDIMappingProto::kBank1IsSelectedFieldNumber, &active_lights);
        }
index d111a7a650dab499c7102f7e37e75a3663a10ffa..1fdb72e6c4006a86910588ba37c47fb0a548b3c1 100644 (file)
@@ -318,7 +318,7 @@ void MIDIMapper::update_lights_lock_held()
                return;
        }
 
-       set<unsigned> active_lights;  // Desired state.
+       map<unsigned, uint8_t> active_lights;  // Desired state.
        if (current_controller_bank == 0) {
                activate_lights_all_buses(MIDIMappingBusProto::kBank1IsSelectedFieldNumber, &active_lights);
        }
@@ -363,7 +363,7 @@ void MIDIMapper::update_lights_lock_held()
        midi_device.update_lights(active_lights);
 }
 
-void MIDIMapper::activate_lights_all_buses(int field_number, set<unsigned> *active_lights)
+void MIDIMapper::activate_lights_all_buses(int field_number, map<unsigned, uint8_t> *active_lights)
 {
        for (size_t bus_idx = 0; bus_idx < size_t(mapping_proto->bus_mapping_size()); ++bus_idx) {
                const MIDIMappingBusProto &bus_mapping = mapping_proto->bus_mapping(bus_idx);
index a25994357c6e81df285fa97fa023ff5d0a0d003a..3ecdee263afe9e8935b1f85ee7b08ff6f89afd74 100644 (file)
@@ -13,7 +13,6 @@
 #include <map>
 #include <memory>
 #include <mutex>
-#include <set>
 #include <string>
 #include <thread>
 
@@ -110,7 +109,7 @@ private:
        void update_highlights();
 
        void update_lights_lock_held();
-       void activate_lights_all_buses(int field_number, std::set<unsigned> *active_lights);
+       void activate_lights_all_buses(int field_number, std::map<unsigned, uint8_t> *active_lights);
 
        std::atomic<bool> should_quit{false};
        int should_quit_fd;
index ac0fea271d12d8837d3f53a3fe67d3eed22819c2..a2329617250bf4b483831e15e6e96945581244b7 100644 (file)
@@ -233,17 +233,12 @@ void MIDIDevice::subscribe_to_port_lock_held(snd_seq_t *seq, const snd_seq_addr_
        }
 
        // The current status of the device is unknown, so refresh it.
-       set<unsigned> active_lights;
-       for (const auto &it : current_light_status) {
-               if (it.second) {
-                       active_lights.insert(it.first);
-               }
-       }
+       map<unsigned, uint8_t> active_lights = move(current_light_status);
        current_light_status.clear();
        update_lights_lock_held(active_lights);
 }
 
-void MIDIDevice::update_lights_lock_held(const set<unsigned> &active_lights)
+void MIDIDevice::update_lights_lock_held(const map<unsigned, uint8_t> &active_lights)
 {
        if (alsa_seq == nullptr) {
                return;
@@ -251,9 +246,10 @@ void MIDIDevice::update_lights_lock_held(const set<unsigned> &active_lights)
 
        unsigned num_events = 0;
        for (unsigned note_num = 1; note_num <= 127; ++note_num) {  // Note: Pitch bend is ignored.
-               bool active = active_lights.count(note_num);
+               const auto it = active_lights.find(note_num);
+               uint8_t velocity = (it == active_lights.end()) ? 0 : it->second;
                if (current_light_status.count(note_num) &&
-                   current_light_status[note_num] == active) {
+                   current_light_status[note_num] == velocity) {
                        // Already known to be in the desired state.
                        continue;
                }
@@ -270,9 +266,9 @@ void MIDIDevice::update_lights_lock_held(const set<unsigned> &active_lights)
 
                // For some reason, not all devices respond to note off.
                // Use note-on with velocity of 0 (which is equivalent) instead.
-               snd_seq_ev_set_noteon(&ev, /*channel=*/0, note_num, active ? 1 : 0);
+               snd_seq_ev_set_noteon(&ev, /*channel=*/0, note_num, velocity);
                WARN_ON_ERROR("snd_seq_event_output", snd_seq_event_output(alsa_seq, &ev));
-               current_light_status[note_num] = active;
+               current_light_status[note_num] = velocity;
        }
        WARN_ON_ERROR("snd_seq_drain_output", snd_seq_drain_output(alsa_seq));
 }
index 498443931ab368ba931d2ead88192a969a27b116..9c76d18b9d1a667c18cf2111f9fc36d4ac63ee80 100644 (file)
@@ -7,7 +7,6 @@
 #include <atomic>
 #include <map>
 #include <mutex>
-#include <set>
 #include <thread>
 
 typedef struct snd_seq_addr snd_seq_addr_t;
@@ -33,7 +32,7 @@ public:
        ~MIDIDevice();
        void start_thread();
 
-       void update_lights(const std::set<unsigned> &active_lights)
+       void update_lights(const std::map<unsigned, uint8_t> &active_lights)
        {
                std::lock_guard<std::mutex> lock(mu);
                update_lights_lock_held(active_lights);
@@ -43,7 +42,7 @@ private:
        void thread_func();
        void handle_event(snd_seq_t *seq, snd_seq_event_t *event);
        void subscribe_to_port_lock_held(snd_seq_t *seq, const snd_seq_addr_t &addr);
-       void update_lights_lock_held(const std::set<unsigned> &active_lights);
+       void update_lights_lock_held(const std::map<unsigned, uint8_t> &active_lights);
 
        std::atomic<bool> should_quit{false};
        int should_quit_fd;
@@ -52,7 +51,7 @@ private:
        MIDIReceiver *receiver;  // Under <mu>.
 
        std::thread midi_thread;
-       std::map<unsigned, bool> current_light_status;  // Keyed by note number. Under <mu>.
+       std::map<unsigned, uint8_t> current_light_status;  // Keyed by note number. Under <mu>.
        snd_seq_t *alsa_seq{nullptr};  // Under <mu>.
        int alsa_queue_id{-1};  // Under <mu>.
        std::atomic<int> num_subscribed_ports{0};
index 5b66b83710f863e74bf5ab8f28cc0d37928a69ea..07f2a53b4241dd7f69b71b62e12de93e83775659 100644 (file)
@@ -49,7 +49,7 @@ inline bool match_bank_helper(const Proto &msg, int bank_field_number, int bank)
 
 // Find what MIDI note the given light (as given by field_number) is mapped to, and enable it.
 template <class Proto>
-void activate_mapped_light(const Proto &msg, int field_number, std::set<unsigned> *active_lights)
+void activate_mapped_light(const Proto &msg, int field_number, std::map<unsigned, uint8_t> *active_lights)
 {
        using namespace google::protobuf;
        const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
@@ -59,7 +59,7 @@ void activate_mapped_light(const Proto &msg, int field_number, std::set<unsigned
        }
        const MIDILightProto &light_proto =
                static_cast<const MIDILightProto &>(reflection->GetMessage(msg, descriptor));
-       active_lights->insert(light_proto.note_number());
+       active_lights->emplace(light_proto.note_number(), light_proto.velocity());
 }
 
 inline double map_controller_to_float(int controller, int val)
index 95ac59094dc6ea3eb8746d7b20e4f3b4c81038b1..8f343863c81b5401e5931174da2cf58e0227b81d 100644 (file)
@@ -16,4 +16,5 @@ message MIDIButtonProto {
 
 message MIDILightProto {
        required int32 note_number = 1;
+       optional int32 velocity = 2 [default=1];
 }