Skip to content

Commit cb50bbf

Browse files
authored
Add soundfile backend to audio decoding benchmarks (#597)
1 parent bf1c2c7 commit cb50bbf

File tree

1 file changed

+23
-43
lines changed

1 file changed

+23
-43
lines changed

benchmarks/decoders/benchmark_audio_decoders.py

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import subprocess
2-
31
from argparse import ArgumentParser
42
from datetime import timedelta
53
from pathlib import Path
@@ -46,30 +44,6 @@ def report_stats(times: Tensor, unit: str = "ms", prefix: str = "") -> float:
4644
)
4745

4846

49-
def get_duration(path: Path) -> str:
50-
try:
51-
result = subprocess.run(
52-
[
53-
"ffprobe",
54-
"-v",
55-
"error",
56-
"-show_entries",
57-
"format=duration",
58-
"-of",
59-
"default=noprint_wrappers=1:nokey=1",
60-
str(path),
61-
],
62-
stdout=subprocess.PIPE,
63-
stderr=subprocess.PIPE,
64-
text=True,
65-
)
66-
67-
# Remove microseconds
68-
return str(timedelta(seconds=float(result.stdout.strip()))).split(".")[0]
69-
except Exception:
70-
return "?"
71-
72-
7347
def decode_with_torchcodec(path: Path) -> None:
7448
AudioDecoder(path).get_all_samples()
7549

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

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

10177
print(
102-
f"Benchmarking {path.name}, duration: {get_duration(path)}, averaging over {args.num_exp} runs:"
78+
f"Benchmarking {path.name}, duration: {duration}, codec: {metadata.codec}, format: {metadata.sample_format}, averaging over {args.num_exp} runs:"
10379
)
10480

105-
times = bench(decode_with_torchcodec, path, num_exp=args.num_exp)
106-
report_stats(times, prefix="torchcodec.AudioDecoder")
81+
for decode_f, kwargs, prefix in (
82+
(decode_with_torchcodec, {}, "torchcodec.AudioDecoder"),
83+
(
84+
decode_with_torchaudio_load,
85+
{"backend": "ffmpeg"},
86+
"torchaudio.load(backend='ffmpeg')",
87+
),
88+
(decode_with_torchaudio_load, {"backend": "sox"}, "torchaudio.load(backend='sox')"),
89+
(
90+
decode_with_torchaudio_load,
91+
{"backend": "soundfile"},
92+
"torchaudio.load(backend='soundfile')",
93+
),
94+
(decode_with_torchaudio_StreamReader, {}, "torchaudio.StreamReader"),
95+
):
10796

108-
times = bench(decode_with_torchaudio_load, path, backend="ffmpeg", num_exp=args.num_exp)
109-
report_stats(times, prefix="torchaudio.load(backend='ffmpeg')")
110-
111-
prefix = "torchaudio.load(backend='sox')"
112-
try:
113-
times = bench(
114-
decode_with_torchaudio_load, path, backend="sox", num_exp=args.num_exp
115-
)
116-
report_stats(times, prefix=prefix)
117-
except RuntimeError:
118-
print(f"{prefix:<40} Not supported")
119-
120-
times = bench(decode_with_torchaudio_StreamReader, path, num_exp=args.num_exp)
121-
report_stats(times, prefix="torchaudio.StreamReader")
97+
try:
98+
times = bench(decode_f, path, **kwargs, num_exp=args.num_exp)
99+
report_stats(times, prefix=prefix)
100+
except RuntimeError:
101+
print(f"{prefix:<40} Not supported")

0 commit comments

Comments
 (0)