class SMF::Track

Public Class Methods

new() click to toggle source
# File lib/smf.rb, line 130
def initialize() @arr = [] end

Public Instance Methods

&(other) click to toggle source
# File lib/smf.rb, line 175
def & (other) calc_set_selfsize(self, other){|a, b| a & b} end
*(times) click to toggle source
# File lib/smf.rb, line 178
def * (times)
  tr = self.class.new
  size = self[-1].offset
  eot, orig = partition{|x| EndOfTrack === x}
  times.times do |i|
    tr += orig.collect{|ev| ev = ev.dup; ev.offset += size * i; ev}
  end
  unless eot.empty?
    x = eot.max
    x.offset += size * times
    tr << x
  end
  tr
end
+(other) click to toggle source
# File lib/smf.rb, line 173
def + (other) calc_set_maxsize(self, other){|a, b| a + b} end
-(other) click to toggle source
# File lib/smf.rb, line 174
def - (other) calc_set_selfsize(self, other){|a, b| a - b} end
<<(ev) click to toggle source
# File lib/smf.rb, line 193
def << (ev)
  @arr << ev
  self
end
==(other) click to toggle source
# File lib/smf.rb, line 147
def == (other)
  self.class == other.class &&
    to_a == other.to_a
end
>>(ev) click to toggle source
# File lib/smf.rb, line 198
def >> (ev)
  @arr.reject!{|x| x.object_id == ev.object_id}
  self
end
concat!(other) click to toggle source
# File lib/smf.rb, line 203
def concat!(other) replace(self + other) end
divert(sq) { |ev| ... } click to toggle source
# File lib/smf/divert.rb, line 8
def divert(sq)
  each do |ev|
    n = yield ev
    if n
      sq[n] ||= Track.new
      sq[n] << ev
    end
  end
  self
end
each() { |ev| ... } click to toggle source
# File lib/smf.rb, line 205
def each
  i = 0
  @arr.compact.sort_by{|x| [x, i += 1]}.
    each do |ev|
    yield ev
  end
  self
end
eql?(other) click to toggle source
# File lib/smf.rb, line 145
def eql? (other) self == other end
hash() click to toggle source
# File lib/smf.rb, line 144
def hash() @arr.hash end
nevts() click to toggle source
# File lib/smf.rb, line 134
def nevts() @arr.count{|x| x} end
replace(another) click to toggle source
# File lib/smf.rb, line 252
def replace(another)
  @arr.replace(another.to_a)
  self
end
sort(&block) click to toggle source
# File lib/smf.rb, line 238
def sort(&block)
  block ||= proc{|a, b| a <=> b}
  _sort(@arr, &block)
end
sort!(&block) click to toggle source
# File lib/smf.rb, line 243
def sort!(&block)
  replace(sort(&block))
  self
end
sort_by(&block) click to toggle source
# File lib/smf.rb, line 248
def sort_by(&block)
  _sort_by(@arr, &block)
end
to_virtual() click to toggle source
# File lib/smf/toy/virtual.rb, line 20
def to_virtual
  v = VirtualTrack.new
  xs = []
  each do |ev|
    case ev
    when NoteOn
      xs.push(ev)
    when NoteOff
      on = nil
      idx = 0
      xs.each_with_index do |x, i|
        idx = i
        if x.ch == ev.ch && x.note == ev.note
          on = x
          break
        end
      end
      if on
        v << VirtualNote.new(on.offset, on.ch, on.note,
                             on.vel, ev.vel, ev.offset - on.offset)
        xs.delete_at(idx)
      end
    else
      v << ev
    end
  end
  v
end
|(other) click to toggle source
# File lib/smf.rb, line 176
def | (other) calc_set_maxsize(self, other){|a, b| a | b} end

Private Instance Methods

_merge(a, b) { |a, b| ... } click to toggle source
# File lib/smf.rb, line 214
def _merge(a, b, &block)
  c = []
  until a.empty? || b.empty?
    c << if yield(a[0], b[0]) <= 0 then a.shift else b.shift end
  end
  c + a + b
end
_sort(xs, &block) click to toggle source
# File lib/smf.rb, line 222
def _sort(xs, &block)
  mid = (xs.size / 2).truncate
  if mid < 1
    xs
  else
    _merge(_sort(xs[0...mid], &block), _sort(xs[mid..-1], &block), &block)
  end
end
_sort_by(xs) { |x| ... } click to toggle source
# File lib/smf.rb, line 231
def _sort_by(xs)
  _sort(xs.collect{|x| [x, yield(x)]}){|a, b| a[1] <=> b[1]}.
    collect!{|x| x[0]}
end
calc_set_maxsize(a, b, &block) click to toggle source
# File lib/smf.rb, line 161
def calc_set_maxsize(a, b, &block)
  a, b = a.to_a, b.to_a
  calc_set_sub(a, b, a + b, &block)
end
calc_set_selfsize(a, b, &block) click to toggle source
# File lib/smf.rb, line 166
def calc_set_selfsize(a, b, &block)
  a, b = a.to_a, b.to_a
  calc_set_sub(a, b, a, &block)
end
calc_set_sub(a, b, e) { |a, b| ... } click to toggle source
# File lib/smf.rb, line 152
def calc_set_sub(a, b, e)
  tr = self.class.new
  n = yield(a, b).reject{|x| EndOfTrack === x}
  eot = e.select{|x| EndOfTrack === x}
  n << eot.max unless eot.empty?
  tr.replace(n)
  tr
end