Home | ER-101 | ER-102 | ER-301 | Wiki | Contact

Generating random "sample chains"

A small experiment and « work in progress »… I was looking for a way to create some random “sample chains” of length n from a very large folder (and sub folders) containing thousands of samples stored on the SD card of the ER-301. I’ve read on this forum that the “sample chains” (aka “multi samples”) consist in a list of paths to audio files stored in a LUA table and saved in a .multi file.

So I wrote a lua script that generates random sample chains of a specified length. It’s probably not very clean and flexible code and it needs to be executed from a computer but if someone wants to use it or improve it or convert it to a Bash script or else… I’ve done this with Lua because it was easier for me, I needed a quick solution ^^

I’m just sharing it for info, I have to admit that it’s not super convenient to use + there are many ways to make it fail but it does the job in my case :smiley:


The Lua script simply lists the files in the « samples » folder and then populates a table containing all the .wav and .aif files it finds recursively in the samples directory (or specific sub folders) and finally generates a new random chain of length n out of this table and stores it in a .multi file ready to be used on the 301. The chains are stores in a “_chains” directory. You need to have Lua installed on your machine of course

1 - Save the following file to the ER-301 directory of the SD card.

random_chain_301.lua
local sample_list = {}
local sample_chain = {}

function list_samples(base)
  local i, sample_list, popen = 0, {}, io.popen
  local dir = ""
  local base = base or dir
  local pfile = popen("find ./samples"..base.." -name '*.aif' -o -name '*wav' -size -3M -type f -not -path '*/\\.*' | cut -c3-")
  for filename in pfile:lines() do
    i = i + 1
    sample_list[i] = filename
  end
  pfile:close()
  return sample_list
end

function randomize_chain(length)
  math.randomseed( os.time() + math.random(256) )
  sample_chain = {}
  for i = 1, length do
    sample_chain[i] = sample_list[math.random(#sample_list)]
  end  
end  

function create_multi_file(chain_name, i)
  file = io.open("./samples/_chains/"..chain_name.."/"..chain_name.."_"..i..".multi", "w")
  file:write( "-- Persistent Data \n local shared = { \n \n } -- shared \n local obj1 = { \n" )
    for i, v in pairs(sample_chain) do
      file:write("    ["..i.. "] = ".. '"1:/ER-301/'..v..'"'.. "; \n")
    end
  file:write("} \n return obj1")
  file:close()
end  

function new_sample_chain(chain_name, length, base, chains)
  sample_chain = {}
  sample_list = list_samples(base)
  os.execute("mkdir -p ./samples/_chains/"..chain_name)
  for i = 1, chains do
    randomize_chain(length)
    create_multi_file(chain_name, i)
  end
end

function init ()

end  

2 - Open a terminal in this folder and create a new chain using the following command. (where ‘name_of the chain’ is the output name of the .multi file, 128 is the length of the chain, and ‘/base_directory’ without space is the folder where the script starts looking for samples, no spaces, if nothing is mentioned it will search the whole ‘samples’ folder, 24 is the amount of chains generated)

lua -e"dofile'random_chain_301.lua' new_sample_chain('name_of_the_chain', 128, '/base_directory', 24)"

Here’s how a .multi file looks like

example.multi
-- Persistent Data
local shared = {

} -- shared
local obj1 = {
        [1] = "1:/ER-301/samples/percussion room/bass drum f.wav";
        [2] = "1:/ER-301/samples/percussion room/bass drum mf.wav";
        [3] = "1:/ER-301/samples/percussion room/bass drum mmp.wav";
        [4] = "1:/ER-301/samples/percussion room/bass drum mp.wav";
        [5] = "1:/ER-301/samples/percussion room/bass drum p.wav";
        [6] = "1:/ER-301/samples/percussion room/c hat f.wav";
        [7] = "1:/ER-301/samples/percussion room/c hat mf.wav";
        [8] = "1:/ER-301/samples/percussion room/c hat p.wav";
        [9] = "1:/ER-301/samples/percussion room/c hat p2.wav";
}
return obj1
3 Likes