1

I am looking to apply a function which computes four acoustic indices (Hf, AEI, ACI and NDSI) to a list of 55 60-second wave objects. I have found code which applies the function to a .wav file, however I'm having difficulty in altering the code to work for each component of a list rather than a .wav file.

This is the code im trying to compute:

#create function to be applied to list of wave objects
indices <- function(x) {
  x <- readWave(x)
  return(c(sh(meanspec(x, plot=FALSE)),
           acoustic_evenness(x) $aei_left,
           ACI(x),
           NDSI(soundscapespec(x, plot=FALSE))
           )
         )
}

# create data frame for indices to be recorded
n <- length(hodsubsamps0820_000000)
num <- rep (NA, n)
res <- data.frame(Hf=num, AEI=num, ACI=num, NDSI=num)

#use function on each list component
for (i in 1:n) res[i,] <- indices(hodsubsamps0820_000000[i])

With this error code:

Error in readWave(x) : 'filename' must be of type character. 
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
HarHar
  • 35
  • 5
  • What does `class(hodsubsamps0820_000000)` return? And `str(hodsubsamps0820_000000)`? – Rui Barradas Sep 14 '20 at 16:18
  • `> class(hodsubsamps0820_000000) [1] "list" > str(hodsubsamps0820_000000) List of 55 $ :Formal class 'Wave' [package "tuneR"] with 6 slots .. ..@ left : int [1:2880000] 173 49 -56 -16 -26 97 172 63 208 8 ... .. ..@ right : int [1:2880000] 173 49 -56 -16 -26 97 172 63 208 8 ... .. ..@ stereo : logi TRUE .. ..@ samp.rate: int 48000 .. ..@ bit : int 16 .. ..@ pcm : logi TRUE` the results of `str(hodsubsamps0820_000000)` is very long but above is the first component – HarHar Sep 14 '20 at 16:27
  • `readWave` is expecting a character string and you are passing it a list. The first list member, `hodsubsamps0820_000000[[1]]` is a S4 class object of class `“Wave”`, so the wave files are already read in. It seems you do *not* need `readWave` in the function. – Rui Barradas Sep 14 '20 at 18:02
  • as @RuiBarradas points out, your wav files are read in, you're trying to apply the functions found in `soundecology` for the acoustic indices (Hf & etc.) which `soundecology` would do using `multiple_sounds`, [intro_vignette](https://cran.r-project.org/web/packages/soundecology/vignettes/intro.html), but it anticipates wav files in a directory, so perhaps just point multiple_sounds(directory ='your_directory' , the first index I want), and generate a csv of each index to cbind together into a data.frame. – Chris Jan 14 '21 at 17:52

0 Answers0