.each.with_index?

EDIT: To save you some reading… I had an extra line of items in bass_notes, once removed,
everything works fine… sorry for the dumbness. :slight_smile:

Hi folks,

I’m doing some tinkering with the code provided by @Ludvig in this thread:

At the bottom of his code is a long live loop :Bassline with lots of calls to a define:

So far I’ve collected all the paramters passed in the calls to two arrays:

bass_note  = [29,34,36,39,41,
              29,34,36,41,39,34,
              36,29,31,34,39,
              36,41,43,36,34,29,
              27,32,34,37,39,
              27,32,34,39,37,32,
              34,27,29,32,37,
              34,39,41,34,32,37,
              34,39,41,34,32,27]

bass_rel =   [0.75,0.75,1,1,0.5,
              0.75,0.75,1,0.5,0.5,0.5,
              0.75,0.75,1,1,0.5,
              0.75,0.75,1,0.5,0.5,0.5,
              0.75,0.75,1,1,0.5,
              0.75,0.75,1,0.5,0.5,0.5,
              0.75,0.75,1,0.5,0.5,0.5]

loops = [1,1,1,1,1,1,0,0,0]

bass  = 4

And rewritten the define basn as follows:

define :basn do |n, r|
  n.each.with_index do |n, id|
    # if loops[bass] is changed while this is playing, the next
    # line lets the define break out and return.
    if loops[bass] == 0 then
      return
    else
      play n, release: r[id], cutoff: rrand(55, 125)
      sleep r[id]
    end
  end
end

Here’s the problem. I have very little experience with indexing, so I dont know
how to tick through the value of r, that is relevant to the value of n…

The way I’m doing it, the bassline plays for a while then stops with
‘nil can’t be coerced into a float’… maybe I have mismatch in the
number of items I’m passing in the arrays?

Help figuring this out would be appreciated, as it drops about
70 lines of code from Luddies original.

Many thanks,

Eli…