Skip to content

Commit fc73770

Browse files
authored
Add special handling to filelike object mp3 (#2477)
Loading and querying file-like object is not possible to use the fallback mechanism introduced in #2419 because file-like objects are not seekable. This commit add special case handling to mp3. For filelike object mp3 input, it was required to pass `format="mp3"` because libsox did not auto detect the format. With the transition of mp3 handling from libsox to ffmpeg, the logic is to let the ffmpeg handle it without waiting for libsox to fail, if the `format="mp3"`
1 parent 9bb0523 commit fc73770

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

torchaudio/backend/sox_io_backend.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ def info(
8484
"""
8585
if not torch.jit.is_scripting():
8686
if hasattr(filepath, "read"):
87+
# Special case for Backward compatibility
88+
# v0.11 -> v0.12, mp3 handling is moved to FFmpeg.
89+
# file-like objects are not necessarily fallback-able
90+
# when they are not seekable.
91+
# The previous libsox-based implementation required `format="mp3"`
92+
# because internally libsox does not auto-detect the format.
93+
# For the special BC for mp3, we handle mp3 differently.
94+
if format == "mp3":
95+
return _fallback_info_fileobj(filepath, format)
8796
sinfo = torchaudio._torchaudio.get_info_fileobj(filepath, format)
8897
if sinfo is not None:
8998
return AudioMetaData(*sinfo)
@@ -194,6 +203,15 @@ def load(
194203
"""
195204
if not torch.jit.is_scripting():
196205
if hasattr(filepath, "read"):
206+
# Special case for Backward compatibility
207+
# v0.11 -> v0.12, mp3 handling is moved to FFmpeg.
208+
# file-like objects are not necessarily fallback-able
209+
# when they are not seekable.
210+
# The previous libsox-based implementation required `format="mp3"`
211+
# because internally libsox does not auto-detect the format.
212+
# For the special BC for mp3, we handle mp3 differently.
213+
if format == "mp3":
214+
return _fallback_load_fileobj(filepath, frame_offset, num_frames, normalize, channels_first, format)
197215
ret = torchaudio._torchaudio.load_audio_fileobj(
198216
filepath, frame_offset, num_frames, normalize, channels_first, format
199217
)

0 commit comments

Comments
 (0)