-
Notifications
You must be signed in to change notification settings - Fork 664
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. Do not rely on global variables for backend switch So that load/save/info/load_wav functions will be torchscript-able 2. Add no_backend module to for the case there is no backend module available [bonus] This allows the whole codebase importable on systems that do not have torchaudio C++ extension nor soundfile.
- Loading branch information
Showing
8 changed files
with
255 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import unittest | ||
|
||
import torchaudio | ||
from torchaudio._internal.module_utils import is_module_available | ||
|
||
|
||
class TestBackendSwitch(unittest.TestCase): | ||
def test_no_backend(self): | ||
torchaudio.set_audio_backend(None) | ||
assert torchaudio.load == torchaudio.backend.no_backend.load | ||
assert torchaudio.load_wav == torchaudio.backend.no_backend.load_wav | ||
assert torchaudio.save == torchaudio.backend.no_backend.save | ||
assert torchaudio.info == torchaudio.backend.no_backend.info | ||
assert torchaudio.get_audio_backend() is None | ||
|
||
@unittest.skipIf( | ||
not is_module_available('torchaudio._torchaudio'), | ||
'torchaudio C++ extension not available') | ||
def test_sox(self): | ||
torchaudio.set_audio_backend('sox') | ||
assert torchaudio.load == torchaudio.backend.sox_backend.load | ||
assert torchaudio.load_wav == torchaudio.backend.sox_backend.load_wav | ||
assert torchaudio.save == torchaudio.backend.sox_backend.save | ||
assert torchaudio.info == torchaudio.backend.sox_backend.info | ||
assert torchaudio.get_audio_backend() == 'sox' | ||
|
||
@unittest.skipIf(not is_module_available('soundfile'), '"soundfile" not available') | ||
def test_soundfile(self): | ||
torchaudio.set_audio_backend('soundfile') | ||
assert torchaudio.load == torchaudio.backend.soundfile_backend.load | ||
assert torchaudio.load_wav == torchaudio.backend.soundfile_backend.load_wav | ||
assert torchaudio.save == torchaudio.backend.soundfile_backend.save | ||
assert torchaudio.info == torchaudio.backend.soundfile_backend.info | ||
assert torchaudio.get_audio_backend() == 'soundfile' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from pathlib import Path | ||
from typing import Any, Callable, Optional, Tuple, Union | ||
|
||
from torch import Tensor | ||
|
||
from . import common | ||
from .common import SignalInfo, EncodingInfo | ||
|
||
|
||
@common._impl_load | ||
def load(filepath: Union[str, Path], | ||
out: Optional[Tensor] = None, | ||
normalization: Union[bool, float, Callable] = True, | ||
channels_first: bool = True, | ||
num_frames: int = 0, | ||
offset: int = 0, | ||
signalinfo: Optional[SignalInfo] = None, | ||
encodinginfo: Optional[EncodingInfo] = None, | ||
filetype: Optional[str] = None) -> Tuple[Tensor, int]: | ||
raise RuntimeError('No audio I/O backend is available.') | ||
|
||
|
||
@common._impl_load_wav | ||
def load_wav(filepath: Union[str, Path], **kwargs: Any) -> Tuple[Tensor, int]: | ||
raise RuntimeError('No audio I/O backend is available.') | ||
|
||
|
||
@common._impl_save | ||
def save(filepath: str, src: Tensor, sample_rate: int, precision: int = 16, channels_first: bool = True) -> None: | ||
raise RuntimeError('No audio I/O backend is available.') | ||
|
||
|
||
@common._impl_info | ||
def info(filepath: str) -> Tuple[SignalInfo, EncodingInfo]: | ||
raise RuntimeError('No audio I/O backend is available.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.