diff --git a/python/paddle/audio/backends/backend.py b/python/paddle/audio/backends/backend.py index 87fd217ee37ab5..13afa8fd786d4e 100644 --- a/python/paddle/audio/backends/backend.py +++ b/python/paddle/audio/backends/backend.py @@ -12,15 +12,25 @@ # See the License for the specific language governing permissions and # limitations under the License -from pathlib import Path -from typing import Optional, Tuple, Union +from __future__ import annotations -import paddle +from typing import TYPE_CHECKING, BinaryIO + +if TYPE_CHECKING: + from pathlib import Path + + from paddle import Tensor class AudioInfo: """Audio info, return type of backend info function""" + sample_rate: int + num_samples: int + num_channels: int + bits_per_sample: int + encoding: str + def __init__( self, sample_rate: int, @@ -28,7 +38,7 @@ def __init__( num_channels: int, bits_per_sample: int, encoding: str, - ): + ) -> None: self.sample_rate = sample_rate self.num_samples = num_samples self.num_channels = num_channels @@ -36,11 +46,11 @@ def __init__( self.encoding = encoding -def info(filepath: str) -> AudioInfo: +def info(filepath: str | BinaryIO) -> AudioInfo: """Get signal information of input audio file. Args: - filepath: audio path or file object. + filepath: audio path or file object. Returns: AudioInfo: info of the given audio. @@ -68,12 +78,12 @@ def info(filepath: str) -> AudioInfo: def load( - filepath: Union[str, Path], + filepath: str | Path, frame_offset: int = 0, num_frames: int = -1, normalize: bool = True, channels_first: bool = True, -) -> Tuple[paddle.Tensor, int]: +) -> tuple[Tensor, int]: """Load audio data from file.Load the audio content start form frame_offset, and get num_frames. Args: @@ -113,12 +123,12 @@ def load( def save( filepath: str, - src: paddle.Tensor, + src: Tensor, sample_rate: int, channels_first: bool = True, - encoding: Optional[str] = None, - bits_per_sample: Optional[int] = 16, -): + encoding: str | None = None, + bits_per_sample: int | None = 16, +) -> None: """ Save audio tensor to file. diff --git a/python/paddle/audio/backends/init_backend.py b/python/paddle/audio/backends/init_backend.py index ada7e21dc3e60c..d3bad4a283a1f6 100644 --- a/python/paddle/audio/backends/init_backend.py +++ b/python/paddle/audio/backends/init_backend.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import sys import warnings -from typing import List import paddle @@ -34,11 +35,11 @@ def _check_version(version: str) -> bool: return True -def list_available_backends() -> List[str]: +def list_available_backends() -> list[str]: """List available backends, the backends in paddleaudio and the default backend. Returns: - List[str]: The list of available backends. + list[str]: The list of available backends. Examples: .. code-block:: python @@ -136,7 +137,7 @@ def get_current_backend() -> str: return "wave_backend" -def set_backend(backend_name: str): +def set_backend(backend_name: str) -> None: """Set the backend by one of the list_audio_backend return. Args: @@ -188,7 +189,7 @@ def set_backend(backend_name: str): setattr(paddle.audio, func, getattr(module, func)) -def _init_set_audio_backend(): +def _init_set_audio_backend() -> None: # init the default wave_backend. for func in ["save", "load", "info"]: setattr(backend, func, getattr(wave_backend, func)) diff --git a/python/paddle/audio/backends/wave_backend.py b/python/paddle/audio/backends/wave_backend.py index 08e107ed3c8dcf..4b3c8f3eaee787 100644 --- a/python/paddle/audio/backends/wave_backend.py +++ b/python/paddle/audio/backends/wave_backend.py @@ -12,9 +12,10 @@ # See the License for the specific language governing permissions and # limitations under the License. +from __future__ import annotations + import wave -from pathlib import Path -from typing import Optional, Tuple, Union +from typing import TYPE_CHECKING, BinaryIO import numpy as np @@ -22,6 +23,11 @@ from .backend import AudioInfo +if TYPE_CHECKING: + from pathlib import Path + + from paddle import Tensor + def _error_message(): package = "paddleaudio" @@ -34,11 +40,11 @@ def _error_message(): return warn_msg -def info(filepath: str) -> AudioInfo: +def info(filepath: str | BinaryIO) -> AudioInfo: """Get signal information of input audio file. Args: - filepath: audio path or file object. + filepath: audio path or file object. Returns: AudioInfo: info of the given audio. @@ -87,12 +93,12 @@ def info(filepath: str) -> AudioInfo: def load( - filepath: Union[str, Path], + filepath: str | Path, frame_offset: int = 0, num_frames: int = -1, normalize: bool = True, channels_first: bool = True, -) -> Tuple[paddle.Tensor, int]: +) -> tuple[Tensor, int]: """Load audio data from file. load the audio content start form frame_offset, and get num_frames. Args: @@ -167,12 +173,12 @@ def load( def save( filepath: str, - src: paddle.Tensor, + src: Tensor, sample_rate: int, channels_first: bool = True, - encoding: Optional[str] = None, - bits_per_sample: Optional[int] = 16, -): + encoding: str | None = None, + bits_per_sample: int | None = 16, +) -> None: """ Save audio tensor to file.