osc-access 0.0.15

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env ruby
2
+ module OSCAccess
3
+
4
+ module Analog
5
+
6
+ def self.new(from, to, options = {})
7
+ case to
8
+ when Array then Output::Set.new(Input.new(from), to, options)
9
+ when ::Range then Output::Range.new(Input.new(from), to, options)
10
+ end
11
+ end
12
+
13
+ module Input
14
+
15
+ def self.new(input)
16
+ case input
17
+ when Array then Input::Set.new(input)
18
+ when ::Range then Input::Range.new(input)
19
+ end
20
+ end
21
+
22
+ class Range
23
+
24
+ def initialize(range)
25
+ @range = range
26
+ end
27
+
28
+ def numerator(input)
29
+ (input - @range.first).to_f
30
+ end
31
+
32
+ def denominator
33
+ (@range.last - @range.first).abs.to_f
34
+ end
35
+
36
+ end
37
+
38
+ class Set
39
+
40
+ def initialize(set)
41
+ @set = set
42
+ end
43
+
44
+ def numerator(input)
45
+ @set.index(input).to_f
46
+ end
47
+
48
+ def denominator
49
+ (@set.size - 1).to_f
50
+ end
51
+
52
+ end
53
+
54
+ end
55
+
56
+ module Output
57
+
58
+ class Range
59
+
60
+ def initialize(from, to_range, options = {})
61
+ @type = options[:type]
62
+
63
+ @from = from
64
+ @to_range = to_range
65
+ end
66
+
67
+ def process(input, options = {})
68
+ to_range_len = (@to_range.last - @to_range.first).abs
69
+
70
+ proportion = to_range_len.to_f / @from.denominator
71
+ abs_output = proportion.to_f * @from.numerator(input)
72
+ output = abs_output + @to_range.first
73
+
74
+ type = options[:type] || @type
75
+ float_requested = !type.nil? && type.to_s.downcase == "float"
76
+ float_requested ? output : output.to_i
77
+ end
78
+
79
+ end
80
+
81
+ class Set
82
+
83
+ def initialize(from, to_set, options = {})
84
+ @from = from
85
+ @to_set = to_set
86
+ end
87
+
88
+ def process(input, options = {})
89
+ proportion = @from.numerator(input) / @from.denominator
90
+ index = [((proportion * @to_set.size).to_i - 1), 0].max
91
+ @to_set.at(index)
92
+ end
93
+
94
+ end
95
+
96
+ end
97
+
98
+ end
99
+
100
+ class Translate
101
+
102
+ def self.using(value, range, options = {})
103
+ to_local = options[:to_local].nil? ? true : options[:to_local]
104
+ new_vals = [value].flatten.compact.map do |single_val|
105
+ if range == :boolean
106
+ to_local ? (single_val <= 0 ? false : true) : (single_val ? 1 : 0)
107
+ else
108
+ if range.kind_of?(Range) || range.kind_of?(Array)
109
+ remote = DefaultRemoteRange
110
+ local = range
111
+ type = options[:type]
112
+ else
113
+ remote = range[:remote] || DefaultRemoteRange
114
+ local = range[:local]
115
+ type = range[:type] || options[:type]
116
+ end
117
+ analog = to_local ? Analog.new(remote, local) : Analog.new(local, remote)
118
+ type = to_local ? type : :float
119
+ analog.process(single_val, :type => type)
120
+ end
121
+ end
122
+ value.kind_of?(Array) ? new_vals : new_vals.first
123
+ end
124
+
125
+ end
126
+
127
+ end
@@ -0,0 +1,47 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ #
4
+ # Zeroconf support
5
+ #
6
+ # http://www.zeroconf.org
7
+ #
8
+ #
9
+
10
+ #
11
+ # much of this code was lifted from Zosc
12
+ # http://github.com/samBiotic/ruby-zosc
13
+ # Copyright (c) 2011 Sam Birkhead and released under the MIT License
14
+ #
15
+ #
16
+
17
+ require "dnssd"
18
+
19
+ module OSCAccess
20
+
21
+ module Zeroconf
22
+
23
+ class Service
24
+
25
+ def initialize(name, port)
26
+ @name, @port = name, port
27
+ end
28
+
29
+ def start
30
+ @thread = Thread.new do
31
+ registrar = DNSSD::Service.new
32
+ registrar.register @name, '_osc._udp', nil, @port do |r|
33
+ end
34
+ end
35
+ self
36
+ end
37
+
38
+ def stop
39
+ @thread.kill unless @thread.nil?
40
+ self
41
+ end
42
+
43
+ end
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ dir = File.dirname(File.expand_path(__FILE__))
4
+ $LOAD_PATH.unshift dir + '/../lib'
5
+
6
+ require 'test/unit'
7
+ require 'osc-access'
8
+
9
+ module TestHelper
10
+
11
+ $port_counter = 8000
12
+
13
+ def self.next_port
14
+ $port_counter += 1
15
+ end
16
+
17
+ class StubObject
18
+
19
+ include OSCAccessible
20
+
21
+ attr_accessor :data
22
+
23
+ end
24
+
25
+ end
@@ -0,0 +1,334 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'helper'
4
+
5
+ class AccessibleTest < Test::Unit::TestCase
6
+
7
+ include OSCAccess
8
+ include TestHelper
9
+
10
+ def test_osc_translate
11
+ outp = StubObject.new.send(:osc_translate, 0.8, :local => 0..127, :remote => 0..1)
12
+ assert_equal(101, outp)
13
+ end
14
+
15
+ def test_osc_translate_default_remote
16
+ outp = StubObject.new.send(:osc_translate, 0.5, 0..127)
17
+ assert_equal(63, outp)
18
+ end
19
+
20
+ def test_osc_translate_float
21
+ outp = StubObject.new.send(:osc_translate, 12, :remote => 0..120, :local => 0..1, :type => :float)
22
+ assert_equal(0.10, outp)
23
+ end
24
+
25
+ def test_osc_send
26
+ sleep(0.5)
27
+ received = nil
28
+ port = TestHelper.next_port
29
+
30
+ server = OSC::EMServer.new(port)
31
+ server.add_method("/test_osc_output") do |message|
32
+ received = message.args[0]
33
+ end
34
+ Thread.new { server.run }
35
+ sleep(0.5)
36
+ obj = StubObject.new
37
+ obj.osc_output(:port => port, :host => "localhost")
38
+ obj.osc_send("/test_osc_output", "hi friend")
39
+ sleep(0.5)
40
+ assert_equal("hi friend", received)
41
+ end
42
+
43
+ def test_osc_send_msg
44
+ sleep(0.5)
45
+ received = nil
46
+ port = TestHelper.next_port
47
+
48
+ server = OSC::EMServer.new(port)
49
+ server.add_method("/test_osc_output") do |message|
50
+ received = message.args[0]
51
+ end
52
+ Thread.new { server.run }
53
+ sleep(0.5)
54
+ obj = StubObject.new
55
+ obj.osc_output(:port => port, :host => "localhost")
56
+ obj.osc_send(OSC::Message.new("/test_osc_output", "hi friend"))
57
+ sleep(0.5)
58
+ assert_equal("hi friend", received)
59
+ end
60
+
61
+ def test_osc_send_accessor_translate
62
+ sleep(0.5)
63
+ received = nil
64
+ port = TestHelper.next_port
65
+
66
+ server = OSC::EMServer.new(port)
67
+ server.add_method("/test_osc_send_accessor_translate") do |message|
68
+ received = message.args[0]
69
+ end
70
+ Thread.new { server.run }
71
+ sleep(0.5)
72
+ obj = StubObject.new
73
+ obj.osc_receive("/test_osc_send_accessor_translate", :accessor => :data, :translate => 0..127)
74
+ obj.osc_output(:port => port, :host => "localhost")
75
+ obj.data = 63
76
+ obj.osc_send(:data)
77
+ sleep(0.5)
78
+ assert_equal(0.5, received.round(1))
79
+ end
80
+
81
+ def test_osc_send_accessor
82
+ sleep(0.5)
83
+ received = nil
84
+ port = TestHelper.next_port
85
+
86
+ server = OSC::EMServer.new(port)
87
+ server.add_method("/test_osc_send_accessor") do |message|
88
+ received = message.args[0]
89
+ end
90
+ Thread.new { server.run }
91
+ sleep(0.5)
92
+ obj = StubObject.new
93
+ obj.osc_receive("/test_osc_send_accessor", :accessor => :data)
94
+ obj.osc_output(:port => port, :host => "localhost")
95
+ obj.data = "blahblah"
96
+ obj.osc_send(:data)
97
+ sleep(0.5)
98
+ assert_equal("blahblah", received)
99
+ end
100
+
101
+ def test_osc_input
102
+ sleep(0.5)
103
+ received = nil
104
+ port = TestHelper.next_port
105
+ obj = StubObject.new
106
+
107
+ obj.osc_input(port)
108
+ obj.osc_start
109
+ obj.osc_receive("/test_osc_input") do |obj, val|
110
+ received = val
111
+ end
112
+ client = OSC::Client.new("localhost", port)
113
+ client.send( OSC::Message.new( "/test_osc_input", "hullo from test_osc_input!"))
114
+ sleep(0.5)
115
+ assert_equal("hullo from test_osc_input!", received)
116
+ end
117
+
118
+ def test_osc_process_ports_args
119
+ obj = StubObject.new
120
+ assert_equal([8000], obj.send(:osc_process_ports_args, [8000]))
121
+ assert_equal([8000], obj.send(:osc_process_ports_args, [[8000]]))
122
+ assert_equal((8000..8010).to_a, obj.send(:osc_process_ports_args, [8000..8010]))
123
+ assert_equal((8000..8010).to_a, obj.send(:osc_process_ports_args, [[8000..8010]]))
124
+ assert_equal([8000], obj.send(:osc_process_ports_args, [:port => 8000]))
125
+ end
126
+
127
+
128
+ def test_osc_receive_translate
129
+ sleep(0.5)
130
+ received = nil
131
+ port = TestHelper.next_port
132
+
133
+ obj = StubObject.new
134
+ obj.osc_start(:input_port => port)
135
+ obj.osc_receive("/test_osc_receive_translate", :translate => 0..127) do |obj, val|
136
+ received = val
137
+ end
138
+ client = OSC::Client.new("localhost", port)
139
+ client.send( OSC::Message.new( "/test_osc_receive_translate", 0.5))
140
+ sleep(0.5)
141
+ assert_equal(63, received)
142
+ end
143
+
144
+ def test_osc_receive_arg
145
+ sleep(0.5)
146
+ received = nil
147
+ port = TestHelper.next_port
148
+
149
+ obj = StubObject.new
150
+ obj.osc_start(:input_port => port)
151
+ obj.osc_receive("/test_osc_receive_arg", :arg => 1) do |obj, val|
152
+ received = val
153
+ end
154
+ client = OSC::Client.new("localhost", port)
155
+ client.send( OSC::Message.new( "/test_osc_receive_arg",5,4,3,2,1))
156
+ sleep(0.5)
157
+ assert_equal(4, received)
158
+ end
159
+
160
+ def test_osc_receive_accessor
161
+ sleep(0.5)
162
+ port = TestHelper.next_port
163
+ obj = StubObject.new
164
+
165
+ obj.osc_start(:input_port => port)
166
+ obj.osc_receive("/test_osc_receive_accessor", :accessor => :data)
167
+ client = OSC::Client.new("localhost", port)
168
+ client.send( OSC::Message.new( "/test_osc_receive_accessor", "hi from test_osc_receive_accessor"))
169
+ sleep(0.5)
170
+ assert_equal("hi from test_osc_receive_accessor", obj.data)
171
+ end
172
+
173
+ def test_load_map_inline_proc
174
+ received = nil
175
+ port = TestHelper.next_port
176
+ map = {
177
+ "/test_load_map_inline_proc" => Proc.new do |instance, val|
178
+ received = val
179
+ end
180
+ }
181
+ obj = StubObject.new
182
+ obj.osc_input(port)
183
+ obj.osc_start
184
+ obj.osc_load_map(map)
185
+ client = OSC::Client.new("localhost", port)
186
+ client.send( OSC::Message.new( "/test_load_map_inline_proc", "hullo from test_load_map_inline_proc!"))
187
+ sleep(0.5)
188
+ assert_equal("hullo from test_load_map_inline_proc!", received)
189
+ end
190
+
191
+ def test_load_map
192
+ received = nil
193
+ port = TestHelper.next_port
194
+ map = {
195
+ "/test_load_map" => {
196
+ :action => Proc.new do |instance, val|
197
+ received = val
198
+ end
199
+ }
200
+ }
201
+ obj = StubObject.new
202
+ obj.osc_input(port)
203
+ obj.osc_start
204
+ obj.osc_load_map(map)
205
+ client = OSC::Client.new("localhost", port)
206
+ sleep(0.5)
207
+ client.send( OSC::Message.new( "/test_load_map", "hullo from test_load_map!"))
208
+ sleep(0.5)
209
+ assert_equal("hullo from test_load_map!", received)
210
+ end
211
+
212
+ def test_load_map_translate
213
+ received = nil
214
+ port = TestHelper.next_port
215
+ map = {
216
+ "/test_load_map_translate" => {
217
+ :translate => 0..127,
218
+ :action => Proc.new do |instance, val|
219
+ received = val
220
+ end
221
+ }
222
+ }
223
+ obj = StubObject.new
224
+ obj.osc_input(port)
225
+ obj.osc_start
226
+ obj.osc_load_map(map)
227
+ client = OSC::Client.new("localhost", port)
228
+ client.send( OSC::Message.new( "/test_load_map_translate", 0.5) )
229
+ sleep(0.5)
230
+ assert_equal(63, received)
231
+ end
232
+
233
+ def test_load_map_arg
234
+ received = nil
235
+ port = TestHelper.next_port
236
+ map = {
237
+ "/test_load_map_arg" => {
238
+ :arg => 2,
239
+ :action => Proc.new do |instance, val|
240
+ received = val
241
+ end
242
+ }
243
+ }
244
+ obj = StubObject.new
245
+ obj.osc_input(port)
246
+ obj.osc_load_map(map)
247
+ obj.osc_start
248
+ client = OSC::Client.new("localhost", port)
249
+ client.send( OSC::Message.new( "/test_load_map_arg",0,1,2,3,4) )
250
+ sleep(0.5)
251
+ assert_equal(2, received)
252
+ end
253
+
254
+ def test_class_included
255
+ o = StubObject.new
256
+ assert_equal(true, o.class.kind_of?(OSCAccess::Class))
257
+ end
258
+
259
+ def test_osc_receive_thru
260
+ sleep(0.5)
261
+ received, received_back = nil, nil
262
+ port1 = TestHelper.next_port
263
+ port2 = TestHelper.next_port
264
+ server = OSC::EMServer.new(port1)
265
+ server.add_method("/test_osc_receive_thru") do |message|
266
+ received_back = message.args[0]
267
+ end
268
+ Thread.new { server.run }
269
+ sleep(0.5)
270
+ obj = StubObject.new
271
+ obj.osc_start(:input_port => port2, :output => { :host => "localhost", :port => port1 })
272
+ obj.osc_receive("/test_osc_receive_thru", :thru => true) do |obj, val|
273
+ received = val
274
+ end
275
+ client = OSC::Client.new("localhost", port2)
276
+ client.send( OSC::Message.new( "/test_osc_receive_thru", "hullo from test_osc_receive_thru!"))
277
+ sleep(0.5)
278
+ assert_equal("hullo from test_osc_receive_thru!", received)
279
+ assert_equal("hullo from test_osc_receive_thru!", received_back)
280
+ end
281
+
282
+ def test_osc_start_input
283
+ sleep(1)
284
+ received = nil
285
+ port = TestHelper.next_port
286
+ obj = StubObject.new
287
+
288
+ obj.osc_start(:input_port => port)
289
+ obj.osc_receive("/test_osc_start_input") do |obj, val|
290
+ received = val
291
+ end
292
+ client = OSC::Client.new("localhost", port)
293
+ client.send( OSC::Message.new( "/test_osc_start_input", "hullo from test_osc_start_input!"))
294
+ sleep(0.5)
295
+ assert_equal("hullo from test_osc_start_input!", received)
296
+ end
297
+
298
+ def test_osc_multiplex_input
299
+ sleep(1)
300
+ received = 0
301
+
302
+ obj = StubObject.new
303
+ obj.osc_start(:input_port => 9080..9082)
304
+ obj.osc_receive("/test_osc_multiplex_input") do |obj, val|
305
+ received += 1
306
+ end
307
+ sleep(0.5)
308
+ 3.times do |i|
309
+ client = OSC::Client.new("localhost", 9080 + i)
310
+ client.send( OSC::Message.new( "/test_osc_multiplex_input", i))
311
+ end
312
+ sleep(0.5)
313
+ assert_equal(3, received)
314
+ end
315
+
316
+ def test_osc_start_output
317
+ sleep(1)
318
+ received = nil
319
+ port = TestHelper.next_port
320
+
321
+ server = OSC::EMServer.new(port)
322
+ server.add_method("/test_osc_start_output") do |message|
323
+ received = message.args[0]
324
+ end
325
+ Thread.new { server.run }
326
+ sleep(0.5)
327
+ obj = StubObject.new
328
+ obj.osc_start(:output => { :port => port, :host => "localhost" })
329
+ obj.osc_send("/test_osc_start_output", "hi from test_osc_start_output")
330
+ sleep(0.5)
331
+ assert_equal("hi from test_osc_start_output", received)
332
+ end
333
+
334
+ end