Get sample duration in beats

sample_duration  path (string)

Given the name of a loaded sample, or a path to a .wav, .wave, .aif or .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. The time returned is scaled to the current bpm.

Introduced in v2.0

Options

rate:

Rate modifier. For example, doubling the rate will halve the duration.

start:

Start position of sample playback as a value from 0 to 1

finish:

Finish position of sample playback as a value from 0 to 1

attack:

Duration of the attack phase of the envelope.

decay:

Duration of the decay phase of the envelope.

sustain:

Duration of the sustain phase of the envelope.

release:

Duration of the release phase of the envelope.

Examples

# Example 1

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 2

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 3

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


 
# Works for negative rates too
 
 



# Example 4

use_sample_bpm :loop_amen
puts sample_duration(:loop_amen)



 
#=> 1