aberant-osc-ruby 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +58 -3
- data/VERSION.yml +2 -2
- data/lib/osc-ruby.rb +0 -1
- data/lib/osc-ruby/bundle.rb +2 -12
- data/lib/osc-ruby/em_server.rb +68 -0
- data/lib/osc-ruby/message.rb +9 -29
- data/lib/osc-ruby/network_packet.rb +1 -1
- data/lib/osc-ruby/osc_packet.rb +7 -2
- data/lib/osc-ruby/osc_types.rb +1 -1
- data/lib/osc-ruby/server.rb +6 -3
- data/spec/builders/message_builder.rb +1 -1
- data/spec/unit/message_bundle_spec.rb +1 -1
- data/spec/unit/message_spec.rb +1 -1
- data/spec/unit/osc_types_spec.rb +0 -1
- metadata +3 -2
data/README.rdoc
CHANGED
@@ -2,16 +2,71 @@
|
|
2
2
|
|
3
3
|
http://opensoundcontrol.org/
|
4
4
|
|
5
|
-
==
|
5
|
+
== DESCRIPTION
|
6
6
|
|
7
|
-
I've used the OSC gem originally created by Tadayoshi Funaba and wanted to update it for ruby 1.9 compatibility. I'm also writing rspec tests for this library to allow others to more easily understand/update the code.
|
7
|
+
I've used the OSC gem originally created by Tadayoshi Funaba and wanted to update it for ruby 1.9 compatibility. I'm also writing rspec tests for this library to allow others to more easily understand/update the code.
|
8
8
|
|
9
|
-
|
9
|
+
Compatible with ruby 1.8, 1.9, and jruby
|
10
10
|
|
11
|
+
== INSTALL
|
12
|
+
|
13
|
+
sudo gem install aberant-osc-ruby
|
14
|
+
|
15
|
+
for the EMServer, you will need EventMachine
|
16
|
+
|
17
|
+
sudo gem install eventmachine
|
18
|
+
|
19
|
+
== EVENT MACHINE EXAMPLE
|
20
|
+
|
21
|
+
# compatible with ruby 1.8, 1.9, and jruby
|
22
|
+
require 'rubygems'
|
23
|
+
require 'osc-ruby'
|
24
|
+
require 'osc-ruby/em_server'
|
25
|
+
|
26
|
+
@server = OSC::EMServer.new( 3333 )
|
27
|
+
@client = OSC::Client.new( 'localhost', 3333 )
|
28
|
+
|
29
|
+
@server.add_method '/greeting' do | message |
|
30
|
+
puts message.to_a
|
31
|
+
end
|
32
|
+
|
33
|
+
Thread.new do
|
34
|
+
@server.run
|
35
|
+
end
|
36
|
+
|
37
|
+
@client.send( OSC::Message.new( "/greeting" , "hullo!" ))
|
38
|
+
|
39
|
+
sleep( 3 )
|
40
|
+
|
41
|
+
== CLASSIC EXAMPLE
|
42
|
+
|
43
|
+
# compatible with ruby 1.8
|
44
|
+
require 'rubygems'
|
45
|
+
require 'osc-ruby'
|
46
|
+
|
47
|
+
|
48
|
+
@server = OSC::Server.new( 3333 )
|
49
|
+
@client = OSC::Client.new( 'localhost', 3333 )
|
50
|
+
|
51
|
+
@server.add_method '/greeting' do | message |
|
52
|
+
puts message.inspect
|
53
|
+
end
|
54
|
+
|
55
|
+
Thread.new do
|
56
|
+
@server.run
|
57
|
+
end
|
58
|
+
|
59
|
+
@client.send( OSC::Message.new( "/greeting", "hullo!" ))
|
60
|
+
|
61
|
+
sleep( 3 )
|
62
|
+
|
63
|
+
|
11
64
|
== CREDITS
|
12
65
|
|
13
66
|
Originally created by...
|
67
|
+
|
14
68
|
Tadayoshi Funaba
|
69
|
+
|
15
70
|
http://www.funaba.org/en/
|
16
71
|
|
17
72
|
thx also to Toby Tripp and Obtiva
|
data/VERSION.yml
CHANGED
data/lib/osc-ruby.rb
CHANGED
data/lib/osc-ruby/bundle.rb
CHANGED
@@ -1,19 +1,9 @@
|
|
1
|
+
require File.join( File.dirname( __FILE__ ), "packet" )
|
2
|
+
|
1
3
|
module OSC
|
2
4
|
class Bundle < Packet
|
3
|
-
extend Forwardable
|
4
|
-
include Enumerable
|
5
|
-
|
6
5
|
attr_accessor :timetag
|
7
6
|
|
8
|
-
undef_method :zip
|
9
|
-
|
10
|
-
# de = (Array.instance_methods - self.instance_methods)
|
11
|
-
# de -= %w(assoc flatten flatten! pack rassoc transpose)
|
12
|
-
# de += %w(include? sort)
|
13
|
-
|
14
|
-
# def_delegators(:@args, *de)
|
15
|
-
|
16
|
-
|
17
7
|
def initialize(timetag=nil, *args)
|
18
8
|
@timetag = timetag
|
19
9
|
@args = args
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'eventmachine'
|
3
|
+
require File.join( File.dirname( __FILE__), '..', 'osc-ruby')
|
4
|
+
|
5
|
+
|
6
|
+
module OSC
|
7
|
+
Channel = EM::Channel.new
|
8
|
+
|
9
|
+
class Connection < EventMachine::Connection
|
10
|
+
|
11
|
+
def receive_data data
|
12
|
+
Channel << OSC::OSCPacket.messages_from_network( data )
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
class EMServer
|
18
|
+
|
19
|
+
def initialize( port = 3333 )
|
20
|
+
@port = port
|
21
|
+
setup_dispatcher
|
22
|
+
@tuples = []
|
23
|
+
end
|
24
|
+
|
25
|
+
def run
|
26
|
+
EM::run { EM::open_datagram_socket "localhost", @port, Connection }
|
27
|
+
end
|
28
|
+
|
29
|
+
def add_method(address_pattern, &proc)
|
30
|
+
matcher = AddressPattern.new( address_pattern )
|
31
|
+
|
32
|
+
@tuples << [matcher, proc]
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
def setup_dispatcher
|
37
|
+
Channel.subscribe do |messages|
|
38
|
+
messages.each do |message|
|
39
|
+
diff = ( message.time || 0 ) - Time.now.to_ntp
|
40
|
+
|
41
|
+
if diff <= 0
|
42
|
+
sendmesg( message )
|
43
|
+
else
|
44
|
+
EM.defer do
|
45
|
+
sleep( diff )
|
46
|
+
sendmesg( message )
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def sendmesg(mesg)
|
54
|
+
@tuples.each do |matcher, obj|
|
55
|
+
if matcher.match?( mesg.address )
|
56
|
+
obj.call( mesg )
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
|
68
|
+
|
data/lib/osc-ruby/message.rb
CHANGED
@@ -1,18 +1,10 @@
|
|
1
|
+
require File.join( File.dirname( __FILE__ ), "packet" )
|
2
|
+
|
1
3
|
module OSC
|
2
4
|
class Message < Packet
|
3
|
-
extend Forwardable
|
4
|
-
# include Enumerable
|
5
|
-
|
6
5
|
attr_accessor :address
|
7
6
|
attr_accessor :time
|
8
|
-
|
9
|
-
# undef_method :zip
|
10
7
|
|
11
|
-
# de = (Array.instance_methods - self.instance_methods)
|
12
|
-
# de -= %w(assoc flatten flatten! pack rassoc transpose)
|
13
|
-
# de += %w(include? sort)
|
14
|
-
|
15
|
-
# def_delegators(:@args, *de)
|
16
8
|
|
17
9
|
def self.new_with_time( address, time, tags=nil, *args )
|
18
10
|
message = new( address, tags, *args )
|
@@ -20,29 +12,17 @@ module OSC
|
|
20
12
|
message
|
21
13
|
end
|
22
14
|
|
23
|
-
def initialize(address,
|
15
|
+
def initialize(address, *args)
|
24
16
|
@address = address
|
25
17
|
@args = []
|
26
18
|
|
27
|
-
|
28
19
|
args.each_with_index do |arg, i|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
when ?*; @args << arg
|
36
|
-
else; raise ArgumentError, 'unknown type'
|
37
|
-
end
|
38
|
-
else
|
39
|
-
case arg
|
40
|
-
when Integer; @args << OSCInt32.new(arg)
|
41
|
-
when Float; @args << OSCFloat32.new(arg)
|
42
|
-
when String; @args << OSCString.new(arg)
|
43
|
-
when OSCArgument; @args << arg
|
44
|
-
end
|
45
|
-
end
|
20
|
+
case arg
|
21
|
+
when Integer; @args << OSCInt32.new(arg)
|
22
|
+
when Float; @args << OSCFloat32.new(arg)
|
23
|
+
when String; @args << OSCString.new(arg)
|
24
|
+
when OSCArgument; @args << arg
|
25
|
+
end
|
46
26
|
end
|
47
27
|
end
|
48
28
|
|
data/lib/osc-ruby/osc_packet.rb
CHANGED
@@ -3,6 +3,7 @@ require 'ostruct'
|
|
3
3
|
|
4
4
|
module OSC
|
5
5
|
class OSCPacket
|
6
|
+
|
6
7
|
def self.messages_from_network( string )
|
7
8
|
messages = []
|
8
9
|
osc = new( string )
|
@@ -51,10 +52,9 @@ module OSC
|
|
51
52
|
|
52
53
|
def get_string
|
53
54
|
result = ''
|
54
|
-
until (c = @packet.getc) ==
|
55
|
+
until (c = @packet.getc) == string_delemeter
|
55
56
|
result << c
|
56
57
|
end
|
57
|
-
|
58
58
|
@packet.skip_padding
|
59
59
|
result
|
60
60
|
end
|
@@ -109,5 +109,10 @@ module OSC
|
|
109
109
|
def bundle?
|
110
110
|
!(@packet.to_s =~ /\A\#bundle/).nil?
|
111
111
|
end
|
112
|
+
|
113
|
+
def string_delemeter
|
114
|
+
# ruby 1.9 has multicharacter support
|
115
|
+
RUBY_VERSION.include?( '1.9' ) ? "\x00" : 0
|
116
|
+
end
|
112
117
|
end
|
113
118
|
end
|
data/lib/osc-ruby/osc_types.rb
CHANGED
data/lib/osc-ruby/server.rb
CHANGED
@@ -55,16 +55,19 @@ private
|
|
55
55
|
def dispatcher
|
56
56
|
loop do
|
57
57
|
mesg = @queue.pop
|
58
|
-
|
59
58
|
dispatch_message( mesg )
|
60
59
|
end
|
61
60
|
end
|
62
61
|
|
63
62
|
def detector
|
64
63
|
loop do
|
65
|
-
pa = @socket.
|
64
|
+
pa, network = @socket.recvfrom(16384)
|
66
65
|
begin
|
67
|
-
|
66
|
+
|
67
|
+
OSCPacket.messages_from_network(pa).each do |x|
|
68
|
+
@queue.push(x)
|
69
|
+
end
|
70
|
+
|
68
71
|
rescue EOFError
|
69
72
|
end
|
70
73
|
end
|
data/spec/unit/message_spec.rb
CHANGED
data/spec/unit/osc_types_spec.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: aberant-osc-ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- aberant
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -32,6 +32,7 @@ files:
|
|
32
32
|
- lib/osc-ruby/core_ext/numeric.rb
|
33
33
|
- lib/osc-ruby/core_ext/object.rb
|
34
34
|
- lib/osc-ruby/core_ext/time.rb
|
35
|
+
- lib/osc-ruby/em_server.rb
|
35
36
|
- lib/osc-ruby/message.rb
|
36
37
|
- lib/osc-ruby/network_packet.rb
|
37
38
|
- lib/osc-ruby/osc_argument.rb
|