# \donttest{
if (require("ggplot2") & require("patchwork")) {
### Generating an artificial audio for the example
## For this example we'll generate a sweep in a noisy soundscape
library(ggplot2)
library(patchwork)
### Downloading audiofiles from public Zenodo library
dir <- paste0(tempdir(), "/forExamples")
dir.create(dir)
recName <- paste0("GAL24576_20250401_", sprintf("%06d", seq(0, 200000, by = 50000)), ".wav")
recDir <- paste(dir, recName, sep = "/")
for (rec in recName) {
print(rec)
url <- paste0("https://zenodo.org/records/17575795/files/",
rec,
"?download=1")
download.file(url,
destfile = paste(dir, rec, sep = "/"),
mode = "wb")
}
time <- sapply(strsplit(recName, "_"), function(x)
paste(substr(x[3], 1, 2), substr(x[3], 3, 4), substr(x[3], 5, 6), sep = ":"))
date <- sapply(strsplit(recName, "_"), function(x)
paste(substr(x[2], 1, 4), substr(x[2], 5, 6), substr(x[2], 7, 8), sep = "-"))
dateTime <- as.POSIXct(paste(date, time))
timeLabels <- time[c(1, 7, 13, 19, 24)]
timeBreaks <- as.character(dateTime[c(1, 7, 13, 19, 24)])
breaks <- round(c(1, cumsum(rep(256 / 6, 6))))
### Running the function
act <- multActivity(dir)
plotN <- 1
sDim <- dim(act$values)
sampRate <- act$info$SAMPRATE[1]
kHz <- cumsum(c(0, rep(sampRate / 6, 6))) / 1000
plotList <- list()
for (cha in c("left", "right")) {
actCurrent <- act$values[, act$info$CHANNEL == cha]
actCurrentDF <- data.frame(
TIME = as.character(rep(dateTime, each = sDim[1])),
SPEC = rep(seq(sDim[1]), sDim[2]),
VAL = factor(c(unlist(actCurrent)), levels = c(0, 1))
)
plotList[[plotN]] <- ggplot(actCurrentDF, aes(x = TIME, y = SPEC, fill = VAL)) +
geom_tile() +
theme_classic() +
scale_y_continuous(expand = c(NA, NA),
labels = kHz,
breaks = breaks) +
scale_x_discrete(expand = c(0, 0),
labels = time) +
scale_fill_manual(values = c("white", "black"),
labels = c("Inactive", "Active")) +
guides(fill = guide_legend(title = "Acoustic Activity")) +
labs(
x = "Time of Day",
y = "Frequency (kHz)",
title = paste("Acoustic Activity in the", cha, "channel")
)
plotN <- plotN + 1
}
plotList[[1]] + plotList[[2]] + plot_layout(guide = "collect")
unlink(recDir)
unlink(dir)
}
# }
Run the code above in your browser using DataLab