]> git.sesse.net Git - nageru/blobdiff - shared/midi_device.cpp
Make the preview light blink and be solid like the play light is.
[nageru] / shared / midi_device.cpp
index 822beaeadbd4d95b3a03fe294c3da069603953e5..a2329617250bf4b483831e15e6e96945581244b7 100644 (file)
@@ -160,6 +160,11 @@ void MIDIDevice::handle_event(snd_seq_t *seq, snd_seq_event_t *event)
                receiver->controller_received(event->data.control.param, event->data.control.value);
                break;
        }
+       case SND_SEQ_EVENT_PITCHBEND: {
+               // Note, -8192 to 8191 instead of 0 to 127.
+               receiver->controller_received(MIDIReceiver::PITCH_BEND_CONTROLLER, event->data.control.value);
+               break;
+       }
        case SND_SEQ_EVENT_NOTEON: {
                receiver->note_on_received(event->data.note.note);
                break;
@@ -228,27 +233,23 @@ 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;
        }
 
        unsigned num_events = 0;
-       for (unsigned note_num = 1; note_num <= 127; ++note_num) {
-               bool active = active_lights.count(note_num);
+       for (unsigned note_num = 1; note_num <= 127; ++note_num) {  // Note: Pitch bend is ignored.
+               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;
                }
@@ -265,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));
 }