Class: Music::Performance::ScoreTimeConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/music-performance/conversion/score_time_converter.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(score, sample_rate) ⇒ ScoreTimeConverter

Returns a new instance of ScoreTimeConverter.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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

Instance Method Details

#convert_partsObject

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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_programObject

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



62
63
64
65
66
67
68
69
# 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