Class: HeadMusic::Rudiment::Alteration

Inherits:
Base
  • Object
show all
Includes:
Comparable, Named
Defined in:
lib/head_music/rudiment/alteration.rb

Overview

An Alteration is a symbol that modifies pitch, such as a sharp, flat, or natural. In French, sharps and flats in the key signature are called "altérations".

Constant Summary collapse

ALTERATION_RECORDS =
[
  {
    identifier: :sharp, cents: 100,
    symbols: [{ascii: "#", unicode: "", html_entity: "♯"}]
  },
  {
    identifier: :flat, cents: -100,
    symbols: [{ascii: "b", unicode: "", html_entity: "♭"}]
  },
  {
    identifier: :natural, cents: 0,
    symbols: [{ascii: "", unicode: "", html_entity: "♮"}]
  },
  {
    identifier: :double_sharp, cents: 200,
    symbols: [{ascii: "x", unicode: "𝄪", html_entity: "𝄪"}]
  },
  {
    identifier: :double_flat, cents: -200,
    symbols: [{ascii: "bb", unicode: "𝄫", html_entity: "𝄫"}]
  }
].freeze
ALTERATION_IDENTIFIERS =
ALTERATION_RECORDS.map { |attributes| attributes[:identifier] }.freeze
SYMBOLS =
ALTERATION_RECORDS.map { |attributes| attributes[:symbols].map { |symbol| [symbol[:unicode], symbol[:ascii]] } }.flatten.freeze
PATTERN =
Regexp.union(SYMBOLS.reject { |s| s.nil? || s.empty? })
MATCHER =
PATTERN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Alteration (private)

Returns a new instance of Alteration.



110
111
112
113
114
115
# File 'lib/head_music/rudiment/alteration.rb', line 110

def initialize(attributes)
  @identifier = attributes[:identifier]
  @cents = attributes[:cents]
  initialize_musical_symbols(attributes[:symbols])
  initialize_localized_names
end

Instance Attribute Details

#alias_name_keysObject (readonly) Originally defined in module Named

Returns the value of attribute alias_name_keys.

#centsObject (readonly)

Returns the value of attribute cents.



12
13
14
# File 'lib/head_music/rudiment/alteration.rb', line 12

def cents
  @cents
end

#identifierObject (readonly)

Returns the value of attribute identifier.



12
13
14
# File 'lib/head_music/rudiment/alteration.rb', line 12

def identifier
  @identifier
end

#musical_symbolsObject (readonly)

Returns the value of attribute musical_symbols.



12
13
14
# File 'lib/head_music/rudiment/alteration.rb', line 12

def musical_symbols
  @musical_symbols
end

#name_keyObject (readonly) Originally defined in module Named

Returns the value of attribute name_key.

Class Method Details

.allObject



44
45
46
# File 'lib/head_music/rudiment/alteration.rb', line 44

def self.all
  ALTERATION_RECORDS.map { |attributes| new(attributes) }
end

.by(key, value) ⇒ Object



64
65
66
67
68
# File 'lib/head_music/rudiment/alteration.rb', line 64

def self.by(key, value)
  all.detect do |alteration|
    alteration.send(key) == value if %i[cents semitones].include?(key.to_sym)
  end
end

.from_pitched_item(input) ⇒ Object



74
75
76
# File 'lib/head_music/rudiment/alteration.rb', line 74

def self.from_pitched_item(input)
  nil
end

.get(identifier) ⇒ Object



56
57
58
59
60
61
62
# File 'lib/head_music/rudiment/alteration.rb', line 56

def self.get(identifier)
  return identifier if identifier.is_a?(HeadMusic::Rudiment::Alteration)

  all.detect do |alteration|
    alteration.representions.include?(identifier)
  end
end

.get_by_name(name) ⇒ Object



70
71
72
# File 'lib/head_music/rudiment/alteration.rb', line 70

def self.get_by_name(name)
  all.detect { |alteration| alteration.name == name.to_s }
end

.symbol?(candidate) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/head_music/rudiment/alteration.rb', line 52

def self.symbol?(candidate)
  SYMBOLS.include?(candidate)
end

.symbolsObject



48
49
50
# File 'lib/head_music/rudiment/alteration.rb', line 48

def self.symbols
  @symbols ||= all.map { |alteration| [alteration.ascii, alteration.unicode] }.flatten.reject { |s| s.nil? || s.empty? }
end

Instance Method Details

#<=>(other) ⇒ Object



99
100
101
102
# File 'lib/head_music/rudiment/alteration.rb', line 99

def <=>(other)
  other = HeadMusic::Rudiment::Alteration.get(other)
  cents <=> other.cents
end

#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named

#initialize_localized_namesObject (private)



117
118
119
120
121
# File 'lib/head_music/rudiment/alteration.rb', line 117

def initialize_localized_names
  # Initialize default English names
  ensure_localized_name(name: identifier.to_s.tr("_", " "), locale_code: :en)
  # Additional localized names will be loaded from locale files
end

#initialize_musical_symbols(list) ⇒ Object (private)



123
124
125
126
127
128
129
130
131
# File 'lib/head_music/rudiment/alteration.rb', line 123

def initialize_musical_symbols(list)
  @musical_symbols = (list || []).map do |record|
    HeadMusic::Rudiment::MusicalSymbol.new(
      unicode: record[:unicode],
      ascii: record[:ascii],
      html_entity: record[:html_entity]
    )
  end
end

#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named

#localized_name_in_default_localeObject (private) Originally defined in module Named

#localized_name_in_locale_matching_language(locale) ⇒ Object (private) Originally defined in module Named

#localized_name_in_matching_locale(locale) ⇒ Object (private) Originally defined in module Named

#localized_namesObject Originally defined in module Named

Returns an array of LocalizedName instances that are synonymous with the name.

#musical_symbolObject



104
105
106
# File 'lib/head_music/rudiment/alteration.rb', line 104

def musical_symbol
  musical_symbols.first
end

#name(locale_code: I18n.locale) ⇒ Object



78
79
80
# File 'lib/head_music/rudiment/alteration.rb', line 78

def name(locale_code: I18n.locale)
  super || identifier.to_s.tr("_", " ")
end

#name=(name) ⇒ Object Originally defined in module Named

#representionsObject



82
83
84
85
# File 'lib/head_music/rudiment/alteration.rb', line 82

def representions
  [identifier, identifier.to_s, name, ascii, unicode, html_entity]
    .reject { |representation| representation.to_s.strip == "" }
end

#semitonesObject



87
88
89
# File 'lib/head_music/rudiment/alteration.rb', line 87

def semitones
  cents / 100.0
end

#to_sObject



95
96
97
# File 'lib/head_music/rudiment/alteration.rb', line 95

def to_s
  unicode
end