Class: Dist

Inherits:
Api show all
Defined in:
lib/api/dist.rb

Overview

a consecutave sequence of morphable tones (TonePart) of varying lengths, and rate of morphs, played without gaps.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Api

#<<, #>>, #parent

Constructor Details

#initializeDist

will have a defult hit at 0 if it has a sound and no hits have been made.



6
7
8
9
10
11
12
# File 'lib/api/dist.rb', line 6

def initialize    
  super
  @dist=SndDist.new
  @hits=HitSq.new
  @hits.add_parent self
  persist_hits(true)
end

Instance Attribute Details

#distObject

Returns the value of attribute dist.



4
5
6
# File 'lib/api/dist.rb', line 4

def dist
  @dist
end

Instance Method Details

#[](i) ⇒ Object

get a Dist child



150
151
152
153
154
155
156
157
158
# File 'lib/api/dist.rb', line 150

def [] i
  child=@dist.get_children[i]
  raise "This Dist has no child at index #{i}. " +
    "It has #{branches} children." if child.nil?
  d=Dist.new
  d.dist=child
  d.make_hits
  d
end

#branchesObject

count children (Dists only)



176
177
178
# File 'lib/api/dist.rb', line 176

def branches
  @dist.get_children.count
end

#clear_hitsObject

delete all hits



144
145
146
147
148
# File 'lib/api/dist.rb', line 144

def clear_hits
  @hits.hits = []
  persist_hits
  self
end

#clear_sndObject

delete all Snd attached to this Dist



202
203
204
205
# File 'lib/api/dist.rb', line 202

def clear_snd
  @dist.tss = []
  self
end

#copyObject

return a new Dist with the same hits and length but not children or sounds.



15
16
17
18
19
20
21
# File 'lib/api/dist.rb', line 15

def copy
  out=Dist.new
  out.length = length
  out.clear_hits
  out.hits << hits
  out
end

#del(ind) ⇒ Object

delete another Dist at index ind



129
130
131
132
# File 'lib/api/dist.rb', line 129

def del ind
  @dist.snd.delete_at ind
  self
end

#drum(weight, amp, f_range = 0.0, tone_num = 1, layers = 1) ⇒ Object

add children who have sounds that make a drum like sound

weight

0 is lowest drum, 1 is highest



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/api/dist.rb', line 25

def drum weight, amp, f_range=0.0, tone_num = 1, layers=1
  layers.times do |i|
    self << Dist.new
    flay = last_born
    min_oc = 1
    max_oc = 5
    oran = max_oc - min_oc
    pos_num = scale_notes.count * oran #possible
    ind = (pos_num * weight).round
    oct_ind =  min_oc + ind / scale_notes.count
    log "drum notes #{ind % scale_notes.count} #{oct_ind}, weight #{weight}", 3
    freq = Note.new(scale_notes[ind % scale_notes.count], oct_ind).freq
    flay.drumify freq, amp/layers/2, f_range, tone_num #test
    
  end
  flay = self[0]
  self << flay.copy
  under_sound = last_born
  under_sound << Snd.new
  under_sound.snd.length = flay.snd.length
  under_sound.snd.freq = flay.snd.freq
  under_sound.snd.amp = amp/2.0
  under_sound.snd.fade
  under_sound.snd.tone.amp.exp = flay.snd.tone.amp.exp
  under_sound.clear_hits # to test
#     branches.times do |i| 
#       self[i].snd.amp=0 if i !=branches-1 # to test
#     end
end

#drumify(freq, amp, f_range, tone_num) ⇒ Object

assumes an already existing Snd and HitSq, just makes the Snd more like a percussive instrument



56
57
58
59
60
61
62
# File 'lib/api/dist.rb', line 56

def drumify freq, amp, f_range, tone_num
  random_sound(freq, f_range, tone_num, amp)
  snd.toneseq.toneparts.last.do_all {|tone| 
    tone.fade
    tone.amp.rand_exp true #below linear
    }
end

#first_bornObject

get first child



168
169
170
171
172
173
174
# File 'lib/api/dist.rb', line 168

def first_born
  child=@dist.get_children.first
  d=Dist.new
  d.dist=child
  d.make_hits
  d
end

#hitsObject

getter for HitSq. Will persist any changes you make to it.



180
181
182
# File 'lib/api/dist.rb', line 180

def hits
  @hits
end

#last_bornObject

get last child



160
161
162
163
164
165
166
# File 'lib/api/dist.rb', line 160

