Exemple #1
0
def list_audio_backends() -> List[str]:
    """List available backends"""
    backends = []
    if is_module_available('soundfile'):
        backends.append('soundfile')
    if is_module_available('torchaudio._torchaudio'):
        backends.append('sox')
    return backends
Exemple #2
0
def _init_extension():
    ext = 'torchaudio._torchaudio'
    if _mod_utils.is_module_available(ext):
        _init_script_module(ext)
    else:
        warnings.warn('torchaudio C++ extension is not available.')
        _init_dummy_module()
Exemple #3
0
def skipIfNoExtension(test_item):
    if is_module_available('torchaudio._torchaudio'):
        return test_item
    if 'TORCHAUDIO_TEST_FAIL_IF_NO_EXTENSION' in os.environ:
        raise RuntimeError('torchaudio C++ extension is not available.')
    return unittest.skip('torchaudio C++ extension is not available')(
        test_item)
Exemple #4
0
def skipIfFormatNotSupported(fmt):
    fmts = []
    if is_module_available("soundfile"):
        import soundfile

        fmts = soundfile.available_formats()
        return skipIf(fmt not in fmts, f'"{fmt}" is not supported by sondfile')
    return skipIf(True, '"soundfile" not available.')
Exemple #5
0
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.')
        _BACKEND = None
Exemple #6
0
def _init_extension():
    if _mod_utils.is_module_available('torchaudio._torchaudio'):
        # Note this import has two purposes
        # 1. to extract the path of the extension module so that
        #    we can initialize the script module with the path.
        # 2. so that torchaudio._torchaudio is accessible in other modules.
        #    Look at sox_io_backend which uses `torchaudio._torchaudio.XXX`,
        #    assuming that the module `_torchaudio` is accessible.
        import torchaudio._torchaudio
        _init_script_module(torchaudio._torchaudio.__file__)
    else:
        warnings.warn('torchaudio C++ extension is not available.')
Exemple #7
0
def list_audio_backends() -> List[str]:
    """List available backends

    Returns:
        List[str]: The list of available backends.
    """
    backends = []
    if _mod_utils.is_module_available('soundfile'):
        backends.append('soundfile')
    if _mod_utils.is_sox_available():
        backends.append('sox_io')
    return backends
Exemple #8
0
def _init_extension():
    if not _mod_utils.is_module_available('torchaudio._torchaudio'):
        warnings.warn('torchaudio C++ extension is not available.')
        return

    suffix = 'pyd' if os.name == 'nt' else 'so'
    path = Path(__file__).parent / 'lib' / f'libtorchaudio.{suffix}'
    # In case `torchaudio` is deployed with `pex` format, this file does not exist.
    # In this case, we expect that `libtorchaudio` is available somewhere
    # in the search path of dynamic loading mechanism, and importing `_torchaudio`,
    # which depends on `libtorchaudio` and dynamic loader will handle it for us.
    if path.exists():
        torch.ops.load_library(path)
        torch.classes.load_library(path)
    # This import is for initializing the methods registered via PyBind11
    from torchaudio import _torchaudio  # noqa
Exemple #9
0
from torchaudio.utils import sox_utils
from torchaudio.backend import sox_io_backend
from torchaudio._internal.module_utils import is_module_available
from parameterized import parameterized

from torchaudio_unittest.common_utils import (
    TempDirMixin,
    TorchaudioTestCase,
    skipIfNoExtension,
    get_wav_data,
)
from .common import name_func

skipIfNoMP3 = unittest.skipIf(
    not is_module_available('torchaudio._torchaudio')
    or 'mp3' not in sox_utils.list_read_formats()
    or 'mp3' not in sox_utils.list_write_formats(),
    '"sox_io" backend does not support MP3')


@skipIfNoExtension
class SmokeTest(TempDirMixin, TorchaudioTestCase):
    """Run smoke test on various audio format

    The purpose of this test suite is to verify that sox_io_backend functionalities do not exhibit
    abnormal behaviors.

    This test suite should be able to run without any additional tools (such as sox command),
    however without such tools, the correctness of each function cannot be verified.
    """
Exemple #10
0
from parameterized import parameterized

from torchaudio._internal.module_utils import is_module_available
from torchaudio_unittest.common_utils import TorchaudioTestCase, skipIfNoModule

