Class: Scruby::Server

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

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

Class Method Summary collapse

Instance Method Summary collapse

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

Options:
  +host+: 
    defaults to 'localhost'
  +port+:
    TCP port defaults to 57111
  +control_buses+
    Number of buses for routing control data defaults to 4096, indices start at 0.
  +audio_buses+
    Number of audio Bus channels for hardware output and input and internal routing, defaults to 128
  +audio_outputs+
    Reserved +buses+ for hardware output, indices available are 0 to +audio_outputs+ - 1 defaults to 8.
  +audio_inputs+
    Reserved +buses+ for hardware input, +audio_outputs+ to (+audio_outputs+ + +audio_inputs+ - 1), defaults to 8.
  +buffers+
    Number of available sample buffers defaults to 1024


47
48
49
50
51
52
53
54
55
56
# File 'lib/scruby/server.rb', line 47

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_busesObject (readonly)

Returns the value of attribute audio_buses.



23
24
25
# File 'lib/scruby/server.rb', line 23

def audio_buses
  @audio_buses
end

#buffersObject (readonly)

Returns the value of attribute buffers.



23
24
25
# File 'lib/scruby/server.rb', line 23

def buffers
  @buffers
end

#control_busesObject (readonly)

Returns the value of attribute control_buses.



23
24
25
# File 'lib/scruby/server.rb', line 23

def control_buses
  @control_buses
end

#hostObject (readonly)

Returns the value of attribute host.



23
24
25
# File 'lib/scruby/server.rb', line 23

def host
  @host
end

#pathObject (readonly)

Returns the value of attribute path.



23
24
25
# File 'lib/scruby/server.rb', line 23

def path
  @path
end

#portObject (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



168
169
170
# File 'lib/scruby/server.rb', line 168

def [] index
  @@servers[index]
end

.[]=(index) ⇒ Object

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



173
174
175
176
# File 'lib/scruby/server.rb', line 173

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

.allObject

Returns an array with all the registered servers



158
159
160
# File 'lib/scruby/server.rb', line 158

def all
  @@servers
end

.clearObject

Clear the servers array



163
164
165
# File 'lib/scruby/server.rb', line 163

def clear
  @@servers.clear
end

Instance Method Details

#allocate(kind, *elements) ⇒ Object

Allocates either buffer or bus indices, should be consecutive



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
153
# File 'lib/scruby/server.rb', line 124

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

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

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/scruby/server.rb', line 65

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

#quitObject

Sends the /quit OSC signal to the scsynth



102
103
104
105
# File 'lib/scruby/server.rb', line 102

def quit
  Server.all.delete self
  send '/quit'
end

#running?Boolean

Returns:

  • (Boolean)


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

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)



109
110
111
112
# File 'lib/scruby/server.rb', line 109

def send message, *args
  message = Message.new message, *args unless Message === message or Bundle === message
  @client.send message
end

#send_bundle(timestamp = nil, *messages) ⇒ Object



114
115
116
# File 'lib/scruby/server.rb', line 114

def send_bundle timestamp = nil, *messages
  send Bundle.new( timestamp, *messages.map{ |message| Message.new *message  } )
end

#send_synth_def(synth_def) ⇒ Object

Encodes and sends a SynthDef to the scsynth server



119
120
121
# File 'lib/scruby/server.rb', line 119

def send_synth_def synth_def
  send Bundle.new( nil, Message.new('/d_recv', Blob.new(synth_def.encode), 0) )
end

#stopObject Also known as: panic



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

def stop
  send "/g_freeAll", 0
  send "/clearSched"
  send "/g_new", 1
end