Class: Music::Transcription::Score

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

Constant Summary collapse

@@check_methods =
[ :check_start_tempo, :check_tempo_changes, :check_meter_changes ]

Instance Attribute Summary collapse

Attributes included from Validatable

#errors

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Validatable

#check_methods, #invalid?, #valid?, #validate

Constructor Details

#initialize(start_meter, start_tempo, meter_changes: {}, tempo_changes: {}, parts: {}, program: Program.new) {|_self| ... } ⇒ Score

Returns a new instance of Score.

Yields:

  • (_self)

Yield Parameters:



10
11
12
13
14
15
16
17
18
19
# File 'lib/music-transcription/model/score.rb', line 10

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

Instance Attribute Details

#meter_changesObject

Returns the value of attribute meter_changes.



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

def meter_changes
  @meter_changes
end

#partsObject

Returns the value of attribute parts.



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

def parts
  @parts
end

#programObject

Returns the value of attribute program.



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

def program
  @program
end

#start_meterObject

Returns the value of attribute start_meter.



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

def start_meter
  @start_meter
end

#start_tempoObject

Returns the value of attribute start_tempo.



7
8
9
# File 'lib/music-transcription/model/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/score.rb', line 7

def tempo_changes
  @tempo_changes
end

Class Method Details

.unpack(packing) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/music-transcription/packing/score_packing.rb', line 33

def self.unpack packing
  unpacked_start_meter = Parsing::meter(packing["start_meter"])
  unpacked_mcs = Hash[ packing["meter_changes"].map do |k,v|
    v = v.clone
    v[0] = Parsing::meter(v[0])
    [k, Change.from_ary(v) ]
  end ]
  
  unpacked_tcs = Hash[ packing["tempo_changes"].map do |k,v|
    [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_start_meter, packing["start_tempo"],
    meter_changes: unpacked_mcs, tempo_changes: unpacked_tcs,
    program: unpacked_prog, parts: unpacked_parts
  )    
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/music-transcription/model/score.rb', line 53

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

#check_meter_changesObject



42
43
44
45
46
47
# File 'lib/music-transcription/model/score.rb', line 42

def check_meter_changes
  nonzero_duration = @meter_changes.select {|k,v| v.duration != 0 }
  if nonzero_duration.any?
    raise NonZeroError, "meter changes #{nonzero_duration} have non-zero duration"
  end
end

#check_start_tempoObject



29
30
31
32
33
# File 'lib/music-transcription/model/score.rb', line 29

def check_start_tempo
  unless @start_tempo > 0
    raise NonPositiveError, "start tempo #{@start_tempo} is not positive"
  end
end

#check_tempo_changesObject



35
36
37
38
39
40
# File 'lib/music-transcription/model/score.rb', line 35

def check_tempo_changes
  not_positive = @tempo_changes.select {|k,v| v.value <= 0}
  if not_positive.any?
    raise NonPositiveError, "tempo changes #{not_positive} are not positive"
  end
end

#cloneObject



49
50
51
# File 'lib/music-transcription/model/score.rb', line 49

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

#durationObject



62
63
64
# File 'lib/music-transcription/model/score.rb', line 62

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
26
27
28
29
30
31
# File 'lib/music-transcription/packing/score_packing.rb', line 5

def pack
  packed_start_meter = start_meter.to_s
  packed_mcs = Hash[ meter_changes.map do |offset,change|
    a = change.pack
    a[0] = a[0].to_s
    [offset,a]
  end ]

  packed_tcs = Hash[ tempo_changes.map do |k,v|
    [k,v.to_ary]
  end ]

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

#validatablesObject



21
22
23
24
25
26
27
# File 'lib/music-transcription/model/score.rb', line 21

def validatables
  return [ @program, @start_meter ] +
    @tempo_changes.values +
    @meter_changes.values + 
    @meter_changes.values.map {|v| v.value} +
    @parts.values 
end