Generative Sequences with the Formula device

Warning: This is some pretty advanced stuff and requires a bit of lua knowledge.

With some clever abuse of the formula device you can generate some pretty cool musical sequences in real time:

An example: generative-acid.xrns (71.9 KB)

To start with we create an instrument and set its pitch modulation range to the maximum value of 96. Then we create a macro control for the pitch slider.

Next we create a formula device and route a rising sawtooth LFO to one its inputs.

If we do something like the following:

seq = {0, 3, 1, 6, 3, 5, 7, 8}

function playSeq()
  idx = floor(A * #seq) + 1
  pitch = seq[idx]
  return (96 + pitch) / 192 -- this bit normalizes the pitch values from semitones to macro values
end

and have our output be

y = playSeq()

We can modulate our pitch macro control with the formula output and play back arbitrary sequences. This can get particularly interesting if we generate new sequences on the fly using some sort of generative/procedural algorithm. In the attached XRNS file I’m using this code to generate the pitch data:

SCALE = {0, 1, 5, 7, 8, 12}

pitches = {}
glides = {}
shouldReset = true

glideChance = 0.25

currentPitch = 0
currentGlide = false

numOctaves = 3
numPitchSteps = 16
numGlideSteps = 32

function generatePitch()
  local idx = floor(random(1, 6))
  local octave = floor(random(0, numOctaves)) * 12

  return SCALE[idx] + octave
end

function generatePitches()
  for i = 1, numPitchSteps do
    pitches[i] = generatePitch()
  end
end

function generateGlide()
  return random() < glideChance
end

function generateGlides()
  for i = 1, numGlideSteps do
    glides[i] = generateGlide()
  end
end

function resetData()
  generatePitches()
  generateGlides()
end

function makePitch(steps)
  return (steps + 96) / 192
end

function tick()
  if LINE == 0 or #pitches == 0 then
    if shouldReset then
      resetData()
      shouldReset = false
    end
    else
      shouldReset = true
    end

  local idx = floor(A * 32)
  local pitchIdx = mod(idx, numPitchSteps) + 1
  local glideIdx = mod(idx, numGlideSteps) + 1

  currentPitch = pitches[pitchIdx]
  currentGlide = glides[glideIdx]
  local glideRate = 1
  if currentGlide then
    glideRate = 0.2
  end

  local pitch = makePitch(currentPitch)

  return (glideRate * pitch) + ((1 - glideRate) * OUTPUT)
end

With the output being:

y = tick()

Check out the attached file to see this in action.

4 Likes

Soooooooo rad!!! :smiley:

Oh shit!

Thank you.

Seriously, I love how it churns out a new sequence each time the pattern loops.

Could listen to this for a looong time :slight_smile:

Nice !

So cool, thanks for sharing.

Any way to edit the lua code in a external editor while renoise is running ?

I usually just copy&pase formula device code to a text editor program.

I wrote my initial version in vim then debugged everything in the formula edit window . Not a great user experience but neither is copying and pasting whenever I need to change something. I would love to be able to have it do some sort of file-watching and just let me edit externally. Even being able to load in a lua file at all would be nice. Also, as long as I’m wishing for things: I would looooove an audio-rate formula device! I tried to implement audio rate FM but it seems that the formula device only updates every tick so my sinusoids were aliasing like I’ve never seen before sad.png

1 Like

Lua is not fast enough to run at sample rate speed.

File-watching + 1.

Would be nice a formula device using c language with some kind of jit compilation.

Compile externally and load as a library.

Or use something like Tiny C https://github.com/TinyCC/tinycc

LuaJit http://luajit.org/luajit.html

Just dreaming.

This is great btw, inspired

Re JIT something like thishttp://www.osar.fr/protoplug/ but native?

Love this, having great fun hacking the code, thanks for showing us how :smiley:

Yes! Much thx. Will have a crack at this asap. Very much getting into generative art/music making. So far have only implemented it via phrase editor and Y command. This looks intriguing thx!

Thanks for much for sharing this! +1

I never thought about using the formula device to solve a problem I’m having. It might just work … :slight_smile:

Cheers

Is there a resource on the web that hosts a variety of formulas?

this is amazing, thank you

I’m trying to make this but am stuck at “Next we create a formula device” Where?

also stuck at “We can modulate our pitch macro control with the formula output” I cannot select a macro for formula output…

seems the demo song is no longer available. thanks for the work …wish i could hear it!

You need to put these three devices on any track:

devices

Make sure the Instr. Macros device is set to the correct instrument, and that the LFO destination is Formula → A.

1 Like

I re-uploaded the example file and updated the original post’s link. It’s neat to see that there is still some interest in this.

3 Likes

dope af :fire:

thanks for sharing this. wish I understood it!

a truly genius idea executed phenomenally