Skip to content

Commit

Permalink
Switched to cleaner aberant-osc-ruby gem, bit of spec refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Macario committed Aug 24, 2009
1 parent 4631508 commit be73c99
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 121 deletions.
4 changes: 2 additions & 2 deletions Rakefile
Expand Up @@ -15,8 +15,8 @@ $hoe = Hoe.spec 'scruby' do
self.summary = %q{SuperCollider client for Ruby}
self.rubyforge_name = self.name
self.extra_deps = [
['maca-arguments','>= 0.6'],
['maca-rosc', '>= 0.0.1']
['maca-arguments', '>= 0.6'],
['aberant-osc-ruby','>= 0.1.5']
]
end

Expand Down
4 changes: 2 additions & 2 deletions lib/scruby.rb
Expand Up @@ -18,14 +18,14 @@
require 'date'
require 'rubygems'
require 'arguments'
require 'rosc'
require 'osc-ruby'
require 'eventmachine'
require 'yaml'

$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))

module Scruby
VERSION = '0.2.6.4'
VERSION = '0.2.6.5'
end

require "scruby/core_ext/object"
Expand Down
6 changes: 3 additions & 3 deletions lib/scruby/group.rb
Expand Up @@ -2,17 +2,17 @@ module Scruby
class Group < Node

def free_all
send '/g_freeAll', self.node_id
send '/g_freeAll', self.id
self
end

def deep_free
send '/g_deepFree', self.node_id
send '/g_deepFree', self.id
self
end

def dump_tree post = false
send '/g_dumpTree', self.node_id, post
send '/g_dumpTree', self.id, post
self
end

Expand Down
82 changes: 31 additions & 51 deletions lib/scruby/server.rb
Expand Up @@ -3,50 +3,31 @@
module Scruby
include OSC

class Message < OSC::Message
def initialize command, *args
class ::OSC::Message
def initialize address, *args
args.peel!
args.collect! do |arg|
@address = address
@args = args.collect do |arg|
case arg
when Array
Blob.new self.class.new(*arg).encode
when true
1
when false
0
when Symbol
arg.to_s
when Integer then OSCInt32.new arg
when Float then OSCFloat32.new arg
when String, Symbol then OSCString.new arg.to_s
when true then OSCInt32.new 1
when false then OSCInt32.new 0
when Array then OSCBlob.new self.class.new(*arg).encode
when OSCArgument then arg
else
arg
raise TypeError.new("#{ arg } is not a valid Message argument.")
end
end
super command, type_tags(args), *args
end

def type_tags *args
args.peel!
args.collect{ |arg| OSC::Packet.tag arg }.join
end
end

class UDPSender < OSC::UDPServer #:nodoc:
include Singleton

alias :udp_send :send
def send command, host, port, *args
args = args.collect{ |arg| arg.kind_of?( Symbol ) ? arg.to_s : arg }
udp_send Message.new( command, *args ), 0, host, port
end

def send_message message, host, port
udp_send message, 0, host, port
end
end
$UDP_Sender = UDPSender.instance

class Server
attr_reader :host, :port, :path, :buffers, :control_buses, :audio_buses
DEFAULTS = { :buffers => 1024, :control_buses => 4096, :audio_buses => 128, :audio_outputs => 8, :audio_inputs => 8 }.freeze
DEFAULTS = { :buffers => 1024, :control_buses => 4096, :audio_buses => 128, :audio_outputs => 8, :audio_inputs => 8,
:host => 'localhost', :port => 57111, :path => '/Applications/SuperCollider/scsynth'
}

# Initializes and registers a new Server instance and sets the host and port for it.
# The server is a Ruby representation of scsynth which can be a local binary or a remote
Expand All @@ -68,31 +49,34 @@ class Server
# +buffers+
# Number of available sample buffers defaults to 1024
def initialize opts = {}
@host = opts.delete(:host) || 'localhost'
@port = opts.delete(:port) || 57111
@path = opts.delete(:path) || '/Applications/SuperCollider/scsynth'
@opts = DEFAULTS.dup.merge opts
@buffers = []
@control_buses = []
@audio_buses = []
@client = SimpleClient.new host, port
Bus.audio self, @opts[:audio_outputs] # register hardware buses
Bus.audio self, @opts[:audio_inputs]
self.class.all << self
end

def host; @opts[:host]; end
def port; @opts[:port]; end
def path; @opts[:path]; end

# Boots the local binary of the scsynth forking a process, it will rise a SCError if the scsynth
# binary is not found in /Applications/SuperCollider/scsynth (default Mac OS path) or given path.
# The default path can be overriden using Server.scsynt_path=('path')
def boot
raise SCError.new('Scsynth not found in the given path') unless File.exists? @path
raise SCError.new('Scsynth not found in the given path') unless File.exists? path
if running?
warn "Server on port #{ @port } allready running"
warn "Server on port #{ port } allready running"
return self
end

ready = false
timeout = Time.now + 2
@thread = Thread.new do
IO.popen "cd #{ File.dirname @path }; ./#{ File.basename @path } -u #{ @port }" do |pipe|
IO.popen "cd #{ File.dirname path }; ./#{ File.basename path } -u #{ port }" do |pipe|
loop do
if response = pipe.gets
puts response
Expand All @@ -101,9 +85,9 @@ def boot
end
end
end
sleep 0.01 until ready or !@thread.alive?
sleep 0.01 # just to be shure
send "/g_new", 1
sleep 0.01 until ready or !@thread.alive? or Time.now > timeout
sleep 0.01 # just to be shure
send "/g_new", 1 # default group
self
end

