Skip to content

Commit

Permalink
Add backend module and put all the backend logics there
Browse files Browse the repository at this point in the history
  • Loading branch information
mthrok committed Jun 8, 2020
1 parent 55d64b1 commit 0139ac1
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 57 deletions.
2 changes: 1 addition & 1 deletion test/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import torchaudio

_TEST_DIR_PATH = os.path.dirname(os.path.realpath(__file__))
BACKENDS = torchaudio._backend.list_audio_backends()
BACKENDS = torchaudio.list_audio_backends()


def get_asset_path(*paths):
Expand Down
8 changes: 4 additions & 4 deletions torchaudio/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
sox_effects,
transforms
)
from torchaudio._backend import (
from torchaudio.backend import (
list_audio_backends,
get_audio_backend,
set_audio_backend,
)
from torchaudio._sox_backend import (
save_encinfo,
sox_signalinfo_t,
sox_encodinginfo_t,
get_sox_option_t,
get_sox_encoding_t,
get_sox_bool,
SignalInfo,
EncodingInfo,
)
from torchaudio._soundfile_backend import SignalInfo, EncodingInfo
from torchaudio._internal import (
module_utils as _mod_utils,
misc_ops as _misc_ops,
Expand Down
32 changes: 32 additions & 0 deletions torchaudio/backend/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from .utils import (
list_audio_backends,
get_audio_backend,
set_audio_backend,
)
from .sox_backend import (
save_encinfo,
sox_signalinfo_t,
sox_encodinginfo_t,
get_sox_option_t,
get_sox_encoding_t,
get_sox_bool,
)
from .common import (
SignalInfo,
EncodingInfo,
)


def _init_backend():
backends = list_audio_backends()
if 'sox' in backends:
set_audio_backend('sox')
elif 'soundfile' in backends:
set_audio_backend('soundfile')
else:
import warnings
warnings.warn('No audio backend is available.')


_init_backend()
del _init_backend
31 changes: 31 additions & 0 deletions torchaudio/backend/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import Any, Optional


class SignalInfo:
def __init__(self,
channels: Optional[int] = None,
rate: Optional[float] = None,
precision: Optional[int] = None,
length: Optional[int] = None) -> None:
self.channels = channels
self.rate = rate
self.precision = precision
self.length = length


class EncodingInfo:
def __init__(self,
encoding: Any = None,
bits_per_sample: Optional[int] = None,
compression: Optional[float] = None,
reverse_bytes: Any = None,
reverse_nibbles: Any = None,
reverse_bits: Any = None,
opposite_endian: Optional[bool] = None) -> None:
self.encoding = encoding
self.bits_per_sample = bits_per_sample
self.compression = compression
self.reverse_bytes = reverse_bytes
self.reverse_nibbles = reverse_nibbles
self.reverse_bits = reverse_bits
self.opposite_endian = opposite_endian
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Any, Optional, Tuple
from typing import Optional, Tuple

import torch
from torch import Tensor
Expand All @@ -8,6 +8,7 @@
module_utils as _mod_utils,
misc_ops as _misc_ops,
)
from .common import SignalInfo, EncodingInfo

if _mod_utils.is_module_available('soundfile'):
import soundfile
Expand All @@ -22,36 +23,6 @@
}


class SignalInfo:
def __init__(self,
channels: Optional[int] = None,
rate: Optional[float] = None,
precision: Optional[int] = None,
length: Optional[int] = None) -> None:
self.channels = channels
self.rate = rate
self.precision = precision
self.length = length


class EncodingInfo:
def __init__(self,
encoding: Any = None,
bits_per_sample: Optional[int] = None,
compression: Optional[float] = None,
reverse_bytes: Any = None,
reverse_nibbles: Any = None,
reverse_bits: Any = None,
opposite_endian: Optional[bool] = None) -> None:
self.encoding = encoding
self.bits_per_sample = bits_per_sample
self.compression = compression
self.reverse_bytes = reverse_bytes
self.reverse_nibbles = reverse_nibbles
self.reverse_bits = reverse_bits
self.opposite_endian = opposite_endian


@_mod_utils.requires_module('soundfile')
def load(filepath: str,
out: Optional[Tensor] = None,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
module_utils as _mod_utils,
misc_ops as _misc_ops,
)
from torchaudio._soundfile_backend import SignalInfo, EncodingInfo
from .common import SignalInfo, EncodingInfo

if _mod_utils.is_module_available('torchaudio._torchaudio'):
from . import _torchaudio
from torchaudio import _torchaudio


@_mod_utils.requires_module('torchaudio._torchaudio')
Expand Down
31 changes: 12 additions & 19 deletions torchaudio/_backend.py → torchaudio/backend/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import warnings
"""Defines utilities for switching audio backends"""
from typing import Optional, List

import torchaudio
from torchaudio._internal.module_utils import is_module_available
from . import _soundfile_backend, _sox_backend
from . import soundfile_backend, sox_backend

__all__ = [
'list_audio_backends',
'get_audio_backend',
'set_audio_backend',
]


def list_audio_backends() -> List[str]:
Expand All @@ -29,9 +35,9 @@ def set_audio_backend(backend: str) -> None:
f'available backends: {list_audio_backends()}.')

if backend == 'sox':
module = _sox_backend
module = sox_backend
elif backend == 'soundfile':
module = _soundfile_backend
module = soundfile_backend
else:
raise NotImplementedError(f'Unexpected backend "{backend}"')

Expand All @@ -41,21 +47,8 @@ def set_audio_backend(backend: str) -> None:

def get_audio_backend() -> Optional[str]:
"""Get the name of the current backend"""
if torchaudio.load == _sox_backend.load:
if torchaudio.load == sox_backend.load:
return 'sox'
if torchaudio.load == _soundfile_backend.load:
if torchaudio.load == soundfile_backend.load:
return 'soundfile'
return None


def _init_backend():
backends = list_audio_backends()
if 'sox' in backends:
set_audio_backend('sox')
elif 'soundfile' in backends:
set_audio_backend('soundfile')
else:
warnings.warn('No audio backend is available.')


_init_backend()

0 comments on commit 0139ac1

Please sign in to comment.