简体   繁体   中英

R: duration of a sound file - sound package duration function returning incorrect value

I am trying to retrieve the duration of a sound file with ".wav" extension. I am using the duration function from the sound package to retrieve the duration. The documentation ( https://www.rdocumentation.org/packages/sound/versions/1.4.5/topics/duration ) says that the function should return the duration of the file in seconds, but I get a different value.

So I have two questions:

(1) Am I getting something wrong here or is the package somehow not working properly?

(2) How else can I get the duration of a wav file?

--- EDIT to include further info---

在此处输入图像描述

and here is the code:

sound::duration("afstandsbediening.wav")
[1] 0.0003401361

I would ideally like to have the duration with precision up to millisecond units.

--- EDIT 2 for more details---

Here is an example of another sound file which is less than one second long. The length property shows 0 seconds.

在此处输入图像描述

R returns:

sound::duration("aansteker.wav")
[1] 0.0002494331

But the duration of the sound file is: 0.996 seconds

在此处输入图像描述

The duration of the afstandsbediening sound file is 1.06 seconds

在此处输入图像描述

Thanks!

update

Ok after some more info in the comments lets take a look at this file.

s <- loadSample("file_example_WAV_1MG.wav")
duration(s)
[1] 33.53

bits(s)
# [1] 16
channels(s)
# [1] 2
rate(s)
# [1] 8000

# get file size
file.info("file_example_WAV_1MG.wav")$size
# [1] 1073218

Now go to this tool / calculator https://www.colincrawley.com/audio-duration-calculator/

在此处输入图像描述

This all looks good right at least it corresponds with the duration given by R based on the other params.

But take a look what happens if you play the file on a different sample rate. I changed it from 8KHz to half of it, so 4 KHz and now we see your duration "play time" is doubled.

this strongly indicates that somehow you expect a duration that is played on a different rate than the rate of the file itself.

在此处输入图像描述

So now to your "aansteker" (lighter) 0.995510 / 0.0002494331 # [1] 3991.1 is almost 4000 times longer that it takes. Might it be possible that you have a file 48KHz (CD quality) and play it at 4KHz? That would make your duration exactly 4000 times longer.

first answer The library works perfectly fine.

library(sound)

s <- loadSample("file_example_WAV_1MG.wav")

duration(s)
# [1] 33.53

File info of the file on my machine (windows)

在此处输入图像描述

Or just create a sound sample and check the duration

s <- Sine(440, 5)
duration(s)

# [1] 5

To reproduce your issue, make sure you show the code, show the file info of the file you want the duration of and what your output is and what it was supposed to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM