aboutsummaryrefslogtreecommitdiff
path: root/pdlib.tcl
blob: 904be91472fa19b6b0bbbecb4254a4dad5232bfe (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
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# TCL objectized library for PD api
# by Federico Ferri <mescalinum@gmail.com> - 2007

package provide pdlib 0.1

package require Tcl 8.5

set verbose 1

namespace eval ::pd {
    proc error_msg {m} {
        return "pdlib: [uplevel {lindex [info level 0] 0}]: error: $m"
    }

    # create additional inlets with this
    proc add_inlet {self sel} {
        if $::verbose {post [info level 0]}
        variable _
        switch -- $sel {
            float {
                set ptr [new_t_float]
                lappend _($self:p_inlet) $ptr
                lappend _($self:t_inlet) "float"
                #lappend _($self:x_inlet) [floatinlet_new [tclpd_get_object $self] $ptr]
            }
            symbol {
                set ptr [new_t_symbol]
                lappend _($self:p_inlet) $ptr
                lappend _($self:t_inlet) "symbol"
                #lappend _($self:x_inlet) [symbolinlet_new [tclpd_get_object $self] $ptr]
            }
            list {
                set ptr [new_t_proxyinlet]
                proxyinlet_init $ptr
                #proxyinlet_list $ptr [gensym list] 2 {{symbol foo} {symbol bar}}
                inlet_new [tclpd_get_object $self] [$ptr cget -pd] 0 {}
                lappend _($self:p_inlet) $ptr
                lappend _($self:t_inlet) "list"
            }
            DISABLED__pointer {
            ## need to think more about this
            #    set ptr [new_t_pointer]
            #    lappend _($self:p_inlet) $ptr
            ##    lappend _($self:x_inlet) [pointerinlet_new [tclpd_get_object $self] $ptr]
            }
            default {
                return -code error [error_msg "unsupported selector: $sel"]
            }
        }
        #return [lindex $_($self:x_inlet) end]
    }

    # get the value of a given inlet (inlets numbered starting from 1)
    proc inlet {self n} {
        if {$::verbose} {post [info level 0]}
        if {$n <= 0} {return {}}
        if {![info exists _($self:p_inlet)] ||
            $n >= [llength $_($self:p_inlet)]} {
            return -code error [error_msg "no such inlet: $n"]
        }
        variable _
        set p_inlet [lindex $_($self:p_inlet) [expr $n-1]]
        if {$_($self:t_inlet) == {list}} {
            return [$p_inlet argv]
        } else {
            return [$p_inlet value]
        }
    }

    # used in object constructor for adding inlets
    proc add_outlet {self sel} {
        if $::verbose {post [info level 0]}
        variable _
        switch -- $sel {
            float {
                lappend _($self:x_outlet) \
                    [outlet_new [tclpd_get_object $self] [gensym "float"]]
            }
            symbol {
                lappend _($self:x_outlet) \
                    [outlet_new [tclpd_get_object $self] [gensym "symbol"]]
            }
            list {
                lappend _($self:x_outlet) \
                    [outlet_new [tclpd_get_object $self] [gensym "list"]]
            }
            default {
                return -code error [error_msg "unsupported selector: $sel"]
            }
        }
        return [lindex $_($self:x_outlet) end]
    }

    # used inside class for outputting some value
    proc outlet {self n sel args} {
        if $::verbose {post [info level 0]}
        variable _
        set outlet [lindex $_($self:x_outlet) $n]
        switch -- $sel {
            float {
                set v [lindex $args 0]
                outlet_float $outlet $v
            }
            symbol {
                set v [lindex $args 0]
                outlet_symbol $outlet $v
            }
            list {
                set v [lindex $args 0]
                set sz [llength $v]
                set aa [new_atom_array $sz]
                for {set i 0} {$i < $sz} {incr i} {
                    set_atom_array $aa $i [lindex $v $i]
                }
                outlet_list $outlet [gensym "list"] $sz $aa
                delete_atom_array $aa $sz
            }
            bang {
                outlet_bang $outlet
            }
            default {
                return -code error [error_msg "unknown selector: $sel"]
            }
        }
    }

    # used in object constructor to create inlets (internal method)
    proc create_iolets {cn self} {
        if $::verbose {post [info level 0]}
        variable class_db
        variable _
        set _($self:p_inlet) {}
        #set _($self:x_inlet) {}
        set _($self:t_inlet) {}
        set _($self:x_outlet) {}
        for {set i 0} {$i < [llength $class_db($cn:d_inlet)]} {incr i} {
            add_inlet $self [lindex $class_db($cn:d_inlet) $i]
        }
        for {set i 0} {$i < [llength $class_db($cn:d_outlet)]} {incr i} {
            add_outlet $self [lindex $class_db($cn:d_outlet) $i]
        }
    }

    # add a class method (that is: a proc named <class>_<sel>)
    proc call_classmethod {classname self sel args} {
        if $::verbose {post [info level 0]}
        set m "${classname}_${sel}"
        if {[llength [info commands "::$m"]] > 0} {
            return [$m $self {*}$args]
        }
    }

    # this handles the pd::class definition
    proc class {classname def} {
        if $::verbose {post [lrange [info level 0] 0 end-1]}
        variable class_db
        array set class_db {}
        set class_db($classname:d_inlet) {}
        set class_db($classname:d_outlet) {}
        # strip comments:
        set def2 [regsub -all -line {#.*$} $def {}]
        set patchable_flag 1
        set noinlet_flag 0
        foreach {id arg} $def2 {
            switch -- $id {
                inlet {
                    lappend class_db($classname:d_inlet) $arg
                }
                outlet {
                    lappend class_db($classname:d_outlet) $arg
                }
                patchable {
                    if {$arg != 0 && $arg != 1} {
                        return -code error [error_msg "patchable must be 0/1"]
                    }
                    set patchable_flag $arg
                }
                noinlet {
                    if {$arg != 0 && $arg != 1} {
                        return -code error [error_msg "noinlet must be 0/1"]
                    }
                    set noinlet_flag $arg
                }
                default {
                    proc ::${classname}_${id} {self args} [concat "global _;" [regsub -all @(\\\$?\[\\w\\?\]+) $arg _(\$self:\\1)]]
                }
            }
        }

        # class level dispatcher (sort of class constructor)
        proc ::$classname {self args} "
            if \$::verbose {::pd::post \[info level 0\]}
            ::pd::create_iolets $classname \$self
            ::pd::call_classmethod $classname \$self constructor {*}\$args
            # object dispatcher
            proc ::\$self {selector args} \"
             if \\\$::verbose {::pd::post \\\[info level 0\\\]}
             ::pd::call_classmethod $classname \$self \\\$selector {*}\\\$args
            \"
            return \$self
        "

        # TODO: c->c_gobj = (typeflag >= CLASS_GOBJ)
        set flag [expr {
            8 * ($noinlet_flag != 0) +
            3 * ($patchable_flag != 0)
        }]

        # this wraps the call to class_new()
        tclpd_class_new $classname $flag
    }

    # wrapper to post() withouth vargs
    proc post {args} {
        poststring2 [concat {*}$args]
    }

    proc assert= {a b} {
        if {$a != $b} {
            post "ASSERTION FAILED: \"$a\" == \"$b\""
            return 0
        }
        return 1
    }

    proc args {} {
        return [uplevel 1 "llength \$args"]
    }

    proc arg_float {n} {
        set v [uplevel 1 "lindex \$args $n"]
        foreach {selector value} $v {break}
        assert= $selector "float"
        return $value
    }

    proc arg_int {n} {
        set v [uplevel 1 "lindex \$args $n"]
        foreach {selector value} $v {break}
        assert= $selector "float"
        return [expr {int($value)}]
    }

    proc arg_symbol {n} {
        set v [uplevel 1 "lindex \$args $n"]
        foreach {selector value} $v {break}
        assert= $selector "symbol"
        return $value
    }

}