Class: Clef::Core::Score

Inherits:
Object
  • Object
show all
Includes:
Metadata
Defined in:
lib/clef/core/score.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Metadata

#metadata, #metadata=, #set_metadata, #update_metadata

Constructor Details

#initialize(metadata: {}) ⇒ Score

Returns a new instance of Score.

Parameters:

  • metadata (Hash) (defaults to: {})


11
12
13
14
# File 'lib/clef/core/score.rb', line 11

def initialize(metadata: {})
  @staff_groups = []
  self. = 
end

Instance Attribute Details

#composerObject

Returns the value of attribute composer.



8
9
10
# File 'lib/clef/core/score.rb', line 8

def composer
  @composer
end

#pluginsObject

Returns the value of attribute plugins.



8
9
10
# File 'lib/clef/core/score.rb', line 8

def plugins
  @plugins
end

#tempoObject

Returns the value of attribute tempo.



8
9
10
# File 'lib/clef/core/score.rb', line 8

def tempo
  @tempo
end

#titleObject

Returns the value of attribute title.



8
9
10
# File 'lib/clef/core/score.rb', line 8

def title
  @title
end

Instance Method Details

#add_staff(staff) ⇒ Score

Parameters:

Returns:

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
# File 'lib/clef/core/score.rb', line 29

def add_staff(staff)
  raise ArgumentError, "staff must be a Clef::Core::Staff" unless staff.is_a?(Staff)
  raise ArgumentError, "duplicate staff id: #{staff.id}" if staves.any? { |existing| existing.id == staff.id }

  default_group.add_staff(staff)
  self
end

#add_staff_group(staff_group) ⇒ Score

Parameters:

Returns:

Raises:

  • (ArgumentError)


18
19
20
21
22
23
24
25
# File 'lib/clef/core/score.rb', line 18

def add_staff_group(staff_group)
  raise ArgumentError, "staff_group must be a Clef::Core::StaffGroup" unless staff_group.is_a?(StaffGroup)
  duplicate = staff_group.staves.find { |staff| staves.any? { |existing| existing.id == staff.id } }
  raise ArgumentError, "duplicate staff id: #{duplicate.id}" if duplicate

  @staff_groups << staff_group
  self
end

#staff_groupsArray<StaffGroup>

Returns:



43
44
45
# File 'lib/clef/core/score.rb', line 43

def staff_groups
  @staff_groups.dup.freeze
end

#stavesArray<Staff>

Returns:



38
39
40
# File 'lib/clef/core/score.rb', line 38

def staves
  @staff_groups.flat_map(&:staves).freeze
end

#to_format(path, **options) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)


67
68
69
70
71
72
73
74
75
# File 'lib/clef/core/score.rb', line 67

def to_format(path, **options)
  case File.extname(path.to_s).downcase
  when ".pdf" then to_pdf(path, **options)
  when ".svg" then to_svg(path, **options)
  when ".mid", ".midi" then to_midi(path, **options)
  else
    raise ArgumentError, "unsupported output format for #{path.inspect}"
  end
end

#to_midi(path, **options) ⇒ Object

Parameters:

  • path (String, #write)
  • options (Hash)


61
62
63
# File 'lib/clef/core/score.rb', line 61

def to_midi(path, **options)
  ::Clef::Midi::Exporter.new(self, **options).export(path)
end

#to_pdf(path, **options) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)


49
50
51
# File 'lib/clef/core/score.rb', line 49

def to_pdf(path, **options)
  ::Clef::Compiler.new(self, **options).compile_to_pdf(path)
end

#to_svg(path, **options) ⇒ Object

Parameters:

  • path (String)
  • options (Hash)


55
56
57
# File 'lib/clef/core/score.rb', line 55

def to_svg(path, **options)
  ::Clef::Compiler.new(self, **options).compile_to_svg(path)
end

#validate(strict: false) ⇒ ValidationResult

Parameters:

  • strict (Boolean) (defaults to: false)

Returns:

Raises:



79
80
81
82
83
84
# File 'lib/clef/core/score.rb', line 79

def validate(strict: false)
  result = ValidationResult.new(validation_issues)
  raise Clef::Error, result.errors.map(&:message).join(", ") if strict && !result.ok?

  result
end

#validation_warningsArray<ValidationIssue>

Returns:



87
88
89
# File 'lib/clef/core/score.rb', line 87

def validation_warnings
  validate.warnings
end