Spaces:
Runtime error
Runtime error
Update audiocraft/data/audio.py
Browse files- audiocraft/data/audio.py +4 -16
audiocraft/data/audio.py
CHANGED
@@ -152,18 +152,15 @@ def audio_read(filepath: tp.Union[str, Path], seek_time: float = 0.,
|
|
152 |
|
153 |
def audio_write(stem_name: tp.Union[str, Path],
|
154 |
wav: torch.Tensor, sample_rate: int,
|
155 |
-
|
156 |
strategy: str = 'peak', peak_clip_headroom_db: float = 1,
|
157 |
rms_headroom_db: float = 18, loudness_headroom_db: float = 14,
|
158 |
loudness_compressor: bool = False,
|
159 |
log_clipping: bool = True, make_parent_dir: bool = True,
|
160 |
add_suffix: bool = True) -> Path:
|
161 |
"""Convenience function for saving audio to disk. Returns the filename the audio was written to.
|
162 |
-
|
163 |
Args:
|
164 |
stem_name (str or Path): Filename without extension which will be added automatically.
|
165 |
-
format (str): Either "wav" or "mp3".
|
166 |
-
mp3_rate (int): kbps when using mp3s.
|
167 |
normalize (bool): if `True` (default), normalizes according to the prescribed
|
168 |
strategy (see after). If `False`, the strategy is only used in case clipping
|
169 |
would happen.
|
@@ -175,7 +172,7 @@ def audio_write(stem_name: tp.Union[str, Path],
|
|
175 |
than the `peak_clip` one to avoid further clipping.
|
176 |
loudness_headroom_db (float): Target loudness for loudness normalization.
|
177 |
loudness_compressor (bool): Uses tanh for soft clipping when strategy is 'loudness'.
|
178 |
-
|
179 |
occurs despite strategy (only for 'rms').
|
180 |
make_parent_dir (bool): Make parent directory if it doesn't exist.
|
181 |
Returns:
|
@@ -190,23 +187,14 @@ def audio_write(stem_name: tp.Union[str, Path],
|
|
190 |
wav = normalize_audio(wav, normalize, strategy, peak_clip_headroom_db,
|
191 |
rms_headroom_db, loudness_headroom_db, log_clipping=log_clipping,
|
192 |
sample_rate=sample_rate, stem_name=str(stem_name))
|
193 |
-
|
194 |
-
if format == 'mp3':
|
195 |
-
suffix = '.mp3'
|
196 |
-
kwargs.update({"compression": mp3_rate})
|
197 |
-
elif format == 'wav':
|
198 |
-
wav = i16_pcm(wav)
|
199 |
-
suffix = '.wav'
|
200 |
-
kwargs.update({"encoding": "PCM_S", "bits_per_sample": 16})
|
201 |
-
else:
|
202 |
-
raise RuntimeError(f"Invalid format {format}. Only wav or mp3 are supported.")
|
203 |
if not add_suffix:
|
204 |
suffix = ''
|
205 |
path = Path(str(stem_name) + suffix)
|
206 |
if make_parent_dir:
|
207 |
path.parent.mkdir(exist_ok=True, parents=True)
|
208 |
try:
|
209 |
-
ta.save(path, wav, sample_rate
|
210 |
except Exception:
|
211 |
if path.exists():
|
212 |
# we do not want to leave half written files around.
|
|
|
152 |
|
153 |
def audio_write(stem_name: tp.Union[str, Path],
|
154 |
wav: torch.Tensor, sample_rate: int,
|
155 |
+
normalize: bool = True,
|
156 |
strategy: str = 'peak', peak_clip_headroom_db: float = 1,
|
157 |
rms_headroom_db: float = 18, loudness_headroom_db: float = 14,
|
158 |
loudness_compressor: bool = False,
|
159 |
log_clipping: bool = True, make_parent_dir: bool = True,
|
160 |
add_suffix: bool = True) -> Path:
|
161 |
"""Convenience function for saving audio to disk. Returns the filename the audio was written to.
|
|
|
162 |
Args:
|
163 |
stem_name (str or Path): Filename without extension which will be added automatically.
|
|
|
|
|
164 |
normalize (bool): if `True` (default), normalizes according to the prescribed
|
165 |
strategy (see after). If `False`, the strategy is only used in case clipping
|
166 |
would happen.
|
|
|
172 |
than the `peak_clip` one to avoid further clipping.
|
173 |
loudness_headroom_db (float): Target loudness for loudness normalization.
|
174 |
loudness_compressor (bool): Uses tanh for soft clipping when strategy is 'loudness'.
|
175 |
+
log_clipping (bool): If True, basic logging on stderr when clipping still
|
176 |
occurs despite strategy (only for 'rms').
|
177 |
make_parent_dir (bool): Make parent directory if it doesn't exist.
|
178 |
Returns:
|
|
|
187 |
wav = normalize_audio(wav, normalize, strategy, peak_clip_headroom_db,
|
188 |
rms_headroom_db, loudness_headroom_db, log_clipping=log_clipping,
|
189 |
sample_rate=sample_rate, stem_name=str(stem_name))
|
190 |
+
suffix = '.wav'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
if not add_suffix:
|
192 |
suffix = ''
|
193 |
path = Path(str(stem_name) + suffix)
|
194 |
if make_parent_dir:
|
195 |
path.parent.mkdir(exist_ok=True, parents=True)
|
196 |
try:
|
197 |
+
ta.save(path, wav, sample_rate)
|
198 |
except Exception:
|
199 |
if path.exists():
|
200 |
# we do not want to leave half written files around.
|