本文介绍了在r中查找wav文件的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道找到 wav 文件持续时间的等式是:

I know the equation to find the duration of a wav file is:

fileLength/(sampleRate*channel*bits per sample/8)

但我未能检索到所有必要的信息来填充 R 中的这个等式.这是我想出的一个例子:

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 <- sound@samp.rate #44100
bit <- sound@bit #16

所以从上面的信息我有:

So from the information above I have:

fileLength/(44100*channel*16/8)

通道将是 1 或 2,这样我就不用担心了,但是文件长度呢?我如何在 R 中检索它?或者我错过了某个包中的某些 getDurationWavFile 方法?

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?

更新:我正在使用 tuneR 库,当我按照建议使用 str(sound) 时,它给了我:

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

推荐答案

既然你知道默认的 summary 函数(这个函数在命令行单独输入对象名称时默认调用)对于此对象类型将打印出持续时间,您只需查看该函数的代码即可了解它是如何计算的.getMethod 函数会让你在引擎盖下达到峰值:

Since you know the default summary function (this function is called by default when an object name is typed alone at the command line) for this object type will print out the duration, you can just look at the code for that function to see how it's calculated. The getMethod function will let you peak under the hood:

library(tuneR)

getMethod('summary','Wave')
Method Definition:

function (object, ...)
{
    l <- length(object@left)
    cat("\nWave Object")
    cat("\n\tNumber of Samples:     ", l)
    cat("\n\tDuration (seconds):    ", round(l/object@samp.rate,
        2))
    cat("\n\tSamplingrate (Hertz):  ", object@samp.rate)
    cat("\n\tChannels (Mono/Stereo):", if (object@stereo)
        "Stereo"
    else "Mono")
    cat("\n\tPCM (integer format):  ", object@pcm)
    cat("\n\tBit (8/16/24/32/64):   ", object@bit)
    cat("\n\nSummary statistics for channel(s):\n\n")
    if (object@stereo)
        print(rbind(left = summary(object@left), right = summary(object@right)))
    else print(summary(object@left))
    cat("\n\n")
}
<environment: namespace:tuneR>

Signatures:
        object
target  "Wave"
defined "Wave"

所以要获取波形文件的长度,请尝试:

So to grab the length of your wave file try:

sound_length <- round(sound@left / sound@samp.rate, 2)

这篇关于在r中查找wav文件的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:36
查看更多