Class: Music::Transcription::NoteScore

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

Direct Known Subclasses

MeasureScore

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_tempo, tempo_changes: {}, parts: {}, program: Program.new) {|_self| ... } ⇒ NoteScore

Returns a new instance of NoteScore.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
15
16
# File 'lib/music-transcription/model/note_score.rb', line 9

def initialize start_tempo, tempo_changes: {}, parts: {}, program: Program.new
  @start_tempo = start_tempo
  @tempo_changes = tempo_changes
  @parts = parts
  @program = program
  
  yield(self) if block_given?
end

Instance Attribute Details

#partsObject

Returns the value of attribute parts.



7
8
9
# File 'lib/music-transcription/model/note_score.rb', line 7

def parts
  @parts
end

#programObject

Returns the value of attribute program.



7
8
9
# File 'lib/music-transcription/model/note_score.rb', line 7

def program
  @program
end

#start_tempoObject

Returns the value of attribute start_tempo.



7
8
9
# File 'lib/music-transcription/model/note_score.rb', line 7

def start_tempo
  @start_tempo
end

#tempo_changesObject

Returns the value of attribute tempo_changes.



7
8
9
# File 'lib/music-transcription/model/note_score.rb', line 7

def tempo_changes
  @tempo_changes
end

Class Method Details

.unpack(packing) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/music-transcription/packing/note_score_packing.rb', line 27

def self.unpack packing
  unpacked_starttempo = Tempo.parse(packing["start_tempo"])
  unpacked_tcs = Hash[ packing["tempo_changes"].map do |k,v|
    v = v.clone
    v[0] = Tempo.parse(v[0])
    [k, Change.from_ary(v) ]
  end ]
  
  unpacked_parts = Hash[ packing["parts"].map do |name,packed|
    [name, Part.unpack(packed)]
  end ]
  
  unpacked_prog = Program.unpack packing["program"]
  
  new(unpacked_starttempo,
    tempo_changes: unpacked_tcs,
    program: unpacked_prog,
    parts: unpacked_parts
  )
end

.valid_tempo_typesObject



26
27
28
# File 'lib/music-transcription/model/note_score.rb', line 26

def self.valid_tempo_types
  [ Tempo::QNPM, Tempo::NPM, Tempo::NPS ]
end

Instance Method Details

#==(other) ⇒ Object



49
50
51
52
53
54
# File 'lib/music-transcription/model/note_score.rb', line 49

def ==(other)
  return @start_tempo == other.start_tempo &&
  @tempo_changes == other.tempo_changes &&
  @parts == other.parts &&
  @program == other.program
end

#check_methodsObject



18
19
20
# File 'lib/music-transcription/model/note_score.rb', line 18

def check_methods
  [ :check_start_tempo_type, :check_tempo_change_types ]
end

#check_start_tempo_typeObject



30
31
32
33
34
35
# File 'lib/music-transcription/model/note_score.rb', line 30

def check_start_tempo_type
  vtts = self.class.valid_tempo_types
  unless vtts.include?(@start_tempo.class)
    raise TypeError, "type of start tempo #{@start_tempo} is not one of valid tempo types: #{vtts}"
  end
end

#check_tempo_change_typesObject



37
38
39
40
41
42
43
# File 'lib/music-transcription/model/note_score.rb', line 37

def check_tempo_change_types
  vtts = self.class.valid_tempo_types
  baddtypes = @tempo_changes.select {|k,v| !vtts.include?(v.value.class) }
  if baddtypes.any?
    raise NonPositiveError, "type of tempo change values #{baddtypes} are not one of valid tempo types: #{vtts}"
  end
end

#cloneObject



45
46
47
# File 'lib/music-transcription/model/note_score.rb', line 45

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

#durationObject



56
57
58
# File 'lib/music-transcription/model/note_score.rb', line 56

def duration
  @parts.map {|p| p.duration }.max
end

#packObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/music-transcription/packing/note_score_packing.rb', line 5

def pack
  packed_starttempo = start_tempo.to_s
  packed_tcs = Hash[ tempo_changes.map do |offset,change|
    a = change.pack
    a[0] = a[0].to_s
    [offset,a]
  end ]

  packed_parts = Hash[
    @parts.map do |name,part|
      [ name, part.pack ]
    end
  ]
  packed_prog = program.pack
  
  { "start_tempo" => packed_starttempo,
    "tempo_changes" => packed_tcs,
    "program" => packed_prog,
    "parts" => packed_parts,
  }
end

#validatablesObject



22
23
24
# File 'lib/music-transcription/model/note_score.rb', line 22

def validatables
  [ @program ] + @parts.values
end