|  | 
|  | 1 | +import importlib | 
|  | 2 | +from collections import namedtuple | 
|  | 3 | + | 
|  | 4 | +import torch | 
|  | 5 | +from torchaudio._internal import module_utils as _mod_utils | 
|  | 6 | + | 
|  | 7 | + | 
|  | 8 | +def _init_extension(): | 
|  | 9 | +    ext = 'torchaudio._torchaudio' | 
|  | 10 | +    if _mod_utils.is_module_available(ext): | 
|  | 11 | +        _init_script_module(ext) | 
|  | 12 | +    else: | 
|  | 13 | +        _init_dummy_module() | 
|  | 14 | + | 
|  | 15 | + | 
|  | 16 | +def _init_script_module(module): | 
|  | 17 | +    path = importlib.util.find_spec(module).origin | 
|  | 18 | +    torch.classes.load_library(path) | 
|  | 19 | +    torch.ops.load_library(path) | 
|  | 20 | + | 
|  | 21 | + | 
|  | 22 | +def _init_dummy_module(): | 
|  | 23 | +    class SignalInfo: | 
|  | 24 | +        """Container class for audio format information | 
|  | 25 | +
 | 
|  | 26 | +
 | 
|  | 27 | +        data class used when torchaudio C++ extension is not available. | 
|  | 28 | +        This class will be used for backends other than SoX, such as soundfile | 
|  | 29 | +        and required to annotate related functions. | 
|  | 30 | +
 | 
|  | 31 | +        This class has to implement the same interface as C++ equivalent. | 
|  | 32 | +        """ | 
|  | 33 | +        def __init__(self, sample_rate: int, num_channels: int, num_samples: int): | 
|  | 34 | +            self.sample_rate = sample_rate | 
|  | 35 | +            self.num_channels = num_channels | 
|  | 36 | +            self.num_samples = num_samples | 
|  | 37 | + | 
|  | 38 | +        def get_sample_rate(self): | 
|  | 39 | +            return self.sample_rate | 
|  | 40 | + | 
|  | 41 | +        def get_num_channels(self): | 
|  | 42 | +            return self.num_channels | 
|  | 43 | + | 
|  | 44 | +        def get_num_samples(self): | 
|  | 45 | +            return self.num_samples | 
|  | 46 | + | 
|  | 47 | +    DummyModule = namedtuple('torchaudio', ['SignalInfo']) | 
|  | 48 | +    module = DummyModule(SignalInfo) | 
|  | 49 | +    setattr(torch.classes, 'torchaudio', module) | 
0 commit comments