# 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.

2 Likes

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

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

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

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

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 â€¦

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:

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.

2 Likes

dope af

thanks for sharing this. wish I understood it!

a truly genius idea executed phenomenally