torchaudio.rst 3.85 KB
Newer Older
1
2
3
torchaudio
==========

moto's avatar
moto committed
4
5
.. currentmodule:: torchaudio

6
7
I/O
---
8

9
10
11
``torchaudio`` top-level module provides the following functions that make
it easy to handle audio data.

moto's avatar
moto committed
12
13
14
15
.. autosummary::
   :toctree: generated
   :nosignatures:
   :template: autosummary/io.rst
16

moto's avatar
moto committed
17
18
19
   info
   load
   save
20

moto's avatar
moto committed
21
.. _backend:
22

moto's avatar
moto committed
23
24
Backend and Dispatcher
----------------------
25

moto's avatar
moto committed
26
27
28
29
Decoding and encoding media is highly elaborated process. Therefore, TorchAudio
relies on third party libraries to perform these operations. These third party
libraries are called ``backend``, and currently TorchAudio integrates the
following libraries.
30

moto's avatar
moto committed
31
Please refer to `Installation <./installation.html>`__ for how to enable backends.
32

moto's avatar
moto committed
33
34
35
Conventionally, TorchAudio has had its I/O backend set globally at runtime
based on availability. However, this approach does not allow applications to
use different backends, and it is not well-suited for large codebases.
36

moto's avatar
moto committed
37
38
For these reasons, in v2.0, we introduced a dispatcher, a new mechanism to allow
users to choose a backend for each function call.
39

moto's avatar
moto committed
40
41
42
When dispatcher mode is enabled, all the I/O functions accept extra keyward argument
``backend``, which specifies the desired backend. If the specified
backend is not available, the function call will fail.
43

moto's avatar
moto committed
44
If a backend is not explicitly chosen, the functions will select a backend to use given order of precedence and library availability.
45

moto's avatar
moto committed
46
The following table summarizes the backends.
47

moto's avatar
moto committed
48
49
50
.. list-table::
   :header-rows: 1
   :widths: 8 12 25 60
51

moto's avatar
moto committed
52
53
54
55
56
57
58
59
60
61
   * - Priority
     - Backend
     - Supported OS
     - Note
   * - 1
     - FFmpeg
     - Linux, macOS, Windows
     - Use :py:func:`~torchaudio.utils.ffmpeg_utils.get_audio_decoders` and
       :py:func:`~torchaudio.utils.ffmpeg_utils.get_audio_encoders`
       to retrieve the supported codecs.
62

moto's avatar
moto committed
63
64
65
66
67
68
69
       This backend Supports various protocols, such as HTTPS and MP4, and file-like objects.
   * - 2
     - SoX
     - Linux, macOS
     - Use :py:func:`~torchaudio.utils.sox_utils.list_read_formats` and
       :py:func:`~torchaudio.utils.sox_utils.list_write_formats`
       to retrieve the supported codecs.
70

moto's avatar
moto committed
71
72
73
74
75
       This backend does *not* support file-like objects.
   * - 3
     - SoundFile
     - Linux, macOS, Windows
     - Please refer to `the official document <https://pysoundfile.readthedocs.io/>`__ for the supported codecs.
76

moto's avatar
moto committed
77
       This backend supports file-like objects.
78

moto's avatar
moto committed
79
.. _dispatcher_migration:
80

moto's avatar
moto committed
81
82
Dispatcher Migration
~~~~~~~~~~~~~~~~~~~~
83

moto's avatar
moto committed
84
85
86
We are migrating the I/O functions to use the dispatcher mechanism, and this
incurs multiple changes, some of which involve backward-compatibility-breaking
changes, and require users to change their function call.
87

moto's avatar
moto committed
88
89
The (planned) changes are as follows. For up-to-date information,
please refer to https://github.com/pytorch/audio/issues/2950
90

moto's avatar
moto committed
91
92
93
94
95
96
97
98
99
* In 2.0, audio I/O backend dispatcher was introduced.
  Users can opt-in to using dispatcher by setting the environment variable
  ``TORCHAUDIO_USE_BACKEND_DISPATCHER=1``.
* In 2.1, the disptcher becomes the default mechanism for I/O.
  Those who need to keep using the previous mechanism (global backend) can do
  so by setting ``TORCHAUDIO_USE_BACKEND_DISPATCHER=0``.
* In 2.2, the legacy global backend mechanism will be removed.
  Utility functions :py:func:`get_audio_backend` and :py:func:`set_audio_backend`
  become no-op.
100

moto's avatar
moto committed
101
102
103
104
Furthermore, we are removing file-like object support from libsox backend, as this
is better supported by FFmpeg backend and makes the build process simpler.
Therefore, beginning with 2.1, FFmpeg and Soundfile are the sole backends that support
file-like objects.
105

106
Backend Utilities
moto's avatar
moto committed
107
-----------------
108

109
110
The following functions are effective only when backend dispatcher is disabled.

moto's avatar
moto committed
111
112
Note that the changes in 2.1 marks :py:func:`get_audio_backend` and
:py:func:`set_audio_backend` deprecated.
113

moto's avatar
moto committed
114
115
116
.. autosummary::
   :toctree: generated
   :nosignatures:
117

moto's avatar
moto committed
118
119
120
   list_audio_backends
   get_audio_backend
   set_audio_backend