Class: Interval::Pitch
- Inherits:
-
Object
- Object
- Interval::Pitch
- Defined in:
- lib/interval.rb
Constant Summary collapse
- NOTE_NAMES =
%w{c d e f g a b}
- NOTE_NAMES_TO_I =
{'c' => 0, 'd' => 1, 'e' => 2, 'f' => 3, 'g' => 4, 'a' => 5, 'b' => 6}
Instance Attribute Summary collapse
-
#accidental ⇒ Object
Returns the value of attribute accidental.
-
#notename_i ⇒ Object
Returns the value of attribute notename_i.
-
#octave ⇒ Object
Returns the value of attribute octave.
Class Method Summary collapse
- .accidental_name(i) ⇒ Object
- .from_int(midiint) ⇒ Object
- .from_string(str) ⇒ Object
-
.note_s_to_i(s) ⇒ Object
end.
- .notename_s(s) ⇒ Object
-
.random(normal = false) ⇒ Object
if normal=true then only return notes you would “normally” see.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
-
#notename ⇒ Object
def notename_i=(i) end.
- #semitone_pitch ⇒ Object
- #to_long_name ⇒ Object
- #to_s ⇒ Object
- #to_short_name ⇒ Object
Instance Attribute Details
#accidental ⇒ Object
Returns the value of attribute accidental.
14 15 16 |
# File 'lib/interval.rb', line 14 def accidental @accidental end |
#notename_i ⇒ Object
Returns the value of attribute notename_i.
13 14 15 |
# File 'lib/interval.rb', line 13 def notename_i @notename_i end |
#octave ⇒ Object
Returns the value of attribute octave.
12 13 14 |
# File 'lib/interval.rb', line 12 def octave @octave end |
Class Method Details
.accidental_name(i) ⇒ Object
110 111 112 113 114 115 116 117 118 |
# File 'lib/interval.rb', line 110 def accidental_name(i) names = { -2 => " double flat", -1 => " flat", 0 => "", 1 => " sharp", 2 => " double sharp" } names[i] end |
.from_int(midiint) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/interval.rb', line 37 def from_int(midiint) p = new p.octave = (midiint - 48) / 12 p.notename_i = {0 => 0, 1 => 0, 2 => 1, 3 => 1, 4 => 2, 5 => 3, 6 => 3, 7 => 4, 8 => 4, 9 => 5, 10 => 5, 11 => 6}[midiint % 12] p.accidental = midiint - (p.octave + 4) * 12 - ([0, 2, 4, 5, 7, 9, 11][p.notename_i]) p end |
.from_string(str) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/interval.rb', line 17 def from_string(str) p = new ary = str.split(//) possible_notename = ary.shift.downcase raise "invalid notename: #{possible_notename}" unless Pitch::NOTE_NAMES_TO_I.has_key?(possible_notename) p.notename_i = note_s_to_i(possible_notename) octave = 0 accidental = 0 while(char = ary.shift) octave = octave + 1 if char == "'" octave = octave - 1 if char == "," accidental = accidental + 1 if char == "#" accidental = accidental - 1 if char == "b" end p.octave = octave p.accidental = accidental p end |
.note_s_to_i(s) ⇒ Object
end
106 107 108 |
# File 'lib/interval.rb', line 106 def note_s_to_i(s) NOTE_NAMES_TO_I[s] end |
.notename_s(s) ⇒ Object
51 52 53 |
# File 'lib/interval.rb', line 51 def notename_s(s) NOTE_NAMES[s] end |
.random(normal = false) ⇒ Object
if normal=true then only return notes you would “normally” see. what is the word for that, natural?
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/interval.rb', line 57 def random(normal=false) p = new p.notename_i = NOTE_NAMES_TO_I.values.rand p.octave = 0 accidentals = [] if normal accidentals = case p.notename when 'c', 'f' [0, 1] when 'e', 'b' [-1, 0] else [-1, 0, 1] end else accidentals = [-1, 0, 1] end p.accidental = accidentals.rand p end |
Instance Method Details
#+(other) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/interval.rb', line 148 def +(other) if other.kind_of?(Interval) plus_interval(other) else raise "todo" end end |
#-(other) ⇒ Object
156 157 158 159 160 161 162 |
# File 'lib/interval.rb', line 156 def -(other) if other.kind_of?(Interval) minus_interval(other) else raise "todo" end end |
#notename ⇒ Object
def notename_i=(i) end
132 133 134 |
# File 'lib/interval.rb', line 132 def notename self.class.notename_s(notename_i) end |
#semitone_pitch ⇒ Object
121 122 123 |
# File 'lib/interval.rb', line 121 def semitone_pitch [0, 2, 4, 5, 7, 9, 11][notename_i] + accidental + (octave * 12) + 48 end |
#to_long_name ⇒ Object
136 137 138 |
# File 'lib/interval.rb', line 136 def to_long_name "%s%s" % [notename.upcase, self.class.accidental_name(accidental)] end |
#to_s ⇒ Object
144 145 146 |
# File 'lib/interval.rb', line 144 def to_s to_short_name end |
#to_short_name ⇒ Object
140 141 142 |
# File 'lib/interval.rb', line 140 def to_short_name "%s%s" % [notename, accidental > 0 ? ('#' * accidental.abs) : ('b' * accidental.abs)] end |