rsc3/oregano/instrument.rkt

131 lines
2.7 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)
2014-05-16 20:50:08 +00:00
(provide (all-defined-out))
2014-05-16 00:57:13 +00:00
;; TODO - remove. using "signal-slider" for testing
(require "gui.rkt")
2014-05-15 21:30:33 +00:00
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-21 15:33:02 +00:00
(define custom-synth-num 1)
(define (gen-synth-name)
(set! current-node-id (add1 current-node-id))
current-node-id)
2014-05-15 22:22:29 +00:00
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])
2014-05-17 22:16:58 +00:00
;; TODO - remove slider
(out bus (mul (signal-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-20 18:04:17 +00:00
;; TODO
(define (make-instrument graph)
2014-05-21 15:33:02 +00:00
(let ([synthedef (letc ([bus 0])
(out bus graph))]
[name (format "synth~a" current-node-id)])
(with-sc3 (lambda (fd)
(send-synth fd name synthdef)))
))
2014-05-20 18:04:17 +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)
2014-05-17 22:16:58 +00:00
; === user instrument funcs ===
2014-05-16 19:29:00 +00:00
(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)))
2014-05-17 22:16:58 +00:00
(define (inst-on inst)
(send-msg (n-run1 inst 1)))
(define (inst-off inst)
2014-05-15 22:34:43 +00:00
(send-msg (n-run1 inst 0)))
2014-05-17 22:16:58 +00:00
(define (set-inst-param inst name val)
(send-msg (n-set1 inst name val)))
2014-05-15 22:22:29 +00:00
#|
- to stop/run:
(send-msg (n-run1 1001 1))
|#
2014-05-17 22:16:58 +00:00
;; ======== example useage ===========
#|
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-17 22:16:58 +00:00
(param-slider "change frequency" 300 1000 400
(lambda (val)
(set-inst-param my-sin "freq" val)))
(param-check-box "synth on" #f
(lambda (v)
(if v
(inst-on my-sin)
(inst-off my-sin))))
(show-gui)
2014-05-15 22:22:29 +00:00
2014-05-15 22:34:43 +00:00
;; example:
2014-05-17 22:16:58 +00:00
(sleep 0.5)
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
|#