Module: Xi::Pattern::Generators

Included in:
Xi::Pattern
Defined in:
lib/xi/pattern/generators.rb

Instance Method Summary collapse

Instance Method Details

#geom(start = 0, grow = 1, length = inf) ⇒ Pattern

Create a geometric series pattern of length values, being start the starting value and step the multiplication factor.

Examples:

peek P.geom                 #=> [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
peek P.geom(3)              #=> [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
peek P.geom(1, 2)           #=> [1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
peek P.geom(1, 1/2, 6)      #=> [1, (1/2), (1/4), (1/8), (1/16), (1/32)]
peek P.geom(1, -1, 8)       #=> [1, -1, 1, -1, 1, -1, 1, -1]

Parameters:

  • start (Numeric) (defaults to: 0)

    (default: 0)

  • grow (Numeric) (defaults to: 1)

    (default: 1)

  • length (Numeric, Symbol) (defaults to: inf)

    number or inf (default: inf)

Returns:



43
44
45
46
47
48
49
50
51
# File 'lib/xi/pattern/generators.rb', line 43

def geom(start=0, grow=1, length=inf)
  Pattern.new(size: length) do |y|
    i = start
    loop_n(length) do
      y << i
      i *= grow
    end
  end
end

#isaw(*args) ⇒ Pattern

Generates an inverse sawtooth waveform, discretized to quant events for the duration of delta cycles

Values range from 0 to 1

Examples:

peek P.isaw(8)
  #=> [(1/1), (7/8), (3/4), (5/8), (1/2), (3/8), (1/4), (1/8), (1/1), (7/8)]

Parameters:

Returns:

See Also:

  • Xi::Pattern.saw


191
192
193
# File 'lib/xi/pattern/generators.rb', line 191

def isaw(*args)
  -P.saw(*args) + 1
end

#rand(list, repeats = 1) ⇒ Pattern

Choose items from the list randomly, repeats number of times

list can be a finite enumerable or Pattern.

Examples:

peek P.rand([1, 2, 3])          #=> [2]
peek P.rand([1, 2, 3, 4], 6)    #=> [1, 3, 2, 2, 4, 3]

