Class: Collavoce::Note
- Inherits:
-
Object
- Object
- Collavoce::Note
- Defined in:
- lib/collavoce/note.rb
Defined Under Namespace
Classes: UnparsableError
Constant Summary collapse
- ShortMessage =
Java::javax.sound.midi.ShortMessage
- NOTES =
{ "C" => 60, "D" => 62, "E" => 64, "F" => 65, "G" => 67, "A" => 69, "B" => 71, }
- DIVISIONS =
{ "w" => 1, "h" => 1.to_f / 2, "q" => 1.to_f / 4, "e" => 1.to_f / 8, "s" => 1.to_f / 16, "t" => 1.to_f / 32 }
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #aug ⇒ Object
- #aug! ⇒ Object
- #dim ⇒ Object
- #dim! ⇒ Object
- #init_from_note(note) ⇒ Object
- #init_from_string(note) ⇒ Object
-
#initialize(note) ⇒ Note
constructor
A new instance of Note.
- #off ⇒ Object
- #on(receiver, channel) ⇒ Object
- #play(receiver, channel, bar_duration) ⇒ Object
Constructor Details
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
7 8 9 |
# File 'lib/collavoce/note.rb', line 7 def duration @duration end |
#value ⇒ Object
Returns the value of attribute value.
6 7 8 |
# File 'lib/collavoce/note.rb', line 6 def value @value end |
Instance Method Details
#==(other) ⇒ Object
84 85 86 |
# File 'lib/collavoce/note.rb', line 84 def ==(other) value == other.value && duration == other.duration end |
#aug ⇒ Object
72 73 74 75 76 |
# File 'lib/collavoce/note.rb', line 72 def aug copy = Note.new(self) copy.aug! copy end |
#aug! ⇒ Object
64 65 66 |
# File 'lib/collavoce/note.rb', line 64 def aug! @value += 1 end |
#dim ⇒ Object
78 79 80 81 82 |
# File 'lib/collavoce/note.rb', line 78 def dim copy = Note.new(self) copy.dim! copy end |
#dim! ⇒ Object
68 69 70 |
# File 'lib/collavoce/note.rb', line 68 def dim! @value -= 1 end |
#init_from_note(note) ⇒ Object
59 60 61 62 |
# File 'lib/collavoce/note.rb', line 59 def init_from_note(note) @value = note.value @duration = note.duration end |
#init_from_string(note) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/collavoce/note.rb', line 37 def init_from_string(note) match = note.match(/^([ABCDEFGR])([#]*)?([b]*)?(\d)?([whqest]*)/) raise UnparsableError.new("Couldn't parse note: #{note}") unless match if match[5].empty? @duration = DIVISIONS["q"] else @duration = 0 match[5].each_char do |c| @duration += DIVISIONS[c] end end if base_value = NOTES[match[1]] offset = (match[2] || "").length offset -= (match[3] || "").length octave = (match[4] || "4").to_i @value = base_value + ((octave - 4) * 12) + offset end end |
#off ⇒ Object
97 98 99 100 101 |
# File 'lib/collavoce/note.rb', line 97 def off = ShortMessage.new .(ShortMessage::NOTE_OFF, @channel, value, 127); @receiver.send(, 0) end |
#on(receiver, channel) ⇒ Object
88 89 90 91 92 93 94 95 |
# File 'lib/collavoce/note.rb', line 88 def on(receiver, channel) @receiver = receiver @channel = channel = ShortMessage.new .(ShortMessage::NOTE_ON, @channel, value, 127); @receiver.send(, 0) end |
#play(receiver, channel, bar_duration) ⇒ Object
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/collavoce/note.rb', line 103 def play(receiver, channel, ) sleep_duration = * duration if value on(receiver, channel) sleep sleep_duration off else sleep sleep_duration end end |