One reason you may get an odd looking order is that the sample path,index method is case sensitive. This means that a sample named Moo.wav would actually play before a sample named cow.wav as it will read Uppercase BEFORE lowercase.
I guess you might be wanting to play a folder of sample continuously end to end. The problem with that is that it is not obvious how to get the sample name to use in sleep sample_duration <name>
to let you sleep the length of each sample before the next one starts.
I played around with some of the other commands in Sonic Pi and came up with a solution which utilises the commands sample_info
and sample_paths
i first ran the program:
path="~/Desktop/samples/"
index=0
puts sample_info path,index
This looked at my samples folder and produced some information about the matching sample
├─ #<SampleBuffer @id=38, @num_chans=2, @num_frames=282241, @sample_rate=44100.0, @duration=6.400022675736961>
Quite a lot of information, but significantly including the sample duration at the end.
Changing this to:
path="~/Desktop/samples/"
index=0
puts sample_duration sample_info path,index
produced the sample duration
├─ 6.400022675736961
In then built up stage by stage a command to extract the sample name:
path="~/Desktop/samples/"
index=0
puts (sample_paths sample_info path,index) #gets info as a ring
puts (sample_paths sample_info path,index)[0] #changes back to a string
puts (sample_paths sample_info path,index)[0].split("/") #splits to a list of strings
puts (sample_paths sample_info path,index)[0].split("/")[-1] #extract the last element which is the name
which produced:
├─ (ring "/Users/rbn/Desktop/samples/MK03.wav")
├─ "/Users/rbn/Desktop/samples/MK03.wav"
├─ ["", "Users", "rbn", "Desktop", "samples", "MK03.wav"]
└─ "MK03.wav"
I then put all of this together, and defined two additional methods to enable these to be used together:
path="/Users/rbn/Desktop/Sonatina Symphonic Orchestra/Samples/Percussion/"
define :getSampleName do |path,index=0|
begin
k=(sample_paths sample_info path,index)[0].split("/")[-1]
rescue
return nil
else
return k
end
end
define :getSampleDuration do |path,index=0|
begin
k=sample_duration sample_info path,index
rescue
return 0.1 #return small number if no sample found
else
return k
end
end
live_loop :splay do
tick
puts getSampleName(path,look)
sample path,look
sleep getSampleDuration(path,look)
end
This final example played all the percussion samples in the Sonatina Symphonic Orchestra percussion folder repeatedly. I added some error handling so it wouldn’t crash if a sample wasn’t found.