Class: Zgomot::Midi::Stream
Class Attribute Summary collapse
-
.streams ⇒ Object
readonly
Returns the value of attribute streams.
Instance Attribute Summary collapse
-
#ch ⇒ Object
readonly
Returns the value of attribute ch.
-
#count ⇒ Object
Returns the value of attribute count.
-
#delay ⇒ Object
readonly
Returns the value of attribute delay.
-
#limit ⇒ Object
readonly
Returns the value of attribute limit.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#patterns ⇒ Object
readonly
Returns the value of attribute patterns.
-
#play_meth ⇒ Object
readonly
Returns the value of attribute play_meth.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
Class Method Summary collapse
- .apply_to_stream(name) ⇒ Object
- .pause(name = nil) ⇒ Object (also: stop)
- .play(name = nil) ⇒ Object (also: run)
- .str(name, pattern = nil, opts = {}, &blk) ⇒ Object
- .tog(name) ⇒ Object
Instance Method Summary collapse
- #dispatch ⇒ Object
- #info ⇒ Object
-
#initialize(name, arity, pattern, opts) ⇒ Stream
constructor
A new instance of Stream.
- #status_eql?(test_status) ⇒ Boolean
- #update_status(new_status) ⇒ Object
Constructor Details
#initialize(name, arity, pattern, opts) ⇒ Stream
Returns a new instance of Stream.
60 61 62 63 64 65 66 67 |
# File 'lib/zgomot/midi/stream.rb', line 60 def initialize(name, arity, pattern, opts) @patterns = [Zgomot::Comp::Pattern.new(pattern)] @delay = (opts[:del].to_f * 60.0/ Zgomot.config[:beats_per_minute].to_f).to_i || 0 @limit, @name, @thread, @status, @count = opts[:lim] || :inf, name, nil, :paused, 0 @ch = Zgomot::Midi::Channel.ch(opts[:ch] || 0) @play_meth = "play#{arity.eql?(-1) ? 0 : arity}".to_sym @status_mutex = Mutex.new end |
Class Attribute Details
.streams ⇒ Object (readonly)
Returns the value of attribute streams.
8 9 10 |
# File 'lib/zgomot/midi/stream.rb', line 8 def streams @streams end |
Instance Attribute Details
#ch ⇒ Object (readonly)
Returns the value of attribute ch.
57 58 59 |
# File 'lib/zgomot/midi/stream.rb', line 57 def ch @ch end |
#count ⇒ Object
Returns the value of attribute count.
56 57 58 |
# File 'lib/zgomot/midi/stream.rb', line 56 def count @count end |
#delay ⇒ Object (readonly)
Returns the value of attribute delay.
57 58 59 |
# File 'lib/zgomot/midi/stream.rb', line 57 def delay @delay end |
#limit ⇒ Object (readonly)
Returns the value of attribute limit.
57 58 59 |
# File 'lib/zgomot/midi/stream.rb', line 57 def limit @limit end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
57 58 59 |
# File 'lib/zgomot/midi/stream.rb', line 57 def name @name end |
#patterns ⇒ Object (readonly)
Returns the value of attribute patterns.
57 58 59 |
# File 'lib/zgomot/midi/stream.rb', line 57 def patterns @patterns end |
#play_meth ⇒ Object (readonly)
Returns the value of attribute play_meth.
57 58 59 |
# File 'lib/zgomot/midi/stream.rb', line 57 def play_meth @play_meth end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
57 58 59 |
# File 'lib/zgomot/midi/stream.rb', line 57 def status @status end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
57 58 59 |
# File 'lib/zgomot/midi/stream.rb', line 57 def thread @thread end |
Class Method Details
.apply_to_stream(name) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/zgomot/midi/stream.rb', line 46 def apply_to_stream(name) stream = streams.values.find{|s| s.name == name.to_s} if stream yield stream else Zgomot.logger.error "STREAM '#{name}' NOT FOUND"; nil end end |
.pause(name = nil) ⇒ Object Also known as: stop
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/zgomot/midi/stream.rb', line 28 def pause(name=nil) if name.nil? streams.values.each do |stream| stream.update_status(:paused) end; true else apply_to_stream(name) do |stream| stream.update_status(:paused) name end end end |
.play(name = nil) ⇒ Object Also known as: run
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/zgomot/midi/stream.rb', line 14 def play(name=nil) if name.nil? streams.values.reduce([]) do |a, s| if s.status_eql?(:paused) s.dispatch a << s.name end; a end else apply_to_stream(name){|stream| stream.status_eql?(:paused) ? (stream.dispatch; name) : nil} end end |
.str(name, pattern = nil, opts = {}, &blk) ⇒ Object
9 10 11 12 13 |
# File 'lib/zgomot/midi/stream.rb', line 9 def str(name, pattern=nil, opts={}, &blk) strm = new(name, blk.arity, pattern, opts) strm.(:play, &blk) @streams[name] = strm end |
.tog(name) ⇒ Object
41 42 43 44 45 |
# File 'lib/zgomot/midi/stream.rb', line 41 def tog(name) apply_to_stream(name) do |stream| stream.status_eql?(:playing) ? pause(name) : play(name) end end |
Instance Method Details
#dispatch ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/zgomot/midi/stream.rb', line 81 def dispatch @count = 0 ch.set_clock update_status(:playing) @thread = Thread.new do while(status_eql?(:playing)) do @count += 1 break if not limit.eql?(:inf) and count > limit if self.respond_to?(play_meth, true) if pattern = self.send(play_meth) ch << pattern Dispatcher.enqueue(ch) else; break; end else raise(Zgomot::Error, 'str block arity not supported') end Zgomot.logger.info "STREAM:#{count}:#{name}" patterns << Zgomot::Comp::Pattern.new(ch.pattern) sleep(ch.length_to_sec) end Zgomot.logger.info "STREAM FINISHED:#{name}" update_status(:paused) end end |
#info ⇒ Object
78 79 80 |
# File 'lib/zgomot/midi/stream.rb', line 78 def info [name, status, ch.number, ch.clock, count, limit, delay].map(&:to_s) end |
#status_eql?(test_status) ⇒ Boolean
73 74 75 76 77 |
# File 'lib/zgomot/midi/stream.rb', line 73 def status_eql?(test_status) @status_mutex.synchronize do @status == test_status end end |
#update_status(new_status) ⇒ Object
68 69 70 71 72 |
# File 'lib/zgomot/midi/stream.rb', line 68 def update_status(new_status) @status_mutex.synchronize do @status = new_status end end |