-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.rkt
112 lines (83 loc) · 3.07 KB
/
init.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#lang racket
(require osc)
(provide (all-defined-out))
;; to start SC server: scsynth -u 9000
;; for debugging, start `dumpOSC 9000`
(define server-port 57110)
(define the-socket (udp-open-socket))
(define (start-server)
(thread
(lambda ()
(system (format "scsynth -u ~a" server-port)))))
(define (listen)
(udp-bind! the-socket "127.0.0.1" 0) ; 0 means choose a random client-port
;; From try-sending.rkt in rack-osc
;; also, we're assuming we won't get any messages longer than 10K
(define receive-buffer (make-bytes 10000 0))
(thread
(lambda ()
(let loop ()
(printf "waiting for incoming messages.\n")
(define-values (len hostname src-port)
(udp-receive! the-socket receive-buffer))
; (printf "current seconds: ~v\n" (current-seconds))
; (printf "len: ~v\nhostname: ~v\nsrc-port: ~v\n" len hostname src-port)
(define received (subbytes receive-buffer 0 len))
; (printf "received buffer: ~v\n" received)
(printf "decoded: ~e\n" (bytes->osc-element received))
(loop)))))
(listen)
;;;; Functions for sending ;;;;;
;; from try-sending.rkt in rack-osc examples
;; osc-message -> void
(define (send-command message)
(udp-send-to the-socket "127.0.0.1" server-port
(osc-element->bytes message)))
; address, [arg list] -> void
(define (send-osc-message addr [args '()])
(send-command (osc-message addr args)))
(define (bytes->osc-blob bytes)
(list 'blob bytes))
;; byte-string -> void
;; TODO - put contract-out
(define (send-synthdef filename)
(send-osc-message #"/d_recv" (list (bytes->osc-blob (file->bytes filename)))))
(define (string/bytes->bytes thing)
(cond [(bytes? thing) thing]
[(string? thing) (string->bytes/utf-8 thing)]
[else (error "be convertable to bytes")]))
(define (load-synthdef filename)
(send-osc-message #"/d_load" (list (string/bytes->bytes filename))))
(define (get-status)
(send-osc-message #"/status"))
(define current-node-id 1000)
(define (gen-node-id)
(set! current-node-id (add1 current-node-id))
current-node-id)
;; represents a synth object/node on the server
;; TODO - add parameters?
;; construct from a macro like overtone?
(struct synth (node-id))
;; TODO - process args
(define (synth-new name [args '()])
(define node-id (gen-node-id))
(let ([action 1] ; add to tail
[parent-node 0])
(send-osc-message #"s_new" (list (string/bytes->bytes name)
node-id
action
parent-node)))
(synth node-id))
(define (synth-play s)
(send-osc-message #"n_run" (list (synth-node-id s) 1)))
(define (synth-stop s)
(send-osc-message #"n_run" (list (synth-node-id s) 0)))
;; synth, [(#"arg1" val1) (#"arg2" val2) ...] -> void
(define (synth-set-params s args)
(send-osc-message #"n_set" (cons (synth-node-id s) (flatten args))))
(define (synth-delete s)
(send-osc-message #"n_free" (list (synth-node-id s))))
(define (reset)
;; remove group 0
(send-osc-message #"/g_freeAll" '(0))
(send-osc-message #"/clearSched"))