I know the equation to find the duration of a wav file is:
fileLength/(sampleRate*channel*bits per sample/8)
But I've failed to retrieve all the necessary information to fill this equation in R. Here is an example of what I've come up with:
sound <- readWave(sound.wav)
sampleRate <- [email protected] #44100
bit <- sound@bit #16
So from the information above I have:
fileLength/(44100*channel*16/8)
The channel will either be 1 or 2, so that I'm not bothered about, but what about the file length? How do I retrieve that in R? Or is there some getDurationWavFile method in some package that I've missed?
Update: I'm using the tuneR library and when I use the str(sound) as suggested it gives me:
Formal class 'Wave' [package "tuneR"] with 6 slots
..@ left : int [1:132301] 0 3290 6514 9605 12502 15145 17482 19464 21052 22213 ...
..@ right : num(0)
..@ stereo : logi FALSE
..@ samp.rate: int 44100
..@ bit : int 16
..@ pcm : logi TRUE
A small adjustment to @gary's answer worked for me:
sound_length <- round(length(sound@left) / [email protected], 2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With