Skip to contents

Using the external program SoX (the Swiss Army knife of sound processing programs), create a spectrogram image file. Note that you must have SoX installed to use this function. Spectrograms will be silently overwritten.

Usage

sox_spectro(
  path,
  dir_out = "Spectrograms",
  prepend = "spectro_",
  width = NULL,
  height = NULL,
  start = NULL,
  end = NULL,
  rate = "20k",
  dry_run = FALSE,
  quiet = FALSE,
  sox_file_path = NULL,
  skip_check = FALSE
)

Arguments

path

Character. Path to wave file.

dir_out

Character. Output directory.

prepend

Character. Text to add to the start of the output file. Defaults to "spectro_".

width

Numeric. Width of the spectrogram image in pixels.

height

Numeric. Height of the spectrogram image in pixels.

start

Numeric/Character. Start the spectrogram at this time (seconds or HH:MM:SS format).

end

Numeric/Character. End time the spectrogram at this time (seconds or HH:MM:SS format).

rate

Numeric. Audio sampling rate to display (used by the rate effect in sox). This effectively limits the upper frequency of the spectrogram to rate/2. The default ("20k"), limits the spectrogram to 10kHz. Use rate = NULL for no limiting.

dry_run

Logical. If TRUE show the sox command, but do not run (for debugging and understanding precise details).

quiet

Logical. Whether to suppress progress messages and other non-essential updates.

sox_file_path

Path to sox file if not installed at the system level, otherwise NULL.

skip_check

Logical. Should the function skip check to ensure SoX is installed. This may allow speed ups if running across large numbers of files.

Value

Does not return anything, but creates a spectrogram image in dir_out.

Details

Most arguments are passed through to the seewave::sox() command.

  • width and height correspond to the -x and -y options for the spectrogram effect.

  • start and end are used by the trim effect

  • rate is passed on to the rate effect

Based on code from Sam Hache.

Examples

# Prep sample file
w <- tuneR::sine(440, duration = 300000)
td <- tempdir()
temp_wave <- glue::glue("{td}/test_wave.wav")
tuneR::writeWave(w, temp_wave)

# Create spectrograms

try({sox_spectro(temp_wave)
sox_spectro(temp_wave, rate = NULL)
sox_spectro(temp_wave, start = 2, end = 3)
sox_spectro(temp_wave, start = "0:01", end = "0:04")
sox_spectro(temp_wave, prepend = "")
})
#> Error in check_sox(sox_file_path) : SoX not available

# Clean up
unlink(temp_wave)
unlink("Spectrograms", recursive = TRUE)