"docs/en/git@developer.sourcefind.cn:OpenDAS/mmcv.git" did not exist on "a364e6cad24119e036e8cd4aa8dcc24fbb84ee6d"
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): ...@@ -40,7 +40,7 @@ def requires_module(*modules: str):
return decorator 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 """Decorator to add deprecation message
Args: Args:
...@@ -51,11 +51,9 @@ def deprecated(direction: str, version: Optional[str] = None): ...@@ -51,11 +51,9 @@ def deprecated(direction: str, version: Optional[str] = None):
def decorator(func): def decorator(func):
@wraps(func) @wraps(func)
def wrapped(*args, **kwargs): def wrapped(*args, **kwargs):
message = ( message = f"{func.__module__}.{func.__name__} has been deprecated. {direction}"
f"{func.__module__}.{func.__name__} has been deprecated " if remove:
f'and will be removed from {"future" if version is None else version} release. ' message += f' It will be removed from {"future" if version is None else version} release. '
f"{direction}"
)
warnings.warn(message, stacklevel=2) warnings.warn(message, stacklevel=2)
return func(*args, **kwargs) return func(*args, **kwargs)
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import io
import math import math
import tempfile
import warnings import warnings
from collections.abc import Sequence from collections.abc import Sequence
from typing import List, Optional, Tuple, Union from typing import List, Optional, Tuple, Union
...@@ -10,6 +10,7 @@ import torch ...@@ -10,6 +10,7 @@ import torch
import torchaudio import torchaudio
from torch import Tensor from torch import Tensor
from torchaudio._extension import fail_if_no_align from torchaudio._extension import fail_if_no_align
from torchaudio._internal.module_utils import deprecated
from .filtering import highpass_biquad, treble_biquad from .filtering import highpass_biquad, treble_biquad
...@@ -1290,6 +1291,7 @@ def spectral_centroid( ...@@ -1290,6 +1291,7 @@ def spectral_centroid(
@torchaudio._extension.fail_if_no_sox @torchaudio._extension.fail_if_no_sox
@deprecated("Please migrate to torchaudio.io.AudioEffector.", remove=False)
def apply_codec( def apply_codec(
waveform: Tensor, waveform: Tensor,
sample_rate: int, sample_rate: int,
...@@ -1304,6 +1306,12 @@ def apply_codec( ...@@ -1304,6 +1306,12 @@ def apply_codec(
.. devices:: CPU .. 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: Args:
waveform (Tensor): Audio data. Must be 2 dimensional. See also ```channels_first```. waveform (Tensor): Audio data. Must be 2 dimensional. See also ```channels_first```.
sample_rate (int): Sample rate of the audio waveform. sample_rate (int): Sample rate of the audio waveform.
...@@ -1322,12 +1330,12 @@ def apply_codec( ...@@ -1322,12 +1330,12 @@ def apply_codec(
Tensor: Resulting Tensor. Tensor: Resulting Tensor.
If ``channels_first=True``, it has `(channel, time)` else `(time, channel)`. If ``channels_first=True``, it has `(channel, time)` else `(time, channel)`.
""" """
bytes = io.BytesIO() with tempfile.TemporaryFile() as f:
torchaudio.backend.sox_io_backend.save( torchaudio.backend.sox_io_backend.save(
bytes, waveform, sample_rate, channels_first, compression, format, encoding, bits_per_sample f, waveform, sample_rate, channels_first, compression, format, encoding, bits_per_sample
) )
bytes.seek(0) f.seek(0)
augmented, sr = torchaudio.backend.sox_io_backend.load(bytes, channels_first=channels_first, format=format) augmented, sr = torchaudio.backend.sox_io_backend.load(f, channels_first=channels_first, format=format)
if sr != sample_rate: if sr != sample_rate:
augmented = resample(augmented, sr, sample_rate) augmented = resample(augmented, sr, sample_rate)
return augmented 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