Randomised Sequences that Stick

Inspired by @martin’s code (thank you) here’s my go at a randomised sequence generator, where the sequence stays stuck until I want to change it. I’m sure this will be useful.

But this is the point where someone gently points out that Sam’s written an in-built one already.

I’m starting to want to have my own function library - is that possible?

#Sequencer Generator
#Randomised sequences that stick
use_bpm 80

define :getseq do |length, notes, seed|
  #Generate a randomised sequence based on the seed
  seq=[]
  use_random_seed seed
  for i in 0..length-1
    seq.push(notes[rrand_i(0,notes.length-1)])
  end
  seq.ring
end

notes = scale :C4, :major, num_octaves: 1

#Change the sequence length and seed
seq1 = getseq(7,notes,1)

live_loop :main do
  8.times do
    play seq1.tick, amp: 0.2
    sleep 0.25
  end
end

live_loop :bass, sync: :main do
  play :C3, amp: 0.2
  sleep 1
end
2 Likes

You can define individual functions, and then allow them to be available on Sonic Pi start up using init.rb - is that close enough?

1 Like

Yes that’s ideal, thanks

1 Like

In fact I think that something like this is just a useful - slightly different effect but easier to code live! A randomised sequence that repeats - that’s the key thing. My ideal would be a sequences that gradually evolves rather than hops between states.

use_random_seed 5
    seq = notes.shuffle
    in_thread do
      16.times do
        osc "/midi4x4",seq.tick,127,0.1,1,1
        sleep 0.25
      end
end
1 Like

Re function library, I do a similar thing by defining my utility functions in a dedicated file, and then using run_file in init.rb :+1:

1 Like

That sounds good, I’ll experiment

NB if you use the latest 3.3beta on Mac released to Patreon supporters today then be aware that the location of init.rb has moved to ~/.sonic-pi/config/init.rb from ~/.sonic-pi/init.rb

1 Like

Windows and Raspberry Pi me.

The ring operations take, shuffle etc. are rich pickings for this directed randomness. But I’ve decided what I need is a shuffle that where I can control the amount of shuffling if you see what I mean. I think I should be able to do that. Like you specify how many elements get swapped, or how far apart they are.

I built my own prototype version of exactly that too a little while ago. Haven’t used it for a little while, so I forget if it was fully tested and working :joy: but I was also wanting such a thing. I am hopeful that a version of it will make its way into the official functions.

1 Like

Ah ah, great minds :smile: I think this would be really good for the live stuff I’m working on now, so I’ll give it a go too.

EDIT mind you, the existing ring functions are at least as rich as what you get in a modular sequencer and more flexible in combo, so maybe I should exhaust those possibilities before chomping at the bit for new functions. Users…can’t live with them…

2 Likes

I have found the new use_random_stream command in latest SP3.3.0beta useful for controlling what is selected. I itilised it for example in my beethoven mashup program I haven’t published this yet because it contained this command, which I used in my own build. I changed the selection during the program using use_random_stream [:white,:pink].choose to change the effect of subsequent “random” commands.

1 Like

Oh yes, that does look interesting. With a bit of experimenting now I have found some nice ways of using the ring functions to give more of evolving feel to a sequence. I guess at bottom I’m trying to hear some of the conventional ways to cadance a solo - increasing complexity, embellishment, motivic development. Simply adding more randomness (which is the easiest to do) is a way, but a bit of a one-trick pony. I think this new use_random_stream could help things along.