(bind-func instrument_find_note
(lambda (notes:NoteData** lgth)
(let ((idx 0)
(i 0)
(old (note_starttime (pref notes 0)))
(active #f)
(starttime 0)
(go #t))
(while (= #t go)
(set! starttime (note_starttime (pref notes i)))
(if (< starttime old)
(begin (set! old starttime) (set! idx i)))
(set! active (note_active (pref notes i)))
(if (not active)
(begin (set! idx i) (set! go #f)))
;; (println '-- i (note_active (pref notes i)) (note_refcnt (pref notes i)))
(set! i (+ i 1))
(if (= i lgth) (set! go #f))
void)
;; (println "note:" idx (note_active (pref notes idx)) (note_refcnt (pref notes idx)))
(pref notes idx))))