快捷方式

torchaudio.sox_effects.apply_effects_tensor

torchaudio.sox_effects.apply_effects_tensor(tensor: Tensor, sample_rate: int, effects: List[List[str]], channels_first: bool = True) Tuple[Tensor, int][source]

将 sox 效果应用于给定的张量

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

注意

此函数仅适用于 CPU 张量。此函数的工作方式与 sox 命令非常相似,但存在细微差异。例如,sox 命令会自动添加某些效果(例如,在 speedpitch 以及其他效果之后添加 rate 效果),但此函数仅应用给定的效果。(因此,要实际应用 speed 效果,您还需要使用所需的采样率提供 rate 效果。)

参数:
  • tensor (torch.Tensor) – 输入 2D CPU 张量。

  • sample_rate (int) – 采样率

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

  • channels_first (bool, 可选) – 指示输入张量的维度是 [channels, time] 还是 [time, channels]

返回值:

结果张量和采样率。结果张量与输入张量具有相同的 dtype 和相同的通道顺序。张量的形状可以根据应用的效果而有所不同。采样率也可以根据应用的效果而有所不同。

返回类型:

(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
... ]
>>>
>>> # Generate pseudo wave:
>>> # normalized, channels first, 2ch, sampling rate 16000, 1 second
>>> sample_rate = 16000
>>> waveform = 2 * torch.rand([2, sample_rate * 1]) - 1
>>> waveform.shape
torch.Size([2, 16000])
>>> waveform
tensor([[ 0.3138,  0.7620, -0.9019,  ..., -0.7495, -0.4935,  0.5442],
        [-0.0832,  0.0061,  0.8233,  ..., -0.5176, -0.9140, -0.2434]])
>>>
>>> # Apply effects
>>> waveform, sample_rate = apply_effects_tensor(
...     wave_form, sample_rate, effects, channels_first=True)
>>>
>>> # Check the result
>>> # The new waveform is sampling rate 8000, 1 second.
>>> # normalization and channel order are preserved
>>> waveform.shape
torch.Size([2, 8000])
>>> waveform
tensor([[ 0.5054, -0.5518, -0.4800,  ..., -0.0076,  0.0096, -0.0110],
        [ 0.1331,  0.0436, -0.3783,  ..., -0.0035,  0.0012,  0.0008]])
>>> sample_rate
8000
示例 - 可 Torchscript 化的转换
>>>
>>> # Use `apply_effects_tensor` in `torch.nn.Module` and dump it to file,
>>> # then run sox effect via Torchscript runtime.
>>>
>>> class SoxEffectTransform(torch.nn.Module):
...     effects: List[List[str]]
...
...     def __init__(self, effects: List[List[str]]):
...         super().__init__()
...         self.effects = effects
...
...     def forward(self, tensor: torch.Tensor, sample_rate: int):
...         return sox_effects.apply_effects_tensor(
...             tensor, sample_rate, self.effects)
...
...
>>> # Create transform object
>>> effects = [
...     ["lowpass", "-1", "300"],  # apply single-pole lowpass filter
...     ["rate", "8000"],  # change sample rate to 8000
... ]
>>> transform = SoxEffectTensorTransform(effects, input_sample_rate)
>>>
>>> # Dump it to file and load
>>> path = 'sox_effect.zip'
>>> torch.jit.script(trans).save(path)
>>> transform = torch.jit.load(path)
>>>
>>>> # Run transform
>>> waveform, input_sample_rate = torchaudio.load("input.wav")
>>> waveform, sample_rate = transform(waveform, input_sample_rate)
>>> assert sample_rate == 8000

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

获取初学者和高级开发人员的深入教程

查看教程

资源

查找开发资源并获得解答

查看资源