|
| 1 | +.. _backend: |
| 2 | + |
| 3 | +torchaudio.backend |
| 4 | +================== |
| 5 | + |
| 6 | +:mod:`torchaudio.backend` module provides implemenrations for audio file I/O, using different backend libraries |
| 7 | +To switch backend, use :py:func:`torchaudio.set_audio_backend`. To check the current backend use :py:func:`torchaudio.get_audio_backend`. |
| 8 | + |
| 9 | +.. warning:: |
| 10 | + Although ``sox`` backend is default for backward compatibility reason, it has a number of issues, therefore it is highly recommended to use ``sox_io`` backend instead. Note, however, that due to the interface refinement, functions defined in ``sox`` backend and those defined in ``sox_io`` backend do not have the signatures. |
| 11 | + |
| 12 | +.. note:: |
| 13 | + Instead of calling functions in :mod:`torchaudio.backend` directly, please use ``torchaudio.info``, ``torhcaudio.load``, ``torchaudio.load_wav`` and ``torchaudio.save`` with proper backend set with :func:`torchaudio.get_audio_backend`. |
| 14 | + |
| 15 | +There are currently three implementations available. |
| 16 | + |
| 17 | + * :ref:`sox<sox_backend>` |
| 18 | + * :ref:`sox_io<sox_io_backend>` |
| 19 | + * :ref:`soundfile<soundfile_backend>` |
| 20 | + |
| 21 | +``sox`` backend is the original backend which is built on ``libsox``. This module is currently default but is known to have number of issues, such as wrong handling of WAV files other than 16-bit signed integer. Users are encouraged to use ``sox_io`` backend. This backend requires C++ extension module and is not available on Windows system. |
| 22 | + |
| 23 | +``sox_io`` backend is the new backend which is built on ``libsox`` and bound to Python with ``Torchscript``. This module is thoroughly tested and addresses all the known issues ``sox`` backend has. Function calls to this backend can be Torchscriptable. This backend requires C++ extension module and is not available on Windows system. |
| 24 | + |
| 25 | +``soundfile`` backend is built on ``PySoundFile``. You need to install ``PySoundFile`` separately. |
| 26 | + |
| 27 | +Common Data Structure |
| 28 | +~~~~~~~~~~~~~~~~~~~~~ |
| 29 | + |
| 30 | +Structures used to exchange data between Python interface and ``libsox``. They are used by :ref:`sox<sox_backend>` and :ref:`soundfile<soundfile_backend>` but not by :ref:`sox_io<sox_io_backend>`. |
| 31 | + |
| 32 | +.. autoclass:: torchaudio.backend.common.SignalInfo |
| 33 | + |
| 34 | +.. autoclass:: torchaudio.backend.common.EncodingInfo |
| 35 | + |
| 36 | +.. _sox_backend: |
| 37 | + |
| 38 | +Sox Backend |
| 39 | +~~~~~~~~~~~ |
| 40 | + |
| 41 | +``sox`` backend is available on ``torchaudio`` installation with C++ extension. It is currently not available on Windows system. |
| 42 | + |
| 43 | +It is currently default backend when it's available. You can switch from another backend to ``sox`` backend with the following; |
| 44 | + |
| 45 | +.. code:: |
| 46 | +
|
| 47 | + torchaudio.set_audio_backend("sox") |
| 48 | +
|
| 49 | +info |
| 50 | +---- |
| 51 | + |
| 52 | +.. autofunction:: torchaudio.backend.sox_backend.info |
| 53 | + |
| 54 | +load |
| 55 | +---- |
| 56 | + |
| 57 | +.. autofunction:: torchaudio.backend.sox_backend.load |
| 58 | + |
| 59 | +.. autofunction:: torchaudio.backend.sox_backend.load_wav |
| 60 | + |
| 61 | + |
| 62 | +save |
| 63 | +---- |
| 64 | + |
| 65 | +.. autofunction:: torchaudio.backend.sox_backend.save |
| 66 | + |
| 67 | +others |
| 68 | +------ |
| 69 | + |
| 70 | +.. automodule:: torchaudio.backend.sox_backend |
| 71 | + :members: |
| 72 | + :exclude-members: info, load, load_wav, save |
| 73 | + |
| 74 | +.. _sox_io_backend: |
| 75 | + |
| 76 | +Sox IO Backend |
| 77 | +~~~~~~~~~~~~~~ |
| 78 | + |
| 79 | +``sox_io`` backend is available on ``torchaudio`` installation with C++ extension. It is currently not available on Windows system. |
| 80 | + |
| 81 | +This new backend is recommended over ``sox`` backend. You can switch from another backend to ``sox_io`` backend with the following; |
| 82 | + |
| 83 | +.. code:: |
| 84 | +
|
| 85 | + torchaudio.set_audio_backend("sox_io") |
| 86 | +
|
| 87 | +The function call to this backend can be Torchsript-able. You can apply :func:`torch.jit.script` and dump the object to file, then call it from C++ application. |
| 88 | + |
| 89 | +info |
| 90 | +---- |
| 91 | + |
| 92 | +.. autoclass:: torchaudio.backend.sox_io_backend.AudioMetaData |
| 93 | + |
| 94 | +.. autofunction:: torchaudio.backend.sox_io_backend.info |
| 95 | + |
| 96 | +load |
| 97 | +---- |
| 98 | + |
| 99 | +.. autofunction:: torchaudio.backend.sox_io_backend.load |
| 100 | + |
| 101 | +.. autofunction:: torchaudio.backend.sox_io_backend.load_wav |
| 102 | + |
| 103 | + |
| 104 | +save |
| 105 | +---- |
| 106 | + |
| 107 | +.. autofunction:: torchaudio.backend.sox_io_backend.save |
| 108 | + |
| 109 | +.. _soundfile_backend: |
| 110 | + |
| 111 | +Soundfile Backend |
| 112 | +~~~~~~~~~~~~~~~~~ |
| 113 | + |
| 114 | +``soundfile`` backend is available when ``PySoundFile`` is installed. This backend works on ``torchaudio`` installation without C++ extension. (i.e. Windows) |
| 115 | + |
| 116 | +You can switch from another backend to ``soundfile`` backend with the following; |
| 117 | + |
| 118 | +.. code:: |
| 119 | +
|
| 120 | + torchaudio.set_audio_backend("soundfile") |
| 121 | +
|
| 122 | +info |
| 123 | +---- |
| 124 | + |
| 125 | +.. autofunction:: torchaudio.backend.soundfile_backend.info |
| 126 | + |
| 127 | +load |
| 128 | +---- |
| 129 | + |
| 130 | +.. autofunction:: torchaudio.backend.soundfile_backend.load |
| 131 | + |
| 132 | +.. autofunction:: torchaudio.backend.soundfile_backend.load_wav |
| 133 | + |
| 134 | + |
| 135 | +save |
| 136 | +---- |
| 137 | + |
| 138 | +.. autofunction:: torchaudio.backend.soundfile_backend.save |
0 commit comments