rsc3/oregano/instrument.rkt

198 lines
4.5 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 sosc/sosc "system.rkt")
2014-05-16 20:50:08 +00:00
2014-05-22 18:09:24 +00:00
(provide
(all-from-out 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-29 17:03:53 +00:00
;(run-super-collider)
2014-05-15 22:22:29 +00:00
2014-05-22 17:39:35 +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-29 05:41:53 +00:00
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
2014-05-22 18:09:24 +00:00
;(out bus (mul (signal-slider "amplitude" 100 800 200) (sin-osc ar freq 0)))))
(out bus (mul 0.2 (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
2014-05-29 05:41:53 +00:00
;; other way
2014-05-22 17:39:35 +00:00
#;(define (make-instrument name graph)
2014-05-29 05:41:53 +00:00
(let ([sd (letc ([bus 0])
(out bus ugen))]
;[name (format "synth~a" current-node-id)]
)
(with-sc3 (lambda (fd)
(send-synth fd name sd)))
))
2014-05-20 18:04:17 +00:00
2014-05-22 17:39:35 +00:00
(define-syntax-rule (make-instrument inst-name ([argname argdefault] ...) ugen)
(let ([sd (letc ([bus 0]
[argname argdefault] ...)
(out bus ugen))]
;[name (format "synth~a" current-node-id)]
)
(with-sc3 (lambda (fd)
(send-synth fd inst-name sd)))))
2014-05-22 16:58:18 +00:00
;; example of definst macro like overtone's definst
#;(define-syntax-rule (define-instrument inst-name [[argname argdefault] ...] ugen)
2014-05-29 05:41:53 +00:00
(define (inst-name [argname argdefault] ...)
(+ argname ...)))
2014-05-15 22:22:29 +00:00
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-29 05:41:53 +00:00
(for ([pair perset-instrument-map])
(with-sc3 (lambda (fd)
(send-synth fd (first pair) (second pair)))))
2014-05-15 21:30:33 +00:00
2014-05-22 16:58:18 +00:00
2014-05-22 17:39:35 +00:00
; === user note funcs ===
2014-05-22 16:58:18 +00:00
2014-05-29 05:41:53 +00:00
(struct note (id [freq #:mutable]) #:transparent)
2014-05-22 16:58:18 +00:00
(define (create-synth name play-on-start [start -1])
2014-05-22 17:39:35 +00:00
(define node-id (gen-node-id))
2014-05-16 19:29:00 +00:00
(send-msg (s-new0 name node-id 1 1))
2014-05-29 05:41:53 +00:00
;; TODO - send in bundle. it run 0 may be sent after
2014-05-16 19:29:00 +00:00
; don't make sound upon creation
2014-05-22 17:39:35 +00:00
(if play-on-start
empty
(send-msg (n-run1 node-id 0)))
2014-05-16 19:29:00 +00:00
node-id)
2014-05-15 21:30:33 +00:00
2014-05-22 17:39:35 +00:00
(define (make-note/option inst-name freq play-on-start)
2014-05-29 05:41:53 +00:00
(define id (create-synth inst-name play-on-start))
(send-msg (n-set1 id "freq" freq))
(note id freq))
2014-05-22 17:39:35 +00:00
(define (play-note inst-name freq)
(make-note/option inst-name freq #t))
(define (make-note inst-name freq)
(make-note/option inst-name freq #f))
(define (play-note-at start dur inst-name freq)
(define node-id (gen-node-id))
2014-06-15 03:23:56 +00:00
;; TODO (if ((eq? start 0)
(send-msg (bundle start (mcons (s-new0 inst-name node-id 1 1)
(mcons (n-set1 node-id "freq" freq) '()))))
(send-msg (bundle (+ start dur) (mcons
(n-run1 node-id 0) '())))
(void))
2014-05-22 17:39:35 +00:00
2014-05-15 22:22:29 +00:00
2014-05-22 16:58:18 +00:00
(define (note-on the-note)
;(send-msg (n-set1 inst "bus" track)) ; TODO
2014-05-22 17:39:35 +00:00
(send-msg (n-run1 (note-id the-note) 1))
2014-05-29 05:41:53 +00:00
(send-msg (n-set1 (note-id the-note) "freq" (note-freq the-note)))
(void))
2014-05-22 16:58:18 +00:00
(define (note-off the-note)
2014-05-29 05:41:53 +00:00
(send-msg (n-run1 (note-id the-note) 0))
(void))
2014-05-22 16:58:18 +00:00
2014-06-12 16:20:20 +00:00
(define (delete-note the-note)
(send-msg (n-free1 (note-id the-note)))
2014-06-12 16:20:20 +00:00
(void))
2014-05-15 22:34:43 +00:00
2014-05-22 16:58:18 +00:00
(define (set-note-param the-note name val)
(if (eq? name "freq")
(set-note-freq! the-note val)
empty)
2014-05-29 05:41:53 +00:00
(send-msg (n-set1 (note-id the-note) name val))
(void))
2014-05-15 22:34:43 +00:00
2014-05-29 18:35:36 +00:00
2014-05-29 18:35:36 +00:00
(define (mouse/x start end)
(mouse-x kr start end 0 0.1))
(define (mouse/y start end)
(mouse-y kr start end 0 0.1))
2014-05-29 05:41:53 +00:00
#|
2014-05-15 22:22:29 +00:00
- to stop/run:
(send-msg (n-run1 1001 1))
|#
2014-05-29 05:41:53 +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-15 22:22:29 +00:00
2014-06-12 16:20:20 +00:00
; (synth-on my-sin 500 1)
; (synth-off my-sin)
(note "weird-synth" 400)
(play-note-at 50 mynote)
(envelope A S D R)
|#