# External sample issues

**URL:** <https://in-thread.sonic-pi.net/t/external-sample-issues/2901>\
**Category:** Support, Help & Resources\
**Created:** [November 9, 2019, 4:16pm UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901 "2019-11-09T16:16:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eyebot](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Eyebot](https://in-thread.sonic-pi.net/u/Eyebot)\
**Post date:** [November 9, 2019, 4:16pm UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901/1 "2019-11-09T16:16:46Z")

</div>

Hello all,

I’m super new to both coding and Sonic Pi but love both so far. I have encountered a few issues with external samples.  
When I play an external sample using live\_loop and hit run again, it plays multiple samples that overlapp and doesn’t kill the previous ones. I know I’m probably not doing something right but I thought this usually shouldn’t happen using live\_loop?

Another issue I’m having is with my folder of samples. I’m using “sample samples, 0” to call my samples from their folder but they come in random, for instance when I type 0 it will not play the first sample in the list but one that is somewhere in the middle. How does Sonic Pi define the list’s order?

Finally, and this is super newbie stuff, but how do I stretch my sample to play the entire length of my sleep cycle to have play continuously without interuption?

Thanks!!

---

<div class="post-metadata">

**Author:** ![Eli](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/eli/32/116_2.png) [@Eli](https://in-thread.sonic-pi.net/u/Eli)\
**Post date:** [November 9, 2019, 6:14pm UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901/2 "2019-11-09T18:14:23Z")

</div>

Hi Eye,

Other people will pitch in with answers no doubt, some will be better than mine, I  
just happen to have got here first.

Lets go with question 3 first… stretching samples and sleeping… heres some example  
code which should explain without too many words.

```ruby
#sleep for the length of the sample.
live_loop :do_sample1 do
  t=[:drum_bass_hard, :drum_heavy_kick].choose
  sample t, pan: 0, amp: 1
  sleep sample_duration(t)
end

#stretch the length of the sample.
live_loop :do_sample2 do
  t=[:drum_bass_hard, :drum_heavy_kick].choose
  sample t, pan: 0, amp: 1, beat_stretch: 2
  sleep 2
end

```

As for question 2, I dont rely on using “sample samples, 0”  
to load samples… if I want a sample, I load it by name…

sample “fred/drums/kick.wav”

It is also possible to put the name into a variable as in the  
above example, and then call it with sample…

t=“fred/drums/kick.wav”  
sample t

It is also possible to set a path to your samples if they  
are nested deep in subfolders, etc…

sample\_paths “/path/to/samples/\*\*”

Even more convenient, you can also drag and drop a sample  
file into the SP coding window and it will copy the path to the  
file for you.

As for your first question, without really seeing your code,  
I cant say why thats happening… so post it up in a reply.

If you use ‘’’ on the line previous to your code in the post,  
it will put it in a neat scrollable box, much easier to read.

‘’’ on the line after you code will close the box, allowing you  
to continue writing your post.

Regards,

Eli…

---

<div class="post-metadata">

**Author:** ![Eyebot](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Eyebot](https://in-thread.sonic-pi.net/u/Eyebot)\
**Post date:** [November 9, 2019, 8:01pm UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901/3 "2019-11-09T20:01:11Z")

</div>

Hey Eli,

Thanks so much for your reply! I actually figured out my first question. The problem was happening when using “sample samples, 0”. However when I load it by sample name directly I didn’t have that issue anymore. Thanks again for your help!

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [November 9, 2019, 8:35pm UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901/4 "2019-11-09T20:35:57Z")

</div>

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:

```ruby
path="~/Desktop/samples/"
index=0
puts sample_info path,index

```

This looked at my samples folder and produced some information about the matching sample

```ruby
├─ #<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:

```ruby
path="~/Desktop/samples/"
index=0
puts sample_duration sample_info path,index

```

produced the sample duration

```ruby
 ├─ 6.400022675736961

```

In then built up stage by stage a command to extract the sample name:

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

```ruby
 ├─ (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:

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

---

<div class="post-metadata">

**Author:** ![Eli](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/eli/32/116_2.png) [@Eli](https://in-thread.sonic-pi.net/u/Eli)\
**Post date:** [November 9, 2019, 9:41pm UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901/5 "2019-11-09T21:41:34Z")

</div>

Yup, Told you other people would have better answers than me. 🙂

Usually its Robin, Martin or Ethan. One day maybe I’ll know as much as they do.

Eli,

---

<div class="post-metadata">

**Author:** ![Eyebot](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Eyebot](https://in-thread.sonic-pi.net/u/Eyebot)\
**Post date:** [November 10, 2019, 12:32am UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901/6 "2019-11-10T00:32:14Z")

</div>

Hi Robin!  
Thanks so much for taking the time to reply! It must the way the samples are labeled, I was actually looking to just play a single sample from a folder, but this is actually very helpful because this could be a great way for me to experiment playing samples continuously.  
Thanks again!

---

<div class="post-metadata">

**Author:** ![Eyebot](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Eyebot](https://in-thread.sonic-pi.net/u/Eyebot)\
**Post date:** [November 10, 2019, 12:33am UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901/7 "2019-11-10T00:33:44Z")

</div>

Thanks Eli, you are still light years ahead of me! 🙂

---

<div class="post-metadata">

**Author:** ![Martin](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/martin/32/157_2.png) [@Martin](https://in-thread.sonic-pi.net/u/Martin)\
**Post date:** [November 10, 2019, 8:53am UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901/8 "2019-11-10T08:53:03Z")

</div>

I will add my two cents and join this discussion 😉 ; I have two strategies I frequently use:

In my `~/.sonic-pi/init.rb` (that’s the place for a Linux system) you can find e. g.:

```ruby
define :beats do
  beats = "~/projects/sonicpi/playground/audio/samples/loops/beats/"
end

```

Let’s assume this folder contains a sample named `my_best_loop.wav` I then load it with:

```ruby
live_loop :test do
  sample beats, "best", beat_stretch: 4
  sleep 4
end

```

I also have entries in the `init.rb` like

```ruby
define :beats do
  collection = "~/a/sample/collection/with/subfolders/"
end

```

as shortcut to a folder which will contain subfolders with samples. Let’s assume the `my_best_loop.wav` would reside in a subfolder called `selected_loops`. It then goes this way:

```ruby
live_loop :test do
  sample , collection + "selected_loops", "best", beat_stretch: 4
  sleep 4
end

```

That way I can reference a whole collection of folders (and samples) without the need to adjust my `init.rb` all the time. But that’s a matter of taste and workflow.

---

<div class="post-metadata">

**Author:** ![Eyebot](https://avatars.discourse-cdn.com/v4/letter/e/a8b319/32.png) [@Eyebot](https://in-thread.sonic-pi.net/u/Eyebot)\
**Post date:** [November 10, 2019, 12:36pm UTC](https://in-thread.sonic-pi.net/t/external-sample-issues/2901/9 "2019-11-10T12:36:42Z")

</div>

Thanks so much Martin, I will try that!
