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

For more info

$ man scsynth

Parameters:

  • opts (Hash) (defaults to: {})

    the options to create a message with.

Options Hash (opts):

  • :path (String) — default: 'scsynt' on Linux, '/Applications/SuperCollider/scsynth' on Mac

    scsynth binary path

  • :host (String) — default: 'localhost'

    SuperCollider Server address

  • :port (Fixnum) — default: 57111

    TCP port

  • :control_buses (Fixnum) — default: 4096

    Number of buses for routing control data, indices start at 0

  • :audio_buses (Fixnum) — default: 8

    Number of audio Bus channels for hardware output and input and internal routing

  • :audio_outputs (Fixnum) — default: 8

    Reserved buses for hardware output, indices start at 0

  • :audio_inputs (Fixnum) — default: 8

    Reserved buses for hardware input, indices starting from the number of audio outputs

  • :buffers (Fixnum) — default: 1024

    Number of available sample buffers



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_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



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

.allObject

Returns an array with all the registered servers



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

def all
  @@servers
end

.clearObject

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

#bootObject

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’)

Raises:



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

#quitObject

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

Returns:

  • (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 message, *args
  message = Message.new message, *args unless Message === message or Bundle === message
  @client.send message
end

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



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

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



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

#stopObject 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