Class: Scruby::Node

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

Direct Known Subclasses

Group, Synth

Constant Summary collapse

ACTIONS =
[:head, :tail, :before, :after, :replace]
@@base_id =
999

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Node

Returns a new instance of Node.



8
9
10
11
12
13
14
15
# File 'lib/scruby/node.rb', line 8

def initialize *args
  args.flatten!
  args.compact!
  args.each{ |s| raise TypeError.new("#{s} should be instance of Server") unless Server === s }
  @id      = args.pop if args.last.is_a? Integer
  @servers = args.empty? ? Server.all : args
  @id    ||= @@base_id += 1
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



4
5
6
# File 'lib/scruby/node.rb', line 4

def group
  @group
end

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/scruby/node.rb', line 4

def id
  @id
end

#serversObject (readonly)

Returns the value of attribute servers.



4
5
6
# File 'lib/scruby/node.rb', line 4

def servers
  @servers
end

Class Method Details

.reset!Object

Reset the node count



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

def self.reset!
  @@base_id = 2000
end

Instance Method Details

#freeObject



22
23
24
25
26
# File 'lib/scruby/node.rb', line 22

def free
  send '/n_free', self.id
  @group, @playing, @running = nil, false, false
  self
end

#map(args) ⇒ Object

Map controls in this Node to read from control or audio rate Buses. Controls are defined in a SynthDef as args or instances of Control or its subclasses. They are specified here using symbols, strings, or indices, and are listed in pairs with Bus objects. The number of sequential controls mapped corresponds to the Bus’ number of channels. If this Node is a Group this will map all Nodes within the Group. Note that with mapMsg if you mix audio and control rate busses you will get an Array of two messages rather than a single message. Integer bus indices are assumed to refer to control buses. To map a control to an audio bus, you must use a Bus object.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/scruby/node.rb', line 39

def map args
  control, audio, content = ['/n_mapn', self.id], ['/n_mapan', self.id], []
  args    = args.to_a.each do |param, bus|
    raise ArgumentError, "`#{ control }` is not a Bus" unless bus.kind_of? Bus
    array = audio   if bus.rate == :audio
    array = control if bus.rate == :control
    array.push param, bus.index, bus.channels if array
  end
  content << control unless control.empty?
  content << audio   unless audio.empty?
  send_bundle nil, *content
  self
end

#move_after(node) ⇒ Object



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

def move_after node
  @group = node.group
  send '/n_after', self.id, node.id
  self
end

#move_before(node) ⇒ Object



59
60
61
62
63
# File 'lib/scruby/node.rb', line 59

def move_before node
  @group = node.group
  send '/n_before', self.id, node.id
  self
end

#playing?Boolean Also known as: running?

def move_to_head group

@group = node.group
@server.each{ |s| s.send '/n_after', self.id, node.id }

end

def move_to_tail group

@group = node.group
@server.each{ |s| s.send '/n_after', self.id, node.id }

end

Returns:

  • (Boolean)


81
# File 'lib/scruby/node.rb', line 81

def playing?; @playing || false; end

#run(run = true) ⇒ Object



28
29
30
31
# File 'lib/scruby/node.rb', line 28

def run run = true
  send '/n_run', self.id, run
  self
end

#send(command, *args) ⇒ Object

Sends a message to all registered servers for this node



96
97
98
99
100
# File 'lib/scruby/node.rb', line 96

def send command, *args
  message = Message.new command, *args
  @servers.each{ |s| s.send message }
  self
end

#send_bundle(timestamp, *messages) ⇒ Object

Sends a bundle to all registered servers for this node



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

def send_bundle timestamp, *messages
  bundle = Bundle.new( timestamp, *messages.map{ |message| Message.new *message } )
  @servers.each{ |s| s.send bundle  }
end

#set(args = {}) ⇒ Object



17
18
19
20
# File 'lib/scruby/node.rb', line 17

def set args = {}
  send '/n_set', self.id, *args.to_a.flatten
  self
end

#traceObject

mapn



54
55
56
57
# File 'lib/scruby/node.rb', line 54

def trace
  send '/n_trace', self.id
  self
end