Error in loop with waves: Number of items to replace is not a multiple of replacement length

I'm extracting some features from audiofiles with the package "seewave". Specifically, I'm trying to get the resulting features when removing silences -such as duration of the signal and pauses-, with the function zapsilw. I have no problem when getting just one audio, but I'm missing something when running the loop. My code goes something like this:

cut_sound <- rep(NA, length(audio_files))
for (i in 1:length(audio_files))
tmp <- readWave(paste('directory'), audio_files[i], sep='\'))
cut_sound[i] <- zapsilw(tmp, threshold = 2)
duration_sound <- duration(cut_sound[i])

The error is: In cut_sound[i] <- zapsilw(tmp, threshold = 2) : number of items to replace is not a multiple of replacement length. What I am trying to get is a new variable with the duration of the new sound with the pauses removed. zapsilw generates a new wave with the pauses removed and plots it. When running the code, I see how each new wave is being generated, but at the end I received that warning message and no duration. I wonder if it may be something related to that, I mean, If I need the new waves, I should storing them in some part in order to retrieve the duration?

I have never used this package but I suggest these changes to your code

cut_sound <- vector(mode = "list", length = length(audio_files))
duration_sound <- vector(mode = "numeric", length = length(audio_files))
for (i in 1:length(audio_files)) {
  tmp <- readWave(paste('directory', audio_files[i], sep='\\')) #sep should be a single \
  cut_sound[[i]] <- zapsilw(tmp, threshold = 2, plot = FALSE)
  duration_sound[i] <- duration(cut_sound[[i]])

I set plot to FALSE in zapsilw() because the documentation seems to say that causes a new wave to be returned.

If plot is FALSE, a new wave is returned. The class of the returned object is set with the argument output.

Each element of cut_sound should be a matrix representing a wave and each element of duration_sound is the duration of one wave.

1 Like

True! I was missing the vector for duration and some of the other changes you suggested. I read the documentation but as I understood, that way it plots one wave instead of the comparison, but it was not like that and now it works faster! Thank you so much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.