Module: Music::Transcription::Conversion

Defined in:
lib/music-transcription/conversion/measure_note_map.rb

Class Method Summary collapse

Class Method Details

.measure_note_map(measure_offsets, measure_durations) ⇒ Object

Converte offsets from measure-based to note-based.

Parameters:

  • measure_offsets (Array)

    Measure offsets to be converted

  • measure_durations (Hash)

    Map measure durations to measure offsets where the duration takes effect.

Raises:

  • (NonZeroError)

    if first measure duration is not mapped to offset 0



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
# File 'lib/music-transcription/conversion/measure_note_map.rb', line 9

def self.measure_note_map measure_offsets, measure_durations
  mnoff_map = {}
  moffs = measure_offsets.uniq.sort
  mdurs = measure_durations.sort
  cur_noff = 0.to_r
  j = 0 # next measure offset to be converted
  
  if mdurs[0][0] != 0
    raise NonZeroError, "measure offset of 1st measure duration must be 0, not #{mdurs[0][0]}"
  end
  
  (0...mdurs.size).each do |i|
    cur_moff, cur_mdur = mdurs[i]
    if i < (mdurs.size - 1)
      next_moff = mdurs[i+1][0]        
    else
      next_moff = Float::INFINITY
    end
    
    while(j < moffs.size && moffs[j] <= next_moff) do
      moff = moffs[j]
      mnoff_map[moff] = cur_noff + (moff - cur_moff)*cur_mdur
      j += 1
    end
    
    cur_noff += (next_moff - cur_moff) * cur_mdur
  end
  
  return mnoff_map
end