Class: HeadMusic::Rudiment::Register
- Includes:
- Comparable
- Defined in:
- lib/head_music/rudiment/register.rb
Overview
The register is a numeric octave identifier used in scientific pitch notation.
A pitch is a spelling plus a register. For example, C4 is middle C and C5 is the C one octave higher. The number changes between the letter names B and C regardless of sharps and flats, so as an extreme example, Cb5 is actually a semitone below B#4.
Constant Summary collapse
- AUDIBLE_REGISTERS =
(0..10).map.freeze
- PATTERN =
Regexp.union(AUDIBLE_REGISTERS.map(&:to_s))
- DEFAULT =
4
Instance Attribute Summary collapse
-
#number ⇒ Object
readonly
Returns the value of attribute number.
Class Method Summary collapse
- .default ⇒ Object
- .from_name(string) ⇒ Object
- .from_number(identifier) ⇒ Object
- .get(identifier) ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #<=>(other) ⇒ Object
- #helmholtz_case ⇒ Object
- #helmholtz_marks ⇒ Object
-
#initialize(number) ⇒ Register
constructor
A new instance of Register.
Constructor Details
#initialize(number) ⇒ Register
Returns a new instance of Register.
45 46 47 |
# File 'lib/head_music/rudiment/register.rb', line 45 def initialize(number) @number = number end |
Instance Attribute Details
#number ⇒ Object (readonly)
Returns the value of attribute number.
41 42 43 |
# File 'lib/head_music/rudiment/register.rb', line 41 def number @number end |
Class Method Details
.default ⇒ Object
37 38 39 |
# File 'lib/head_music/rudiment/register.rb', line 37 def self.default @registers[DEFAULT] ||= new(DEFAULT) end |
.from_name(string) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/head_music/rudiment/register.rb', line 29 def self.from_name(string) return unless string.to_s.match?(HeadMusic::Rudiment::Spelling::MATCHER) _letter, _sign, register_string = string.to_s.match(HeadMusic::Rudiment::Spelling::MATCHER).captures @registers ||= {} @registers[register_string.to_i] ||= new(register_string.to_i) if register_string end |
.from_number(identifier) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/head_music/rudiment/register.rb', line 21 def self.from_number(identifier) return nil unless identifier.to_s == identifier.to_i.to_s return nil unless (-2..12).cover?(identifier.to_i) @registers ||= {} @registers[identifier.to_i] ||= new(identifier.to_i) end |
.get(identifier) ⇒ Object
17 18 19 |
# File 'lib/head_music/rudiment/register.rb', line 17 def self.get(identifier) from_number(identifier) || from_name(identifier) || default end |
Instance Method Details
#+(other) ⇒ Object
53 54 55 |
# File 'lib/head_music/rudiment/register.rb', line 53 def +(other) self.class.get(to_i + other.to_i) end |
#-(other) ⇒ Object
57 58 59 |
# File 'lib/head_music/rudiment/register.rb', line 57 def -(other) self.class.get(to_i - other.to_i) end |
#<=>(other) ⇒ Object
49 50 51 |
# File 'lib/head_music/rudiment/register.rb', line 49 def <=>(other) to_i <=> other.to_i end |
#helmholtz_case ⇒ Object
61 62 63 64 65 |
# File 'lib/head_music/rudiment/register.rb', line 61 def helmholtz_case return :upper if number < 3 :lower end |
#helmholtz_marks ⇒ Object
67 68 69 70 71 72 |
# File 'lib/head_music/rudiment/register.rb', line 67 def helmholtz_marks return "," * (2 - number) if number < 2 return "'" * (number - 3) if number > 3 "" end |