Trigger sample

sample

[name_or_path (symbol_or_string)]

This is the main method for playing back recorded sound files (samples). Sonic Pi comes with lots of great samples included (see the section under help) but you can also load and play wav|wave|aif|aiff files from anywhere on your computer too. The 'rate' parameter affects both the speed and the pitch of the playback. See the examples for details. Check out the use_sample_pack and use_sample_pack_as methods for details on making it easy to work with a whole folder of your own sample files. Note, that on the first trigger of a sample, Sonic Pi has to load the sample which takes some time and may cause timing issues. To preload the samples you wish to work with consider load_sample and load_samples.

Introduced in v2.0.0

Example 0 


sample :perc_bell



 
 # plays one of Sonic Pi's built in samples



Example 1 


sample '/home/yourname/path/to/a/sample.wav'



 
 # plays a wav|wave|aif|aiff file from your local filesystem



Example 2 


# Lets play with the rate parameter
# play one of the included samples
sample :loop_amen
sleep sample_duration(:loop_amen)

# Setting a rate of 0.5 will cause the sample to
#   a) play half as fast
#   b) play an octave down in pitch
#
# Listen:
sample :loop_amen, rate: 0.5
sleep sample_duration(:loop_amen, rate: 0.5)

# Setting a really low number means the sample takes
# a very long time to finish! Also it sounds very
# different to the original sound
sample :loop_amen, rate: 0.05
sleep sample_duration(:loop_amen, rate: 0.05)


 
 
 
 
 # this sleeps for exactly the length of the sample
 
 
 
 
 
 
 
 
 
 
 
 
 
 



Example 3 


# Setting a really negative number can be lots of fun
# It plays the sample backwards!
sample :loop_amen, rate: -1
sleep sample_duration(:loop_amen, rate: 1) 

                                            
                                            
sample :loop_amen, rate: -0.5
sleep sample_duration(:loop_amen, rate: 0.5)



 
 
 
 
 # there's no need to give sample_duration a negative number though
 
 # Using a rate of -0.5 is just like using the positive 0.5
 # (lower in pitch and slower) except backwards
 
 # there's no need to give sample_duration a negative number though



Example 4 


# BE CAREFUL
# Don't set the rate to 0 though because it will get stuck
# and won't make any sound at all!
# We can see that the following would take Infinity seconds to finish
puts sample_duration(:loop_amen, rate: 0)


 
 
 
 
 
 



Example 5 


# Just like the play method, we can assign our sample player
# to a variable and control the rate parameter whilst it's playing.
#
# The following example sounds a bit like a vinyl speeding up
s = sample :loop_amen_full, rate: 0.05
sleep 1
control(s, rate: 0.2)
sleep 1
control(s, rate: 0.4)
sleep 1
control(s, rate: 0.6)
sleep 1
control(s, rate: 0.8)
sleep 1
control(s, rate: 1)


 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 



Example 6 


# Using the :start and :finish parameters you can play a section of the sample.
# The default start is 0 and the default finish is 1
sample :loop_amen, start: 0.5, finish: 1



 
 
 
 # play the last half of a sample



Example 7 


# You can also play part of any sample backwards by using a start value that's
# higher than the finish
sample :loop_amen, start: 1, finish: 0.5



 
 
 
 # play the last half backwards