;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; A simple example of the use of the
;; Audio Analysis Audio Unit 'aufx' 'asys' 'moso'
;; Draws a simple sonogram with powe
;;
;; NOTE: you will need to install the
;; impromptu audiounits for this to work
;; they should have been included in you download
;; if not check the website for audiounit downloads
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(au:clear-graph)
;; load sample player and pass through analysis
(define player (au:make-node "aumu" "play" "MOSO"))
(define asys (au:make-node "aufx" "asys" "MOSO"))
(au:connect-node player 0 asys 0)
(au:connect-node asys 0 *au:output-node* 0)
(au:update-graph)
;; load your audio file
(define audio-data (au:load-audio-data "/tmp/myaudiofile.aif"))
;; set audio data to midi-pitch 60
(au:play:set-sample-data player 60 audio-data)
;; create an empty mutable array (this will get filled with magnitude data
(define fft-data (objc:call "NSMutableArray" "array"))
;; create canvas and make a solid black image
(define canvas (gfx:make-canvas 800 400))
(define img (gfx:make-image 800 400))
(gfx:clear-image img '(0 0 0 1))
;; print out frequency bins (this information is static i.e. does not change)
(define freq-data (objc:call "NSMutableArray" "array"))
(au:asys:get-frequency-array asys freq-data)
(print freq-data)
;; run spectral viewer with power colour
(define loop
(lambda (time x)
(let ((power (/ (au:get-param asys *asys:sum* *au:global-scope* 0) 3000)))
(gfx:clear-image img '(0 0 0 .01))
(au:asys:get-fft-array asys fft-data)
(dotimes (y 70)
(let ((v (/ (objc:nsnumber->number (objc:call fft-data "objectAtIndex:" (* y 2))) 100)))
(gfx:path2image (gfx:make-circle x (* y 6) 3) img
(list)
(list power (+ .4 v) 1 (+ .1 v)))))
(gfx:draw-image time canvas img 1)
(apply + (objc:nsarray->list fft-data))
(callback (+ time 1000) 'loop (+ time 2000) (modulo (+ x 5) 800)))))
(loop (now) 0)
;; play your audio data
(play-sound (now) player 60 120 (+ (* *minute* 3)))