Audio file length (e.g. .wav) using RubyAudio

How to determine the length (in ms) of an audio file (e.g. .wav) using RubyAudio

s = RubyAudio::Sound.open("1.wav") 
+4
source share
3 answers

You can get SongInfo by:

 songInfo = s.info 

And then the song information contains the sampling rate and the number of frames that you can use to calculate the duration of the audio file:

 duration = songInfo.frames / songInfo.samplerate 
+2
source

From a quick look at the docs, it looks like you can't do it with RubyAudio.

Have you tried watching ruby-mp3info ? I do not know if it will be actively developed, and if it works for several audio formats, but it claims that it can give you the duration of mp3.

An alternative way would be to make an estimate based on bit rate and file length.

0
source

RubyAudio did not seem to be updated after six years, and its documentation is sparse. If you are able, I would recommend using rtaglib instead .

However, if you are married to RubyAudio it looks like you can get both frame count ( Audio::Soundfile#frames ) and sample (frame) speed ( Audio::Soundfile#samplerate ). Knowing this, you should be able to split the number of frames with the sampling rate to get the file length in seconds.

0
source

All Articles