aboutsummaryrefslogtreecommitdiff
path: root/examples/dynroute.tcl
blob: 04cb3c93312ae12f4d033fb6d328df1dd0a817a5 (plain)
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
package require Tclpd 0.2.3
package require TclpdLib 0.19

# dynroute: dynamically route messages based on first element
# non-matching arguments are sent to last inlet
# constructor: <float>   specify the number of outlets (default: 1)
# send commands to the right inlet
# available commands:
# add <atom> <float>     route selector <atom> to output number <float>
# remove <atom> <float>  remove previously created routing
# clear

proc+ dynroute::constructor {self args} {
    pd::add_inlet $self list

    set @num_outlets [pd::arg 0 int]
    if {$@num_outlets < 0} {set @num_outlets 2}

    for {set i 0} {$i < $@num_outlets} {incr i} {
        pd::add_outlet $self list
    }

    set @routing {}
}

proc+ dynroute::0_list {self args} {
    set sel [pd::arg 0 any]
    set out [expr {$@num_outlets-1}]
    catch {set out [dict get $@routing $sel]}
    pd::outlet $self $out list $args
}

proc+ dynroute::1_add {self args} {
    set sel [pd::arg 0 any]
    set out [pd::arg 1 int]
    if {$out < 0 || $out >= $@num_outlets} {
        pd::post "error: add: outlet number out of range"
        return
    }
    dict set @routing $sel $out
}

proc+ dynroute::1_remove {self args} {
    set sel [pd::arg 0 any]
    set out [pd::arg 1 int]
    if {$out < 0 || $out >= $@num_outlets} {
        pd::post "error: add: outlet number out of range"
        return
    }
    catch {dict unset @routing $sel $out}
}

proc+ dynroute::1_clear {self} {
    set @routing {}
}

pd::class dynroute