# Saving External Samples into Variables

**URL:** <https://in-thread.sonic-pi.net/t/saving-external-samples-into-variables/6730>\
**Category:** Support, Help & Resources\
**Created:** [April 29, 2022, 7:05pm UTC](https://in-thread.sonic-pi.net/t/saving-external-samples-into-variables/6730 "2022-04-29T19:05:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ChaSiuBao](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/chasiubao/32/2280_2.png) [@ChaSiuBao](https://in-thread.sonic-pi.net/u/ChaSiuBao)\
**Post date:** [April 29, 2022, 7:05pm UTC](https://in-thread.sonic-pi.net/t/saving-external-samples-into-variables/6730/1 "2022-04-29T19:05:07Z")

</div>

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.

```ruby
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!

---

<div class="post-metadata">

**Author:** ![nlb](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nlb/32/997_2.png) [@nlb](https://in-thread.sonic-pi.net/u/nlb)\
**Post date:** [April 29, 2022, 8:21pm UTC](https://in-thread.sonic-pi.net/t/saving-external-samples-into-variables/6730/2 "2022-04-29T20:21:33Z")

</div>

hi,

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

Edit : better answer 🙂

```ruby
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](https://in-thread.sonic-pi.net/t/sample-name-stored-in-a-variable/1292/5)

---

<div class="post-metadata">

**Author:** ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)\
**Post date:** [April 30, 2022, 9:29am UTC](https://in-thread.sonic-pi.net/t/saving-external-samples-into-variables/6730/3 "2022-04-30T09:29:29Z")

</div>

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:

```ruby
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:

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

```

and then the script stops with this error:

```ruby
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 🙂

Does that match with what you were observing?

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

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

```

Then it prints out this in the log panel:

```ruby
 └─ "/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?
