Skip to main content

Reading, cutting and writing sound files in R

A reference to come back to, with the traps marked

NoteCredit

This is our own reference, organized by what you want to do. Its coverage follows the import and export sections of the “Sound” tutorial in Marcelo Araya-Salas’s Bioacoustic analysis in R (OTS, 2025; page; © 2025 Marcelo Araya-Salas), whose summary table of import and export functions comes from Sueur (2018). His page also compares the R sound classes in more depth than we do here.

Use this page after you have worked through Sound, digitized and Sound in R. The examples run on the 16.5 s E. marnockii clip from those pages.

library(tuneR)
library(seewave)
frog <- readWave("sounds/marnockii-bfl-2026.wav")

Which function for which job

You want to Use Watch out for
Read a WAV tuneR::readWave(file) A file with more than two channels comes back as a WaveMC, not a Wave (see below)
Read part of a WAV readWave(file, from = 3, to = 4, units = "seconds") Only the requested part is read, so it is fast on large files
See sampling rate, bits and length without reading the audio readWave(file, header = TRUE) Returns a list, not a Wave
Read an MP3 tuneR::readMP3(file) Reads the whole file, so cut afterwards. Decoding can shift timing slightly, as in the mp3 figure on the basics page
Read WAV, MP3 or other formats by file name warbleR::read_sound_file(file, path = dir) Needs the file name and a folder
Summarize a folder of recordings warbleR::info_sound_files(path = dir) One row per file, and it opens each one
Cut a piece out of a Wave tuneR::extractWave(w, from, to, xunit = "time") or seewave::cutw(w, f, from, to, output = "Wave") cutw() needs f and returns a matrix unless output = "Wave"
Make a stereo file mono tuneR::mono(w, "left") or "right" Seewave functions silently use the left channel only
Write a WAV tuneR::writeWave(w, "file.wav") Scale first with normalize() if you are changing bit depth
Lower the sampling rate av::av_audio_convert(in, out, sample_rate = 22050) tuneR::downsample() and seewave::resamp() drop samples with no filter, so frequencies above the new Nyquist limit fold back down
Compress losslessly seewave::wav2flac(file) Calls an outside flac program that must be installed (Sys.which("flac") tells you)
Make an MP3 av::av_audio_convert(in, out, bit_rate = 64000) For listening and demonstrations. Do not analyze the result for anything except timing and counts

Examples

What is in a folder?

info <- warbleR::info_sound_files(path = "sounds")
info[, c("sound.files", "sample.rate", "channels", "bits", "duration")]
             sound.files sample.rate channels bits duration
1 marnockii-bfl-2026.wav        44.1        1   16 16.50000
2     nitidus-series.mp3        44.1        1   16 84.03592

Run this on a new folder of recordings before you do anything else. A mix of sampling rates, bit depths or channel counts in one folder is a common source of surprises later.

Reading by file name

x <- warbleR::read_sound_file("marnockii-bfl-2026.wav", path = "sounds")
class(x)
[1] "Wave"
attr(,"package")
[1] "tuneR"

Many channels, one microphone

Field recorders often write four or more channels to one file, and readWave() returns a WaveMC for those. The WaveMC stores its samples in a matrix in @.Data, one column per channel. To use seewave you pick one channel and build an ordinary Wave. Here is a stand-in with four channels, two of which carry the recording.

four <- WaveMC(data = cbind(frog@left, frog@left, 0L, 0L),
               samp.rate = 44100, bit = 16)
class(four)
[1] "WaveMC"
attr(,"package")
[1] "tuneR"
dim(four@.Data)
[1] 727650      4
one <- Wave(left = four@.Data[, 1], samp.rate = four@samp.rate, bit = four@bit)
class(one)
[1] "Wave"
attr(,"package")
[1] "tuneR"

Before you choose a channel, check which ones carry sound. Comparing the largest sample in each column is enough.

apply(four@.Data, 2, function(x) max(abs(x)))
[1] 29490 29490     0     0

Cut, normalize and write

piece <- extractWave(frog, from = 3.15, to = 3.60, xunit = "time")
piece <- normalize(piece, unit = "16", level = 0.9)
writeWave(piece, "call-3.wav")
file.size("call-3.wav")
[1] 39772

normalize(unit = "16", level = 0.9) scales the loudest sample to 90% of the 16-bit range. The size is about 2 bytes per sample plus a 44-byte header, which is a good check that nothing odd happened.

Lowering the sampling rate

av::av_audio_convert("sounds/marnockii-bfl-2026.wav", "low.wav",
                     sample_rate = 11025, verbose = FALSE)
[1] "low.wav"
low <- readWave("low.wav")
low@samp.rate
[1] 11025
duration(low)
[1] 16.50005

The new Nyquist limit is 5.5 kHz, so the faint 6 kHz harmonic is removed and the 2 and 4 kHz bands stay. av filters before it resamples. If you did this with downsample() instead, a 6 kHz harmonic would reappear near 5 kHz as a false frequency. The Sound, digitized page shows both.

Other classes of sound objects

We use the Wave class from tuneR for everything. Other packages store sound in their own classes (sound in phonTools, audioSample in audio), and plain R objects such as vectors, matrices and time series can also hold a waveform. Seewave accepts a vector or a matrix if you also give it the sampling rate in f, and a time series carries its own rate. The tutorial by Araya-Salas compares these classes and shows how to read each one. Unless you inherit someone else’s code, you can ignore them.

References

  • Araya-Salas, M. 2025. Sound. In Bioacoustic analysis in R, OTS course. https://marce10.github.io/OTS_BIR_2025/sound.html
  • Araya-Salas, M., and G. Smith-Vidaurre. 2017. warbleR: an R package to streamline analysis of animal acoustic signals. Methods in Ecology and Evolution 8:184–191.
  • Sueur, J. 2018. Sound Analysis and Synthesis with R. Springer.

Prepared with Claude Code (Claude Sonnet 5.5, Anthropic): the table, examples and a first draft of the text.