When using torchaudio.load() with a filepath that includes a tilde (~) to represent the home directory, the function fails to load the audio file. This is because the tilde is not being expanded to the full home directory path.
Quick patch for the impatient among us:
sed -i '251i\ filepath = os.path.expanduser(filepath)' /data/data/com.termux/files/usr/lib/python3.11/site-packages/torchaudio/backend/sox_io_backend.py
only for this particular version, see below!
Steps to Reproduce
Use a filepath with a tilde in the torchaudio.load() function.
Observe that the function fails to load the audio file.
Error Message
RuntimeError: Failed to load audio from ~/downloads/Sample_shop_1.wav
Expected Behavior
The torchaudio.load() function should expand the tilde to the full home directory path and successfully load the audio file.
Actual Behavior
The function fails to load the audio file, treating the tilde as a literal character in the filepath.
Proposed Fix
In the load() function in sox_io_backend.py, add tilde expansion before the filepath is processed:
if not torch.jit.is_scripting():
if hasattr(filepath, "read"):
# ... (existing code for file-like objects)
filepath = os.path.expanduser(filepath)
filepath = os.fspath(filepath)
This change ensures that any tilde in the filepath is expanded to the full home directory path before being processed by os.fspath(filepath) and torch.ops.torchaudio.sox_io_load_audio_file.
Additional Context
This issue was encountered while using the TTS library, which internally uses torchaudio.load(). The fix has been tested and confirmed to resolve the issue in the Termux environment.
Possible Impact
This change would improve the usability of torchaudio.load() for users who rely on tilde expansion in filepaths, especially in Unix-like environments. It maintains consistency with common Unix path handling conventions.
🐛 Describe the bug
Description
When using
torchaudio.load()
with a filepath that includes a tilde (~
) to represent the home directory, the function fails to load the audio file. This is because the tilde is not being expanded to the full home directory path.Quick patch for the impatient among us:
sed -i '251i\ filepath = os.path.expanduser(filepath)' /data/data/com.termux/files/usr/lib/python3.11/site-packages/torchaudio/backend/sox_io_backend.py
Steps to Reproduce
torchaudio.load()
function.Error Message
Expected Behavior
The
torchaudio.load()
function should expand the tilde to the full home directory path and successfully load the audio file.Actual Behavior
The function fails to load the audio file, treating the tilde as a literal character in the filepath.
Proposed Fix
In the
load()
function insox_io_backend.py
, add tilde expansion before the filepath is processed:This change ensures that any tilde in the filepath is expanded to the full home directory path before being processed by
os.fspath(filepath)
andtorch.ops.torchaudio.sox_io_load_audio_file
.Additional Context
This issue was encountered while using the TTS library, which internally uses
torchaudio.load()
. The fix has been tested and confirmed to resolve the issue in the Termux environment.Possible Impact
This change would improve the usability of
torchaudio.load()
for users who rely on tilde expansion in filepaths, especially in Unix-like environments. It maintains consistency with common Unix path handling conventions.NB. Update of Torch to
Successfully installed torchaudio-2.3.1
fails for another reason, this one https://github.com/pytorch/audio/issues/18, but it is another issueVersions
Environment