torchaudio.sox_effects¶
资源初始化 / 关闭¶
列出支持的效果¶
应用效果¶
在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
注意
此函数仅适用于 CPU 张量。 此功能的工作方式与 command 非常相似,但有轻微的 差异。例如,command 会自动添加某些效果(例如 effect after and and other effects),但此函数不会 仅应用给定的效果。(因此,要实际应用效果,您还需要 需要以所需的采样率生效。
sox
sox
rate
speed
pitch
speed
rate
- 参数
张量 (Torch.Tensor) - 输入 2D CPU 张量。
sample_rate (int) – 采样率
effects (List[List[str]]) - 效果列表。
channels_first (bool) – 指示输入 Tensor 的维度是 或
[channels, time]
[time, channels]
- 返回
生成的 Tensor 和 sample rate。 生成的 Tensor 与输入 Tensor 相同,并且 相同的频道顺序。Tensor 的形状可以根据 应用的效果。采样率也可能根据应用的效果而有所不同。
dtype
- 返回类型
- 示例 - 基本用法
>>> >>> # 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 并保持样品不变。
sox
sox
rate
speed
pitch
speed
rate
speed
- 参数
path (路径类对象或类文件对象) –
音频数据源。当函数没有被 TorchScript 编译时, (例如),接受以下类型:
torch.jit.script
path-like
: 文件路径file-like
: 具有方法的对象, 返回最大长度的字节字符串。read(size: int) -> bytes
size
当函数由 TorchScript 编译时,只允许使用 type。
str
注意:此参数被特意注释为仅用于 TorchScript 编译器兼容性。
str
effects (List[List[str]]) - 效果列表。
normalize (bool) - 当 时,此函数始终返回 ,样本值为 标准化为 . 如果 input file 是整数 WAV,则 give 会将生成的 Tensor 类型更改为 integer 类型。此参数对其他格式没有影响 而不是整数 WAV 类型。
True
float32
[-1.0, 1.0]
False
channels_first (bool) – 当为 True 时,返回的 Tensor 具有维度 。 否则,返回的 Tensor 的维度为 。
[channel, time]
[time, channel]
format (str, optional) – 使用给定格式覆盖格式检测。 当 libsox 无法推断格式时,提供参数可能会有所帮助 from 标头或扩展,
- 返回
生成的 Tensor 和 sample rate。 如果 ,则生成的 Tensor 始终为 type。 如果且输入音频文件为整数型 WAV 文件,则 生成的 Tensor 具有相应的整数类型。(注意:不支持 24 位整数类型) 如果 ,则生成的 Tensor 具有维度 , 否则。
normalize=True
float32
normalize=False
channels_first=True
[channel, time]
[time, channel]
- 返回类型
- 示例 - 基本用法
>>> >>> # 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