Class: Music::Transcription::Part

Inherits:
Object
  • Object
show all
Includes:
Validatable
Defined in:
lib/music-transcription/model/part.rb,
lib/music-transcription/packing/part_packing.rb

Instance Attribute Summary collapse

Attributes included from Validatable

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validatable

#invalid?, #valid?, #validate

Constructor Details

#initialize(start_dynamic, notes: [], dynamic_changes: {}) {|_self| ... } ⇒ Part

Returns a new instance of Part.

Yields:

  • (_self)

Yield Parameters:



11
12
13
14
15
16
17
# 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

Instance Attribute Details

#dynamic_changesObject

Returns the value of attribute dynamic_changes.



9
10
11
# File 'lib/music-transcription/model/part.rb', line 9

def dynamic_changes
  @dynamic_changes
end

#notesObject

Returns the value of attribute notes.



9
10
11
# File 'lib/music-transcription/model/part.rb', line 9

def notes
  @notes
end

#start_dynamicObject

Returns the value of attribute start_dynamic.



9
10
11
# File 'lib/music-transcription/model/part.rb', line 9

def start_dynamic
  @start_dynamic
end

Class Method Details

.unpack(packing) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# 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

Instance Method Details

#==(other) ⇒ Object



31
32
33
34
35
# 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_methodsObject



19
20
21
# File 'lib/music-transcription/model/part.rb', line 19

def check_methods
  [:ensure_start_dynamic, :ensure_dynamic_change_values_range ]
end

#cloneObject



27
28
29
# File 'lib/music-transcription/model/part.rb', line 27

def clone
  Marshal.load(Marshal.dump(self))
end

#durationObject



37
38
39
# 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_rangeObject



47
48
49
50
51
52
# 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_dynamicObject



41
42
43
44
45
# 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

#packObject



5
6
7
8
9
10
11
12
13
14
15
16
# 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

#validatablesObject



23
24
25
# File 'lib/music-transcription/model/part.rb', line 23

def validatables
  @notes
end