oldpar <- par(no.readonly = TRUE)
### Generating an artificial audio for the example
## For this example we'll generate a sweep in a noisy soundscape
library(tuneR)
# Define parameters for the artificial audio
samprate <- 12050
dur <- 59
n <- samprate * dur
# White noise
set.seed(413)
noise <- rnorm(n)
# Linear fade-out envelope
fade <- seq(1, 0, length.out = n)
# Apply fade
signal <- noise * fade
# Create Wave object
wave <- Wave(
left = signal,
samp.rate = samprate,
bit = 16
)
# Running singleSat() on the artificial audio
sat <- singleSat(wave, timeBin = 10)
# Now we can plot the results
# In the left we have a periodogram and in the right saturaion values
# along one minute
par(mfrow = c(1,2))
image(periodogram(wave, width = 8192, normalize = FALSE), xlab = "Time (s)",
ylab = "Frequency (hz)", axes = FALSE)
axis(1, labels = seq(0,60, 10), at = seq(0,7e5,length.out = 7))
axis(2)
plot(sat, xlab = "Time (s)", ylab = "Soundscape Saturation (%)",
type = "b", pch = 16, axes = FALSE)
axis(1, labels = paste0(c("0-10","10-20","20-30","30-40","40-50","50-59"),
"s"), at = 1:6)
axis(2)
par(oldpar)
# \donttest{
# Getting audiofile from the online Zenodo library
dir <- paste(tempdir(), "forExample", sep = "/")
dir.create(dir)
rec <- paste0("GAL24576_20250401_", sprintf("%06d", 0),".wav")
recDir <- paste(dir,rec , sep = "/")
url <- paste0("https://zenodo.org/records/17575795/files/", rec, "?download=1")
# Downloading the file, might take some time denpending on your internet
download.file(url, destfile = recDir, mode = "wb")
# Now we calculate soundscape saturation for both sides of the recording
sat <- singleSat(recDir)
# Printing the results
print(sat)
barplot(sat, col = c("darkgreen", "red"),
names.arg = c("Left", "Right"), ylab = "Soundscape Saturation (%)")
unlink(dir, recursive = TRUE)
# }
Run the code above in your browser using DataLab