Class: Zgomot::Midi::Note
Constant Summary collapse
- PITCH_CLASS =
{ :C => 0, :Bs => 0, :Cs => 1, :Db => 1, :D => 2, :Ds => 3, :Ed => 3, :E => 4, :Fd => 4, :F => 5, :Es => 5, :Fs => 6, :Gb => 6, :G => 7, :Gs => 8, :Ab => 8, :A => 9, :As => 10, :Bb => 10, :B => 11, :Cb => 11, :R => -1, }
- LENGTH =
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024].select{|d| d <= Clock.resolution}
- OCTAVE =
(-1..9).to_a
Instance Attribute Summary collapse
-
#channel ⇒ Object
Returns the value of attribute channel.
-
#global_offset ⇒ Object
Returns the value of attribute global_offset.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#midi ⇒ Object
readonly
Returns the value of attribute midi.
-
#octave ⇒ Object
readonly
Returns the value of attribute octave.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#pitch_class ⇒ Object
readonly
Returns the value of attribute pitch_class.
-
#time ⇒ Object
Returns the value of attribute time.
-
#time_scale ⇒ Object
readonly
Returns the value of attribute time_scale.
-
#velocity ⇒ Object
readonly
Returns the value of attribute velocity.
Instance Method Summary collapse
- #bpm!(bpm) ⇒ Object
-
#initialize(args) ⇒ Note
constructor
A new instance of Note.
- #length!(v) ⇒ Object
- #length_to_sec ⇒ Object
- #note_off ⇒ Object
- #note_on ⇒ Object
- #octave!(oct) ⇒ Object
- #to_midi ⇒ Object
- #to_s ⇒ Object
- #valocity!(v) ⇒ Object
Constructor Details
#initialize(args) ⇒ Note
Returns a new instance of Note.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/zgomot/midi/note.rb', line 31 def initialize(args) @pitch_class, @octave = case args[:pitch] when Array then args[:pitch] when Symbol then [args[:pitch], 4] else raise(Zgomot::Error, "#{args[:pitch].inspect} is invalid pitch") end @length, @velocity = args[:length], args[:velocity] @midi = pitch_to_midi(pitch_class, octave) @time_scale = 1.0 raise(Zgomot::Error, "#{octave} is invalid octave") unless OCTAVE.include?(octave) raise(Zgomot::Error, "#{length} is invalid duration") unless LENGTH.include?(length) raise(Zgomot::Error, "#{args[:pitch].inspect} is invalid") if midi.nil? raise(Zgomot::Error, "#{velocity} is invalid velocity") unless velocity < 1.0 end |
Instance Attribute Details
#channel ⇒ Object
Returns the value of attribute channel.
30 31 32 |
# File 'lib/zgomot/midi/note.rb', line 30 def channel @channel end |
#global_offset ⇒ Object
Returns the value of attribute global_offset.
30 31 32 |
# File 'lib/zgomot/midi/note.rb', line 30 def global_offset @global_offset end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
29 30 31 |
# File 'lib/zgomot/midi/note.rb', line 29 def length @length end |
#midi ⇒ Object (readonly)
Returns the value of attribute midi.
29 30 31 |
# File 'lib/zgomot/midi/note.rb', line 29 def midi @midi end |
#octave ⇒ Object (readonly)
Returns the value of attribute octave.
29 30 31 |
# File 'lib/zgomot/midi/note.rb', line 29 def octave @octave end |
#offset ⇒ Object
Returns the value of attribute offset.
30 31 32 |
# File 'lib/zgomot/midi/note.rb', line 30 def offset @offset end |
#pitch_class ⇒ Object (readonly)
Returns the value of attribute pitch_class.
29 30 31 |
# File 'lib/zgomot/midi/note.rb', line 29 def pitch_class @pitch_class end |
#time ⇒ Object
Returns the value of attribute time.
30 31 32 |
# File 'lib/zgomot/midi/note.rb', line 30 def time @time end |
#time_scale ⇒ Object (readonly)
Returns the value of attribute time_scale.
29 30 31 |
# File 'lib/zgomot/midi/note.rb', line 29 def time_scale @time_scale end |
#velocity ⇒ Object (readonly)
Returns the value of attribute velocity.
29 30 31 |
# File 'lib/zgomot/midi/note.rb', line 29 def velocity @velocity end |
Instance Method Details
#bpm!(bpm) ⇒ Object
48 49 50 |
# File 'lib/zgomot/midi/note.rb', line 48 def bpm!(bpm) @time_scale = 1.0/bpm.to_f; self end |
#length!(v) ⇒ Object
54 55 56 |
# File 'lib/zgomot/midi/note.rb', line 54 def length!(v) @length = v; self end |
#length_to_sec ⇒ Object
63 64 65 |
# File 'lib/zgomot/midi/note.rb', line 63 def length_to_sec time_scale*Clock.whole_note_sec/length end |
#note_off ⇒ Object
66 67 68 |
# File 'lib/zgomot/midi/note.rb', line 66 def note_off note_on + length_to_sec end |
#note_on ⇒ Object
60 61 62 |
# File 'lib/zgomot/midi/note.rb', line 60 def note_on time + offset end |
#octave!(oct) ⇒ Object
51 52 53 |
# File 'lib/zgomot/midi/note.rb', line 51 def octave!(oct) @octave = oct; self end |
#to_midi ⇒ Object
69 70 71 |
# File 'lib/zgomot/midi/note.rb', line 69 def to_midi self end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/zgomot/midi/note.rb', line 45 def to_s "[#{pitch_class.to_s},#{octave}].#{length}.#{midi}.#{velocity}" end |
#valocity!(v) ⇒ Object
57 58 59 |
# File 'lib/zgomot/midi/note.rb', line 57 def valocity!(v) @length = v; self end |