• 文档 >
  • AudioEffector 用法 >
  • 旧版本 (稳定版)
快捷方式

AudioEffector 用法

作者: Moto Hira

本教程展示了如何使用 torchaudio.io.AudioEffector 将各种效果和编解码器应用于波形张量。

注意

本教程需要 FFmpeg 库。请参考 FFmpeg 依赖项 了解详细信息。

概述

AudioEffector 结合了 StreamWriterStreamReader 提供的内存编码、解码和滤波功能。

下图说明了该过程。

https://download.pytorch.org/torchaudio/tutorial-assets/AudioEffector.png
import torch
import torchaudio

print(torch.__version__)
print(torchaudio.__version__)
2.6.0
2.6.0
from torchaudio.io import AudioEffector, CodecConfig

import matplotlib.pyplot as plt
from IPython.display import Audio
for k, v in torchaudio.utils.ffmpeg_utils.get_versions().items():
    print(k, v)
libavcodec (60, 3, 100)
libavdevice (60, 1, 100)
libavfilter (9, 3, 100)
libavformat (60, 3, 100)
libavutil (58, 2, 100)

用法

要使用 AudioEffector,请使用 effectformat 实例化它,然后将波形传递给 apply()stream() 方法。

effector = AudioEffector(effect=..., format=...,)

# Apply at once
applied = effector.apply(waveform, sample_rate)

apply 方法一次将效果和编解码器应用于整个波形。因此,如果输入波形很长,并且内存消耗是一个问题,则可以使用 stream 方法逐块处理。

# Apply chunk by chunk
for applied_chunk = effector.stream(waveform, sample_rate):
    ...

示例

src = torchaudio.utils.download_asset("tutorial-assets/Lab41-SRI-VOiCES-src-sp0307-ch127535-sg0042.wav")
waveform, sr = torchaudio.load(src, channels_first=False)

原始音频

show(effect=None)
effector tutorial


效果

tempo(速度)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#atempo

show("atempo=0.7")
effector tutorial


show("atempo=1.8")
effector tutorial


highpass(高通)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#highpass

show("highpass=frequency=1500")
effector tutorial


lowpass(低通)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#lowpass

show("lowpass=frequency=1000")
effector tutorial


allpass(全通)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#allpass

show("allpass")
effector tutorial


bandpass(带通)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#bandpass

show("bandpass=frequency=3000")
effector tutorial


bandreject(带阻)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#bandreject

show("bandreject=frequency=3000")
effector tutorial


echo(回声)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#aecho

show("aecho=in_gain=0.8:out_gain=0.88:delays=6:decays=0.4")
effector tutorial


show("aecho=in_gain=0.8:out_gain=0.88:delays=60:decays=0.4")
effector tutorial


show("aecho=in_gain=0.8:out_gain=0.9:delays=1000:decays=0.3")
effector tutorial


chorus(合唱)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#chorus

show("chorus=0.5:0.9:50|60|40:0.4|0.32|0.3:0.25|0.4|0.3:2|2.3|1.3")
effector tutorial


fft filter(FFT 滤波器)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#afftfilt

# fmt: off
show(
    "afftfilt="
    "real='re * (1-clip(b * (b/nb), 0, 1))':"
    "imag='im * (1-clip(b * (b/nb), 0, 1))'"
)
effector tutorial


show(
    "afftfilt="
    "real='hypot(re,im) * sin(0)':"
    "imag='hypot(re,im) * cos(0)':"
    "win_size=512:"
    "overlap=0.75"
)
effector tutorial


show(
    "afftfilt="
    "real='hypot(re,im) * cos(2 * 3.14 * (random(0) * 2-1))':"
    "imag='hypot(re,im) * sin(2 * 3.14 * (random(1) * 2-1))':"
    "win_size=128:"
    "overlap=0.8"
)
# fmt: on
effector tutorial


vibrato(颤音)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#vibrato

show("vibrato=f=10:d=0.8")
effector tutorial
/pytorch/audio/ci_env/lib/python3.10/site-packages/IPython/lib/display.py:188: RuntimeWarning: invalid value encountered in cast
  return scaled.astype("<h").tobytes(), nchan


tremolo(颤音)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#tremolo

show("tremolo=f=8:d=0.8")
effector tutorial


crystalizer(晶化器)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#crystalizer

show("crystalizer")
effector tutorial


flanger(镶边)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#flanger

show("flanger")
effector tutorial


phaser(相位器)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#aphaser

show("aphaser")
effector tutorial


pulsator(脉冲器)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#apulsator

show("apulsator", stereo=True)
effector tutorial


haas(哈斯效应)

https://ffmpeg.cpp.org.cn/ffmpeg-filters.html#haas

show("haas")
effector tutorial


编解码器

def show_multi(configs):
    results = []
    for config in configs:
        effector = AudioEffector(**config)
        results.append(effector.apply(waveform, int(sr)))

    num_configs = len(configs)
    figsize = (6.4, 0.3 + num_configs * 0.9)
    f, axes = plt.subplots(num_configs, 1, figsize=figsize, sharex=True)
    for result, ax in zip(results, axes):
        ax.specgram(result[:, 0], Fs=sr)
    f.set_tight_layout(True)

    return [Audio(r.numpy().T, rate=sr) for r in results]

ogg

results = show_multi(
    [
        {"format": "ogg"},
        {"format": "ogg", "encoder": "vorbis"},
        {"format": "ogg", "encoder": "opus"},
    ]
)
effector tutorial

ogg - 默认编码器 (flac)



ogg - vorbis



ogg - opus



mp3

https://trac.ffmpeg.org/wiki/Encode/MP3

results = show_multi(
    [
        {"format": "mp3"},
        {"format": "mp3", "codec_config": CodecConfig(compression_level=1)},
        {"format": "mp3", "codec_config": CodecConfig(compression_level=9)},
        {"format": "mp3", "codec_config": CodecConfig(bit_rate=192_000)},
        {"format": "mp3", "codec_config": CodecConfig(bit_rate=8_000)},
        {"format": "mp3", "codec_config": CodecConfig(qscale=9)},
        {"format": "mp3", "codec_config": CodecConfig(qscale=1)},
    ]
)
effector tutorial

default(默认)



compression_level=1(压缩级别=1)



compression_level=9(压缩级别=9)



bit_rate=192k(比特率=192k)



bit_rate=8k(比特率=8k)



qscale=9(质量等级=9)



qscale=1(质量等级=1)



标签: torchaudio.io

脚本总运行时间: ( 0 分钟 3.057 秒)

由 Sphinx-Gallery 生成的图库

文档

访问 PyTorch 的全面开发者文档

查看文档

教程

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

查看教程

资源

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

查看资源