Class: Scruby::Audio::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/scruby/audio/server.rb

Constant Summary collapse

@@sc_path =
'/Applications/SuperCollider/scsynth'
@@servers =
[]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host = 'localhost', port = 57111) ⇒ Server

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

server already running. Server class keeps an array with all the instantiated servers



33
34
35
36
# File 'lib/scruby/audio/server.rb', line 33

def initialize( host = 'localhost', port = 57111)
  @host, @port = host, port
  @@servers << self
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



25
26
27
# File 'lib/scruby/audio/server.rb', line 25

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



25
26
27
# File 'lib/scruby/audio/server.rb', line 25

def port
  @port
end

Class Method Details

.[](index) ⇒ Object

Return a server corresponding to the specified index of the registered servers array



95
96
97
# File 'lib/scruby/audio/server.rb', line 95

def [](index)
  @@servers[index]
end

.[]=(index) ⇒ Object

Set a server to the specified index of the registered servers array



100
101
102
103
# File 'lib/scruby/audio/server.rb', line 100

def []=(index)
  @@servers[index]
  @@servers.uniq!
end

.allObject

Returns an array with all the registered servers



90
91
92
# File 'lib/scruby/audio/server.rb', line 90

def all
  @@servers
end

.sc_pathObject

Get the scsynth path



85
86
87
# File 'lib/scruby/audio/server.rb', line 85

def sc_path
  @@sc_path
end

.sc_path=(path) ⇒ Object

Specify the scsynth binary path



80
81
82
# File 'lib/scruby/audio/server.rb', line 80

def sc_path=( path )
  @@sc_path = path
end

Instance Method Details

#bootObject

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/scsynt (default Mac OS path) or given path. The default path can be overriden using Server.scsynt_path=(‘path’)

Raises:



41
42
43
44
45
46
47
48
# File 'lib/scruby/audio/server.rb', line 41

def boot
  raise SCError.new('Scsynth not found in the given path') unless File.exists?( @@sc_path )
  Thread.new do
    path = @@sc_path.scan(/[^\/]+/)
    @server_pipe = IO.popen( "cd /#{ path[0..-2].join('/') }; ./#{ path.last } -u #{ @port }" )
    loop { p Special.new(@server_pipe.gets.chomp) }
  end unless @server_pipe        
end

#send(command, *args) ⇒ Object

Sends an OSC command to the scsyth server. E.g. server.send('/dumpOSC', 1)



60
61
62
# File 'lib/scruby/audio/server.rb', line 60

def send( command, *args )
  return $UDP_Sender.send( command, @host, @port, *args )
end

#send_synth_def(synth_def) ⇒ Object

Encodes and sends a SynthDef to the scsynth server



65
66
67
68
69
# File 'lib/scruby/audio/server.rb', line 65

def send_synth_def( synth_def )
  *blob = OSC::Blob.new( synth_def.encode ), 0
  def_message = OSC::Message.new( '/d_recv', $UDP_Sender.type_tag( blob ), *blob )
  send_message( OSC::Bundle.new( nil, def_message ) )
end

#stopObject

Sends the /quit OSC signal to the scsynth server and kills the forked process if the scsynth server is running locally



52
53
54
55
56
# File 'lib/scruby/audio/server.rb', line 52

def stop
  send '/quit'
  Process.kill 'KILL', @server_pipe.pid if @server_pipe
  @server_pipe = nil
end