class StringyFi::Note
Attributes
alter[RW]
fractional_duration[RW]
name[RW]
octave[RW]
tempo[RW]
Public Class Methods
new(name, octave=4, alter=nil, duration=1, duration_type="quarter")
click to toggle source
# File lib/stringyfi/note.rb, line 7 def initialize(name, octave=4, alter=nil, duration=1, duration_type="quarter") self.name = "#{name}".upcase self.octave = "#{octave}".to_i self.alter = alter.to_i rescue 0 self.fractional_duration = calculate_fractional_duration(duration, duration_type) end
Public Instance Methods
<=>(other)
click to toggle source
# File lib/stringyfi/note.rb, line 93 def <=>(other) self.to_note_id <=> other.to_note_id end
calculate_fractional_duration(duration, duration_type)
click to toggle source
# File lib/stringyfi/note.rb, line 18 def calculate_fractional_duration(duration, duration_type) divisor = { "half" => 2.0, "quarter" => 4.0, "eighth" => 8.0, "16th" => 16.0, "32nd" => 32.0 }[duration_type] || 1.0 duration/divisor end
inspect()
click to toggle source
# File lib/stringyfi/note.rb, line 89 def inspect "\"#{to_str}\"" end
rest?()
click to toggle source
# File lib/stringyfi/note.rb, line 14 def rest? name == "" end
stringy_durations(shortest_fractional_duration)
click to toggle source
retrun [short, medium, long, very_long] repeats for the note TODO: scale medium, long, very_long durations correctly
# File lib/stringyfi/note.rb, line 31 def stringy_durations(shortest_fractional_duration) time_units = (fractional_duration / shortest_fractional_duration).to_i case when time_units >= 8 [0, 0, 0, 1] when time_units >= 4 [0, 0, 1, 0] when time_units >= 2 [0, 1, 0, 0] else [1, 0, 0, 0] end end
to_note_id()
click to toggle source
Returns the note ID - unique for each frequency
# File lib/stringyfi/note.rb, line 81 def to_note_id "#{octave}:#{name}:#{alter}" end
to_note_name()
click to toggle source
Returns the note name, regardless of octave
# File lib/stringyfi/note.rb, line 65 def to_note_name case alter when 1 "#{name}#" when -1 "#{name}b" else name end end
to_s()
click to toggle source
# File lib/stringyfi/note.rb, line 76 def to_s to_note_name end
to_str()
click to toggle source
# File lib/stringyfi/note.rb, line 85 def to_str to_note_id end
to_stringy(current_octave)
click to toggle source
# File lib/stringyfi/note.rb, line 45 def to_stringy(current_octave) relative_octave = octave - current_octave + 1 case alter when 1 "#{name}#{relative_octave}S" when -1 sharpy_name = { 'B' => 'A', 'E' => 'D', 'A' => 'G', 'D' => 'C', 'G' => 'F', }[name] "#{sharpy_name}#{relative_octave}S" else "#{name}#{relative_octave}" end end