class Music::Performance::ScoreTimeConverter

Utility class to convert a score from note-based to time-based offsets

Public Class Methods

new(score, sample_rate) click to toggle source
# File lib/music-performance/conversion/score_time_converter.rb, line 9
def initialize score, sample_rate
  tempo_computer = ValueComputer.new(
    score.start_tempo, score.tempo_changes)
  
  bdcs = Hash[ score.meter_changes.map do |offset,change|
    newchange = change.clone
    newchange.value = change.value.beat_duration
    [offset, newchange]
  end ]
  beat_duration_computer = ValueComputer.new(
    score.start_meter.beat_duration, bdcs)
  
  @note_time_converter = NoteTimeConverter.new(
    tempo_computer, beat_duration_computer, sample_rate)
  @score = score
  @note_time_map = make_note_time_map(gather_all_offsets)
end

Public Instance Methods

convert_parts() click to toggle source

Convert note-based offsets & durations to time-based.

# File lib/music-performance/conversion/score_time_converter.rb, line 28
def convert_parts
  newparts = {}
  @score.parts.each do |name,part|
    offset = 0
    
    newnotes = part.notes.map do |note|
      starttime = @note_time_map[offset]
      endtime = @note_time_map[offset + note.duration]
      offset += note.duration
      newnote = note.clone
      newnote.duration = endtime - starttime
      newnote
    end
    
    new_dcs = Hash[
      part.dynamic_changes.map do |offset, change|
        timeoffset = @note_time_map[offset]
        newchange = change.clone
        newchange.duration = @note_time_map[offset + change.duration]
        
        [timeoffset,newchange]
      end
    ]
    
    newparts[name] = Music::Transcription::Part.new(
      part.start_dynamic,
      notes: newnotes,
      dynamic_changes: new_dcs
    )
  end
  return newparts
end
convert_program() click to toggle source

Convert note-based offsets & durations to time-based.

# File lib/music-performance/conversion/score_time_converter.rb, line 62
def convert_program
  newsegments = @score.program.segments.map do |segment|
    first = @note_time_map[segment.first]
    last = @note_time_map[segment.last]
    first...last
  end
  Program.new(newsegments)
end

Private Instance Methods

gather_all_offsets() click to toggle source
# File lib/music-performance/conversion/score_time_converter.rb, line 73
def gather_all_offsets
  note_offsets = Set.new [0]
  
  @score.parts.each do |name, part|
    offset = 0.to_r
    part.notes.each do |note|
      offset += note.duration
      note_offsets << offset
    end
    
    part.dynamic_changes.each do |change_offset, change|
      note_offsets << change_offset
      note_offsets << (change_offset + change.duration)
    end
  end
  
  @score.program.segments.each do |segment|
    note_offsets << segment.first
    note_offsets << segment.last
  end

  return note_offsets
end
make_note_time_map(note_offsets) click to toggle source
# File lib/music-performance/conversion/score_time_converter.rb, line 97
def make_note_time_map note_offsets
  return @note_time_converter.map_note_offsets_to_time_offsets note_offsets
end