class Music::Transcription::Part

Attributes

dynamic_changes[RW]
notes[RW]
start_dynamic[RW]

Public Class Methods

new(start_dynamic, notes: [], dynamic_changes: {}) { |self| ... } click to toggle source
# File lib/music-transcription/model/part.rb, line 11
def initialize start_dynamic, notes: [], dynamic_changes: {}
  @notes = notes
  @start_dynamic = start_dynamic
  @dynamic_changes = dynamic_changes
  
  yield(self) if block_given?
end
unpack(packing) click to toggle source
# File lib/music-transcription/packing/part_packing.rb, line 18
def self.unpack packing
  unpacked_notes = Note.split_parse(packing["notes"])
  unpacked_dcs = Hash[ packing["dynamic_changes"].map do |offset,change|
    [ offset,Change.unpack(change) ]
  end ]
  
  new(
    packing["start_dynamic"],
    notes: unpacked_notes,
    dynamic_changes: unpacked_dcs
  )
end

Public Instance Methods

==(other) click to toggle source
# File lib/music-transcription/model/part.rb, line 31
def ==(other)
  return (@notes == other.notes) &&
  (@start_dynamic == other.start_dynamic) &&
  (@dynamic_changes == other.dynamic_changes)
end
check_methods() click to toggle source
# File lib/music-transcription/model/part.rb, line 19
def check_methods
  [:ensure_start_dynamic, :ensure_dynamic_change_values_range ]
end
clone() click to toggle source
# File lib/music-transcription/model/part.rb, line 27
def clone
  Marshal.load(Marshal.dump(self))
end
duration() click to toggle source
# File lib/music-transcription/model/part.rb, line 37
def duration
  return @notes.inject(0) { |sum, note| sum + note.duration }
end
ensure_dynamic_change_values_range() click to toggle source
# File lib/music-transcription/model/part.rb, line 47
def ensure_dynamic_change_values_range
  outofrange = @dynamic_changes.values.select {|v| !v.value.between?(0,1) }
  if outofrange.any?
    raise RangeError, "dynamic change values #{outofrange} are not between 0 and 1"
  end
end
ensure_start_dynamic() click to toggle source
# File lib/music-transcription/model/part.rb, line 41
def ensure_start_dynamic
  unless @start_dynamic.between?(0,1)
    raise RangeError, "start dynamic #{@start_dynamic} is not between 0 and 1"
  end
end
pack() click to toggle source
# File lib/music-transcription/packing/part_packing.rb, line 5
def pack
  packed_notes = notes.map {|n| n.to_s }.join(" ")
  packed_dcs = Hash[ dynamic_changes.map do |offset,change|
    [ offset, change.pack ]
  end ]
    
  {
    'notes' => packed_notes,
    'start_dynamic' => start_dynamic,
    'dynamic_changes' => packed_dcs
  }
end
validatables() click to toggle source
# File lib/music-transcription/model/part.rb, line 23
def validatables
  @notes
end