rsc3/oregano/instrument.rkt

88 lines
1.6 KiB
Racket
Raw Normal View History

2014-05-15 21:30:33 +00:00
#lang racket
(require rsc3 rhs/rhs)
2014-05-15 22:22:29 +00:00
;; simplifies sending messages to server
(define (send-msg msg)
(with-sc3 (lambda (fd)
(send fd msg))))
(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 0.2 (sin-osc ar freq 0)))))
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-15 21:30:33 +00:00
;; setup
2014-05-15 22:22:29 +00:00
;; show osc messages on server
(with-sc3 (lambda (fd)
(send fd (dump-osc 1))))
(with-sc3 reset)
;; send synthdefs
(with-sc3 (lambda (fd)
2014-05-15 22:34:43 +00:00
(send-synth fd "sin-inst" (wave-instrument sin-osc))
(send-synth fd "saw-inst" saw-instrument)))
2014-05-15 21:30:33 +00:00
(define (make-instrument ins)
(match ins
2014-05-15 22:22:29 +00:00
['sin (let ([node-id (gen-node-id)])
(send-msg (s-new0 "sin-inst" node-id 1 1))
; don't make sound upon creation
(send-msg (n-run1 node-id 0))
node-id)]
[else (error "unknown instrument used")]))
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 ===========
(define my-sin (make-instrument 'sin))
2014-05-15 22:34:43 +00:00
;; example:
; (note-on my-sin 500 1)
; (note-off my-sin)
2014-05-15 22:22:29 +00:00