Commit d6dd497c authored by moto's avatar moto Committed by Facebook GitHub Bot
Browse files

Update and deprecate apply_codec function (#3386)

Summary:
To prepare for the upcoming removal of file-like object support from sox_io backend,
this commit changes apply_codec function to use tempfile.

`apply_codec` function is now deprecated and users are encourated to use `torchaudio.io.AudioEffector`.
We will not remove the function itself, but will remove the entry from the doc.

Pull Request resolved: https://github.com/pytorch/audio/pull/3386

Reviewed By: hwangjeff

Differential Revision: D46330610

Pulled By: mthrok

fbshipit-source-id: 3071bdefa05b4cbb9f00629bef50f0981eae89b4
parent d5d94b7e
......@@ -40,7 +40,7 @@ def requires_module(*modules: str):
return decorator
def deprecated(direction: str, version: Optional[str] = None):
def deprecated(direction: str, version: Optional[str] = None, remove: bool = False):
"""Decorator to add deprecation message
Args:
......@@ -51,11 +51,9 @@ def deprecated(direction: str, version: Optional[str] = None):
def decorator(func):
@wraps(func)
def wrapped(*args, **kwargs):
message = (
f"{func.__module__}.{func.__name__} has been deprecated "
f'and will be removed from {"future" if version is None else version} release. '
f"{direction}"
)
message = f"{func.__module__}.{func.__name__} has been deprecated. {direction}"
if remove:
message += f' It will be removed from {"future" if version is None else version} release. '
warnings.warn(message, stacklevel=2)
return func(*args, **kwargs)
......
# -*- coding: utf-8 -*-
import io
import math
import tempfile
import warnings
from collections.abc import Sequence
from typing import List, Optional, Tuple, Union
......@@ -10,6 +10,7 @@ import torch
import torchaudio
from torch import Tensor
from torchaudio._extension import fail_if_no_align
from torchaudio._internal.module_utils import deprecated
from .filtering import highpass_biquad, treble_biquad
......@@ -1290,6 +1291,7 @@ def spectral_centroid(
@torchaudio._extension.fail_if_no_sox
@deprecated("Please migrate to torchaudio.io.AudioEffector.", remove=False)
def apply_codec(
waveform: Tensor,
sample_rate: int,
......@@ -1304,6 +1306,12 @@ def apply_codec(
.. devices:: CPU
.. warnings::
This function has been deprecated.
Please migrate to :py:class:`torchaudio.io.AudioEffector`, which works on all platforms,
and supports streaming processing.
Args:
waveform (Tensor): Audio data. Must be 2 dimensional. See also ```channels_first```.
sample_rate (int): Sample rate of the audio waveform.
......@@ -1322,12 +1330,12 @@ def apply_codec(
Tensor: Resulting Tensor.
If ``channels_first=True``, it has `(channel, time)` else `(time, channel)`.
"""
bytes = io.BytesIO()
torchaudio.backend.sox_io_backend.save(
bytes, waveform, sample_rate, channels_first, compression, format, encoding, bits_per_sample
)
bytes.seek(0)
augmented, sr = torchaudio.backend.sox_io_backend.load(bytes, channels_first=channels_first, format=format)
with tempfile.TemporaryFile() as f:
torchaudio.backend.sox_io_backend.save(
f, waveform, sample_rate, channels_first, compression, format, encoding, bits_per_sample
)
f.seek(0)
augmented, sr = torchaudio.backend.sox_io_backend.load(f, channels_first=channels_first, format=format)
if sr != sample_rate:
augmented = resample(augmented, sr, sample_rate)
return augmented
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment