Saving External Samples into Variables

Hi, I’m very new to Sonic Pi.

I’m trying to take wav files, save them into variables, place them in an array, and use a loop for playback. I tried adding the external samples to a ring array and iterate with .tick but nothing happened.

puts (ring "/Users/username/folder/track01.wav", "/Users/username/folder/track02.wav").tick

Also tried holding external sample directories in variables then add to ring, then tried .to_a but that didn’t work either.

a = "/Users/username/folder/track01.wav"
b = "/Users/username/folder/track02.wav"
s= (ring a, b).tick #=> :a
puts "this is a ring",s
sa = s.to_a
puts "this is an array",sa

Any help would be appreciated!

hi,

can you try this code after according changes to your wav file path

Edit : better answer :slight_smile:

a = "D:/audio/mesSamples/drumbeats/acoustic_2_3.wav"
b = "D:/audio/mesSamples/drumbeats/breakbeats_2_1.wav"

s = (ring a, b)
a = s.to_a

s.length.times do
  current = s.tick
  sample current
  sleep sample_duration(current)
end
sample a[1]

and tell me if it’s ok for you.

edit : have a look at this post Sample name stored in a variable - #5 by robin.newman

Hey @ChaSiuBao!

Nlb shows one way that should work.

As for the issues that you were having originally - if I take your second example, substitute the sample paths for some on my own system and run it:

a = "C:/Program Files/Sonic Pi/etc/samples/ambi_choir.flac"
b = "C:/Program Files/Sonic Pi/etc/samples/ambi_drone.flac"
s= (ring a, b).tick #=> :a
puts "this is a ring",s
sa = s.to_a
puts "this is an array",sa

Then I get this in the log panel:

 └─ "this is a ring" "C:/Program Files/Sonic Pi/etc/samples/ambi_choir.flac"

and then the script stops with this error:

Runtime Error: [buffer 7, line 5] - NoMethodError
Thread death!
 undefined method `to_a' for "C:/Program Files/Sonic Pi/etc/samples/ambi_choir.flac":String
...

For me, this error is happening because by the time the s.to_a is executed, s is a string, the path to a sample - and Sonic Pi doesn’t know how to convert a string into an array.

Why is s a string by that point? this is because further above, when we call tick on the ring, it returns the next item in the ring - a string :slight_smile:

Does that match with what you were observing?

Also, regarding your first example - if I run it:

puts (ring "/Users/username/folder/track01.wav", "/Users/username/folder/track02.wav").tick

Then it prints out this in the log panel:

 └─ "/Users/username/folder/track01.wav"

Do you get the same?
When you say “tried adding the external samples to a ring array and iterate with .tick but nothing happened” - there must have been more code to that, can you share it?