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

;;

;; Silly Synth

;; Create a silly little synth with a trivial Cocoa UI

;; 

;; OBJCBridge.h  header file inside the nib-demo in the dmg

;;

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


(define osc1 (au:make-node "aumf" "osc_" "MOSO"))

(define osc2 (au:make-node "aumf" "osc_" "MOSO"))

(define osc3 (au:make-node "aumf" "osc_" "MOSO"))

(define filter (au:make-node "aufx" "lpas" "appl"))

(define reverb (au:make-node "aufx" "mrev" "appl"))

(au:connect-node osc1 0 osc2 0)

(au:connect-node osc3 0 osc2 1)

(au:connect-node osc2 0 filter 0)

(au:connect-node filter 0 reverb 0)

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

(au:update-graph)


(au:open-view reverb)

(au:open-view filter)


(define window (objc:make "NSWindow" "initWithContentRect:styleMask:backing:defer:"

                          (list 200 200 400 300)

                          1 2 0))


(objc:call window "orderFront:" 0)

(objc:call window "setTitle:" "My Silly Synth")


(define sliders

   (make-list-with-proc 5 (lambda (i)

                             (let ((slider (objc:make "NSSlider" "initWithFrame:"

                                                      (list (+ 20 (* i 80)) 60 30 200))))

                                (objc:call slider "setTarget:" *objc:bridge*)

                                (objc:call slider "setAction:" "floatAction:")

                                (objc:call slider "setTag:" i)

                                (if (not (= i 0))

                                    (objc:call slider "setMaxValue:" 2000.0))

                                (objc:call (objc:call window "contentView") "addSubview:" slider)

                                slider))))

                                                                         

(define text-fields

   (make-list-with-proc 5 (lambda (i)

                             (let ((tf (objc:make "NSTextField" "initWithFrame:"

                                                  (list (+ 5 (* i 80)) 20 60 20))))

                                (objc:call (objc:call window "contentView") "addSubview:" tf)                                

                                tf))))


(define objc:action

   (lambda (id val)

      (let* ((tf (list-ref text-fields id))

             (slider (list-ref sliders id))

             (value (objc:call slider "floatValue")))

         (objc:call tf "setStringValue:"

                    (number->string value))

         (case id

               ((0)

                (au:set-param (now) osc1 *osc_:amplitude* *au:global-scope* 0 value))

               ((1)

                (au:set-param (now) osc1 *osc_:frequency* *au:global-scope* 0 value))

               ((2)

                (au:set-param (now) osc2 *osc_:frequency* *au:global-scope* 0 value))

               ((3)

                (au:set-param (now) osc3 *osc_:amplitude* *au:global-scope* 0 value))

               ((4)

                (au:set-param (now) osc3 *osc_:frequency* *au:global-scope* 0 value))))))