Class: StringyFi::Note
- Inherits:
-
Object
- Object
- StringyFi::Note
- Defined in:
- lib/stringyfi/note.rb
Instance Attribute Summary collapse
-
#alter ⇒ Object
Returns the value of attribute alter.
-
#fractional_duration ⇒ Object
Returns the value of attribute fractional_duration.
-
#name ⇒ Object
Returns the value of attribute name.
-
#octave ⇒ Object
Returns the value of attribute octave.
-
#tempo ⇒ Object
Returns the value of attribute tempo.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #calculate_fractional_duration(duration, duration_type) ⇒ Object
-
#initialize(name, octave = 4, alter = nil, duration = 1, duration_type = "quarter") ⇒ Note
constructor
A new instance of Note.
- #inspect ⇒ Object
- #rest? ⇒ Boolean
-
#stringy_durations(shortest_fractional_duration) ⇒ Object
retrun [short, medium, long, very_long] repeats for the note TODO: scale medium, long, very_long durations correctly.
-
#to_note_id ⇒ Object
Returns the note ID - unique for each frequency.
-
#to_note_name ⇒ Object
Returns the note name, regardless of octave.
- #to_s ⇒ Object
- #to_str ⇒ Object
- #to_stringy(current_octave) ⇒ Object
Constructor Details
#initialize(name, octave = 4, alter = nil, duration = 1, duration_type = "quarter") ⇒ Note
Returns a new instance of Note.
7 8 9 10 11 12 |
# 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 |
Instance Attribute Details
#alter ⇒ Object
Returns the value of attribute alter.
3 4 5 |
# File 'lib/stringyfi/note.rb', line 3 def alter @alter end |
#fractional_duration ⇒ Object
Returns the value of attribute fractional_duration.
5 6 7 |
# File 'lib/stringyfi/note.rb', line 5 def fractional_duration @fractional_duration end |
#name ⇒ Object
Returns the value of attribute name.
3 4 5 |
# File 'lib/stringyfi/note.rb', line 3 def name @name end |
#octave ⇒ Object
Returns the value of attribute octave.
3 4 5 |
# File 'lib/stringyfi/note.rb', line 3 def octave @octave end |
#tempo ⇒ Object
Returns the value of attribute tempo.
4 5 6 |
# File 'lib/stringyfi/note.rb', line 4 def tempo @tempo end |
Instance Method Details
#<=>(other) ⇒ Object
93 94 95 |
# File 'lib/stringyfi/note.rb', line 93 def <=>(other) self.to_note_id <=> other.to_note_id end |
#calculate_fractional_duration(duration, duration_type) ⇒ Object
18 19 20 21 22 23 24 25 26 27 |
# 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 ⇒ Object
89 90 91 |
# File 'lib/stringyfi/note.rb', line 89 def inspect "\"#{to_str}\"" end |
#rest? ⇒ Boolean
14 15 16 |
# File 'lib/stringyfi/note.rb', line 14 def rest? name == "" end |
#stringy_durations(shortest_fractional_duration) ⇒ Object
retrun [short, medium, long, very_long] repeats for the note TODO: scale medium, long, very_long durations correctly
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# 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 ⇒ Object
Returns the note ID - unique for each frequency
81 82 83 |
# File 'lib/stringyfi/note.rb', line 81 def to_note_id "#{octave}:#{name}:#{alter}" end |
#to_note_name ⇒ Object
Returns the note name, regardless of octave
65 66 67 68 69 70 71 72 73 74 |
# 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 ⇒ Object
76 77 78 |
# File 'lib/stringyfi/note.rb', line 76 def to_s to_note_name end |
#to_str ⇒ Object
85 86 87 |
# File 'lib/stringyfi/note.rb', line 85 def to_str to_note_id end |
#to_stringy(current_octave) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# 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 |