def last_born
  child=@dist.get_children.last
  d=Dist.new
  d.dist=child
  d.make_hits
  d
end

#lengthObject

get the total length in frames



140
141
142
# File 'lib/api/dist.rb', line 140

def length
  @dist.len
end

#length=(set) ⇒ Object

set the total length in frames



134
135
136
137
138
# File 'lib/api/dist.rb', line 134

def length= set
  @dist.len = set.round
#    @dist.tss.each {|tss| tss.len = set }
  self
end

#make(num = 1, i = 0) ⇒ Object

add num TonePart to Snd at i’s ToneSeq, with my #length as max.



120
121
122
# File 'lib/api/dist.rb', line 120

def make(num=1, i=0)
  snd(i).toneseq.make(num)
end

#MapperObject

shortcut to create a Mapper, with Mapper#map_to= me



125
126
127
# File 'lib/api/dist.rb', line 125

def Mapper
  Mapper.new(self)
end

#persist_hits(is_def = false) ⇒ Object

(internal use only) copy our hits down to the underlining object



218
219
220
221
222
# File 'lib/api/dist.rb', line 218

def persist_hits(is_def = false)
  @dist.hits = hits.hits
  @dist.hits = [0.0] if is_def && hits.hits.count == 0
  self
end

#phObject

print hits for debugging



64
65
66
# File 'lib/api/dist.rb', line 64

def ph
  puts hits.hits.inspect
end

#random_melodies(min_b_len, reps, mel_layers = 4, start_octave = 2, max_amp = 0.5, seqs_max = 0, chance = 0.4, random_del_chance = 0.25, max_b_mult = 4) ⇒ Object

give mel_layers children each with a unique melody in incrementing octaves.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/api/dist.rb', line 86

def random_melodies min_b_len, reps, mel_layers = 4, start_octave = 2, max_amp = 0.5, 
    seqs_max =0, chance = 0.4, random_del_chance = 0.25, max_b_mult=4
  mel_layers.times do |i|
    self << Dist.new 
    melody = last_born
    map = melody.Mapper
    map.random_melody min_b_len, reps, start_octave+i, chance, max_amp/mel_layers, rand(seqs_max).to_i, max_b_mult
    
    # each layer picks a few bars to delete every note from.
    to_del = []
    map.mapee(0).hits.count.times do |h|
      if rand < random_del_chance
        to_del << h
      end
    end
#      to_del = [1] #test
    map.each_dist do |d| 
      d[0].hits.delete_arr to_del
#        d[0].ph
    end
#      puts to_del
#      map.dist.branches.times {|j| todel << map[j] if j!=0 }
#      map.dist >> todel
  end
end

#random_sound(root_f, f_range, parts, amp, max_delay = 0.0) ⇒ Object

add an Snd to self with some random properties



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/api/dist.rb', line 69

def random_sound root_f, f_range, parts, amp, max_delay = 0.0
#    tone_num.times do |i|
    self << Snd.new
    delay = rand*max_delay
    1.times {delay*=rand}
    snd.toneseq.random(rand(parts).to_i, [true,false].sample, delay, amp, root_f+f_range*root_f, root_f-f_range*root_f)
    snd.toneseq.toneparts.each do |tp|
#        tp.do_all {|tone| tone.set_freq root_f}
#        range = root_f * f_range
#        tp.do_all {|tone| tone.set_freq_final root_f - range/2.0 + rand*range}
    end
#      last_born.clear_hits
#      last_born << delay
#    end
end

#set_child_len(val) ⇒ Object

sets the length of all children Dist to val



113
114
115
116
117
# File 'lib/api/dist.rb', line 113

def set_child_len val
  branches.times do |i|
    self[i].length = val
  end
end

#snd(i = 0) ⇒ Object

getter for Snd. Will persist any changes you make to it.



184
185
186
187
188
189
190
191
# File 'lib/api/dist.rb', line 184

def snd i=0
  snd=Snd.new
  snd.add_parent self
  new=@dist.tss[i]
  raise "Dist has no sound at index #{i}. It has #{sounds} sounds." if new.nil?
  snd<< new
  snd
end

#snd_eachObject

run on all sounds



207
208
209
210
211
212
# File 'lib/api/dist.rb', line 207

def snd_each
  sounds.times {|i|
    yield(snd i)
  }
  self
end

#soundsObject

count sounds



214
215
216
# File 'lib/api/dist.rb', line 214

def sounds
  @dist.tss.count
end