Individual sound files for each selection (or how to create a warbleR function)
Posted on June 6, 2017
A friend of mine wants to “create individual sound files for each selection” in a selection table. This is a good opportunity to show how to create a function that works iteratively on signals in a selection table (like most warbleR.
It takes 3 main steps:
Create a (internal) function that does what we want on a single selection (i.e. a single row of the selection table)
Add and (X)lapply loop to run the function from step 1 iteratively on each row
Put all inside a new function
Other things can be added to the function to check arguments and set working directory, but this is not critical.
Step 1: Create function that does what we want on a single selection
First install and/or load [warbleR]((https://cran.r-project.org/package=warbleR) (which also loads tuneR:
Save the example sound files as .wav in a temporary working directory (to test the code):
We can check that now there are 4 sound files in the working directory:
The selection table selec.table contains columns for sound file name, start and end of signals (in seconds), and a unique ID for each selection:
Now write a code that takes the selection from a single row, extracts the .wav file segment, and save it to the working directory. To do this use the functions readWave and writeWave from [tuneR]((https://cran.r-project.org/package=tuneR):
Note that the sound file name (with the file extension removed) and selection ID were used to name the cut. This is important because it will produce a unique name for each cut, so they won’t be overwritten.
The following code shows that there is a new sound file corresponding to the new cut:
Now put the code inside a function. The index number 1 used above has to be replaced by ‘i’ so it runs one each row iteratively. I also added a few more line of codes to:
Allow adding margins at both sides of the signals
Adjust the start and end when the margin surpasses the start (0) and end of the whole sound file
Step 2: Add a (X)lapply function
Several versions of the lapply function can be used. The function pblapply from the package [pbapply]((https://cran.r-project.org/package=pbapply) provides a nice progress bar. The following code should produce cuts for each selection in selec.table:
We could also used lapply (no progress bar) or mclapply (parallel, or even pbmclapply (pbmcapply. In fact, all these options are included in most [warbleR]((https://cran.r-project.org/package=pbmcapply) functions.
Step 3: Put all the code inside a new function
Just copy/paste the cutFUN function and the (X)lapply function inside a new function:
Let’s see if it works. First we should remove the cuts we created previously. The following code removes only the cuts but not the original sound files (filtered out based on file size):
And run the function:
The cuts now are found in the working directory:
I put together all the code in a new [warbleR]((https://cran.r-project.org/package=warbleR) function called cut_sels (available in version 1.1.9, currrently only on github). I added a few more arguments (labels, overwrite, parallel, …), some argument checks to warn users when using invalid values, and parallel and progress bar options. Here is the code in case you are curious:
That’s is it. If you developed a function that you think could be useful to other people we could include it in [warbleR]((https://cran.r-project.org/package=warbleR).