Class: Musicality::Score::Tempo

Inherits:
Musicality::Score show all
Defined in:
lib/musicality/notation/model/score.rb,
lib/musicality/composition/dsl/score_methods.rb,
lib/musicality/performance/midi/score_sequencing.rb,
lib/musicality/printing/lilypond/score_engraving.rb,
lib/musicality/notation/conversion/score_conversion.rb,
lib/musicality/performance/supercollider/score_conducting.rb

Overview

Tempo-based score with meter, bar lines, and a fixed pulse (beat).

Offsets and durations are based on note duration, but note duration is determined by the tempo, which can change.

Tempo values are in beats-per-minute.

Constant Summary

Constants inherited from Musicality::Score

DEFAULT_START_DYNAMIC

Constants included from Packable

Packable::PACKED_CLASS_KEY

Instance Attribute Summary collapse

Attributes inherited from Musicality::Score

#composer, #key_changes, #parts, #program, #sections, #start_key, #title

Instance Method Summary collapse

Methods inherited from Musicality::Score

#clone, #collated?, #duration, #dynamic_change, #notes, #repeat, #section

Methods included from Validatable

#errors, #invalid?, #valid?, #validate

Methods included from Packable

#class_str, included, #init_params, #pack, pack_val, recover_class, unpack_val

Constructor Details

#initialize(start_meter, start_tempo, tempo_changes: {}, meter_changes: {}, parts: {}, program: [], title: nil, composer: nil, sections: {}, start_key: Keys::C_MAJOR, key_changes: {}) ⇒ Tempo

See Score#initialize for remaining kwargs



81
82
83
84
85
86
87
88
# File 'lib/musicality/notation/model/score.rb', line 81

def initialize start_meter, start_tempo, tempo_changes: {}, meter_changes: {}, parts: {}, program: [], title: nil, composer: nil, sections: {}, start_key: Keys::C_MAJOR, key_changes: {}
  @start_tempo = start_tempo
  @tempo_changes = tempo_changes
  @start_meter = start_meter
  @meter_changes = meter_changes
  
  super(parts: parts, program: program, title: title, composer: composer, sections: sections, start_key: start_key, key_changes: key_changes)
end

Instance Attribute Details

#meter_changesObject

Returns the value of attribute meter_changes.



78
79
80
# File 'lib/musicality/notation/model/score.rb', line 78

def meter_changes
  @meter_changes
end

#start_meterObject

Returns the value of attribute start_meter.



78
79
80
# File 'lib/musicality/notation/model/score.rb', line 78

def start_meter
  @start_meter
end

#start_tempoObject

Returns the value of attribute start_tempo.



78
79
80
# File 'lib/musicality/notation/model/score.rb', line 78

def start_tempo
  @start_tempo
end

#tempo_changesObject

Returns the value of attribute tempo_changes.



78
79
80
# File 'lib/musicality/notation/model/score.rb', line 78

def tempo_changes
  @tempo_changes
end

Instance Method Details

#==(other) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/musicality/notation/model/score.rb', line 98

def ==(other)
  return super(other) &&
  @start_tempo == other.start_tempo &&
  @tempo_changes == other.tempo_changes
  @start_meter == other.start_meter &&
  @meter_changes == other.meter_changes
end

#beat_durationsObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/musicality/notation/conversion/score_conversion.rb', line 11

def beat_durations
  bdurs = @meter_changes.map do |offset,change_or_meter|
    if change_or_meter.is_a? Meter
      [ offset, change_or_meter.beat_duration ]
    else
      [ offset, change_or_meter.end_value.beat_duration ]
    end
  end.sort
  
  if bdurs.empty? || bdurs[0][0] != 0
    bdurs.unshift([0.to_r,@start_meter.beat_duration])
  end

  return Hash[ bdurs ]
end

#check_methodsObject



90
91
92
# File 'lib/musicality/notation/model/score.rb', line 90

def check_methods
  super() + [:check_start_tempo, :check_tempo_changes, :check_start_meter, :check_meter_changes]
end

#key_change(new_key, offset: 0) ⇒ Object



73
74
75
# File 'lib/musicality/composition/dsl/score_methods.rb', line 73

def key_change new_key, offset: 0
  key_changes[self.duration + offset] = Change::Immediate.new(new_key)
end

#measure_duration(note_offset = self.duration) ⇒ Object

Returns the measure duration of the most recent meter duration since the given note offset, or of the start meter if there are no meter changes.



108
109
110
111
112
113
114
115
# File 'lib/musicality/notation/model/score.rb', line 108

def measure_duration note_offset = self.duration
  if meter_changes.any?
    candidates = meter_changes.select {|noff,change| noff <= note_offset }
    candidates.max[1].end_value.measure_duration
  else
    start_meter.measure_duration
  end
end

#meter_change(new_meter, offset: 0) ⇒ Object



69
70
71
# File 'lib/musicality/composition/dsl/score_methods.rb', line 69

def meter_change new_meter, offset: 0
  meter_changes[self.duration + offset] = Change::Immediate.new(new_meter)
end

#tempo_change(new_tempo, transition_dur: 0, offset: 0) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/musicality/composition/dsl/score_methods.rb', line 61

def tempo_change new_tempo, transition_dur: 0, offset: 0
  if transition_dur == 0
    tempo_changes[self.duration + offset] = Change::Immediate.new(new_tempo)
  else
    tempo_changes[self.duration + offset] = Change::Gradual.linear(new_tempo, transition_dur)
  end
end

#to_lilypond(selected_parts = @parts.keys) ⇒ Object

See ScoreEngraver#make_lilypond for details on supported keyword args



6
7
8
# File 'lib/musicality/printing/lilypond/score_engraving.rb', line 6

def to_lilypond selected_parts = @parts.keys
  ScoreEngraver.new(self).make_lilypond(selected_parts)
end

#to_midi_seq(tempo_sample_rate, **kwargs) ⇒ Object



11
12
13
# File 'lib/musicality/performance/midi/score_sequencing.rb', line 11

def to_midi_seq tempo_sample_rate, **kwargs
  to_timed(tempo_sample_rate).to_midi_seq(**kwargs)
end

#to_osc(tempo_sample_rate, base_fpath, **kwargs) ⇒ Object



11
12
13
# File 'lib/musicality/performance/supercollider/score_conducting.rb', line 11

def to_osc tempo_sample_rate, base_fpath, **kwargs
  to_timed(tempo_sample_rate).to_osc(base_fpath, **kwargs)
end

#to_timed(tempo_sample_rate) ⇒ Object

Convert to timed score by converting note-based offsets and durations to time-based. This eliminates the use of meters and tempos.



7
8
9
# File 'lib/musicality/notation/conversion/score_conversion.rb', line 7

def to_timed tempo_sample_rate
  ScoreConverter.new(self, tempo_sample_rate).convert_score
end

#validatablesObject



94
95
96
# File 'lib/musicality/notation/model/score.rb', line 94

def validatables
  super() + [ @start_meter ] + @meter_changes.values.map {|v| v.end_value}
end