Class: WTFChord::Pitch

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Comparable
Defined in:
lib/wtf_chord/pitch.rb

Direct Known Subclasses

InstrumentPitch

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(note, octave = nil) ⇒ Pitch

Returns a new instance of Pitch.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
# File 'lib/wtf_chord/pitch.rb', line 13

def initialize(note, octave = nil)
  raise ArgumentError unless note.is_a?(Note)

  @note   = note
  @octave = octave || 4
end

Instance Attribute Details

#noteObject (readonly)

Returns the value of attribute note.



11
12
13
# File 'lib/wtf_chord/pitch.rb', line 11

def note
  @note
end

#octaveObject (readonly)

Returns the value of attribute octave.



11
12
13
# File 'lib/wtf_chord/pitch.rb', line 11

def octave
  @octave
end

Instance Method Details

#-(amount) ⇒ Object



45
46
47
# File 'lib/wtf_chord/pitch.rb', line 45

def - amount
  move -amount
end

#-@Object



49
50
51
# File 'lib/wtf_chord/pitch.rb', line 49

def -@
  -to_i
end

#<=>(other) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/wtf_chord/pitch.rb', line 65

def <=> other
  case other
  when Note     then @note <=> other
  when Integer  then to_i  <=> other
  when Pitch    then to_i  <=> other.to_i
  end
end

#==(other) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/wtf_chord/pitch.rb', line 57

def == other
  case other
  when Integer, Pitch then other == to_i
  when String then other.casecmp(to_str).zero?
  when Note   then other == @note
  end
end

#chord(name) ⇒ Object



73
74
75
# File 'lib/wtf_chord/pitch.rb', line 73

def chord(name)
  Chord.new(self, name)
end

#move(amount) ⇒ Object Also known as: +



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/wtf_chord/pitch.rb', line 30

def move(amount)
  return self if amount.zero?

  to_octave, to_pos = (to_i + amount).divmod(12)

  if to_pos.zero?
    to_octave -= 1
    to_pos = 12
  end

  SCALE[to_pos][to_octave]
end

#to_iObject



53
54
55
# File 'lib/wtf_chord/pitch.rb', line 53

def to_i
  (@octave * 12) + @note.position
end

#to_sObject Also known as: inspect



24
25
26
# File 'lib/wtf_chord/pitch.rb', line 24

def to_s
  "#{@note.to_s}#{octave}"
end

#to_strObject



20
21
22
# File 'lib/wtf_chord/pitch.rb', line 20

def to_str
  "#{@note.key}#{octave}"
end