1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#--
# This file is part of Sonic Pi: http://sonic-pi.net
# Full project source: https://github.com/samaaron/sonic-pi
# License: https://github.com/samaaron/sonic-pi/blob/master/LICENSE.md
#
# Copyright 2013, 2014, 2015, 2016 by Sam Aaron (http://sam.aaron.name).
# All rights reserved.
#
# Permission is granted for use, copying, modification, and
# distribution of modified versions of this work as long as this
# notice is included.
#++
require_relative "node"
module SonicPi
class Group < Node
def initialize(id, comms, name="")
super(id, comms)
@nodes = {}
@pending_nodes = {}
@pending_nodes_mut = Mutex.new
@nodes_mut = Mutex.new
@name = name
@on_destroyed_callbacks << lambda do
@pending_nodes.values.each do |pn|
# These nodes have been triggered from Sonic Pi, but the
# containing group must have been killed on the server before
# the messages managed to get out. Emulate the server's node
# end OSC message.
# It's therefore possible that multiple of these messages
# could be sent - however, this won't cause any issues as the
# node won't run on_destroyed handlers multiple times and the
# default /n_end handlers remove themselves.
@comms.async_event ['/n_end/', pn.id], {}
end
end
end
def subnode_add(n)
@nodes_mut.synchronize do
@nodes[n.id] = n
end
@pending_nodes_mut.synchronize do
@pending_nodes[n.id] = n
end
n.on_started do
@pending_nodes_mut.synchronize do
@pending_nodes.delete(n.id)
end
end
end
def subnode_rm(n)
@nodes_mut.synchronize do
@nodes.delete n.id
end
end
def clear
@comms.group_clear @id
self
end
def deep_clear
@comms.group_deep_clear @id
self
end
def to_s
"#<SonicPi::Group #{@name} @id=#{@id}>"
end
def inspect
"[#{@name}:#{@id}]"
end
end
end
|