Class: MIDI::Measures

Inherits:
Array
  • Object
show all
Defined in:
lib/midilib/measure.rb

Overview

A specialized container for MIDI::Measure objects, which can be use to map event times to measure numbers. Please note that this object has to be remade when events are deleted/added in the sequence.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Array

#mergesort

Constructor Details

#initialize(max_time, ppqd) ⇒ Measures

Constructor


57
58
59
60
61
# File 'lib/midilib/measure.rb', line 57

def initialize(max_time, ppqd)
  super(0)
  @max_time = max_time
  @ppqd = ppqd
end

Instance Attribute Details

#max_timeObject (readonly)

The highest event time in the sequence (at the time when the object was created)


51
52
53
# File 'lib/midilib/measure.rb', line 51

def max_time
  @max_time
end

#ppqdObject (readonly)

The ppqd from the sequence


54
55
56
# File 'lib/midilib/measure.rb', line 54

def ppqd
  @ppqd
end

Instance Method Details

#measure_for_event(e) ⇒ Object

Returns the MIDI::Measure object where the event is located. Returns nil if the event isn't found in the container (should never happen if the MIDI::Measures object is up to date).


66
67
68
# File 'lib/midilib/measure.rb', line 66

def measure_for_event(e)
  detect { |m| m.contains_event?(e) }
end

#to_mbt(e) ⇒ Object

Returns the event's time as a formatted MBT string (Measure:Beat:Ticks) as found in MIDI sequencers.


72
73
74
75
76
77
# File 'lib/midilib/measure.rb', line 72

def to_mbt(e)
  m = measure_for_event(e)
  b = (e.time_from_start.to_f - m.start.to_f) / @ppqd
  b *= 24 / m.metronome_ticks
  sprintf("%d:%02d:%03d", m.measure_number, b.to_i + 1, (b - b.to_i) * @ppqd)
end