Class: Musicality::Score

Inherits:
Object
  • Object
show all
Includes:
Packable, Validatable
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

Direct Known Subclasses

Tempo, Timed

Defined Under Namespace

Classes: Tempo, Timed

Constant Summary collapse

DEFAULT_START_DYNAMIC =
Dynamics::MF

Constants included from Packable

Packable::PACKED_CLASS_KEY

Instance Attribute Summary collapse

Instance Method Summary collapse

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(parts: {}, program: [], title: nil, composer: nil, sections: {}, start_key: Keys::C_MAJOR, key_changes: {}, auditions: []) {|_self| ... } ⇒ Score

Returns a new instance of Score.

Yields:

  • (_self)

Yield Parameters:



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/musicality/notation/model/score.rb', line 32

def initialize parts: {}, program: [], title: nil, composer: nil, sections: {}, start_key: Keys::C_MAJOR, key_changes: {}, auditions: []
  @parts = parts
  @program = program
  @title = title
  @composer = composer
  @sections = sections
  @start_key = start_key
  @key_changes = key_changes
  @auditions = auditions
  yield(self) if block_given?
end

Instance Attribute Details

#auditionsObject

Returns the value of attribute auditions.



13
14
15
# File 'lib/musicality/notation/model/score.rb', line 13

def auditions
  @auditions
end

#composer(value = nil) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/musicality/notation/model/score.rb', line 24

def composer value = nil
  if value.nil?
    return @composer
  else
    @composer = value
  end
end

#key_changesObject

Returns the value of attribute key_changes.



13
14
15
# File 'lib/musicality/notation/model/score.rb', line 13

def key_changes
  @key_changes
end

#partsObject

Returns the value of attribute parts.



13
14
15
# File 'lib/musicality/notation/model/score.rb', line 13

def parts
  @parts
end

#programObject

Returns the value of attribute program.



13
14
15
# File 'lib/musicality/notation/model/score.rb', line 13

def program
  @program
end

#sectionsObject

Returns the value of attribute sections.



13
14
15
# File 'lib/musicality/notation/model/score.rb', line 13

def sections
  @sections
end

#start_keyObject

Returns the value of attribute start_key.



13
14
15
# File 'lib/musicality/notation/model/score.rb', line 13

def start_key
  @start_key
end

#title(value = nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/musicality/notation/model/score.rb', line 16

def title value = nil
  if value.nil?
    return @title
  else
    @title = value
  end
end

Instance Method Details

#==(other) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/musicality/notation/model/score.rb', line 53

def ==(other)
  return self.class == other.class &&
    @parts == other.parts && @program == other.program &&
    @program == other.program && @sections == other.sections &&
    @title == other.title && @composer == other.composer &&
    @start_key == other.start_key && @key_changes == other.key_changes
end

#audition(part_name, program, &block) ⇒ Object



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

def audition part_name, program, &block
  audition = Audition.new(part_name, program)
  audition.instance_eval(&block)
  @auditions.push audition
end

#check_methodsObject



45
46
47
# File 'lib/musicality/notation/model/score.rb', line 45

def check_methods
  [:check_program, :check_parts, :check_start_key, :check_key_changes ]
end

#cloneObject



49
50
51
# File 'lib/musicality/notation/model/score.rb', line 49

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

#collated?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/musicality/notation/model/score.rb', line 65

def collated?
  @program.size == 1 && @program[0].first == 0 && @program[0].last == duration
end

#durationObject



61
62
63
# File 'lib/musicality/notation/model/score.rb', line 61

def duration
  @parts.map {|name,part| part.duration }.max || 0.to_r
end

#dynamic_change(new_dynamic, transition_dur: 0, offset: 0) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/musicality/composition/dsl/score_methods.rb', line 51

def dynamic_change new_dynamic, transition_dur: 0, offset: 0
  if transition_dur == 0
    change = (transition_dur == 0) ? Change::Immediate.new(new_tempo) : Change::Gradual.linear(new_tempo, transition_dur)
    parts.values.each do |part|
      part.tempo_changes[self.duration + offset] = change
    end
  end
end

#notes(part_notes) ⇒ Object

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/musicality/composition/dsl/score_methods.rb', line 23

def notes part_notes
  raise ArgumentError, "No part notes given" if part_notes.empty?

  durs_uniq = part_notes.values.map do |notes|
    notes.map {|n| n.duration }.inject(0,:+)
  end.uniq
  raise DurationMismatchError, "New part note durations do not all match" if durs_uniq.size > 1
  dur = durs_uniq.first

  a = starting_part_dur = self.duration
  part_notes.each do |part,notes|
    unless parts.has_key? part
      parts[part] = Part.new DEFAULT_START_DYNAMIC
      if starting_part_dur > 0
        parts[part].notes.push Note.new(starting_part_dur)
      end
    end
    parts[part].notes += notes
  end
  (parts.keys - part_notes.keys).each do |part|
    parts[part].notes.push Note.new(dur)
  end

  b = self.duration
  program.push a...b
  a...b
end

#repeat(arg) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/musicality/composition/dsl/score_methods.rb', line 11

def repeat arg
  case arg
  when Range
    program.push arg
  when String,Symbol
    program.push @sections.fetch(arg)
  else
    raise ArgumentError, "Arg is not a Range, String, or Symbol"
  end
end

#section(title, &block) ⇒ Object



4
5
6
7
8
9
# File 'lib/musicality/composition/dsl/score_methods.rb', line 4

def section title, &block
  a = duration
  self.instance_eval(&block)
  b = duration
  @sections[title] = a...b
end

#validatablesObject



44
# File 'lib/musicality/notation/model/score.rb', line 44

def validatables; @parts.values; end