backend.rst 7.89 KB
Newer Older
1
2
3
4
5
.. _backend:

torchaudio.backend
==================

6
7
Overview
~~~~~~~~
8

9
10
11
12
13
14
15
16
17
18
19
20
:mod:`torchaudio.backend` module provides implementations for audio file I/O functionalities, which are ``torchaudio.info``, ``torchaudio.load``, ``torchaudio.load_wav`` and ``torchaudio.save``.

There are currently four implementations available.

* :ref:`"sox" <sox_backend>` (deprecated, default on Linux/macOS)
* :ref:`"sox_io" <sox_io_backend>` (default on Linux/macOS from the 0.8.0 release)
* :ref:`"soundfile" - legacy interface <soundfile_legacy_backend>` (deprecated, default on Windows)
* :ref:`"soundfile" - new interface <soundfile_backend>` (default on Windows from the 0.8.0 release)

On Windows, only the ``"soundfile"`` backend (with both interfaces) are available. It is recommended to use the new interface as the legacy interface is deprecated.

On Linux/macOS, please use ``"sox_io"`` backend. The use of ``"sox"`` backend is strongly discouraged as it cannot correctly handle formats other than 16-bit integer WAV. See `#726 <https://github.com/pytorch/audio/pull/726>`_ for the detail.
21
22

.. note::
23
24
25
26
27
28
   Instead of calling functions in ``torchaudio.backend`` directly, please use ``torchaudio.info``, ``torchaudio.load``, ``torchaudio.load_wav`` and ``torchaudio.save`` with proper backend set with :func:`torchaudio.set_audio_backend`.

Availability
------------

``"sox"`` and ``"sox_io"`` backends require C++ extension module, which is included in Linux/macOS binary distributions. These backends are not available on Windows.
29

30
``"soundfile"`` backend requires ``SoundFile``. Please refer to `the SoundFile documentation <https://pysoundfile.readthedocs.io/en/latest/>`_ for the installation.
31

32
33
Changes in default backend and deprecation
------------------------------------------
34

35
Backend module is going through a major overhaul. The following table summarizes the timeline for the changes and deprecations.
36

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 +--------------------+--------------------------+-----------------------+------------------------+
 | **Backend**        | **0.7.0**                | **0.8.0**             | **0.9.0**              |
 +====================+==========================+=======================+========================+
 | ``"sox"``          | Default on Linux/macOS   | Available             | Removed                |
 | (deprecated)       |                          |                       |                        |
 +--------------------+--------------------------+-----------------------+------------------------+
 | ``"sox_io"``       | Available                | Default on Linx/macOS | Default on Linux/macOS |
 +--------------------+--------------------------+-----------------------+------------------------+
 | ``"soundfile"``    | Default on Windows       | Available             | Removed                |
 | (legacy interface, |                          |                       |                        |
 | deprecated)        |                          |                       |                        |
 +--------------------+--------------------------+-----------------------+------------------------+
 | ``"soundfile"``    | Available                | Default on Windows    | Default on Windows     |
 | (new interface)    |                          |                       |                        |
 +--------------------+--------------------------+-----------------------+------------------------+
52

53
54
55
* The default backend for Linux/macOS will be changed from ``"sox"`` to ``"sox_io"`` in ``0.8.0`` release.
* The ``"sox"`` backend will be removed in the ``0.9.0`` release.
* Starting from the 0.8.0 release, ``"soundfile"`` backend will use the new interface, which has the same interface as ``"sox_io"`` backend. The legacy interface will be removed in the ``0.9.0`` release.
56
57
58
59

Common Data Structure
~~~~~~~~~~~~~~~~~~~~~

60
61
62
63
64
65
66
67
68
Structures used to report the metadata of audio files.

AudioMetaData
-------------

.. autoclass:: torchaudio.backend.common.AudioMetaData

SignalInfo (Deprecated)
-----------------------
69
70
71

.. autoclass:: torchaudio.backend.common.SignalInfo

72
73
74
75
EncodingInfo (Deprecated)
-------------------------

.. autoclass:: torchaudio.backend.common.EncodingInfo
76
77
78

.. _sox_backend:

79
80
Sox Backend (Deprecated)
~~~~~~~~~~~~~~~~~~~~~~~~
81

