Skip to content

Commit 28de9fb

Browse files
committed
Update documentation and fix docstrings
1 parent 8181a83 commit 28de9fb

File tree

14 files changed

+569
-188
lines changed

14 files changed

+569
-188
lines changed

docs/source/backend.rst

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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

docs/source/index.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
torchaudio
2-
===========
2+
==========
33

44
The :mod:`torchaudio` package consists of I/O, popular datasets and common audio transformations.
55

66
.. toctree::
77
:maxdepth: 2
88
:caption: Package Reference
99

10-
sox_effects
10+
torchaudio
11+
backend
12+
functional
13+
transforms
1114
datasets
15+
models
16+
sox_effects
1217
compliance.kaldi
1318
kaldi_io
14-
transforms
15-
functional
1619
utils
17-
18-
.. automodule:: torchaudio
19-
:members:

docs/source/sox_effects.rst

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,52 @@
1-
.. role:: hidden
2-
:class: hidden-section
1+
.. _sox_effects:
32

43
torchaudio.sox_effects
54
======================
65

76
.. currentmodule:: torchaudio.sox_effects
87

8+
.. warning::
9+
10+
The :py:class:`SoxEffect` and :py:class:`SoxEffectsChain` classes are deprecated. Please migrate to :func:`apply_effects_tensor` and :func:`apply_effects_file`.
11+
12+
Resource initialization / shutdown
13+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14+
15+
.. autofunction:: init_sox_effects
16+
17+
.. autofunction:: shutdown_sox_effects
18+
19+
Listing supported effects
20+
~~~~~~~~~~~~~~~~~~~~~~~~~
21+
22+
.. autofunction:: effect_names
23+
24+
Applying effects
25+
~~~~~~~~~~~~~~~~
26+
927
Apply SoX effects chain on torch.Tensor or on file and load as torch.Tensor.
1028

29+
Applying effects on Tensor
30+
--------------------------
31+
1132
.. autofunction:: apply_effects_tensor
1233

34+
Applying effects on file
35+
------------------------
36+
1337
.. autofunction:: apply_effects_file
1438

15-
Create SoX effects chain for preprocessing audio.
39+
Legacy
40+
~~~~~~
1641

17-
:hidden:`SoxEffect`
18-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42+
SoxEffect
43+
---------
1944

2045
.. autoclass:: SoxEffect
2146
:members:
2247

23-
:hidden:`SoxEffectsChain`
24-
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48+
SoxEffectsChain
49+
---------------
2550

2651
.. autoclass:: SoxEffectsChain
2752
:members: append_effect_to_chain, sox_build_flow_effects, clear_chain, set_input_file

docs/source/torchaudio.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
torchaudio
2+
==========
3+
4+
I/O functionalities
5+
~~~~~~~~~~~~~~~~~~~
6+
7+
Audio I/O functions are implemented in :ref:`torchaudio.backend<backend>` module, but for the ease of use, the following functions are made available on :mod:`torchaudio` module. There are different backends available and you can switch backends with :func:`set_audio_backend`.
8+
9+
Refer to :ref:`backend` for the detail.
10+
11+
.. function:: torchaudio.info(filepath: str, ...)
12+
13+
Fetch meta data of an audio file. Refer to :ref:`backend` for the detail.
14+
15+
.. function:: torchaudio.load(filepath: str, ...)
16+
17+
Load audio file into torch.Tensor object. Refer to :ref:`backend` for the detail.
18+
19+
.. function:: torchaudio.load_wav(filepath: str, ...)
20+
21+
Load audio file into torch.Tensor, Refer to :ref:`backend` for the detail.
22+
23+
.. function:: torchaudio.save(filepath: str, src: torch.Tensor, sample_rate: int, ...)
24+
25+
Save torch.Tensor object into an audio format. Refer to :ref:`backend` for the detail.
26+
27+
.. currentmodule:: torchaudio
28+
29+
Backend Utilities
30+
~~~~~~~~~~~~~~~~~
31+
32+
.. autofunction:: list_audio_backends
33+
34+
.. autofunction:: get_audio_backend
35+
36+
.. autofunction:: set_audio_backend
37+
38+
Sox Effects Utilities
39+
~~~~~~~~~~~~~~~~~~~~~
40+
41+
.. autofunction:: initialize_sox
42+
43+
.. autofunction:: shutdown_sox

