torchaudio_load: Load Audio File

Description Usage Arguments Value Examples

View source: R/backend-common.R

Description

Loads an audio file from disk into a tensor

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
torchaudio_load(
  filepath,
  out = NULL,
  normalization = NULL,
  channels_first = TRUE,
  duration = 0L,
  offset = 0L,
  unit = c("sample", "time"),
  signalinfo = NULL,
  encodinginfo = NULL,
  filetype = NULL
)

Arguments

filepath

(str): Path to audio file

out

(Tensor): An optional output tensor to use instead of creating one. (Default: NULL)

normalization

(NULL, bool, float or function): Optional normalization. If boolean TRUE, then output is divided by 2^31. Assuming the input is signed 32-bit audio, this normalizes to [-1, 1]. If numeric, then output is divided by that number. If function, then the output is passed as a paramete to the given function, then the output is divided by the result. If NULL, defaults to specific loader behaviour (Default: NULL)

channels_first

(bool): Set channels first or length first in result. (Default: TRUE)

duration

(int): Number of frames (or seconds) to load. 0 to load everything after the offset. (Default: 0)

offset

(int): Number of frames (or seconds) from the start of the file to begin data loading. (Default: 0)

unit

(str): "sample" or "time". If "sample" duration and offset will be interpreted as frames, and as seconds otherwise.

signalinfo

(str): A sox_signalinfo_t type, which could be helpful if the audio type cannot be automatically determined. (Default: NULL)

encodinginfo

(str): A sox_encodinginfo_t type, which could be set if the audio type cannot be automatically determined. (Default: NULL)

filetype

(str): A filetype or extension to be set if sox cannot determine it automatically. (Default: NULL)

Value

1
2
3
4
list(Tensor, int): An output tensor of size `[C x L]` or `[L x C]` where
    L is the number of audio frames and
    C is the number of channels.
    An integer which is the sample rate of the audio (as listed in the metadata of the file)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
## Not run: 
if(torch::torch_is_installed()) {
mp3_filename <- system.file("sample_audio_2.mp3", package = "torchaudio")
data = torchaudio_load(mp3_filename)
print(data[[1]]$size())
norm_fun <- function(x) torch::torch_abs(x)$max()
data_vol_normalized = torchaudio_load(mp3_filename, normalization= norm_fun)
print(data_vol_normalized[[1]]$abs()$max())
}


## End(Not run)

torchaudio documentation built on May 5, 2021, 5:06 p.m.