From 3986bbede7431a3073028297a52bdcdece2c7257 Mon Sep 17 00:00:00 2001 From: "5684185+vsariola@users.noreply.github.com" <5684185+vsariola@users.noreply.github.com> Date: Tue, 15 Oct 2024 00:24:18 +0300 Subject: [PATCH] fix(tracker/gomidi): consume all available midi.Messages --- tracker/gomidi/midi.go | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/tracker/gomidi/midi.go b/tracker/gomidi/midi.go index 27acccf..956ac8e 100644 --- a/tracker/gomidi/midi.go +++ b/tracker/gomidi/midi.go @@ -87,23 +87,26 @@ func (m *RTMIDIContext) HandleMessage(msg midi.Message, timestampms int32) { } func (c *RTMIDIContext) NextEvent() (event tracker.MIDINoteEvent, ok bool) { - select { - case msg := <-c.events: - var channel uint8 - var velocity uint8 - var key uint8 - if msg.GetNoteOn(&channel, &key, &velocity) { - return tracker.MIDINoteEvent{Frame: 0, On: true, Channel: int(channel), Note: key}, true - } else if msg.GetNoteOff(&channel, &key, &velocity) { - return tracker.MIDINoteEvent{Frame: 0, On: false, Channel: int(channel), Note: key}, true + for { + select { + case msg := <-c.events: + var channel uint8 + var velocity uint8 + var key uint8 + if msg.GetNoteOn(&channel, &key, &velocity) { + return tracker.MIDINoteEvent{Frame: 0, On: true, Channel: int(channel), Note: key}, true + } else if msg.GetNoteOff(&channel, &key, &velocity) { + return tracker.MIDINoteEvent{Frame: 0, On: false, Channel: int(channel), Note: key}, true + } + // TODO: handle control messages with something like: + // if msg.GetControlChange(&channel, &controller, &value) { + // .... + // if the message is not any recognized type, ignore it and continue looping + default: + // Note (@LeStahL): This empty select case is needed to make the implementation non-blocking. + return tracker.MIDINoteEvent{}, false } - // TODO: handle control messages with something like: - // if msg.GetControlChange(&channel, &controller, &value) { - // .... - default: - // Note (@LeStahL): This empty select case is needed to make the implementation non-blocking. } - return tracker.MIDINoteEvent{}, false } func (c *RTMIDIContext) BPM() (bpm float64, ok bool) {