Class: Tetsujin::Theory::Note

Inherits:
Object
  • Object
show all
Defined in:
lib/tetsujin/theory/note.rb

Defined Under Namespace

Classes: Factory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pitch_class:, octave: 4) ⇒ Note

Returns a new instance of Note.

Parameters:

  • pitch_class (Integer)

    ピッチクラス

  • octave (Integer) (defaults to: 4)

    オクターブ

Raises:

  • (TypeError)

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

#octaveObject (readonly)

Returns the value of attribute octave.


5
6
7
# File 'lib/tetsujin/theory/note.rb', line 5

def octave
  @octave
end

#pitch_classObject (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 音名とオクターブの比較結果.

Parameters:

Returns:

  • (Integer)

    音名とオクターブの比較結果


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 音名とオクターブが一致しているか.

Parameters:

Returns:

  • (Boolean)

    音名とオクターブが一致しているか


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 移動後の音.

Parameters:

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つの音の音程.

Parameters:

Returns:


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

#hashInteger

Returns ハッシュ値.

Returns:

  • (Integer)

    ハッシュ値


71
72
73
# File 'lib/tetsujin/theory/note.rb', line 71

def hash
  [pitch_class, octave].hash
end

#subtract(interval) ⇒ Tetsujin::Theory::Note

Returns 移動後の音.

Parameters:

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_sString

Returns 音名とオクターブを結合した文字列 (例: “G♯4/A♭4”).

Returns:

  • (String)

    音名とオクターブを結合した文字列 (例: “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