(bind-func dequeue_midi_event
  (let ((head 0) (tail 0))
    (lambda ()
      (set! tail (enqueue_midi_event.tail:i64))
      (if (< (+ head XTM_MIDI_EVENT_QUEUE_SIZE) tail)
          (begin (println "Warning: MIDI overrun (head:" head "tail:" tail ") events dropped")
                 (set! head tail)))
      (if (< head tail)
          (let ((res (aref-ptr XTM_MIDI_EVENT_QUEUE (% head XTM_MIDI_EVENT_QUEUE_SIZE))))
            (set! head (+ head 1))
            res)
          null)))) ;; underrun