if is_module_available("unidecode") and is_module_available("inflect"):
    from pipeline_tacotron2.text.text_preprocessing import text_to_sequence
    from pipeline_tacotron2.text.numbers import (
        _remove_commas,
        _expand_pounds,
        _expand_dollars,
        _expand_decimal_point,
        _expand_ordinal,
        _expand_number,
    )


@skipIfNoModule("unidecode")
@skipIfNoModule("inflect")
class TestTextPreprocessor(TorchaudioTestCase):
    @parameterized.expand([
        [
            "dr.  Strange?",
            [15, 26, 14, 31, 26, 29, 11, 30, 31, 29, 12, 25, 18, 16, 10]
        ],
        ["ML, is        fun.", [24, 23, 6, 11, 20, 30, 11, 17, 32, 25, 7]],
        [
            "I love torchaudio!",
            [
                20, 11, 23, 26, 33, 16, 11, 31, 26, 29, 14, 19, 12, 32, 15, 20,
                26, 2
Exemple #11
0
    skipIfNoExtension,
    skipIfNoModule,
    skipIfNoExec,
    get_asset_path,
    get_sinusoid,
    get_wav_data,
    save_wav,
    load_wav,
    sox_utils,
)
from .common import (
    load_params,
    name_func,
)

if _mod_utils.is_module_available("requests"):
    import requests


@skipIfNoExtension
class TestSoxEffects(PytorchTestCase):
    def test_init(self):
        """Calling init_sox_effects multiple times does not crush"""
        for _ in range(3):
            sox_effects.init_sox_effects()


@skipIfNoExtension
class TestSoxEffectsTensor(TempDirMixin, PytorchTestCase):
    """Test suite for `apply_effects_tensor` function"""
    @parameterized.expand(list(
import os
from typing import Optional, Tuple

import torch
from torch import Tensor

from torchaudio._internal import (
    module_utils as _mod_utils,
    misc_ops as _misc_ops,
)
from .common import SignalInfo, EncodingInfo

if _mod_utils.is_module_available('soundfile'):
    import soundfile


_subtype_to_precision = {
    'PCM_S8': 8,
    'PCM_16': 16,
    'PCM_24': 24,
    'PCM_32': 32,
    'PCM_U8': 8
}


@_mod_utils.requires_module('soundfile')
def load(filepath: str,
         out: Optional[Tensor] = None,
         normalization: Optional[bool] = True,
         channels_first: Optional[bool] = True,
         num_frames: int = 0,
Exemple #13
0
"""Test suites for numerical compatibility with librosa"""
import os
import unittest
from distutils.version import StrictVersion

import torch
import torchaudio
import torchaudio.functional as F
from torchaudio._internal.module_utils import is_module_available

LIBROSA_AVAILABLE = is_module_available('librosa')

if LIBROSA_AVAILABLE:
    import numpy as np
    import librosa
    import scipy

import pytest

from . import common_utils


@unittest.skipIf(not LIBROSA_AVAILABLE, "Librosa not available")
class TestFunctional(common_utils.TorchaudioTestCase):
    """Test suite for functions in `functional` module."""
    def test_griffinlim(self):
        # NOTE: This test is flaky without a fixed random seed
        # See https://github.com/pytorch/audio/issues/382
        torch.random.manual_seed(42)
        tensor = torch.rand((1, 1000))
Exemple #14
0
# To use this file, the dependency (https://github.com/vesis84/kaldi-io-for-python)
# needs to be installed. This is a light wrapper around kaldi_io that returns
# torch.Tensors.
from typing import Any, Callable, Iterable, Tuple

import torch
from torch import Tensor
from torchaudio._internal import module_utils as _mod_utils

if _mod_utils.is_module_available('kaldi_io', 'numpy'):
    import numpy as np
    import kaldi_io

__all__ = [
    'read_vec_int_ark',
    'read_vec_flt_scp',
    'read_vec_flt_ark',
    'read_mat_scp',
    'read_mat_ark',
]


def _convert_method_output_to_tensor(
        file_or_fd: Any,
        fn: Callable,
        convert_contiguous: bool = False) -> Iterable[Tuple[str, Tensor]]:
    r"""Takes a method invokes it. The output is converted to a tensor.

    Args:
        file_or_fd (str/FileDescriptor): File name or file descriptor
        fn (Callable): Function that has the signature (file name/descriptor) and converts it to
Exemple #15
0
def skipIfNoModule(module, display_name=None):
    display_name = display_name or module
    return unittest.skipIf(not is_module_available(module),
                           f'"{display_name}" is not available')
Exemple #16
0
from . import (
    sox_utils,
)

from torchaudio._internal import module_utils as _mod_utils


if _mod_utils.is_module_available('torchaudio._torchaudio'):
    sox_utils.set_verbosity(1)
Exemple #17
0
import math
import shutil
import tempfile
import unittest

import torch
import torchaudio
from torchaudio.utils import sox_utils
from torchaudio._internal.module_utils import is_module_available

from torchaudio_unittest.common_utils import get_asset_path

BACKENDS = []
BACKENDS_MP3 = []

if is_module_available('soundfile'):
    BACKENDS.append('soundfile')

if is_module_available('torchaudio._torchaudio'):
    BACKENDS.append('sox')

    if ('mp3' in sox_utils.list_read_formats()
            and 'mp3' in sox_utils.list_write_formats()):
        BACKENDS_MP3 = ['sox']


def create_temp_assets_dir():
    """
    Creates a temporary directory and moves all files from test/assets there.
    Returns a Tuple[string, TemporaryDirectory] which is the folder path
    and object.
Exemple #18
0
"""The new soundfile backend which will become default in 0.8.0 onward"""
from typing import Tuple, Optional
import warnings

import torch
from torchaudio._internal import module_utils as _mod_utils
from .common import AudioMetaData

if _mod_utils.is_module_available("soundfile"):
    import soundfile

# Mapping from soundfile subtype to number of bits per sample.
# This is mostly heuristical and the value is set to 0 when it is irrelevant
# (lossy formats) or when it can't be inferred.
# For ADPCM (and G72X) subtypes, it's hard to infer the bit depth because it's not part of the standard:
# According to https://en.wikipedia.org/wiki/Adaptive_differential_pulse-code_modulation#In_telephony,
# the default seems to be 8 bits but it can be compressed further to 4 bits.
# The dict is inspired from
# https://github.com/bastibe/python-soundfile/blob/744efb4b01abc72498a96b09115b42a4cabd85e4/soundfile.py#L66-L94
_SUBTYPE_TO_BITS_PER_SAMPLE = {
    'PCM_S8': 8,  # Signed 8 bit data
    'PCM_16': 16,  # Signed 16 bit data
    'PCM_24': 24,  # Signed 24 bit data
    'PCM_32': 32,  # Signed 32 bit data
    'PCM_U8': 8,  # Unsigned 8 bit data (WAV and RAW only)
    'FLOAT': 32,  # 32 bit float data
    'DOUBLE': 64,  # 64 bit float data
    'ULAW': 8,  # U-Law encoded. See https://en.wikipedia.org/wiki/G.711#Types
    'ALAW': 8,  # A-Law encoded. See https://en.wikipedia.org/wiki/G.711#Types
    'IMA_ADPCM': 0,  # IMA ADPCM.
    'MS_ADPCM': 0,  # Microsoft ADPCM.
Exemple #19
0
        if self.backend is None:
            assert torchaudio.get_audio_backend() is None
        else:
            assert torchaudio.get_audio_backend() == self.backend
        assert torchaudio.load == self.backend_module.load
        assert torchaudio.load_wav == self.backend_module.load_wav
        assert torchaudio.save == self.backend_module.save
        assert torchaudio.info == self.backend_module.info


class TestBackendSwitch_NoBackend(BackendSwitchMixin,
                                  common_utils.TorchaudioTestCase):
    backend = None
    backend_module = torchaudio.backend.no_backend


@unittest.skipIf(not is_module_available('torchaudio._torchaudio'),
                 'torchaudio C++ extension not available')
class TestBackendSwitch_SoX(BackendSwitchMixin,
                            common_utils.TorchaudioTestCase):
    backend = 'sox'
    backend_module = torchaudio.backend.sox_backend


@unittest.skipIf(not is_module_available('soundfile'),
                 '"soundfile" not available')
class TestBackendSwitch_soundfile(BackendSwitchMixin,
                                  common_utils.TorchaudioTestCase):
    backend = 'soundfile'
    backend_module = torchaudio.backend.soundfile_backend