Class: HeadMusic::Notation::NotationStyle

Inherits:
Object
  • Object
show all
Defined in:
lib/head_music/notation/notation_style.rb

Overview

A named notation tradition: how instruments are notated (clef, sounding transposition, staff structure) for a given context.

default lists every instrument. Named styles (british_brass_band, concert_pitch, german, italian) are sparse OVERLAYS: they list only the instruments whose notation differs, and any instrument they don’t mention falls back to default.

HeadMusic::Notation::NotationStyle.get(:british_brass_band) .notation_for(“euphonium”) #=> treble clef, sounding transposition -14

Uses a lightweight registry rather than the Named mixin: style keys are internal identifiers with no translation surface.

Constant Summary collapse

STYLES =
YAML.load_file(File.expand_path("notation_styles.yml", __dir__)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash_key) ⇒ NotationStyle (private)

Returns a new instance of NotationStyle.

Raises:

  • (KeyError)


61
62
63
64
65
66
67
68
# File 'lib/head_music/notation/notation_style.rb', line 61

def initialize(hash_key)
  @key = hash_key.to_sym
  record = STYLES[@key.to_s]
  raise KeyError, "Unknown notation style: #{@key}" unless record

  @name = record["name"]
  @instrument_notations = record["instrument_notations"] || {}
end

Instance Attribute Details

#instrument_notationsObject (readonly, protected)

Returns the value of attribute instrument_notations.



57
58
59
# File 'lib/head_music/notation/notation_style.rb', line 57

def instrument_notations
  @instrument_notations
end

#keyObject (readonly)

Returns the value of attribute key.



19
20
21
# File 'lib/head_music/notation/notation_style.rb', line 19

def key
  @key
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/head_music/notation/notation_style.rb', line 19

def name
  @name
end

Class Method Details

.allObject



34
35
36
# File 'lib/head_music/notation/notation_style.rb', line 34

def all
  STYLES.keys.map { |style_key| get(style_key) }
end

.defaultObject



30
31
32
# File 'lib/head_music/notation/notation_style.rb', line 30

def default
  get(:default)
end

.get(identifier) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/head_music/notation/notation_style.rb', line 22

def get(identifier)
  return identifier if identifier.is_a?(self)

  @styles ||= {}
  hash_key = HeadMusic::Utilities::HashKey.for(identifier)
  @styles[hash_key] ||= new(hash_key)
end

Instance Method Details

#default_data(name_key) ⇒ Object (private)



70
71
72
73
74
# File 'lib/head_music/notation/notation_style.rb', line 70

def default_data(name_key)
  return nil if @key == :default

  self.class.default.instrument_notations[name_key]
end

#memoObject (private)



76
77
78
# File 'lib/head_music/notation/notation_style.rb', line 76

def memo
  @memo ||= {}
end

#notation_for(instrument) ⇒ Object

Resolve an instrument’s notation in this style, falling back to default. Accepts an Instrument or any key/name Instrument.get understands. Returns an InstrumentNotation, or nil if the instrument is unknown.



44
45
46
47
48
49
50
51
52
53
# File 'lib/head_music/notation/notation_style.rb', line 44

def notation_for(instrument)
  instrument = HeadMusic::Instruments::Instrument.get(instrument)
  return nil unless instrument&.name_key

  name_key = instrument.name_key.to_s
  data = instrument_notations[name_key] || default_data(name_key)
  return nil unless data

  memo[name_key] ||= HeadMusic::Notation::InstrumentNotation.new(instrument: instrument, data: data)
end