pytorch / audio

Data manipulation and transformation for audio signal processing, powered by PyTorch
https://pytorch.org/audio
BSD 2-Clause "Simplified" License
2.43k stars 635 forks source link

Termux patch for default APT version of audio - for relative file paths, tilde (~) expansion not working in filepath for torchaudio.load() #3802

Open Manamama opened 1 week ago

Manamama commented 1 week ago

🐛 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

  1. Use a filepath with a tilde in the torchaudio.load() function.
  2. 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.

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 issue

Versions

Environment