Get sample duration in seconds

sample_duration

[path (string)]

Given the name of a loaded sample, or a path to a wav|wave|aif|aiff file this returns the length of time that the sample would play for. It's useful when looping samples to make sure there are no gaps - see the examples. You may pass a rate opt which it will use to scale the returned time to match the duration at that rate.

Introduced in v2.0

Example 0 

loop do  
  sample :loop_amen
  sleep sample_duration(:loop_amen)
end


# Using sample_duration here means the loop plays back without any gaps or breaks
# Play amen break
# sleep for duration of amen break
 



Example 1 

loop do 
  sample :loop_amen, rate: 0.75
  sleep sample_duration(:loop_amen, rate: 0.75)
end


# You can also use rate if you want to keep a seamless loop whilst adjusting the speed
 
 
 



Example 2 

loop do
  sample :loop_amen, rate: -1
  sleep sample_duration :loop_amen, rate: -1
end


 
# Works for negative rates too