Class: HeadMusic::Rudiment::Note
- Inherits:
-
RhythmicElement
- Object
- Base
- RhythmicElement
- HeadMusic::Rudiment::Note
- Includes:
- Named
- Defined in:
- lib/head_music/rudiment/note.rb
Overview
A Note is a fundamental musical element consisting of a pitch and a duration. This is the rudiment version, representing the abstract concept of a note independent of its placement in a composition.
For notes placed within a composition context, see HeadMusic::Content::Note
Constant Summary collapse
- PITCH_PATTERN =
Regex pattern for parsing note strings like “C#4 quarter” or “Eb3 dotted half” Extract the core pattern from Spelling::MATCHER without anchors.
The alteration group wraps the whole pattern in (?:…) before making it optional. Interpolating “#…?” instead binds the “?” to the pattern’s final alternative alone, which truncates “C##4” to a bare “C#” and loses the register.
/([A-G])((?:#{HeadMusic::Rudiment::Alteration::PATTERN.source})?)(-?\d+)?/i- MATCHER =
/^\s*(#{PITCH_PATTERN.source})\s+(.+)$/i
Instance Attribute Summary collapse
-
#alias_name_keys ⇒ Object
included
from Named
readonly
Returns the value of attribute alias_name_keys.
-
#name_key ⇒ Object
included
from Named
readonly
Returns the value of attribute name_key.
-
#pitch ⇒ Object
readonly
Returns the value of attribute pitch.
Class Method Summary collapse
-
.fetch_or_create(pitch, rhythmic_value) ⇒ Object
private
-
.get(pitch, rhythmic_value = nil) ⇒ Object
-
.get_from_string(string) ⇒ Object
private
Parse a lone string as “pitch rhythmic_value” (e.g., “F#4 dotted-quarter”), falling back to just a pitch with a default quarter note.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Transpose the note up by an interval or semitones.
-
#-(other) ⇒ Object
Transpose the note down by an interval or semitones.
-
#<=>(other) ⇒ Object
-
#==(other) ⇒ Object
-
#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object
included
from Named
-
#initialize(pitch, rhythmic_value) ⇒ Note
constructor
A new instance of Note.
-
#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object
included
from Named
-
#localized_name_in_default_locale ⇒ Object
included
from Named
private
-
#localized_name_in_locale_matching_language(locale) ⇒ Object
included
from Named
private
-
#localized_name_in_matching_locale(locale) ⇒ Object
included
from Named
private
-
#localized_names ⇒ Object
included
from Named
Returns an array of LocalizedName instances that are synonymous with the name.
-
#name ⇒ Object
-
#name=(name) ⇒ Object
included
from Named
-
#sounded? ⇒ Boolean
-
#to_s ⇒ Object
-
#with_pitch(new_pitch) ⇒ Object
Change the pitch while keeping the same rhythmic value.
-
#with_rhythmic_value(new_rhythmic_value) ⇒ Object
Override to maintain pitch when changing rhythmic value.
Constructor Details
#initialize(pitch, rhythmic_value) ⇒ Note
Returns a new instance of Note.
58 59 60 61 |
# File 'lib/head_music/rudiment/note.rb', line 58 def initialize(pitch, rhythmic_value) super(rhythmic_value) @pitch = pitch end |
Instance Attribute Details
#alias_name_keys ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute alias_name_keys.
#name_key ⇒ Object (readonly) Originally defined in module Named
Returns the value of attribute name_key.
#pitch ⇒ Object (readonly)
Returns the value of attribute pitch.
12 13 14 |
# File 'lib/head_music/rudiment/note.rb', line 12 def pitch @pitch end |
Class Method Details
.fetch_or_create(pitch, rhythmic_value) ⇒ Object (private)
52 53 54 55 56 |
# File 'lib/head_music/rudiment/note.rb', line 52 def self.fetch_or_create(pitch, rhythmic_value) @notes ||= {} hash_key = [pitch.to_s, rhythmic_value.to_s].join("_") @notes[hash_key] ||= new(pitch, rhythmic_value) end |
.get(pitch, rhythmic_value = nil) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/head_music/rudiment/note.rb', line 27 def self.get(pitch, rhythmic_value = nil) return pitch if pitch.is_a?(HeadMusic::Rudiment::Note) return get_from_string(pitch) if rhythmic_value.nil? && pitch.is_a?(String) pitch = HeadMusic::Rudiment::Pitch.get(pitch) rhythmic_value = HeadMusic::Rudiment::RhythmicValue.get(rhythmic_value || :quarter) fetch_or_create(pitch, rhythmic_value) end |
.get_from_string(string) ⇒ Object (private)
Parse a lone string as “pitch rhythmic_value” (e.g., “F#4 dotted-quarter”), falling back to just a pitch with a default quarter note.
38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/head_music/rudiment/note.rb', line 38 def self.get_from_string(string) match = string.match(MATCHER) if match pitch_obj = HeadMusic::Rudiment::Pitch.get(match[1]) rhythmic_value_obj = HeadMusic::Rudiment::RhythmicValue.get(match[5]) return fetch_or_create(pitch_obj, rhythmic_value_obj) if pitch_obj && rhythmic_value_obj end pitch_obj = HeadMusic::Rudiment::Pitch.get(string) return fetch_or_create(pitch_obj, HeadMusic::Rudiment::RhythmicValue.get(:quarter)) if pitch_obj nil end |
Instance Method Details
#+(other) ⇒ Object
Transpose the note up by an interval or semitones
87 88 89 90 |
# File 'lib/head_music/rudiment/note.rb', line 87 def +(other) new_pitch = pitch + other self.class.get(new_pitch, rhythmic_value) end |
#-(other) ⇒ Object
Transpose the note down by an interval or semitones
93 94 95 96 |
# File 'lib/head_music/rudiment/note.rb', line 93 def -(other) new_pitch = pitch - other self.class.get(new_pitch, rhythmic_value) end |
#<=>(other) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/head_music/rudiment/note.rb', line 79 def <=>(other) return nil unless other.is_a?(HeadMusic::Rudiment::RhythmicElement) return super unless other.is_a?(self.class) [rhythmic_value, pitch] <=> [other.rhythmic_value, other.pitch] end |
#==(other) ⇒ Object
74 75 76 77 |
# File 'lib/head_music/rudiment/note.rb', line 74 def ==(other) other = HeadMusic::Rudiment::Note.get(other) super && pitch == other.pitch end |
#ensure_localized_name(name:, locale_code: Locale::DEFAULT_CODE, abbreviation: nil) ⇒ Object Originally defined in module Named
#localized_name(locale_code: Locale::DEFAULT_CODE) ⇒ Object Originally defined in module Named
#localized_name_in_default_locale ⇒ Object (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_names ⇒ Object Originally defined in module Named
Returns an array of LocalizedName instances that are synonymous with the name.
#name ⇒ Object
66 67 68 |
# File 'lib/head_music/rudiment/note.rb', line 66 def name "#{pitch} #{rhythmic_value}" end |
#name=(name) ⇒ Object Originally defined in module Named
#sounded? ⇒ Boolean
108 109 110 |
# File 'lib/head_music/rudiment/note.rb', line 108 def sounded? true end |
#to_s ⇒ Object
70 71 72 |
# File 'lib/head_music/rudiment/note.rb', line 70 def to_s name end |
#with_pitch(new_pitch) ⇒ Object
Change the pitch while keeping the same rhythmic value
104 105 106 |
# File 'lib/head_music/rudiment/note.rb', line 104 def with_pitch(new_pitch) self.class.get(new_pitch, rhythmic_value) end |
#with_rhythmic_value(new_rhythmic_value) ⇒ Object
Override to maintain pitch when changing rhythmic value
99 100 101 |
# File 'lib/head_music/rudiment/note.rb', line 99 def with_rhythmic_value(new_rhythmic_value) self.class.get(pitch, new_rhythmic_value) end |