• Docs >
  • AudioEffector Usages >
  • Nightly (unstable)
Shortcuts

AudioEffector Usages

Author: Moto Hira

This tutorial shows how to use torchaudio.io.AudioEffector to apply various effects and codecs to waveform tensor.

Note

This tutorial requires FFmpeg libraries. Please refer to FFmpeg dependency for the detail.

Overview

AudioEffector combines in-memory encoding, decoding and filtering that are provided by StreamWriter and StreamReader.

The following figure illustrates the process.

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

print(torch.__version__)
print(torchaudio.__version__)
2.4.0.dev20240415
2.2.0.dev20240416
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)

Usage

To use AudioEffector, instantiate it with effect and format, then either pass the waveform to apply() or stream() method.

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

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

apply method applies effect and codec to the entire waveform at once. So if the input waveform is long, and memory consumption is an issue, one can use stream method to process chunk by chunk.

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

Example

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

Original

show(effect=None)
effector tutorial


Effects

tempo

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

show("atempo=0.7")
effector tutorial


show("atempo=1.8")
effector tutorial


highpass

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

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


lowpass

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

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


allpass

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

show("allpass")
effector tutorial


bandpass

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

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