|
| 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 | + """Data class for audio format information |
| 25 | +
|
| 26 | + Used when torchaudio C++ extension is not available for annotating |
| 27 | + sox_io backend functions so that torchaudio is still importable |
| 28 | + without extension. |
| 29 | + This class has to implement the same interface as C++ equivalent. |
| 30 | + """ |
| 31 | + def __init__(self, sample_rate: int, num_channels: int, num_samples: int): |
| 32 | + self.sample_rate = sample_rate |
| 33 | + self.num_channels = num_channels |
| 34 | + self.num_samples = num_samples |
| 35 | + |
| 36 | + def get_sample_rate(self): |
| 37 | + return self.sample_rate |
| 38 | + |
| 39 | + def get_num_channels(self): |
| 40 | + return self.num_channels |
| 41 | + |
| 42 | + def get_num_samples(self): |
| 43 | + return self.num_samples |
| 44 | + |
| 45 | + DummyModule = namedtuple('torchaudio', ['SignalInfo']) |
| 46 | + module = DummyModule(SignalInfo) |
| 47 | + setattr(torch.classes, 'torchaudio', module) |
0 commit comments