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)