Expand All @@ -116,6 +100,7 @@ def stop
send "/clearSched"
send "/g_new", 1
end
alias :panic :stop

# Sends the /quit OSC signal to the scsynth
def quit
Expand All @@ -126,13 +111,8 @@ def quit
# Sends an OSC command or +Message+ to the scsyth server.
# E.g. +server.send('/dumpOSC', 1)+
def send message, *args
case message
when Bundle, Message
$UDP_Sender.send_message message, @host, @port
else
$UDP_Sender.send message, @host, @port, *args
end
self
message = Message.new message, *args unless Packet === message
@client.send message
end

def send_bundle timestamp = nil, *messages
Expand All @@ -141,7 +121,7 @@ def send_bundle timestamp = nil, *messages

# Encodes and sends a SynthDef to the scsynth server
def send_synth_def synth_def
send Bundle.new( nil, Message.new( '/d_recv', Blob.new(synth_def.encode), 0 ) )
send Bundle.new( nil, Message.new( '/d_recv', OSCBlob.new(synth_def.encode), 0 ) )
end

# Allocates either buffer or bus indices, should be consecutive
Expand Down
10 changes: 5 additions & 5 deletions scruby.gemspec
Expand Up @@ -2,11 +2,11 @@

Gem::Specification.new do |s|
s.name = %q{scruby}
s.version = "0.2.6.4"
s.version = "0.2.6.5"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Macario Ortega"]
s.date = %q{2009-08-22}
s.date = %q{2009-08-24}
s.default_executable = %q{livecode.rb}
s.description = %q{}
s.email = ["macarui@gmail.com"]
Expand All @@ -27,16 +27,16 @@ Gem::Specification.new do |s|

if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
s.add_runtime_dependency(%q<maca-arguments>, [">= 0.6"])
s.add_runtime_dependency(%q<maca-rosc>, [">= 0.0.1"])
s.add_runtime_dependency(%q<aberant-osc-ruby>, [">= 0.1.5"])
s.add_development_dependency(%q<hoe>, [">= 2.3.2"])
else
s.add_dependency(%q<maca-arguments>, [">= 0.6"])
s.add_dependency(%q<maca-rosc>, [">= 0.0.1"])
s.add_dependency(%q<aberant-osc-ruby>, [">= 0.1.5"])
s.add_dependency(%q<hoe>, [">= 2.3.2"])
end
else
s.add_dependency(%q<maca-arguments>, [">= 0.6"])
s.add_dependency(%q<maca-rosc>, [">= 0.0.1"])
s.add_dependency(%q<aberant-osc-ruby>, [">= 0.1.5"])
s.add_dependency(%q<hoe>, [">= 2.3.2"])
end
end
2 changes: 1 addition & 1 deletion spec/buffer_spec.rb
Expand Up @@ -3,7 +3,7 @@
require 'date'
require 'arguments'
require 'tempfile'
require 'osc'
require 'osc-ruby'
require "scruby/buffer"
require "scruby/bus"
require "scruby/server"
Expand Down
2 changes: 1 addition & 1 deletion spec/bus_spec.rb
@@ -1,7 +1,7 @@
require File.expand_path(File.dirname(__FILE__)) + "/helper"

require 'tempfile'
require 'osc'
require 'osc-ruby'
require "scruby/core_ext/numeric"
require "scruby/bus"
require "scruby/server"
Expand Down
2 changes: 1 addition & 1 deletion spec/group_spec.rb
Expand Up @@ -5,7 +5,7 @@
require "scruby/node"
require "scruby/group"
require 'scruby/bus'
require 'osc'
require 'osc-ruby'
require 'scruby/server'
require File.join( File.expand_path(File.dirname(__FILE__)), "server")

Expand Down
2 changes: 1 addition & 1 deletion spec/node_spec.rb
Expand Up @@ -3,7 +3,7 @@
require "scruby/core_ext/typed_array"
require "scruby/node"
require "scruby/bus"
require 'osc'
require 'osc-ruby'
require 'scruby/server'
require File.join( File.expand_path(File.dirname(__FILE__)), "server")

Expand Down
4 changes: 2 additions & 2 deletions spec/server_spec.rb
Expand Up @@ -2,7 +2,7 @@

require 'arguments'
require 'tempfile'
require 'osc'
require 'osc-ruby'
require 'scruby/node'
require 'scruby/core_ext/array'
require 'scruby/core_ext/typed_array'
Expand Down Expand Up @@ -38,7 +38,7 @@ def == other
describe Message do
it "should encode array as Message Blob" do
m = Message.new "/b_allocRead", 1, "path", 1, -1, ["/b_query", 1]
p m.encode
m.encode.should == "/b_allocRead\000\000\000\000,isiib\000\000\000\000\000\001path\000\000\000\000\000\000\000\001\377\377\377\377\000\000\000\024/b_query\000\000\000\000,i\000\000\000\000\000\001"
end
end

Expand Down

0 comments on commit be73c99

Please sign in to comment.