Generative Sequences with the Formula device


(portabello) #1

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: [sharedmedia=core:attachments:7995]

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.


(dblue) #2

Soooooooo rad!!! :smiley:


(James Britt / Neurogami) #3

Oh shit!

Thank you.


(danoise) #4

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:


(gentleclockdivider) #5

Nice !


(vmuriel) #6

So cool, thanks for sharing.

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


(Zer0 Fly) #7

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


(portabello) #8

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


(vmuriel) #9

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.


(boonier) #10

This is great btw, inspired

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


#11

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


(ABįSƧȔš ṼØȊÐƎ) #12

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!


(The_Traveler) #13

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


(Psynapsex) #14

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


(ideomux) #15

this is amazing, thank you