Skip to content

Instantly share code, notes, and snippets.

@jeremywen
Last active February 3, 2021 01:37
Show Gist options
  • Save jeremywen/8d3d517c1dec1ce45107 to your computer and use it in GitHub Desktop.
Save jeremywen/8d3d517c1dec1ce45107 to your computer and use it in GitHub Desktop.
This sonic pi v2.8 code will generate euclidean loops as wav files from source samples. This is inspired by Micah Frank and this post: http://puremagnetik.com/post/133330599365/ableton-live-pack-clips-electronic-beats-loops-csound
###################################################################################################################
# WARNING! Don't stop this script. It is best just to let this script stop itself :-)
# If you stop this while it is running and recording, the temp file will keep growing and
# eventually fill your hard drive! If you do happen to stop it, you can either run this
# file again or run these lines in a separate buffer:
# client = SonicPi::OSC::UDPClient.new('localhost', 4557)
# client.send("/stop-recording")
# client.send("/delete-recording")
###################################################################################################################
#options
writeFiles = true
numberOfloopsToGenerate = 4
numberOfBeatsInLoop = 4
outputDir = File.expand_path("~/Downloads")
#the source samples to slice
samples = [:guit_harmonics, :guit_e_fifths, :ambi_piano]
#samples = Dir.glob("/path/to/wav/files/*.wav")
#osc client controls recording
client = SonicPi::OSC::UDPClient.new('localhost', 4557)
#just in case a previous recording was started but not stopped
client.send("/stop-recording")
client.send("/delete-recording")
#generate euclidian loops with slices of samples
numberOfloopsToGenerate.times do
use_random_seed Time.now.to_i
bpm = rrand_i(60, 110)
#pick 6 samples if there are more than 6
rndSamples = samples.length > 6 ? Array.new(6).collect{choose(samples)} : samples
starts = Array.new(6).collect{rand(1)}
amp = 2
rate = 1
attack = 0.01
decay = 0.2
load_samples rndSamples
live_loop :loop1 do
use_bpm bpm
sample rndSamples[0], attack: attack, decay: decay, sustain_level:0, start: starts[0], rate: rate, amp: amp
sleep 1.125
end
live_loop :loop2 do
use_bpm bpm
sample rndSamples[1], attack: attack, decay: decay, sustain_level:0, start: starts[1], rate: rate, amp: amp
sleep 0.25
end
live_loop :loop3 do
use_bpm bpm
sample rndSamples[2], attack: attack, decay: decay, sustain_level:0, start: starts[2], rate: rate, amp: amp
sleep 0.66
end
live_loop :loop4 do
use_bpm bpm
sample rndSamples[3], attack: attack, decay: decay, sustain_level:0, start: starts[3], rate: rate, amp: amp
sleep 2.25
end
live_loop :loop5 do
use_bpm bpm
sample rndSamples[4], attack: attack, decay: decay, sustain_level:0, start: starts[4], rate: rate, amp: amp
sleep 3.75
end
live_loop :loop6 do
use_bpm bpm
sample rndSamples[5], attack: attack, decay: decay, sustain_level:0, start: starts[5], rate: rate, amp: amp
sleep 3.0
end
# The wav file recordings always have ~1.5 seconds of silence at the beginning - I assume this is because of
# the time it takes to communicate with backend server, load samples, and run this code?!?!
client.send("/start-recording") unless !writeFiles
sleep numberOfBeatsInLoop #this is where you can end up with large temp files - if you stop running while sleeping after start-recording
client.send("/stop-recording") unless !writeFiles
client.send("/save-recording", "123", "#{outputDir}/sonic-pi-recording-#{Time.now.to_i}.wav") unless !writeFiles
end
#stop everything when done generating loops
client.send("/stop-all-jobs")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment