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:



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

#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 (readonly)

Returns the value of attribute length.



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

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 (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_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



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

def note_off
  note_on + length_to_sec
end

#note_onObject



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_midiObject



69
70
71
# File 'lib/zgomot/midi/note.rb', line 69

def to_midi
  self
end

#to_sObject



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