Class: Zgomot::Midi::Note

Inherits:
Object show all
Defined in:
lib/zgomot/midi/note.rb

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

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Note

Returns a new instance of Note.

Raises:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/zgomot/midi/note.rb', line 32

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

#channelObject

Returns the value of attribute channel.



30
31
32
# File 'lib/zgomot/midi/note.rb', line 30

def channel
  @channel
end

#global_offsetObject

Returns the value of attribute global_offset.



30
31
32
# File 'lib/zgomot/midi/note.rb', line 30

def global_offset
  @global_offset
end

#lengthObject

Returns the value of attribute length.



30
31
32
# File 'lib/zgomot/midi/note.rb', line 30

def length
  @length
end

#midiObject (readonly)

Returns the value of attribute midi.



29
30
31
# File 'lib/zgomot/midi/note.rb', line 29

def midi
  @midi
end

#octaveObject (readonly)

Returns the value of attribute octave.



29
30
31
# File 'lib/zgomot/midi/note.rb', line 29

def octave
  @octave
end

#offsetObject

Returns the value of attribute offset.



30
31
32
# File 'lib/zgomot/midi/note.rb', line 30

def offset
  @offset
end

#pitch_classObject (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

#timeObject

Returns the value of attribute time.



30
31
32
# File 'lib/zgomot/midi/note.rb', line 30

def time
  @time
end

#time_scaleObject (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

#velocityObject

Returns the value of attribute velocity.



30
31
32
# File 'lib/zgomot/midi/note.rb', line 30

def velocity
  @velocity
end

Instance Method Details

#bpm!(bpm) ⇒ Object



51
52
53
# File 'lib/zgomot/midi/note.rb', line 51

def bpm!(bpm)
  @time_scale = 1.0/bpm.to_f; self
end

#length_to_secObject



63
64
65
# File 'lib/zgomot/midi/note.rb', line 63

def length_to_sec
  time_scale*Clock.whole_note_sec/length
end

#note_offObject



67
68
69
# File 'lib/zgomot/midi/note.rb', line 67

def note_off
  note_on + length_to_sec
end

#note_onObject



59
60
61
# File 'lib/zgomot/midi/note.rb', line 59

def note_on
  time + offset
end

#octave!(oct) ⇒ Object



55
56
57
# File 'lib/zgomot/midi/note.rb', line 55

def octave!(oct)
  @octave = oct; self
end

#to_midiObject



71
72
73
# File 'lib/zgomot/midi/note.rb', line 71

def to_midi
  self
end

#to_sObject



47
48
49
# File 'lib/zgomot/midi/note.rb', line 47

def to_s
  "[#{pitch_class.to_s},#{octave}].#{length}.#{midi}.#{velocity}"
end