Class: Zgomot::Comp::Chord

Inherits:
Object show all
Defined in:
lib/zgomot/comp/chord.rb

Defined Under Namespace

Classes: Progression

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Chord

Returns a new instance of Chord.



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/zgomot/comp/chord.rb', line 42

def initialize(args)
  @length, @velocity, @chord = args[:length], args[:velocity], args[:chord]
  (@intervals =  Chord.chord_intervals[chord]) || raise(Zgomot::Error, "#{chord.inspect} is invalid")
  @time_scale, @inversion, @reverse = 1.0, 0, false
  @tonic = case args[:tonic]
            when Array then args[:tonic]
            when Symbol then [args[:tonic], 4]
            when nil then [:C,4]
            else raise(Zgomot::Error, "#{args[:tonic].inspect} is invalid tonic")
          end
end

Class Attribute Details

.chord_intervalsObject (readonly)

Returns the value of attribute chord_intervals.



36
37
38
# File 'lib/zgomot/comp/chord.rb', line 36

def chord_intervals
  @chord_intervals
end

Instance Attribute Details

#arpObject (readonly)

Returns the value of attribute arp.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def arp
  @arp
end

#chordObject (readonly)

Returns the value of attribute chord.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def chord
  @chord
end

#clockObject (readonly)

Returns the value of attribute clock.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def clock
  @clock
end

#intervalsObject (readonly)

Returns the value of attribute intervals.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def intervals
  @intervals
end

#inversionObject (readonly)

Returns the value of attribute inversion.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def inversion
  @inversion
end

#itemsObject (readonly)

Returns the value of attribute items.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def items
  @items
end

#lengthObject (readonly)

Returns the value of attribute length.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def length
  @length
end

#reverseObject (readonly)

Returns the value of attribute reverse.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def reverse
  @reverse
end

#time_scaleObject (readonly)

Returns the value of attribute time_scale.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def time_scale
  @time_scale
end

#tonicObject (readonly)

Returns the value of attribute tonic.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def tonic
  @tonic
end

#velocityObject (readonly)

Returns the value of attribute velocity.



39
40
41
# File 'lib/zgomot/comp/chord.rb', line 39

def velocity
  @velocity
end

Instance Method Details

#arp!(v) ⇒ Object



70
71
72
# File 'lib/zgomot/comp/chord.rb', line 70

def arp!(v)
  @notes = nil; @arp = v; self
end

#bpm!(v) ⇒ Object



79
80
81
# File 'lib/zgomot/comp/chord.rb', line 79

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

#channel=(chan) ⇒ Object



97
98
99
# File 'lib/zgomot/comp/chord.rb', line 97

def channel=(chan)
  notes.each{|n| n.channel = chan}
end

#inv!(v) ⇒ Object



73
74
75
# File 'lib/zgomot/comp/chord.rb', line 73

def inv!(v)
  @notes = nil; @inversion = v; self
end

#length!(v) ⇒ Object



85
86
87
# File 'lib/zgomot/comp/chord.rb', line 85

def length!(v)
  @length = v; self
end

#length_to_secObject



91
92
93
# File 'lib/zgomot/comp/chord.rb', line 91

def length_to_sec
  time_scale*Zgomot::Midi::Clock.whole_note_sec*(1.0/length + (arp.to_f.eql?(0.0) ? 0.0 : intervals.length.to_f/arp.to_f))
end

#note(number) ⇒ Object



62
63
64
# File 'lib/zgomot/comp/chord.rb', line 62

def note(number)
  Zgomot::Midi::Note.new(:pitch => pitches[number], :length => length, :velocity => velocity)
end

#notesObject



65
66
67
68
69
# File 'lib/zgomot/comp/chord.rb', line 65

def notes
  @notes ||= pitches.map do |p|
               Zgomot::Midi::Note.new(:pitch => p, :length => length, :velocity => velocity)
             end
end

#octave!(v) ⇒ Object



82
83
84
# File 'lib/zgomot/comp/chord.rb', line 82

def octave!(v)
  @notes = nil; @octave = v; self
end

#offset=(time) ⇒ Object



100
101
102
# File 'lib/zgomot/comp/chord.rb', line 100

def offset=(time)
  notes.each{|n| n.offset = time}
end

#pitchesObject



54
55
56
57
58
59
60
61
# File 'lib/zgomot/comp/chord.rb', line 54

def pitches
  last_pitch, octave = tonic; pitches = [tonic]
  intervals.each_index{|i| pitches << PitchClass.next(tonic.first, intervals[i])}
  nts = pitches[1..-1].map do |p|
          octave += 1 if p < last_pitch; last_pitch = p.value; [last_pitch, octave]
        end.unshift(tonic)
  @reverse ? invert(nts).reverse : invert(nts)
end

#rev!Object



76
77
78
# File 'lib/zgomot/comp/chord.rb', line 76

def rev!
  @reverse = true; self
end

#time=(time) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/zgomot/comp/chord.rb', line 103

def time=(time)
  @clock = Zgomot::Midi::Clock.new
  clock.update(time)
  notes.each do |n|
    n.time = clock.current_time
    clock.update(Zgomot::Midi::Clock.whole_note_sec/arp.to_f) if arp.to_f > 0.0
  end
end

#to_midiObject



94
95
96
# File 'lib/zgomot/comp/chord.rb', line 94

def to_midi
  notes.map{|n| n.to_midi}
end

#valocity!(v) ⇒ Object



88
89
90
# File 'lib/zgomot/comp/chord.rb', line 88

def valocity!(v)
  @length = v; self
end