Skip to content

Add soundfile backend to audio decoding benchmarks #597

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 1, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 23 additions & 43 deletions benchmarks/decoders/benchmark_audio_decoders.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import subprocess

from argparse import ArgumentParser
from datetime import timedelta
from pathlib import Path
Expand Down Expand Up @@ -46,30 +44,6 @@ def report_stats(times: Tensor, unit: str = "ms", prefix: str = "") -> float:
)


def get_duration(path: Path) -> str:
try:
result = subprocess.run(
[
"ffprobe",
"-v",
"error",
"-show_entries",
"format=duration",
"-of",
"default=noprint_wrappers=1:nokey=1",
str(path),
],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)

# Remove microseconds
return str(timedelta(seconds=float(result.stdout.strip()))).split(".")[0]
except Exception:
return "?"


def decode_with_torchcodec(path: Path) -> None:
AudioDecoder(path).get_all_samples()

Expand Down Expand Up @@ -97,25 +71,31 @@ def decode_with_torchaudio_load(path: Path, backend: str) -> None:
args = parser.parse_args()
path = Path(args.path)

metadata = AudioDecoder(path).metadata
duration = str(timedelta(seconds=metadata.duration_seconds_from_header)).split(".")[0]

print(
f"Benchmarking {path.name}, duration: {get_duration(path)}, averaging over {args.num_exp} runs:"
f"Benchmarking {path.name}, duration: {duration}, codec: {metadata.codec}, format: {metadata.sample_format}, averaging over {args.num_exp} runs:"
)

times = bench(decode_with_torchcodec, path, num_exp=args.num_exp)
report_stats(times, prefix="torchcodec.AudioDecoder")
for decode_f, kwargs, prefix in (
(decode_with_torchcodec, {}, "torchcodec.AudioDecoder"),
(
decode_with_torchaudio_load,
{"backend": "ffmpeg"},
"torchaudio.load(backend='ffmpeg')",
),
(decode_with_torchaudio_load, {"backend": "sox"}, "torchaudio.load(backend='sox')"),
(
decode_with_torchaudio_load,
{"backend": "soundfile"},
"torchaudio.load(backend='soundfile')",
),
(decode_with_torchaudio_StreamReader, {}, "torchaudio.StreamReader"),
):

times = bench(decode_with_torchaudio_load, path, backend="ffmpeg", num_exp=args.num_exp)
report_stats(times, prefix="torchaudio.load(backend='ffmpeg')")

prefix = "torchaudio.load(backend='sox')"
try:
times = bench(
decode_with_torchaudio_load, path, backend="sox", num_exp=args.num_exp
)
report_stats(times, prefix=prefix)
except RuntimeError:
print(f"{prefix:<40} Not supported")

times = bench(decode_with_torchaudio_StreamReader, path, num_exp=args.num_exp)
report_stats(times, prefix="torchaudio.StreamReader")
try:
times = bench(decode_f, path, **kwargs, num_exp=args.num_exp)
report_stats(times, prefix=prefix)
except RuntimeError:
print(f"{prefix:<40} Not supported")
Loading