rsc3/oregano/instrument.rkt

151 lines
3.2 KiB
Racket
Raw Normal View History

2014-05-15 21:30:33 +00:00
#lang racket
2014-05-16 00:57:13 +00:00
(require rsc3 rhs/rhs)
2014-05-15 21:30:33 +00:00
2014-05-16 00:57:13 +00:00
;; --- gui stuff
2014-05-16 20:50:08 +00:00
;; TODO - put in another file.
2014-05-16 00:57:13 +00:00
(require (prefix-in gui: racket/gui))
(define frame (new gui:frame% [label "Sliders"]))
(gui:send frame show #t)
2014-05-16 00:57:13 +00:00
;; parent should be a frame
(new gui:slider% [parent frame]
[label "freq"]
[min-value 300]
[max-value 1000]
[init-value 400]
;; callback receives the slider object and an event object
[callback (lambda (s event)
(send-msg (n-set1 1001 "freq" (gui:send s get-value))))])
;; hypothetical usage
#;(add-filter track2 (lpf #:resonance .3
#:cutoff (slider 300 800 500)))
(define (slider name min-n max-n init)
;; TODO - determine available bus
2014-05-16 00:57:13 +00:00
(define bus-id 16)
(new gui:slider% [parent frame]
[label name]
2014-05-16 00:57:13 +00:00
[min-value min-n]
[max-value max-n]
[init-value init]
;; callback receives the slider object and an event object
[callback (lambda (s event)
(send-msg
(c-set1 bus-id (/ (gui:send s get-value) 1000))))])
(send-msg (c-set1 bus-id (/ init 1000)))
2014-05-16 00:57:13 +00:00
(in 1 kr bus-id))
2014-05-16 20:50:08 +00:00
(new gui:check-box%
[label "note-on"]
[parent frame]
[value #f]
[callback (lambda (element event)
(if (gui:send element get-value)
(send-msg (n-run1 1001 1))
(send-msg (n-run1 1001 0))))])
; ------- end of gui stuff
2014-05-16 00:57:13 +00:00
2014-05-15 21:30:33 +00:00
2014-05-15 22:22:29 +00:00
2014-05-16 00:57:13 +00:00
;; simplifies sending osc messages to server
2014-05-15 22:22:29 +00:00
(define (send-msg msg)
(with-sc3 (lambda (fd)
2014-05-16 00:57:13 +00:00
(send fd msg))))
2014-05-15 22:22:29 +00:00
(define current-node-id 1000)
(define (gen-node-id)
(set! current-node-id (add1 current-node-id))
current-node-id)
2014-05-15 22:34:43 +00:00
(define (wave-instrument wave-func)
2014-05-15 22:22:29 +00:00
(letc ([bus 0]
[freq 440])
(out bus (mul 0.2 (wave-func ar freq 0)))))
2014-05-15 21:30:33 +00:00
(define sin-instrument
(letc ([bus 0]
[freq 440])
(out bus (mul (slider "amplitude" 100 800 200) (sin-osc ar freq 0)))))
2014-05-15 21:30:33 +00:00
2014-05-15 22:22:29 +00:00
(define saw-instrument
(letc ([bus 0]
[freq 440])
2014-05-15 22:37:23 +00:00
(out bus (mul 0.2 (saw ar freq)))))
2014-05-15 22:22:29 +00:00
2014-05-16 20:03:30 +00:00
(define moog-instrument
(letc ([bus 0]
[freq 440])
(out bus (moog-ff
2014-05-16 20:50:08 +00:00
(mul (saw ar freq) 0.1)
(mouse-y kr 200 30000 1 0.1) 3 0))))
2014-05-16 20:03:30 +00:00
2014-05-15 21:30:33 +00:00
;; setup
2014-05-15 22:22:29 +00:00
;; show osc messages on server
(send-msg (dump-osc 1))
2014-05-15 22:22:29 +00:00
(with-sc3 reset)
2014-05-16 20:03:30 +00:00
(define perset-instrument-map
2014-05-16 20:50:08 +00:00
`(("sin-inst" ,sin-instrument)
("saw-inst" ,saw-instrument)
("moog-inst" ,moog-instrument)))
2014-05-16 20:03:30 +00:00
2014-05-15 22:22:29 +00:00
;; send synthdefs
2014-05-16 20:50:08 +00:00
(map (lambda (pair)
(with-sc3 (lambda (fd)
(send-synth fd (first pair) (second pair)))))
perset-instrument-map)
2014-05-15 21:30:33 +00:00
2014-05-16 19:29:00 +00:00
(define (create-synth name node-id)
(send-msg (s-new0 name node-id 1 1))
; don't make sound upon creation
(send-msg (n-run1 node-id 0))
node-id)
(define (preset-instrument name)
(let ([node-id (gen-node-id)])
(create-synth name node-id)))
2014-05-15 21:30:33 +00:00
2014-05-15 22:22:29 +00:00
2014-05-15 22:34:43 +00:00
(define (note-on inst freq track)
(send-msg (n-set1 inst "freq" freq))
(send-msg (n-set1 inst "bus" track))
(send-msg (n-run1 inst 1)))
(define (note-off inst)
(send-msg (n-run1 inst 0)))
2014-05-15 22:22:29 +00:00
#|
- to stop/run:
(send-msg (n-run1 1001 1))
|#
;; ======== test run ===========
2014-05-16 19:29:00 +00:00
(define my-sin (preset-instrument "sin-inst"))
2014-05-15 22:22:29 +00:00
2014-05-15 22:34:43 +00:00
;; example:
(sleep 1)
2014-05-16 20:50:08 +00:00
; (note-on my-sin 500 1)
2014-05-15 22:34:43 +00:00
; (note-off my-sin)
2014-05-15 22:22:29 +00:00
2014-05-16 00:57:13 +00:00