clip_file <- "marnockii-bfl-2026.wav"
clip_url <- "https://ut-fri-biodiversity.github.io/eleuth-calls/figures/sound/marnockii-bfl-2026.wav"
if (!file.exists(clip_file)) download.file(clip_url, clip_file, mode = "wb")Sound in R: a guided tour
Read one recording, look at it, measure it, and break it on purpose
The order of topics follows the “Sound in R” part of Marcelo Araya-Salas’s tutorial Sound in Bioacoustic analysis in R (OTS, 2025; page, course repository; © 2025 Marcelo Araya-Salas). The code, text, exercises and recording here are our own. His page also covers the sound (phonTools) and audioSample (audio) classes, which we do not use, and is the place to look if you meet them.
Sound, digitized explained what a recording is. Here you read one into R. The plan is to run a short script first and see a result, then go back through it line by line, then try things that fail.
You need the clip from that page, a 16.5 s recording of a calling male Eleutherodactylus marnockii at BFL (Travis Co., Austin, Texas). Run this once in the R console on the pod. It saves the file to your home folder.
The whole thing first
This script reads the recording, draws it, finds the calls and measures the pitch of one. Run it as it is.
library(tuneR)
library(seewave)
frog <- readWave(clip_file)
spectro(frog, wl = 512, ovlp = 75, flim = c(0, 8), osc = TRUE, scale = FALSE,
palette = reverse.gray.colors.2)
calls <- timer(frog, f = frog@samp.rate, threshold = 10, dmin = 0.05,
msmooth = c(2205, 90), plot = FALSE)
call_starts <- calls$s.start
length(call_starts)[1] 13
median(diff(call_starts))[1] 1.126254
one_call <- cutw(frog, f = frog@samp.rate, from = 3.15, to = 3.60, output = "Wave")
spec <- meanspec(one_call, f = one_call@samp.rate, wl = 512, plot = FALSE)
specprop(spec, f = one_call@samp.rate)$mode[1] 2067.188
The script found 13 calls, spaced a median of 1.13 s apart (about 53 calls per minute), and the call between 3.15 and 3.60 s has its energy peak at 2067 Hz. Those numbers came from the file, and the sentence you just read was written by R from them, so it will update if the file or the code changes.
The same script, one step at a time
Here is the script again with every step numbered. Nothing in it runs this time, because the output above already shows what it does.
1library(tuneR)
library(seewave)
2frog <- readWave(clip_file)
3spectro(frog, wl = 512, ovlp = 75, flim = c(0, 8),
osc = TRUE, scale = FALSE, palette = reverse.gray.colors.2)
4calls <- timer(frog, f = frog@samp.rate, threshold = 10,
dmin = 0.05, msmooth = c(2205, 90), plot = FALSE)
5call_starts <- calls$s.start
6length(call_starts)
7median(diff(call_starts))
8one_call <- cutw(frog, f = frog@samp.rate, from = 3.15, to = 3.60,
output = "Wave")
9spec <- meanspec(one_call, f = one_call@samp.rate, wl = 512,
plot = FALSE)
10specprop(spec, f = one_call@samp.rate)$mode- 1
-
tuneRreads and writes sound files.seewaveanalyzes them. Almost everything in this course uses both. - 2
-
readWave()reads a WAV file into aWaveobject. This is the only line that touches the file on disk. Everything after it works onfrog. - 3
-
spectro()draws the spectrogram.wl = 512is the window length in samples (512 / 44,100 = 11.6 ms),ovlp = 75is the overlap between windows in percent, andflimlimits the frequency axis to 0 to 8 kHz.osc = TRUEadds the oscillogram on top.scale = FALSEleaves off the color key. - 4
-
timer()finds stretches where the smoothed loudness stays above a threshold.fis the sampling rate,thresholdis a percentage of the maximum, andmsmoothsmooths the loudness over 2,205 samples (50 ms) so that the pulses within one call merge into one stretch.dminignores anything shorter than 0.05 s. - 5
-
calls$s.startholds where each stretch starts, in seconds. Because of the smoothing, these times are blurred by a few tens of milliseconds. They are good for counting and spacing, and too rough for measuring call duration. - 6
- The number of stretches is the number of calls.
- 7
-
diff()gives the gap between consecutive start times, andmedian()gives the typical one. - 8
-
cutw()cuts the part of the recording between two times into a new, shorterWave. We used the window from the zoomed figure on the previous page. - 9
-
meanspec()averages the spectrum over the whole cut. The result is a table of frequency against relative energy. - 10
-
specprop()summarizes a spectrum.$modeis the frequency with the most energy, which is the dominant frequency. It comes back in hertz.
Two parts of this script are doing most of the work and both rely on a setting you chose. The dominant frequency depends on wl, because the spectrum has one value per frequency bin and the bins here are 44,100 / 512 = 86 Hz wide. A result of 2,067 Hz means “in the bin that contains 2,067 Hz”. The call count depends on threshold and msmooth. If you change them and the count changes, the count was never fixed by the frog. Check it against the oscillogram before you report it.
What a Wave object holds
Printing frog shows the header.
frog
Wave Object
Number of Samples: 727650
Duration (seconds): 16.5
Samplingrate (Hertz): 44100
Channels (Mono/Stereo): Mono
PCM (integer format): TRUE
Bit (8/16/24/32/64): 16
The object has six slots, and @ opens one.
frog@samp.rate[1] 44100
length(frog@left)[1] 727650
length(frog@left) / frog@samp.rate[1] 16.5
head(frog@left, 10) [1] -163 -176 -162 -117 -135 -113 -67 -39 -39 -52
range(frog@left)[1] -29490 26902
The first value is the sampling rate in samples per second, 44,100. The second is the number of samples in the recording, which is the 727,650 from the previous page. Dividing one by the other gives the duration in seconds. head(frog@left, 10) is the start of the list of numbers that is the recording. They are whole numbers because this is a 16-bit file, so they can run from -32,768 to 32,767, and range() shows how close the loudest sample comes to those limits.
You can learn what is in a file without reading all of it, which matters for long recordings.
readWave(clip_file, header = TRUE)$sample.rate
[1] 44100
$channels
[1] 1
$bits
[1] 16
$samples
[1] 727650
You can also read only part of a file. This reads 1 s starting at 3 s.
part <- readWave(clip_file, from = 3, to = 4, units = "seconds")
part
Wave Object
Number of Samples: 44100
Duration (seconds): 1
Samplingrate (Hertz): 44100
Channels (Mono/Stereo): Mono
PCM (integer format): TRUE
Bit (8/16/24/32/64): 16
An MP3 comes in through a different function but ends up as the same kind of object. This is a recording of E. nitidus from the Calls, notes, and pulses page, which exists as an mp3.
mp3 <- readMP3("nitidus-series.mp3")
mp3
Wave Object
Number of Samples: 3705984
Duration (seconds): 84.04
Samplingrate (Hertz): 44100
Channels (Mono/Stereo): Mono
PCM (integer format): TRUE
Bit (8/16/24/32/64): 16
Once you have a Wave, it does not matter where it came from. Everything else in this page works on both. What changes is how far you can trust the measurements, which is the point of the mp3 section on the previous page.
Break it on purpose
Each of these fails in a way you will meet on a real file. The errors are real, and the chunks below run with error: true so you can see them.
1. A sound shorter than the window
short <- extractWave(frog, from = 3.2, to = 3.205, xunit = "time")
meanspec(short, f = short@samp.rate, wl = 512, plot = FALSE)Error in `seq.default()`:
! wrong sign in 'by' argument
The message mentions seq.default and a “wrong sign in ‘by’ argument”, which says nothing about the actual problem. The clip is 5 ms long and the analysis window is 512 samples, which is 11.6 ms. The window does not fit inside the sound. A single pulse can be this short, and so can a call in a species with brief calls. Use a smaller wl, or cut a longer piece.
meanspec(short, f = short@samp.rate, wl = 128, plot = FALSE)[1:3, ] x y
[1,] 0.0000000 0.92960979
[2,] 0.3445313 0.57600377
[3,] 0.6890625 0.02462836
2. The signal is on the right channel
A WAV can have two channels. Seewave functions read only the left one, and they do not tell you. We build a stereo file with the frog on the right channel and nothing on the left.
right <- Wave(left = rep(0L, length(frog@left)), right = frog@left,
samp.rate = 44100, bit = 16)
right@stereo[1] TRUE
spec_right <- meanspec(right, f = 44100, wl = 512, plot = FALSE)
specprop(spec_right, f = 44100)$modeError in `if (any(spec < 0)) ...`:
! missing value where TRUE/FALSE needed
The file has a recording in it, but the spectrum is all NaN and the error points at a line inside specprop() that has nothing to do with the cause. Seewave never looked at the right channel. Choose the channel yourself, with mono():
spec_fixed <- meanspec(mono(right, "right"), f = 44100, wl = 512, plot = FALSE)
specprop(spec_fixed, f = 44100)$mode[1] 2067.188
When something returns NaN, NA or an empty plot, run frog@stereo before you do anything else. Recorders such as the one that made this clip often write four channels, and then readWave() returns a different object class (WaveMC) that you have to reduce to one channel first. The reference page has the lines for that.
3. Changing wl changes the answer
This one does not produce an error, which makes it harder to catch. The dominant frequency of the same call with three window lengths:
sapply(c(128, 512, 2048), function(wl) {
spec <- meanspec(one_call, f = one_call@samp.rate, wl = wl, plot = FALSE)
specprop(spec, f = one_call@samp.rate)$mode
})[1] 2067.188 2067.188 2110.254
Frequency resolution is 44,100 / wl, which is 345 Hz at wl = 128 and 22 Hz at wl = 2048. Here the three answers agree within 43 Hz, because this call has a sharp peak. They could not agree more closely than the bins allow. At wl = 128 the only values on offer are multiples of 345 Hz (1,723, 2,067, 2,411 and so on), so a call whose true peak sits between two bins gets rounded to the nearer one. Use one wl for all your calls and write it in your methods. The same trade-off is why long windows blur pulses together (see the Calls, notes, and pulses page).
Your turn
Measure the dominant frequency of all 13 calls. You have the start time of each call in call_starts, and from the “whole thing” script you already know how to cut a window, make a spectrum and take its mode. Fill in the blanks.
dom_freq <- function(start) {
one <- cutw(frog, f = frog@samp.rate, from = ___, to = ___,
output = "Wave")
spec <- meanspec(one, f = one@samp.rate, wl = 512, plot = FALSE)
specprop(spec, f = one@samp.rate)$___
}
sapply(call_starts, dom_freq)Then answer two questions in your own notebook. How much do the 13 values differ from each other, compared with the width of one frequency bin? And what would you have to change to measure the duration of each call instead? (Hint: the answer is not timer() with these settings. Look at note 5 above.)
dom_freq <- function(start) {
one <- cutw(frog, f = frog@samp.rate, from = start, to = start + 0.3,
output = "Wave")
spec <- meanspec(one, f = one@samp.rate, wl = 512, plot = FALSE)
specprop(spec, f = one@samp.rate)$mode
}
round(sapply(call_starts, dom_freq)) [1] 2067 2153 2067 2067 2067 2067 2067 2067 2067 1981 1981 1981 1981
The values sit at 1,981, 2,067 or 2,153 Hz. Those are neighboring bins, 86 Hz apart, so the calls are as similar as this measurement can tell. A small difference between calls is not real unless it is larger than the bin width. For durations you need the start and end of each call from the oscillogram, which is what worksheet 06 and the team measurement template do.
Where this goes next
The same steps scale up in two directions. warbleR wraps them into functions that take a table of selections and return a table of measurements (see the Sep 14 deck and the team measurement template). The reference page lists the functions for reading, cutting, converting and writing sound files, with the problems each one has.
References
- Araya-Salas, M. 2025. Sound. In Bioacoustic analysis in R, OTS course. https://marce10.github.io/OTS_BIR_2025/sound.html
- Ligges, U., S. Krey, O. Mersmann, and S. Schnackenberg. tuneR: Analysis of Music and Speech. https://CRAN.R-project.org/package=tuneR
- Sueur, J. 2018. Sound Analysis and Synthesis with R. Springer.
- Sueur, J., T. Aubin, and C. Simonis. 2008. Seewave, a free modular tool for sound analysis and synthesis. Bioacoustics 18:213–226.
Recording by T. J. Devitt.
Prepared with Claude Code (Claude Sonnet 5.5, Anthropic): the code tour, exercises and a first draft of the text. Figure alt text added with Claude Opus 5.5.