rlapplywavdownsampling

Bulk downsampling list of wave objects using seewave::resamp


I am trying to downsample a list of wave objects using seewave::resamp.

To get my list I have imported a .wav file and split it into 10 second clips following @Jota 's answer here

So to get my list of wave object I have done the following (this is using the example from the above answer):

library(seewave)

# your audio file (using example file from seewave package)
data(tico)
audio <- tico # this is an S4 class object

# the frequency of your audio file
freq <- 22050

# the length and duration of your audio file
totlen <- length(audio)
totsec <- totlen/freq

# the duration that you want to chop the file into
seglen <- 0.5

# defining the break points
breaks <- unique(c(seq(0, totsec, seglen), totsec))
index <- 1:(length(breaks)-1)

# a list of all the segments
subsamps <- lapply(index, function(i) audio[(breaks[i]*freq):(breaks[i+1]*freq)])

I now have my list of wave objects. If do the following for individual objects it works:

resamp(subsamps[[1]], f = 48000, g = 22050, output = "Wave")

But when I try and do it to the list of objects it comes up with an error:

test_wave_downsample <- lapply(subsamps, function(i) resamp(subsamps[[i]], f = 22050, g = 8000, output = "Wave"))
 
Error in subsamps[[i]] : invalid subscript type 'S4'

I am pretty sure this is something to do with the way I using lapply as the S4 object is not an issue when done individually, but as someone who is new to using the apply family I am not sure what.

I have had a look around an can't find much on using existing functions within lapply or if that can be an issue.

Any advice greatly appreciated.


Solution

  • After asking around I have been given the a solution that works:

    f <- function(x) {
      resamp(x, f = 22050, g = 8000, output = "Wave")
    }
    ā€‹
    test_wave_downsample <- lapply(subsamps, f)