;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;

;; ROUTE EXTERNAL MIDI SOURCE TO

;; EXTERNAL MIDI DESTINATION

;;

;; Listens for incoming midi-events on first available

;; source and plays those events back to the first

;; available destination.

;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


; make sure that everything is disconnected

(au:clear-graph)


; setup simple au graph

; piano -> output

(define piano (au:make-node "aumu" "dls " "appl"))

(au:connect-node piano 0 *au:output-node* 0)

(au:update-graph)


; now print available midi sources

(io:print-midi-sources)


; print available midi destinations

(io:print-midi-destinations)


;; if you want to receive midi in impromptu

;; you must register which process you

;; would like to receive midi messages in

;; usually this will be the primary process


;; start impromptu midi input on the primary process

(io:midi-register-events "primary process")


;; now check for midi destinations again

;; and you should now see an impromptu input

(io:print-midi-destinations)


; Connect an available midi source.

;

; Note: you can connect to as many different

; sources as you have available. Make sure

; you retain a reference to each sources id.

; The id is the value returned by midi-source.

(define src (io:midi-source 1))


; Connect the first available midi destination.

;

; Note: you can connect to as many different

; destinations as you have available. Make sure

; you retain a reference to each destination device.

; The device is the value returned by midi-destination.

(define dest (io:midi-destination 0))


; recieve midi events from src and pass them

; straight on to an audiounit

(define io:midi-in 

   (lambda (dev type channel a b)

      (print dev type channel a b)

      (au:midi-out (now) piano type channel a b)))



;; sending midi data to destintation

(io:midi-out (now) dest *io:midi-on* 0 60 80)

(io:midi-out (+ (now) 10000) dest *io:midi-on* 0 60 0)


(io:midi-out (now) dest *io:midi-on* 0 70 80)

(io:midi-out (+ (now) 40000) dest *io:midi-on* 0 70 0)