快捷方式

torchaudio.sox_effects.apply_effects_file

torchaudio.sox_effects.apply_effects_file(path: str, effects: List[List[str]], normalize: bool = True, channels_first: bool = True, format: Optional[str] = None) Tuple[Tensor, int][source]

将 Sox 效果应用于音频文件,并将结果数据加载为 Tensor

This feature supports the following devices: CPU This API supports the following properties: TorchScript

注意

此函数的用法与 sox 命令非常相似,但存在细微差异。例如,sox 命令会自动添加某些效果(如 speedpitch 等之后添加 rate 效果),但此函数仅应用给定的效果。因此,要实际应用 speed 效果,您还需要指定所需的采样率,因为在内部,speed 效果仅改变采样率而不触动样本。

参数:
  • path (path-like object) – 音频数据的源文件路径。

  • effects (List[List[str]]) – 效果列表。

  • normalize (bool, optional) –

    当为 True 时,此函数将原生样本类型转换为 float32。默认值:True

    如果输入文件是整数 WAV,设置为 False 会将结果 Tensor 类型更改为整数类型。此参数对整数 WAV 以外的格式无效。

  • channels_first (bool, optional) – 当为 True 时,返回的 Tensor 维度为 [channel, time]。否则,返回的 Tensor 维度为 [time, channel]

  • format (str or None, optional) – 使用给定的格式覆盖格式检测。当 libsox 无法从文件头或扩展名推断格式时,提供此参数可能会有帮助。

返回值:

结果 Tensor 和采样率。如果 normalize=True,结果 Tensor 始终为 float32 类型。如果 normalize=False 且输入音频文件是整数 WAV 文件,则结果 Tensor 具有相应的整数类型(注意不支持 24 位整数类型)。如果 channels_first=True,结果 Tensor 维度为 [channel, time],否则为 [time, channel]

返回类型:

(Tensor, int)

示例 - 基本用法
>>>
>>> # Defines the effects to apply
>>> effects = [
...     ['gain', '-n'],  # normalises to 0dB
...     ['pitch', '5'],  # 5 cent pitch shift
...     ['rate', '8000'],  # resample to 8000 Hz
... ]
>>>
>>> # Apply effects and load data with channels_first=True
>>> waveform, sample_rate = apply_effects_file("data.wav", effects, channels_first=True)
>>>
>>> # Check the result
>>> waveform.shape
torch.Size([2, 8000])
>>> waveform
tensor([[ 5.1151e-03,  1.8073e-02,  2.2188e-02,  ...,  1.0431e-07,
         -1.4761e-07,  1.8114e-07],
        [-2.6924e-03,  2.1860e-03,  1.0650e-02,  ...,  6.4122e-07,
         -5.6159e-07,  4.8103e-07]])
>>> sample_rate
8000
示例 - 对数据集应用随机速度扰动
>>>
>>> # Load data from file, apply random speed perturbation
>>> class RandomPerturbationFile(torch.utils.data.Dataset):
...     """Given flist, apply random speed perturbation
...
...     Suppose all the input files are at least one second long.
...     """
...     def __init__(self, flist: List[str], sample_rate: int):
...         super().__init__()
...         self.flist = flist
...         self.sample_rate = sample_rate
...
...     def __getitem__(self, index):
...         speed = 0.5 + 1.5 * random.randn()
...         effects = [
...             ['gain', '-n', '-10'],  # apply 10 db attenuation
...             ['remix', '-'],  # merge all the channels
...             ['speed', f'{speed:.5f}'],  # duration is now 0.5 ~ 2.0 seconds.
...             ['rate', f'{self.sample_rate}'],
...             ['pad', '0', '1.5'],  # add 1.5 seconds silence at the end
...             ['trim', '0', '2'],  # get the first 2 seconds
...         ]
...         waveform, _ = torchaudio.sox_effects.apply_effects_file(
...             self.flist[index], effects)
...         return waveform
...
...     def __len__(self):
...         return len(self.flist)
...
>>> dataset = RandomPerturbationFile(file_list, sample_rate=8000)
>>> loader = torch.utils.data.DataLoader(dataset, batch_size=32)
>>> for batch in loader:
>>>     pass
使用 apply_effects_file 的教程
Audio Feature Augmentation

音频特征增强

音频特征增强

文档

访问 PyTorch 完整的开发者文档

查看文档

教程

获取针对初学者和高级开发者的深度教程

查看教程

资源

查找开发资源并获得问题解答

查看资源