docs/source/utils.rst

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,11 @@
1-
.. role:: hidden
2-
:class: hidden-section
1+
torchaudio.utils
2+
================
33

44
torchaudio.utils.sox_utils
5-
==========================
5+
~~~~~~~~~~~~~~~~~~~~~~~~~~
66

7-
Utility module to configure libsox. This affects functionalities in ``sox_io`` backend and ``torchaudio.sox_effects``.
7+
Utility module to configure libsox.
8+
This affects functionalities in :ref:`Sox IO backend<sox_io_backend>` and :ref:`Sox Effects<sox_effects>`.
89

9-
.. currentmodule:: torchaudio.utils.sox_utils
10-
11-
.. autofunction:: set_seed
12-
13-
.. autofunction:: set_verbosity
14-
15-
.. autofunction:: set_buffer_size
16-
17-
.. autofunction:: set_use_threads
18-
19-
.. autofunction:: list_effects
20-
21-
.. autofunction:: list_formats
10+
.. automodule:: torchaudio.utils.sox_utils
11+
:members:

torchaudio/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
@_mod_utils.deprecated(
3636
"Please remove the function call to initialize_sox. "
3737
"Resource initialization is now automatically handled.")
38-
def initialize_sox() -> int:
38+
def initialize_sox():
3939
"""Initialize sox effects.
4040
41-
This function is deprecated. See ``torchaudio.sox_effects.init_sox_effects``
41+
This function is deprecated. See :py:func:`torchaudio.sox_effects.init_sox_effects`
4242
"""
4343
_init_sox_effects()
4444

@@ -51,6 +51,6 @@ def initialize_sox() -> int:
5151
def shutdown_sox():
5252
"""Shutdown sox effects.
5353
54-
This function is deprecated. See ``torchaudio.sox_effects.shutdown_sox_effects``
54+
This function is deprecated. See :py:func:`torchaudio.sox_effects.shutdown_sox_effects`
5555
"""
5656
_shutdown_sox_effects()

torchaudio/backend/common.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33

44
class SignalInfo:
5+
"""Data class returned ``info`` functions.
6+
7+
Used by :py:func:`torchaudio.backend.sox_backend.info` and
8+
:py:func:`torchaudio.backend.soundfile_backend.info`
9+
10+
See https://fossies.org/dox/sox-14.4.2/structsox__signalinfo__t.html
11+
12+
:ivar Optional[int] channels: The number of channels
13+
:ivar Optional[float] rate: Sampleing rate
14+
:ivar Optional[int] precision: Bit depth
15+
:ivar Optional[int] length: For :ref:`sox backend<sox_backend>`, the number of samples.
16+
(frames * channels). For :ref:`soundfile backend<soundfile_backend>`, the number of frames.
17+
"""
518
def __init__(self,
619
channels: Optional[int] = None,
720
rate: Optional[float] = None,
@@ -14,6 +27,21 @@ def __init__(self,
1427

1528

1629
class EncodingInfo:
30+
"""Data class returned ``info`` functions.
31+
32+
Used by :py:func:`torchaudio.backend.sox_backend.info` and
33+
:py:func:`torchaudio.backend.soundfile_backend.info`
34+
35+
See https://fossies.org/dox/sox-14.4.2/structsox__encodinginfo__t.html
36+
37+
:ivar Optional[int] encoding: sox_encoding_t
38+
:ivar Optional[int] bits_per_sample: bit depth
39+
:ivar Optional[float] compression: Compression option
40+
:ivar Any reverse_bytes:
41+
:ivar Any reverse_nibbles:
42+
:ivar Any reverse_bits:
43+
:ivar Optional[bool] opposite_endian:
44+
"""
1745
def __init__(self,
1846
encoding: Any = None,
1947
bits_per_sample: Optional[int] = None,

0 commit comments

Comments
 (0)