Repository: pytorch/audio

Issue:
_audio_backends platform independent
## 🐛 Bug

The constant [_audio_backends](https://github.com/pytorch/audio/blob/c29598d54185d73b4ed04103330573e190bbdb69/torchaudio/_backend.py#L12) is referenced by [BACKENDS](https://github.com/pytorch/audio/blob/0e5581cb2a9616205a00cbabf4c9a30613a1037f/test/common_utils.py#L10) in the common utilities of the test folder. [test_batch_mfcc](https://github.com/pytorch/audio/blob/0e5581cb2a9616205a00cbabf4c9a30613a1037f/test/test_batch_consistency.py#L181) is skipped if the 'sox' key is not present in that constant, but it always is. That means this test will be executed in environments where the package may not exist.

```
(base) PS C:\Users\chris\dev\audio> python .\test\test_batch_consistency.py TestTransforms.test_batch_mfcc
E
======================================================================
ERROR: test_batch_mfcc (__main__.TestTransforms)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\tools\Anaconda3\lib\contextlib.py", line 74, in inner
    return func(*args, **kwds)
  File ".\test\test_batch_consistency.py", line 185, in test_batch_mfcc
    waveform, _ = torchaudio.load(test_filepath)
  File "c:\users\chris\dev\audio\torchaudio\__init__.py", line 87, in load
    filetype=filetype,
  File "c:\users\chris\dev\audio\torchaudio\_sox_backend.py", line 38, in load
    import _torch_sox
ModuleNotFoundError: No module named '_torch_sox'

----------------------------------------------------------------------
Ran 1 test in 0.001s

FAILED (errors=1)
```

## To Reproduce

Steps to reproduce the behavior:

1. Remove sox from your environment
2. Run ```python test/test_batch_consistency.py TestTransform.test_batch_mfcc```

I can provide more detailed information if required.

## Expected behavior

The test should be skipped if sox is not available.

## Environment

```
(base) PS C:\Users\chris\dev\audio> python .\collect_env_1.py
Collecting environment information...
PyTorch version: 1.6.0a0+8a60d8b
Is debug build: No
CUDA used to build PyTorch: None

OS: Microsoft Windows 10 Home
GCC version: Could not collect
CMake version: version 3.14.0

Python version: 3.7
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip] numpy==1.18.1
[pip] numpydoc==0.9.2
[pip] torch==1.6.0a0+8a60d8b
[pip] torchaudio==0.5.0a0+5a75b63
[conda] blas                      1.0                         mkl
[conda] mkl                       2020.0                      166
[conda] mkl-include               2020.0                      166
[conda] mkl-service               2.3.0            py37hb782905_0
[conda] mkl_fft                   1.0.15           py37h14836fe_0
[conda] mkl_random                1.1.0            py37h675688f_0
[conda] numpy                     1.18.1           py37h93ca92e_0
[conda] numpy-base                1.18.1           py37hc3f5095_1
[conda] numpydoc                  0.9.2                      py_0
[conda] torch                     1.6.0a0+8a60d8b           dev_0    <develop>
[conda] torchaudio                0.5.0a0+5a75b63           dev_0    <develop>
```
## Additional context




Discussion:
A simple fix would be to make `_audio_backends` also depend on platform [here](https://github.com/pytorch/audio/blob/master/torchaudio/_backend.py#L12).

#561 fixed the particular error you saw.
It would be nice if it can detect which backend is available at run time.

Also we need to document which one is the default backend.
`soundfile` seems to be a reasonable default as it's available on Windows too (also it is easy to install), but looking at [setup.py](https://github.com/pytorch/audio/blob/master/setup.py#L37-L53), it expects SoX.
> It would be nice if it can detect which backend is available at run time.

Yes. In some sense and depending on the definition of run time, this is already the case, see [here](https://github.com/pytorch/audio/blob/master/torchaudio/_backend.py) and [here](https://github.com/pytorch/audio/blob/master/test/common_utils.py#L86).

There's also a partial discussion of backend dispatch in #425.

> Also we need to document which one is the default backend.
> `soundfile` seems to be a reasonable default as it's available on Windows too (also it is easy to install), but looking at [setup.py](https://github.com/pytorch/audio/blob/master/setup.py#L37-L53), it expects SoX.

The default backend for torchaudio has been sox. torchaudio only officially support macos and linux. We do not officially support windows, but there the default is soundfile, see #425 for discussion.

Resolving patch:
diff --git a/torchaudio/_backend.py b/torchaudio/_backend.py
--- a/torchaudio/_backend.py
+++ b/torchaudio/_backend.py
@@ -8,8 +8,12 @@
 from . import _soundfile_backend, _sox_backend
 
 
-_audio_backend = "soundfile" if platform.system() == "Windows" else "sox"
-_audio_backends = {"sox": _sox_backend, "soundfile": _soundfile_backend}
+if platform.system() == "Windows":
+    _audio_backend = "soundfile"
+    _audio_backends = {"soundfile": _soundfile_backend}
+else:
+    _audio_backend = "sox"
+    _audio_backends = {"sox": _sox_backend, "soundfile": _soundfile_backend}
 
 
 def set_audio_backend(backend: str) -> None:


Test patch: