torchaudio
is an extension for torch
providing
audio loading, transformations, common architectures for signal
processing, pre-trained weights and access to commonly used datasets.
The package is a port to R of PyTorch’s
TorchAudio.
torchaudio
was originally developed by Athos Damiani as part of Curso-R work. Development will
continue under the roof of the mlverse organization, together
with torch
itself, torchvision
,
luz
, and a
number of extensions building on torch
.
The CRAN release can be installed with:
install.packages("torchaudio")
You can install the development version from GitHub with:
::install_github("mlverse/torchaudio") remotes
torchaudio
also supports loading sound files in the wav
and mp3 format. We call waveform the resulting raw audio signal.
library(torchaudio)
= "https://pytorch.org/tutorials/_static/img/steam-train-whistle-daniel_simon-converted-from-mp3.wav"
url = tempfile(fileext = ".wav")
filename = httr::GET(url, httr::write_disk(filename, overwrite = TRUE))
r
= transform_to_tensor(tuneR_loader(filename))
waveform_and_sample_rate = waveform_and_sample_rate[[1]]
waveform = waveform_and_sample_rate[[2]]
sample_rate
paste("Shape of waveform: ", paste(dim(waveform), collapse = " "))
#> [1] "Shape of waveform: 2 276858"
paste("Sample rate of waveform: ", sample_rate)
#> [1] "Sample rate of waveform: 44100"
plot(waveform[1], col = "royalblue", type = "l")
lines(waveform[2], col = "orange")
<- transform_spectrogram()(waveform)
specgram
paste("Shape of spectrogram: ", paste(dim(specgram), collapse = " "))
#> [1] "Shape of spectrogram: 2 201 1385"
<- as.array(specgram$log2()[1]$t())
specgram_as_array image(specgram_as_array[,ncol(specgram_as_array):1], col = viridis::viridis(n = 257, option = "magma"))
Please note that the torchaudio project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.