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
/([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.
54 55 56 57 |
# File 'lib/head_music/rudiment/note.rb', line 54 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)
48 49 50 51 52 |
# File 'lib/head_music/rudiment/note.rb', line 48 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
23 24 25 26 27 28 29 30 |
# File 'lib/head_music/rudiment/note.rb', line 23 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.
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/head_music/rudiment/note.rb', line 34 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
83 84 85 86 |
# File 'lib/head_music/rudiment/note.rb', line 83 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
89 90 91 92 |
# File 'lib/head_music/rudiment/note.rb', line 89 def -(other) new_pitch = pitch - other self.class.get(new_pitch, rhythmic_value) end |
#<=>(other) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/head_music/rudiment/note.rb', line 75 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
70 71 72 73 |
# File 'lib/head_music/rudiment/note.rb', line 70 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
62 63 64 |
# File 'lib/head_music/rudiment/note.rb', line 62 def name "#{pitch} #{rhythmic_value}" end |
#name=(name) ⇒ Object Originally defined in module Named
#sounded? ⇒ Boolean
104 105 106 |
# File 'lib/head_music/rudiment/note.rb', line 104 def sounded? true end |
#to_s ⇒ Object
66 67 68 |
# File 'lib/head_music/rudiment/note.rb', line 66 def to_s name end |
#with_pitch(new_pitch) ⇒ Object
Change the pitch while keeping the same rhythmic value
100 101 102 |
# File 'lib/head_music/rudiment/note.rb', line 100 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
95 96 97 |
# File 'lib/head_music/rudiment/note.rb', line 95 def with_rhythmic_value(new_rhythmic_value) self.class.get(pitch, new_rhythmic_value) end |