Class: Tetsujin::Theory::Note
- Inherits:
-
Object
- Object
- Tetsujin::Theory::Note
- Defined in:
- lib/tetsujin/theory/note.rb
Defined Under Namespace
Classes: Factory
Instance Attribute Summary collapse
-
#octave ⇒ Object
readonly
Returns the value of attribute octave.
-
#pitch_class ⇒ Object
readonly
Returns the value of attribute pitch_class.
Instance Method Summary collapse
-
#<=>(other) ⇒ Integer
音名とオクターブの比較結果.
-
#==(other) ⇒ Boolean
(also: #eql?)
音名とオクターブが一致しているか.
-
#add(interval) ⇒ Tetsujin::Theory::Note
移動後の音.
-
#diff(other) ⇒ Tetsujin::Theory::Interval
2つの音の音程.
-
#hash ⇒ Integer
ハッシュ値.
-
#initialize(pitch_class:, octave: 4) ⇒ Note
constructor
A new instance of Note.
-
#subtract(interval) ⇒ Tetsujin::Theory::Note
移動後の音.
-
#to_s ⇒ String
音名とオクターブを結合した文字列 (例: “G♯4/A♭4”).
Constructor Details
#initialize(pitch_class:, octave: 4) ⇒ Note
Returns a new instance of Note.
26 27 28 29 30 31 32 33 |
# File 'lib/tetsujin/theory/note.rb', line 26 def initialize(pitch_class:, octave: 4) raise TypeError unless pitch_class.is_a?(Integer) raise TypeError unless octave.is_a?(Integer) raise ArgumentError unless PITCH_CLASS_TO_NOTE_NAME.key?(pitch_class) @pitch_class = pitch_class @octave = octave end |
Instance Attribute Details
#octave ⇒ Object (readonly)
Returns the value of attribute octave.
5 6 7 |
# File 'lib/tetsujin/theory/note.rb', line 5 def octave @octave end |
#pitch_class ⇒ Object (readonly)
Returns the value of attribute pitch_class.
5 6 7 |
# File 'lib/tetsujin/theory/note.rb', line 5 def pitch_class @pitch_class end |
Instance Method Details
#<=>(other) ⇒ Integer
Returns 音名とオクターブの比較結果.
77 78 79 80 |
# File 'lib/tetsujin/theory/note.rb', line 77 def <=>(other) return nil unless other.is_a?(self.class) octave == other.octave ? pitch_class <=> other.pitch_class : octave <=> other.octave end |
#==(other) ⇒ Boolean Also known as: eql?
Returns 音名とオクターブが一致しているか.
64 65 66 67 |
# File 'lib/tetsujin/theory/note.rb', line 64 def ==(other) return false unless other.is_a?(self.class) pitch_class == other.pitch_class && octave == other.octave end |
#add(interval) ⇒ Tetsujin::Theory::Note
Returns 移動後の音.
43 44 45 46 |
# File 'lib/tetsujin/theory/note.rb', line 43 def add(interval) added_octaves, new_pitch_class = (pitch_class + interval.value).divmod(notes_in_octave) self.class.new(pitch_class: new_pitch_class, octave: octave + added_octaves) end |
#diff(other) ⇒ Tetsujin::Theory::Interval
Returns 2つの音の音程.
57 58 59 60 |
# File 'lib/tetsujin/theory/note.rb', line 57 def diff(other) pitch_diff = (other.octave - octave) * notes_in_octave + other.pitch_class - pitch_class Tetsujin::Theory::Interval.new(value: pitch_diff) end |
#hash ⇒ Integer
Returns ハッシュ値.
71 72 73 |
# File 'lib/tetsujin/theory/note.rb', line 71 def hash [pitch_class, octave].hash end |
#subtract(interval) ⇒ Tetsujin::Theory::Note
Returns 移動後の音.
50 51 52 53 |
# File 'lib/tetsujin/theory/note.rb', line 50 def subtract(interval) subtracted_octaves, new_pitch_class = (pitch_class - interval.value).divmod(notes_in_octave) self.class.new(pitch_class: new_pitch_class, octave: octave + subtracted_octaves) end |
#to_s ⇒ String
Returns 音名とオクターブを結合した文字列 (例: “G♯4/A♭4”).
36 37 38 39 |
# File 'lib/tetsujin/theory/note.rb', line 36 def to_s note_names = PITCH_CLASS_TO_NOTE_NAME[pitch_class] note_names.map { |note_name| "#{note_name}#{octave}" }.join("/") end |