目录

torchaudio.sox_effects

资源初始化 / 关闭

torchaudio.sox_effects.init_sox_effects()[来源]

初始化使用 sox 效果所需的资源。

注意

您无需手动调用此函数。它会自动调用。

初始化后,您无需在 SOX 效果,但只要尚未调用,就可以安全地这样做。 调用后,您将无法再使用 SoX 效果和初始化 将导致错误。

torchaudio.sox_effects.shutdown_sox_effects()[来源]

清理使用 SOX 效果所需的资源。

注意

您无需手动调用此函数。它会自动调用。

多次调用此函数是安全的。 调用后,您将无法再使用 SoX 效果,并且 再次初始化将导致错误。

列出支持的效果

torchaudio.sox_effects.effect_names()→ List[str][来源]

获取有效 sox 效果名称的列表

返回

可用效果器名称的列表。

返回类型

列表[str]

>>> torchaudio.sox_effects.effect_names()
['allpass', 'band', 'bandpass', ... ]

应用效果

在Torch上应用 SoX 效果链。Tensor 或 on file 并加载为 torch。张肌。

对 Tensor 应用效果

torchaudio.sox_effects.apply_effects_tensor(张量:Torch。张量、sample_rate:int、效果:List[List[str]]、channels_first:bool = True → Tuple[torch.Tensor, int][来源]

将 sox 效果应用于给定的 Tensor

注意

此功能的工作方式与 command 非常相似,但有轻微的 差异。例如,command 会自动添加某些效果(例如 effect after and and other effects),但此函数不会 仅应用给定的效果。(因此,要实际应用效果,您还需要 需要以所需的采样率生效。soxsoxratespeedpitchspeedrate

参数
  • 张量Torch.Tensor) - 输入 2D 张量。

  • sample_rateint) – 采样率

  • effectsList[List[str]]) - 效果列表。

  • channels_firstbool) – 指示输入 Tensor 的维度是 或[channels, time][time, channels]

返回

生成的 Tensor 和 sample rate。 生成的 Tensor 与输入 Tensor 相同,并且 相同的频道顺序。Tensor 的形状可以根据 应用的效果。采样率也可能根据应用的效果而有所不同。dtype

返回类型

元组[torch.张量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

对文件应用效果

torchaudio.sox_effects.apply_effects_file(路径:str,效果:List[List[str]],归一化:bool = True,channels_first:bool = True,格式:可选[str] = None→ Tuple[torch.Tensor, int][来源]

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

注意

此功能的工作方式与 command 非常相似,但有轻微的 差异。例如,commnad 会自动添加某些 effect (例如 effect after , 等),但此函数仅应用给定的 影响。因此,要实际应用效果,您还需要以所需的采样率提供效果,因为在内部,效果只会改变采样 rate 并保持样品不变。soxsoxratespeedpitchspeedratespeed

参数
  • path路径类对象类文件对象) –

    音频数据源。当函数没有被 TorchScript 编译时, (例如),接受以下类型:torch.jit.script

    • path-like: 文件路径

    • file-like: 具有方法的对象, 返回最大长度的字节字符串。read(size: int) -> bytessize

    当函数由 TorchScript 编译时,只允许使用 type。str

    注意:此参数被特意注释为仅用于 TorchScript 编译器兼容性。str

  • effectsList[List[str]]) - 效果列表。

  • normalizebool) - 当 时,此函数始终返回 ,样本值为 标准化为 . 如果 input file 是整数 WAV,则 give 会将生成的 Tensor 类型更改为 integer 类型。此参数对其他格式没有影响 而不是整数 WAV 类型。Truefloat32[-1.0, 1.0]False

  • channels_firstbool) – 当为 True 时,返回的 Tensor 具有维度 。 否则,返回的 Tensor 的维度为 。[channel, time][time, channel]

  • formatstroptional) – 使用给定格式覆盖格式检测。 当 libsox 无法推断格式时,提供参数可能会有所帮助 from 标头或扩展,

返回

生成的 Tensor 和 sample rate。 如果 ,则生成的 Tensor 始终为 type。 如果且输入音频文件为整数型 WAV 文件,则 生成的 Tensor 具有相应的整数类型。(注意:不支持 24 位整数类型) 如果 ,则生成的 Tensor 具有维度 , 否则。normalize=Truefloat32normalize=Falsechannels_first=True[channel, time][time, channel]

返回类型

元组[torch.张量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

文档

访问 PyTorch 的全面开发人员文档

查看文档

教程

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

查看教程

资源

查找开发资源并解答您的问题

查看资源