|
1 | | -from pathlib import Path |
2 | | -from typing import Any, Callable, Optional, Tuple, Union |
3 | | - |
4 | | -from torch import Tensor |
5 | 1 | from torchaudio._internal import module_utils as _mod_utils |
6 | 2 | from torchaudio import ( |
7 | 3 | compliance, |
|
11 | 7 | transforms |
12 | 8 | ) |
13 | 9 | from torchaudio.backend import ( |
14 | | - _get_audio_backend_module, |
15 | 10 | list_audio_backends, |
16 | 11 | get_audio_backend, |
17 | 12 | set_audio_backend, |
@@ -57,116 +52,3 @@ def shutdown_sox(): |
57 | 52 | This function is deprecated. See ``torchaudio.sox_effects.shutdown_sox_effects`` |
58 | 53 | """ |
59 | 54 | _shutdown_sox_effects() |
60 | | - |
61 | | - |
62 | | -def load(filepath: Union[str, Path], |
63 | | - out: Optional[Tensor] = None, |
64 | | - normalization: Union[bool, float, Callable] = True, |
65 | | - channels_first: bool = True, |
66 | | - num_frames: int = 0, |
67 | | - offset: int = 0, |
68 | | - signalinfo: Optional[SignalInfo] = None, |
69 | | - encodinginfo: Optional[EncodingInfo] = None, |
70 | | - filetype: Optional[str] = None) -> Tuple[Tensor, int]: |
71 | | - r"""Loads an audio file from disk into a tensor |
72 | | -
|
73 | | - Args: |
74 | | - filepath (str or Path): Path to audio file |
75 | | - out (Tensor, optional): An output tensor to use instead of creating one. (Default: ``None``) |
76 | | - normalization (bool, float, or callable, optional): If boolean `True`, then output is divided by `1 << 31` |
77 | | - (assumes signed 32-bit audio), and normalizes to `[-1, 1]`. |
78 | | - If `float`, then output is divided by that number |
79 | | - If `Callable`, then the output is passed as a parameter |
80 | | - to the given function, then the output is divided by |
81 | | - the result. (Default: ``True``) |
82 | | - channels_first (bool, optional): Set channels first or length first in result. (Default: ``True``) |
83 | | - num_frames (int, optional): Number of frames to load. 0 to load everything after the offset. |
84 | | - (Default: ``0``) |
85 | | - offset (int, optional): Number of frames from the start of the file to begin data loading. |
86 | | - (Default: ``0``) |
87 | | - signalinfo (sox_signalinfo_t, optional): A sox_signalinfo_t type, which could be helpful if the |
88 | | - audio type cannot be automatically determined. (Default: ``None``) |
89 | | - encodinginfo (sox_encodinginfo_t, optional): A sox_encodinginfo_t type, which could be set if the |
90 | | - audio type cannot be automatically determined. (Default: ``None``) |
91 | | - filetype (str, optional): A filetype or extension to be set if sox cannot determine it |
92 | | - automatically. (Default: ``None``) |
93 | | -
|
94 | | - Returns: |
95 | | - (Tensor, int): An output tensor of size `[C x L]` or `[L x C]` where L is the number |
96 | | - of audio frames and C is the number of channels. An integer which is the sample rate of the |
97 | | - audio (as listed in the metadata of the file) |
98 | | -
|
99 | | - Example |
100 | | - >>> data, sample_rate = torchaudio.load('foo.mp3') |
101 | | - >>> print(data.size()) |
102 | | - torch.Size([2, 278756]) |
103 | | - >>> print(sample_rate) |
104 | | - 44100 |
105 | | - >>> data_vol_normalized, _ = torchaudio.load('foo.mp3', normalization=lambda x: torch.abs(x).max()) |
106 | | - >>> print(data_vol_normalized.abs().max()) |
107 | | - 1. |
108 | | -
|
109 | | - """ |
110 | | - return _get_audio_backend_module().load( |
111 | | - filepath, |
112 | | - out=out, |
113 | | - normalization=normalization, |
114 | | - channels_first=channels_first, |
115 | | - num_frames=num_frames, |
116 | | - offset=offset, |
117 | | - signalinfo=signalinfo, |
118 | | - encodinginfo=encodinginfo, |
119 | | - filetype=filetype, |
120 | | - ) |
121 | | - |
122 | | - |
123 | | -def load_wav(filepath: Union[str, Path], **kwargs: Any) -> Tuple[Tensor, int]: |
124 | | - r""" Loads a wave file. It assumes that the wav file uses 16 bit per sample that needs normalization by shifting |
125 | | - the input right by 16 bits. |
126 | | -
|
127 | | - Args: |
128 | | - filepath (str or Path): Path to audio file |
129 | | -
|
130 | | - Returns: |
131 | | - (Tensor, int): An output tensor of size `[C x L]` or `[L x C]` where L is the number |
132 | | - of audio frames and C is the number of channels. An integer which is the sample rate of the |
133 | | - audio (as listed in the metadata of the file) |
134 | | - """ |
135 | | - kwargs['normalization'] = 1 << 16 |
136 | | - return load(filepath, **kwargs) |
137 | | - |
138 | | - |
139 | | -def save(filepath: str, src: Tensor, sample_rate: int, precision: int = 16, channels_first: bool = True) -> None: |
140 | | - r"""Convenience function for `save_encinfo`. |
141 | | -
|
142 | | - Args: |
143 | | - filepath (str): Path to audio file |
144 | | - src (Tensor): An input 2D tensor of shape `[C x L]` or `[L x C]` where L is |
145 | | - the number of audio frames, C is the number of channels |
146 | | - sample_rate (int): An integer which is the sample rate of the |
147 | | - audio (as listed in the metadata of the file) |
148 | | - precision (int, optional): Bit precision (Default: ``16``) |
149 | | - channels_first (bool, optional): Set channels first or length first in result. ( |
150 | | - Default: ``True``) |
151 | | - """ |
152 | | - |
153 | | - return _get_audio_backend_module().save( |
154 | | - filepath, src, sample_rate, precision=precision, channels_first=channels_first |
155 | | - ) |
156 | | - |
157 | | - |
158 | | -def info(filepath: str) -> Tuple[SignalInfo, EncodingInfo]: |
159 | | - r"""Gets metadata from an audio file without loading the signal. |
160 | | -
|
161 | | - Args: |
162 | | - filepath (str): Path to audio file |
163 | | -
|
164 | | - Returns: |
165 | | - (sox_signalinfo_t, sox_encodinginfo_t): A si (sox_signalinfo_t) signal |
166 | | - info as a python object. An ei (sox_encodinginfo_t) encoding info |
167 | | -
|
168 | | - Example |
169 | | - >>> si, ei = torchaudio.info('foo.wav') |
170 | | - >>> rate, channels, encoding = si.rate, si.channels, ei.encoding |
171 | | - """ |
172 | | - return _get_audio_backend_module().info(filepath) |
0 commit comments