Parameters:

  • list (#each)

    list of values

  • repeats (Integer, Symbol) (defaults to: 1)

    number or inf (default: 1)

Returns:

See Also:



67
68
69
70
71
72
# File 'lib/xi/pattern/generators.rb', line 67

def rand(list, repeats=1)
  Pattern.new(list, size: repeats) do |y|
    ls = list.to_a
    loop_n(repeats) { y << ls.sample }
  end
end

#saw(quant, delta = 1) ⇒ Pattern

Generates values from a sawtooth waveform, discretized to quant events for the duration of delta cycles

Values range from 0 to 1

Examples:

peek P.saw(8)
  #=> [(0/1), (1/8), (1/4), (3/8), (1/2), (5/8), (3/4), (7/8), (0/1), (1/8)]

quant determines the size, delta the total duration

P.saw(8).size           #=> 8
P.saw(22).duration      #=> (1/1)
P.saw(19, 2).duration   #=> (2/1)

Parameters:

Returns:



168
169
170
171
172
173
174
# File 'lib/xi/pattern/generators.rb', line 168

def saw(quant, delta=1)
  Pattern.new(size: quant, delta: delta / quant) do |y|
    quant.times do |i|
      y << i / quant
    end
  end
end

#series(start = 0, step = 1, length = inf) ⇒ Pattern

Create an arithmetic series pattern of length values, being start the starting value and step the addition factor.

Examples:

peek P.series                 #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
peek P.series(3)              #=> [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
peek P.series(0, 2)           #=> [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
peek P.series(0, 0.25, 8)     #=> [0, 0.25, 0.5, 0.75, 1.0, 1.25, 1.5, 1.75]

Parameters:

  • start (Numeric) (defaults to: 0)

    (default: 0)

  • step (Numeric) (defaults to: 1)

    (default: 1)

  • length (Numeric, Symbol) (defaults to: inf)

    number or inf (default: inf)

Returns:



18
19
20
21
22
23
24
25
26
# File 'lib/xi/pattern/generators.rb', line 18

def series(start=0, step=1, length=inf)
  Pattern.new(size: length) do |y|
    i = start
    loop_n(length) do
      y << i
      i += step
    end
  end
end

#shuf(list, repeats = 1) ⇒ Pattern

Shuffle the list in random order, and use the same random order repeats times

list can be a finite enumerable or Pattern.

Examples:

peek P.shuf([1, 2, 3, 4, 5])    #=> [5, 3, 4, 1, 2]
peek P.shuf([1, 2, 3], 3)       #=> [2, 3, 1, 2, 3, 1, 2, 3, 1]

Parameters:

  • list (#each)

    list of values

  • repeats (Integer, Symbol) (defaults to: 1)

    number or inf (default: 1)

Returns:

See Also:



115
116
117
118
119
120
121
122
# File 'lib/xi/pattern/generators.rb', line 115

def shuf(list, repeats=1)
  Pattern.new(list, size: list.size * repeats) do |y|
    xs = list.to_a.shuffle
    loop_n(repeats) do |i|
      xs.each { |x| y << x }
    end
  end
end

#sin(quant, delta = 1) ⇒ Pattern

Generates values from a sinewave discretized to quant events for the duration of delta cycles.

Values range from 0 to 1

Examples:

peek P.sin(8).map { |i| i.round(2) }
  #=> [0.5, 0.85, 1.0, 0.85, 0.5, 0.15, 0.0, 0.15, 0.5, 0.85]

quant determines the size, delta the total duration

P.sin(8).size           #=> 8
P.sin(22).duration      #=> (1/1)
P.sin(19, 2).duration   #=> (2/1)

Parameters:

Returns:



142
143
144
145
146
147
148
# File 'lib/xi/pattern/generators.rb', line 142

def sin(quant, delta=1)
  Pattern.new(size: quant, delta: delta / quant) do |y|
    quant.times do |i|
      y << (Math.sin(i / quant * 2 * Math::PI) + 1) / 2
    end
  end
end

#tri(quant, delta = 1) ⇒ Pattern

Generates a triangle waveform, discretized to quant events for the duration of delta cycles

Values range from 0 to 1

Examples:

peek P.tri(8)
  #=> [(0/1), (1/4), (1/2), (3/4), (1/1), (3/4), (1/2), (1/4), (0/1), (1/4)]

Parameters:

Returns:



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/xi/pattern/generators.rb', line 208

def tri(quant, delta=1)
  Pattern.new(size: quant, delta: delta / quant) do |y|
    half_quant = quant / 2
    up_half = half_quant.to_f.ceil
    down_half = quant - up_half

    up_half.times do |i|
      y << i / half_quant
    end
    down_half.times do |i|
      j = down_half - i
      y << j / half_quant
    end
  end
end

#xrand(list, repeats = 1) ⇒ Pattern

Choose randomly, but only allow repeating the same item after yielding all items from the list.

list can be a finite enumerable or Pattern.

Examples:

peek P.xrand([1, 2, 3, 4, 5])    #=> [4]
peek P.xrand([1, 2, 3], 8)       #=> [1, 3, 2, 3, 1, 2, 3, 2]

Parameters:

  • list (#each)

    list of values

  • repeats (Integer, Symbol) (defaults to: 1)

    number or inf (default: 1)

Returns:

See Also:



89
90
91
92
93
94
95
96
97
98
# File 'lib/xi/pattern/generators.rb', line 89

def xrand(list, repeats=1)
  Pattern.new(list, size: repeats) do |y|
    ls = list.to_a
    xs = nil
    loop_n(repeats) do |i|
      xs = ls.shuffle if i % ls.size == 0
      y << xs[i % ls.size]
    end
  end
end