Find the length of a sample

I have a long sample that I want to play so I am using the following command

 r1 >> loop('rain',0,dur=100)  

But I have to work out how many beats the sample is made up of and I am doing this by trial and error. I can make life easier by using

seconds = Clock.seconds_to_beats
r1 >> loop('rain',0,dur=seconds(10))  

and then just putting in the length of the sample in seconds. This then makes me immune to any changes in bpm but is too much like hard work.

Ideally I want to be able to get the duration information directly from the sample. Is there a way of doing this ??

Currently that information isn’t available. The wave module could be used to find that information though by opening the file, reading the number of frames and dividing it by the sample rate. It could be done on loading samples tbf but I’m not doing any development at the moment.

No worries, just being lazy I can find out the length seconds and then translate to number of beats

Mutagen is a Python module to handle audio metadata. It supports ASF, FLAC, MP4, Monkey’s Audio, MP3, Musepack, Ogg Opus, Ogg FLAC, Ogg Speex, Ogg Theora, Ogg Vorbis, True Audio, WavPack, OptimFROG, and AIFF audio files. All versions of ID3v2 are supported, and all standard ID3v2.4 frames are parsed. It can read Xing headers to accurately calculate the bitrate and length of MP3s. ID3 and APEv2 tags can be edited regardless of audio format. It can also manipulate Ogg streams on an individual packet/page level.

https://mutagen.readthedocs.io/en/latest/

With the python mutagen module, you can know the length of a file, but you also have to convert that duration to bpm of the clock, here is a brief example.

import mutagen
audio = mutagen.File("/home/raulxubuntu/.local/lib/python3.6/site-packages/FoxDot/snd/_loop_/otros/sonicPi/guit_em9.flac")
longitud = audio.info.length
minutos, segundos = divmod(longitud, 60)
minutos, segundos = float(minutos*Clock.bpm/60), float(segundos*Clock.bpm/60)
print(minutos,segundos)
p1 >> loop('/home/raulxubuntu/.local/lib/python3.6/site-packages/FoxDot/snd/_loop_/otros/sonicPi/guit_em9.flac',dur=segundos)

Please note that mutagen is not for wav files

the multiplication symbol is not seen because it is an arterisk

minutos, segundos = float(minutos x Clock.bpm/60), float(segundos X Clock.bpm/60)

Hope you don’t mind but I edited the code sample given to use preformatted text, which preserves the asterix. Thanks for sharing the information about the library - probably very useful for users but as it doesn’t work with .wav file it won’t be able to tell you the length of FoxDot samples, which is a shame