Class: Scruby::Server
Constant Summary collapse
- DEFAULTS =
{ :buffers => 1024, :control_buses => 4096, :audio_buses => 128, :audio_outputs => 8, :audio_inputs => 8, :host => 'localhost', :port => 57111, :path => '/Applications/SuperCollider/scsynth' }
- @@servers =
[]
Instance Attribute Summary collapse
-
#audio_buses ⇒ Object
readonly
Returns the value of attribute audio_buses.
-
#buffers ⇒ Object
readonly
Returns the value of attribute buffers.
-
#control_buses ⇒ Object
readonly
Returns the value of attribute control_buses.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Class Method Summary collapse
-
.[](index) ⇒ Object
Return a server corresponding to the specified index of the registered servers array.
-
.[]=(index) ⇒ Object
Set a server to the specified index of the registered servers array.
-
.all ⇒ Object
Returns an array with all the registered servers.
-
.clear ⇒ Object
Clear the servers array.
Instance Method Summary collapse
-
#allocate(kind, *elements) ⇒ Object
Allocates either buffer or bus indices, should be consecutive.
-
#boot ⇒ Object
Boots the local binary of the scsynth forking a process, it will rise a SCError if the scsynth binary is not found in path.
-
#initialize(opts = {}) ⇒ Server
constructor
Initializes and registers a new Server instance and sets the host and port for it.
-
#quit ⇒ Object
Sends the /quit OSC signal to the scsynth.
- #running? ⇒ Boolean
-
#send(message, *args) ⇒ Object
Sends an OSC command or
Message
to the scsyth server. - #send_bundle(timestamp = nil, *messages) ⇒ Object
-
#send_synth_def(synth_def) ⇒ Object
Encodes and sends a SynthDef to the scsynth server.
- #stop ⇒ Object (also: #panic)
Constructor Details
#initialize(opts = {}) ⇒ 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
For more info
$ man scsynth
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/scruby/server.rb', line 46 def initialize opts = {} @opts = DEFAULTS.dup.merge opts @buffers = [] @control_buses = [] @audio_buses = [] @client = Client.new port, host Bus.audio self, @opts[:audio_outputs] # register hardware buses Bus.audio self, @opts[:audio_inputs] self.class.all << self end |
Instance Attribute Details
#audio_buses ⇒ Object (readonly)
Returns the value of attribute audio_buses.
23 24 25 |
# File 'lib/scruby/server.rb', line 23 def audio_buses @audio_buses end |
#buffers ⇒ Object (readonly)
Returns the value of attribute buffers.
23 24 25 |
# File 'lib/scruby/server.rb', line 23 def buffers @buffers end |
#control_buses ⇒ Object (readonly)
Returns the value of attribute control_buses.
23 24 25 |
# File 'lib/scruby/server.rb', line 23 def control_buses @control_buses end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
23 24 25 |
# File 'lib/scruby/server.rb', line 23 def host @host end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
23 24 25 |
# File 'lib/scruby/server.rb', line 23 def path @path end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
23 24 25 |
# File 'lib/scruby/server.rb', line 23 def port @port end |
Class Method Details
.[](index) ⇒ Object
Return a server corresponding to the specified index of the registered servers array
167 168 169 |
# File 'lib/scruby/server.rb', line 167 def [] index @@servers[index] end |
.[]=(index) ⇒ Object
Set a server to the specified index of the registered servers array
172 173 174 175 |
# File 'lib/scruby/server.rb', line 172 def []= index @@servers[index] @@servers.uniq! end |
.all ⇒ Object
Returns an array with all the registered servers
157 158 159 |
# File 'lib/scruby/server.rb', line 157 def all @@servers end |
.clear ⇒ Object
Clear the servers array
162 163 164 |
# File 'lib/scruby/server.rb', line 162 def clear @@servers.clear end |
Instance Method Details
#allocate(kind, *elements) ⇒ Object
Allocates either buffer or bus indices, should be consecutive
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 |
# File 'lib/scruby/server.rb', line 123 def allocate kind, *elements collection = instance_variable_get "@#{kind}" elements.flatten! max_size = @opts[kind] if collection.compact.size + elements.size > max_size raise SCError, "No more indices available -- free some #{ kind } before allocating more." end return collection.concat(elements) unless collection.index nil # just concat arrays if no nil item indices = [] collection.each_with_index do |item, index| # find n number of consecutive nil indices break if indices.size >= elements.size if item.nil? indices << index else indices.clear end end case when indices.size >= elements.size collection[indices.first, elements.size] = elements when collection.size + elements.size <= max_size collection.concat elements else raise SCError, "No block of #{ elements.size } consecutive #{ kind } indices is available." end end |
#boot ⇒ Object
Boots the local binary of the scsynth forking a process, it will rise a SCError if the scsynth binary is not found in path. The default path can be overriden using Server.scsynt_path=(‘path’)
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/scruby/server.rb', line 64 def boot raise SCError.new('Scsynth not found in the given path') unless File.exists? path if 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| loop do if response = pipe.gets puts response ready = true if response.match /ready/ end end end end 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 |
#quit ⇒ Object
Sends the /quit OSC signal to the scsynth
101 102 103 104 |
# File 'lib/scruby/server.rb', line 101 def quit Server.all.delete self send '/quit' end |
#running? ⇒ Boolean
89 90 91 |
# File 'lib/scruby/server.rb', line 89 def running? @thread and @thread.alive? ? true : false end |
#send(message, *args) ⇒ Object
Sends an OSC command or Message
to the scsyth server. E.g. server.send(‘/dumpOSC’, 1)
108 109 110 111 |
# File 'lib/scruby/server.rb', line 108 def send , *args = Message.new , *args unless Message === or Bundle === @client.send end |
#send_bundle(timestamp = nil, *messages) ⇒ Object
113 114 115 |
# File 'lib/scruby/server.rb', line 113 def send_bundle = nil, * send Bundle.new( , *.map{ || Message.new * } ) end |
#send_synth_def(synth_def) ⇒ Object
Encodes and sends a SynthDef to the scsynth server
118 119 120 |
# File 'lib/scruby/server.rb', line 118 def send_synth_def synth_def send Bundle.new( nil, Message.new('/d_recv', Blob.new(synth_def.encode), 0) ) end |
#stop ⇒ Object Also known as: panic
93 94 95 96 97 |
# File 'lib/scruby/server.rb', line 93 def stop send "/g_freeAll", 0 send "/clearSched" send "/g_new", 1 end |