Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform backend initialization in backend.__init__ #707

Merged
merged 1 commit into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions torchaudio/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from . import utils
from .utils import (
_get_audio_backend_module,
list_audio_backends,
Expand All @@ -16,3 +17,6 @@
SignalInfo,
EncodingInfo,
)


utils._init_audio_backend()
30 changes: 20 additions & 10 deletions torchaudio/backend/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
from typing import Any, Optional

from torchaudio._internal import module_utils as _mod_utils
Expand All @@ -6,16 +7,6 @@
_BACKEND = None
_BACKENDS = {}

if _mod_utils.is_module_available('soundfile'):
_BACKENDS['soundfile'] = soundfile_backend
if _mod_utils.is_module_available('torchaudio._torchaudio'):
_BACKENDS['sox'] = sox_backend

if 'sox' in _BACKENDS:
_BACKEND = 'sox'
elif 'soundfile' in _BACKENDS:
_BACKEND = 'soundfile'


def list_audio_backends():
return list(_BACKENDS.keys())
Expand Down Expand Up @@ -50,3 +41,22 @@ def _get_audio_backend_module() -> Any:
if _BACKEND is None:
raise RuntimeError('Backend is not initialized.')
return _BACKENDS[_BACKEND]


def _init_audio_backend():
global _BACKEND
global _BACKENDS

_BACKENDS = {}
if _mod_utils.is_module_available('soundfile'):
_BACKENDS['soundfile'] = soundfile_backend
if _mod_utils.is_module_available('torchaudio._torchaudio'):
_BACKENDS['sox'] = sox_backend

if 'sox' in _BACKENDS:
_BACKEND = 'sox'
elif 'soundfile' in _BACKENDS:
_BACKEND = 'soundfile'
else:
warnings.warn('No audio backend is available.')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that a warning is more appropriate.

_BACKEND = None