Class: HeadMusic::Alteration

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

Overview

A 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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#centsObject (readonly)

Returns the value of attribute cents.



8
9
10
# File 'lib/head_music/alteration.rb', line 8

def cents
  @cents
end

#identifierObject (readonly)

Returns the value of attribute identifier.



8
9
10
# File 'lib/head_music/alteration.rb', line 8

def identifier
  @identifier
end

#musical_symbolsObject (readonly)

Returns the value of attribute musical_symbols.



8
9
10
# File 'lib/head_music/alteration.rb', line 8

def musical_symbols
  @musical_symbols
end

Class Method Details

.allObject



37
38
39
# File 'lib/head_music/alteration.rb', line 37

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

.by(key, value) ⇒ Object



61
62
63
64
65
# File 'lib/head_music/alteration.rb', line 61

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

.get(identifier) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/head_music/alteration.rb', line 53

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

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

.matcherObject



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

def self.matcher
  @matcher ||= Regexp.new symbols.join("|")
end

.symbol?(candidate) ⇒ Boolean

Returns:

  • (Boolean)


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

def self.symbol?(candidate)
  candidate =~ /^(#{matcher})$/
end

.symbolsObject



41
42
43
# File 'lib/head_music/alteration.rb', line 41

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

Instance Method Details

#<=>(other) ⇒ Object



88
89
90
91
# File 'lib/head_music/alteration.rb', line 88

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

#musical_symbolObject



93
94
95
# File 'lib/head_music/alteration.rb', line 93

def musical_symbol
  musical_symbols.first
end

#nameObject



67
68
69
# File 'lib/head_music/alteration.rb', line 67

def name
  identifier.to_s.tr("_", " ")
end

#representionsObject



71
72
73
74
# File 'lib/head_music/alteration.rb', line 71

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

#semitonesObject



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

def semitones
  cents / 100.0
end

#to_sObject



84
85
86
# File 'lib/head_music/alteration.rb', line 84

def to_s
  unicode
end