82
The ``"sox"`` backend is available on Linux/macOS and not available on Windows. This backend is currently the default when available, but is deprecated and will be removed in ``0.9.0`` release.
83

84
You can switch from another backend to ``sox`` backend with the following;
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

.. code::

   torchaudio.set_audio_backend("sox")

info
----

.. autofunction:: torchaudio.backend.sox_backend.info

load
----

.. autofunction:: torchaudio.backend.sox_backend.load

.. autofunction:: torchaudio.backend.sox_backend.load_wav


save
----

.. autofunction:: torchaudio.backend.sox_backend.save

others
------

.. automodule:: torchaudio.backend.sox_backend
   :members:
   :exclude-members: info, load, load_wav, save

.. _sox_io_backend:

Sox IO Backend
~~~~~~~~~~~~~~

120
121
122
The ``"sox_io"`` backend is available on Linux/macOS and not available on Windows. This backend is recommended over the ``"sox"`` backend, and will become the default in the ``0.8.0`` release.

I/O functions of this backend support `TorchScript <https://pytorch.org/docs/stable/jit.html>`_.
123

124
You can switch from another backend to the ``sox_io`` backend with the following;
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

.. code::

   torchaudio.set_audio_backend("sox_io")

info
----

.. autofunction:: torchaudio.backend.sox_io_backend.info

load
----

.. autofunction:: torchaudio.backend.sox_io_backend.load

.. autofunction:: torchaudio.backend.sox_io_backend.load_wav


save
----

.. autofunction:: torchaudio.backend.sox_io_backend.save

148
.. _soundfile_legacy_backend:
149
150
151
152

Soundfile Backend
~~~~~~~~~~~~~~~~~

153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
The ``"soundfile"`` backend is available when `SoundFile <https://pysoundfile.readthedocs.io/en/latest/>`_ is installed. This backend is the default on Windows.

The ``"soundfile"`` backend has two interfaces, legacy and new.

* In the ``0.7.0`` release, the legacy interface is used by default when switching to the ``"soundfile"`` backend.
* In the ``0.8.0`` release, the new interface will become the default.
* In the ``0.9.0`` release, the legacy interface will be removed.

To change the interface, set ``torchaudio.USE_SOUNDFILE_LEGACY_INTERFACE`` flag **before** switching the backend.

.. code::

   torchaudio.USE_SOUNDFILE_LEGACY_INTERFACE = True
   torchaudio.set_audio_backend("soundfile")  # The legacy interface

   torchaudio.USE_SOUNDFILE_LEGACY_INTERFACE = False
   torchaudio.set_audio_backend("soundfile")  # The new interface

Legacy Interface (Deprecated)
-----------------------------

``"soundfile"`` backend with legacy interface is currently the default on Windows, however this interface is deprecated and will be removed in the ``0.9.0`` release.
175

176
To switch to this backend/interface, set ``torchaudio.USE_SOUNDFILE_LEGACY_INTERFACE`` flag **before** switching the backend.
177
178
179

.. code::

180
181
   torchaudio.USE_SOUNDFILE_LEGACY_INTERFACE = True
   torchaudio.set_audio_backend("soundfile")  # The legacy interface
182
183

info
184
^^^^
185
186
187
188

.. autofunction:: torchaudio.backend.soundfile_backend.info

load
189
^^^^
190
191
192
193
194
195
196

.. autofunction:: torchaudio.backend.soundfile_backend.load

.. autofunction:: torchaudio.backend.soundfile_backend.load_wav


save
197
^^^^
198
199

.. autofunction:: torchaudio.backend.soundfile_backend.save
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231

.. _soundfile_backend:

New Interface
-------------

The ``"soundfile"`` backend with new interface will become the default in the ``0.8.0`` release.

To switch to this backend/interface, set ``torchaudio.USE_SOUNDFILE_LEGACY_INTERFACE`` flag **before** switching the backend.

.. code::

   torchaudio.USE_SOUNDFILE_LEGACY_INTERFACE = False
   torchaudio.set_audio_backend("soundfile")  # The new interface

info
^^^^

.. autofunction:: torchaudio.backend._soundfile_backend.info

load
^^^^

.. autofunction:: torchaudio.backend._soundfile_backend.load

.. autofunction:: torchaudio.backend._soundfile_backend.load_wav


save
^^^^

.. autofunction:: torchaudio.backend._soundfile_backend.save