當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python PyTorch apply_effects_tensor用法及代碼示例


本文簡要介紹python語言中 torchaudio.sox_effects.apply_effects_tensor 的用法。

用法:

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

參數

  • tensor(torch.Tensor) -輸入 2D CPU 張量。

  • sample_rate(int) -采樣率

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

  • channels_first(bool,可選的) -指示輸入張量的維度是[channels, time]還是[time, channels]

返回

生成的張量和采樣率。生成的張量與輸入張量具有相同的dtype,並且通道順序相同。根據應用的效果,張量的形狀可能會有所不同。采樣率也可以根據應用的效果而有所不同。

返回類型

(張量,int)

對給定的張量應用 sox 效果

注意

此函數僅適用於 CPU 張量。此函數的工作方式與sox 命令非常相似,但有細微差別。例如,sox 命令會自動添加某些效果(如speedpitch 和其他效果之後的rate 效果),但此函數隻應用給定的效果。 (因此,要實際應用speed 效果,還需要給rate 效果提供所需的采樣率。)。

示例 - 基本用法
>>>
>>> # 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-able 變換
>>>
>>> # 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.org大神的英文原創作品 torchaudio.sox_effects.apply_effects_tensor。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。