Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Typing][C-125,C-126] Add type annotations for python/paddle/audio/backends/* #67002

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions python/paddle/audio/backends/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,45 @@
# 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,
num_samples: int,
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
self.bits_per_sample = bits_per_sample
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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.

Expand Down
11 changes: 6 additions & 5 deletions python/paddle/audio/backends/init_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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))
26 changes: 16 additions & 10 deletions python/paddle/audio/backends/wave_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,22 @@
# 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

import paddle

from .backend import AudioInfo

if TYPE_CHECKING:
from pathlib import Path

from paddle import Tensor


def _error_message():
package = "paddleaudio"
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.

Expand Down