knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(avutils)

After loading the package, we first need to set up a few parameters so that the functions in the package can find sox and ffmpeg which are the programs that do the actual audio/video manipulation (see here: http://sox.sourceforge.net/ and here: https://www.ffmpeg.org/download.html).

There are essentially two options to get R access sox and ffmpeg. Either you have installed these programs, or you just have saved the program binaries somewhere.

In the first case, you just need to run the following command.

set_binaries()

This will give you some (hopefully) informative output as to whether R could get access.

The second option requires that you downloaded sox and ffmpeg and stored them somewhere you remember. In that case, you need to tell R where the two files are, also with the set_binaries() function:

set_binaries(pathtoffmpeg = "~/Documents/utilities/ffmpeg", 
             pathtosox = "~/Documents/utilities/sox")

Again, you will get some information printed out that tells you whether this step succeeded.

if (Sys.info()["user"] == "christofneumann") {
  set_binaries(pathtoffmpeg = "~/Documents/utilities/ffmpeg", 
               pathtosox = "~/Documents/utilities/sox")
}

if (Sys.info()["user"] == "cn") set_binaries()

Importantly, this step needs to be done every time you restart R.

Then we can test whether this actually worked. If the following command doesn't result in an error and returns version numbers for both programs this means that probably the actual functions of the avutils package should work as well.

test_binaries()

Alternatively, you can also check whether the options for both sox and ffmpeg were set correctly.

getOption("avutils_sox")
getOption("avutils_ffmpeg")

Next, we can run some actual tests. For this, we create a temporary folder (one that is deleted as soon as you close R). Then we generate some sound, and save it as wave file into this temporary folder. This is done instead of providing an example wave file.

tdir <- normalizePath(tempdir(), winslash = "/")

You can make R open this location in your file browser:

# Windows:
shell.exec(tdir)

# Mac:
system2("open", tdir)

Now, we create some audio (simple sine waves) of known duration, sampling rate and number of channels (the most crucial features for the purpose of theis package).

w1 <- sine(freq = 440, duration = 0.9, bit = 32, stereo = TRUE, xunit = "time")
writeWave(w1, filename = file.path(tdir, "file1.wav"))
w2 <- noise(kind = "white", duration = 3.65, bit = 64, stereo = FALSE, xunit = "time")
writeWave(w2, filename = file.path(tdir, "file2.wav"))
tdir

Next, we use the package function audio_info() to extract some information about the stored audio files.

# get locations
x <- list.files(tdir, pattern = "\\.wav$", full.names = TRUE)
audio_info(filein = x)

We can also split files.

split_audio(filein = x[1], split = 0.42)
y <- list.files(tdir, pattern = "file1_", full.names = TRUE)
ai <- audio_info(filein = y)
# and check whether durations match
audio_info(filein = x[1])$duration
sum(ai$duration)
audio_info(filein = x[1])$samples
sum(ai$samples)


gobbios/avutils documentation built on Feb. 19, 2020, 9:44 a.m.