Skip to content

Instantly share code, notes, and snippets.

@mbutz
Created July 3, 2016 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mbutz/1c4ce54abbb5c42237f720d8ba86c59b to your computer and use it in GitHub Desktop.
Save mbutz/1c4ce54abbb5c42237f720d8ba86c59b to your computer and use it in GitHub Desktop.
Slicing and Rearranging Samples
# Martin Butz, mb@mkblog.org
#
# Code heavily based on:
# Xavier Riley, https://gist.github.com/xavriley/30444c1773434014c9ee
#
# Uses a sample from freesound:
# https://freesound.org/people/jobro/sounds/52535/
# 32 bars, 128 beats (one beat = quarter)
frsnd_sample = "/home/marty/projects/leuphana-local/sonic-pi/mb/samples/149826__jobro__big-time-breakbeat.wav"
define :play_slice do |sample_name, no_of_slices, slice_no, playback_rate, no_of_beats|
# convert index from bar counter to index
pattern_index = slice_no - 1
slice_times = range(0, 1, step: 1.0/no_of_slices, inclusive: true).each_cons(2).to_a
# range(0, 1, step: 1.0/4, inclusive: true).each_cons(2).to_a
# each_cons: Iterates the given block for each array of consecutive <n> elements
# to_a => convert to array such as
#
# => [[0, 0.25], [0.25, 0.5], [0.5, 0.75], [0.75, 1.0]]
st,fn = slice_times[pattern_index]
# Permute slice values if index is negative
fn,st = st,fn if pattern_index < 0
# if sample is sliced into 4 parts, beat_stretch_time for complete sample will be 4
beat_stretch_time = no_of_beats * no_of_slices
sample_complete_time = sample_duration(sample_name, start: st, finish: fn, rate: playback_rate, beat_stretch: beat_stretch_time)
sample_slice_time = sample_duration(sample_name, start: st, finish: fn, rate: playback_rate, beat_stretch: beat_stretch_time)
sleep_time = sample_slice_time
puts "---------------------------------"
puts "Playing #{slice_no} from #{no_of_slices} right now."
puts "---------------------------------"
##| puts "Current Slice Times => #{slice_times[pattern_index]}"
##| puts "Beat Stretch Time: #{beat_stretch_time}"
##| puts "Sample Complete Time #{sample_complete_time}"
##| puts "Sample Slice Time => #{sample_slice_time}"
##| puts "Sleep Time => #{sleep_time}"
# play the sample slice
sample sample_name, start: st, finish: fn, rate: playback_rate, beat_stretch: beat_stretch_time
sleep sleep_time
end
# play the whole amen loop in one peace
live_loop :amen_01 do
stop
with_bpm 140 do
play_slice :loop_amen, 1, 1, 1, 4
end
end
# chop it in 4 slices and play them back in row; each slice 1 beat long
live_loop :amen_02 do
stop
with_bpm 140 do
play_slice :loop_amen, 4, 1, 1, 1
play_slice :loop_amen, 4, 2, 1, 1
play_slice :loop_amen, 4, 3, 1, 1
play_slice :loop_amen, 4, 4, 1, 1
end
end
# chop it in 4 slices and play them back randomly
live_loop :amen_03 do
stop
with_bpm 140 do
play_slice :loop_amen, 4, (ring 1, 2, 3, 4).choose, 1, 1
end
end
# A long breakbeat from freesound
# chop it in 32 slices and play them back successively
live_loop :breakbeat_freesound_01 do
stop
with_bpm 140 do
play_slice frsnd_sample, 32, 1, 1, 4 # crash cymbal on 1
play_slice frsnd_sample, 32, 2, 1, 4 # bass work
play_slice frsnd_sample, 32, 3, 1, 4 # double bass
play_slice frsnd_sample, 32, 4, 1, 4 # small break
play_slice frsnd_sample, 32, 5, 1, 4
play_slice frsnd_sample, 32, 6, 1, 4 # syncopating snare
play_slice frsnd_sample, 32, 7, 1, 4
play_slice frsnd_sample, 32, 8, 1, 4 # small break
play_slice frsnd_sample, 32, 9, 1, 4 # crash cymbal on 2
play_slice frsnd_sample, 32, 10, 1, 4 # syncopating snare, double bass
play_slice frsnd_sample, 32, 11, 1, 4
play_slice frsnd_sample, 32, 12, 1, 4 # small break
play_slice frsnd_sample, 32, 13, 1, 4
play_slice frsnd_sample, 32, 14, 1, 4 # syncopating snare
play_slice frsnd_sample, 32, 15, 1, 4
play_slice frsnd_sample, 32, 16, 1, 4 # syncopating snare and hihat
play_slice frsnd_sample, 32, 17, 1, 4 # crash cymbal on 1
play_slice frsnd_sample, 32, 18, 1, 4 # syncopating snare
play_slice frsnd_sample, 32, 19, 1, 4
play_slice frsnd_sample, 32, 20, 1, 4 # syncopating snare and hihat
play_slice frsnd_sample, 32, 21, 1, 4 # heavy bass
play_slice frsnd_sample, 32, 22, 1, 4 # syncopating snare
play_slice frsnd_sample, 32, 23, 1, 4 # heavy bass
play_slice frsnd_sample, 32, 24, 1, 4 # syncopating snare and hihat
play_slice frsnd_sample, 32, 25, 1, 4 # offbeat crash cymbals
play_slice frsnd_sample, 32, 26, 1, 4
play_slice frsnd_sample, 32, 27, 1, 4 # heavy bass
play_slice frsnd_sample, 32, 28, 1, 4 # syncopating snare and hihat
play_slice frsnd_sample, 32, 29, 1, 4 # heavy bass
play_slice frsnd_sample, 32, 30, 1, 4
play_slice frsnd_sample, 32, 31, 1, 4
play_slice frsnd_sample, 32, 32, 1, 4 # syncopating snare and hihat
end
end
# A long breakbeat from freesound
# play back randomly one of the 32 slices
live_loop :breakbeat_freesound_01 do
#stop
with_bpm 140 do
play_slice frsnd_sample, 32, (range 1, 32, step: 1, inclusive: true).choose, 1, 4
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment