Class: Zgomot::Midi::Stream

Inherits:
Object show all
Defined in:
lib/zgomot/midi/stream.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, arity, pattern, opts) ⇒ Stream

Returns a new instance of Stream.



66
67
68
69
70
71
72
73
# File 'lib/zgomot/midi/stream.rb', line 66

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

.streamsObject (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

#chObject (readonly)

Returns the value of attribute ch.



63
64
65
# File 'lib/zgomot/midi/stream.rb', line 63

def ch
  @ch
end

#countObject

Returns the value of attribute count.



62
63
64
# File 'lib/zgomot/midi/stream.rb', line 62

def count
  @count
end

#delayObject (readonly)

Returns the value of attribute delay.



63
64
65
# File 'lib/zgomot/midi/stream.rb', line 63

def delay
  @delay
end

#limitObject (readonly)

Returns the value of attribute limit.



63
64
65
# File 'lib/zgomot/midi/stream.rb', line 63

def limit
  @limit
end

#nameObject (readonly)

Returns the value of attribute name.



63
64
65
# File 'lib/zgomot/midi/stream.rb', line 63

def name
  @name
end

#patternsObject (readonly)

Returns the value of attribute patterns.



63
64
65
# File 'lib/zgomot/midi/stream.rb', line 63

def patterns
  @patterns
end

#play_methObject (readonly)

Returns the value of attribute play_meth.



63
64
65
# File 'lib/zgomot/midi/stream.rb', line 63

def play_meth
  @play_meth
end

#statusObject (readonly)

Returns the value of attribute status.



63
64
65
# File 'lib/zgomot/midi/stream.rb', line 63

def status
  @status
end

#threadObject (readonly)

Returns the value of attribute thread.



63
64
65
# File 'lib/zgomot/midi/stream.rb', line 63

def thread
  @thread
end

Class Method Details

.apply_to_stream(name) ⇒ Object



52
53
54
55
56
57
58
59
# File 'lib/zgomot/midi/stream.rb', line 52

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

.delete(name) ⇒ Object



46
47
48
49
50
51
# File 'lib/zgomot/midi/stream.rb', line 46

def delete(name)
  apply_to_stream(name) do |stream|
    stream.update_status(:paused)
    streams.delete(name.to_s).name
  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.define_meta_class_method(: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

#dispatchObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/zgomot/midi/stream.rb', line 87

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)
                if ch.length_to_sec > 0.0
                  sleep(ch.length_to_sec)
                else
                  break
                end
              end
    Zgomot.logger.info "STREAM FINISHED:#{name}"
    update_status(:paused)
  end
end

#infoObject



84
85
86
# File 'lib/zgomot/midi/stream.rb', line 84

def info
  [name, status, ch.number, ch.clock, count, limit, delay].map(&:to_s)
end

#status_eql?(test_status) ⇒ Boolean

Returns:

  • (Boolean)


79
80
81
82
83
# File 'lib/zgomot/midi/stream.rb', line 79

def status_eql?(test_status)
  @status_mutex.synchronize do
    @status == test_status
  end
end

#update_status(new_status) ⇒ Object



74
75
76
77
78
# File 'lib/zgomot/midi/stream.rb', line 74

def update_status(new_status)
  @status_mutex.synchronize do
    @status = new_status
  end
end