"vscode:/vscode.git/clone" did not exist on "110e0066735a3bd431c2640ae168fc040d7c0806"
backend.rst 6.09 KB
Newer Older
1
2
3
4
5
.. _backend:

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

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

9
10
11
12
: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.

13
14
15
16
* :ref:`"sox_io" <sox_io_backend>` (default on Linux/macOS)
* :ref:`"sox" <sox_backend>` (deprecated, will be removed in 0.9.0 release)
* :ref:`"soundfile" <soundfile_backend>` (default on Windows)
* :ref:`"soundfile" (legacy interface) <soundfile_legacy_backend>` (deprecated, will be removed in 0.9.0 release)
17

18
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.
19
20

.. note::
21
22
23
24
25
26
   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.
27

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

30
31
Changes in default backend and deprecation
------------------------------------------
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Backend module is going through a major overhaul. The following table summarizes the timeline for the deprecations and removals.

 +--------------------+-----------------------+------------------------+
 | **Backend**        | **0.8.0**             | **0.9.0**              |
 +====================+=======================+========================+
 | ``"sox_io"``       | Default on Linx/macOS | Default on Linux/macOS |
 +--------------------+-----------------------+------------------------+
 | ``"sox"``          | Available             | Removed                |
 | (deprecated)       |                       |                        |
 +--------------------+-----------------------+------------------------+
 | ``"soundfile"``    | Default on Windows    | Default on Windows     |
 +--------------------+-----------------------+------------------------+
 | ``"soundfile"``    | Available             | Removed                |
 | (legacy interface, |                       |                        |
 | deprecated)        |                       |                        |
 +--------------------+-----------------------+------------------------+

* The ``"sox"`` and ``"soundfile" (legacy interface)`` backends are deprecated and will be removed in 0.9.0 release.
51
52
53
54

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

55
56
57
58
59
60
61
62
63
Structures used to report the metadata of audio files.

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

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

SignalInfo (Deprecated)
-----------------------
64
65
66

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

67
68
69
70
EncodingInfo (Deprecated)
-------------------------

.. autoclass:: torchaudio.backend.common.EncodingInfo
71

72
.. _sox_io_backend:
73

74
75
Sox IO Backend
~~~~~~~~~~~~~~
76

77
The ``"sox_io"`` backend is available and default on Linux/macOS and not available on Windows.
78

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

You can switch from another backend to the ``sox_io`` backend with the following;
82
83
84

.. code::

85
   torchaudio.set_audio_backend("sox_io")
86
87
88
89

info
----

90
.. autofunction:: torchaudio.backend.sox_io_backend.info
91
92
93
94

load
----

95
.. autofunction:: torchaudio.backend.sox_io_backend.load
96

97
.. autofunction:: torchaudio.backend.sox_io_backend.load_wav
98
99
100
101
102


save
----

103
.. autofunction:: torchaudio.backend.sox_io_backend.save
104

105
.. _sox_backend:
106

107
108
Sox Backend (Deprecated)
~~~~~~~~~~~~~~~~~~~~~~~~
109

110
The ``"sox"`` backend is available on Linux/macOS and not available on Windows. This backend is deprecated and will be removed in ``0.9.0`` release.
111

112
You can switch from another backend to ``sox`` backend with the following;
113
114
115

.. code::

116
   torchaudio.set_audio_backend("sox")
117
118
119
120

info
----

121
.. autofunction:: torchaudio.backend.sox_backend.info
122
123
124
125

load
----

126
.. autofunction:: torchaudio.backend.sox_backend.load
127

128
.. autofunction:: torchaudio.backend.sox_backend.load_wav
129
130
131
132
133


save
----

134
.. autofunction:: torchaudio.backend.sox_backend.save
135

136
137
138
139
140
141
142
143
others
------

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

.. _soundfile_backend:
144
145
146
147

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

148
149
The ``"soundfile"`` backend is available when `SoundFile <https://pysoundfile.readthedocs.io/en/latest/>`_ is installed. This backend is the default on Windows.

150
You can switch from another backend to the ``"soundfile"`` backend with the following;
151
152
153

.. code::

154
   torchaudio.set_audio_backend("soundfile")
155

156
157
.. note::
    If you are switching from `"soundfile" (legacy interface) <soundfile_legacy_backend>` backend, set ``torchaudio.USE_SOUNDFILE_LEGACY_INTERFACE`` flag **before** switching the backend.
158
159

info
160
^^^^
161

162
.. autofunction:: torchaudio.backend._soundfile_backend.info
163
164

load
165
^^^^
166

167
.. autofunction:: torchaudio.backend._soundfile_backend.load
168

169
.. autofunction:: torchaudio.backend._soundfile_backend.load_wav
170
171
172


save
173
^^^^
174

175
.. autofunction:: torchaudio.backend._soundfile_backend.save
176

177
.. _soundfile_legacy_backend:
178

179
180
Legacy Interface (Deprecated)
-----------------------------
181

182
``"soundfile"`` backend with legacy interface is made available for backward compatibility reason, however this interface is deprecated and will be removed in the ``0.9.0`` release.
183
184
185
186
187

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

.. code::

188
189
   torchaudio.USE_SOUNDFILE_LEGACY_INTERFACE = True
   torchaudio.set_audio_backend("soundfile")  # The legacy interface
190
191
192
193

info
^^^^

194
.. autofunction:: torchaudio.backend.soundfile_backend.info
195
196
197
198

load
^^^^

199
.. autofunction:: torchaudio.backend.soundfile_backend.load
200

201
.. autofunction:: torchaudio.backend.soundfile_backend.load_wav
202
203
204
205
206


save
^^^^

207
.. autofunction:: torchaudio.backend.soundfile_backend.save