Created
June 12, 2014 08:58
-
-
Save xavriley/7aa8cd4c332d3204296f to your computer and use it in GitHub Desktop.
Sonic Pi random beat slicer/remixer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Beat slicer | |
# Taking a sample, we can divide it into n slices | |
# (in this example we'll use 16). Using the :start | |
# and :finish parameters for the sample method, we | |
# can 'remix' the sound by playing the slices in a | |
# random order | |
sample_name = :loop_amen | |
n = 16.0 | |
# start and finish range from 0 to 1 | |
slice_dur = 1 / n | |
# to sleep for the right amount of time, | |
# we calculate a 16th of the sample_duration | |
sleep_dur = sample_duration(sample_name) / n | |
# First we work out an array of all the points between | |
# 0 and 1 to use for start times. | |
beat_points = (0..(n-1)).map { |i| i * slice_dur } | |
beat_points.concat([1]) # to get the last beat - will become clear later | |
# This gives us an array like so: | |
# [ | |
# 0.0, 0.0625, 0.125, 0.1875, 0.25, 0.3125, 0.375, | |
# 0.4375, 0.5, 0.5625, 0.625, 0.6875, 0.75, 0.8125, | |
# 0.875, 0.9375, 1 | |
# ] | |
# | |
# What we need is to pair each of these together to | |
# give us the start and finish times of each slice. | |
# Luckily this is exactly what Ruby's each_cons method does! | |
start_and_end_times = beat_points.each_cons(2).to_a | |
# This means start_and_end_times is now an array like this: | |
# | |
# [[0.0, 0.0625], [0.0625, 0.125], [0.125, 0.1875], [0.1875, 0.25], | |
# [0.25, 0.3125], [0.3125, 0.375], [0.375, 0.4375], [0.4375, 0.5], | |
# [0.5, 0.5625], [0.5625, 0.625], [0.625, 0.6875], [0.6875, 0.75], | |
# [0.75, 0.8125], [0.8125, 0.875], [0.875, 0.9375], [0.9375, 1]] | |
# | |
# So we have our start and end times for each of the 16 slices. | |
# NOTE the 1 at the end - that was added earlier to make sure | |
# we can play the last slice | |
random_beat_slices = start_and_end_times.shuffle | |
loop do | |
random_beat_slices.each do |s_and_e| | |
sample sample_name, attack: 0, release: 0.05, start: s_and_e.first, finish: s_and_e.last | |
sleep sleep_dur | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment