chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 12:40:42 +08:00
commit e25996e7db
15472 changed files with 3536181 additions and 0 deletions
+27
View File
@@ -0,0 +1,27 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from . import init_backend
from .init_backend import (
get_current_backend,
list_available_backends,
set_backend,
)
init_backend._init_set_audio_backend()
__all__ = [
'get_current_backend',
'list_available_backends',
'set_backend',
]
+164
View File
@@ -0,0 +1,164 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License
from __future__ import annotations
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 | BinaryIO) -> AudioInfo:
"""Get signal information of input audio file.
Args:
filepath: audio path or file object.
Returns:
AudioInfo: info of the given audio.
Example:
.. code-block:: pycon
>>> import os
>>> import paddle
>>> sample_rate = 16000
>>> wav_duration = 0.5
>>> num_channels = 1
>>> num_frames = sample_rate * wav_duration
>>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
>>> waveform = wav_data.tile([num_channels, 1])
>>> base_dir = os.getcwd()
>>> filepath = os.path.join(base_dir, "test.wav")
>>> paddle.audio.save(filepath, waveform, sample_rate)
>>> wav_info = paddle.audio.info(filepath)
"""
# for API doc
raise NotImplementedError("please set audio backend")
def load(
filepath: str | Path,
frame_offset: int = 0,
num_frames: int = -1,
normalize: bool = True,
channels_first: bool = True,
) -> tuple[Tensor, int]:
"""Load audio data from file.Load the audio content start form frame_offset, and get num_frames.
Args:
frame_offset: from 0 to total frames,
num_frames: from -1 (means total frames) or number frames which want to read,
normalize:
if True: return audio which norm to (-1, 1), dtype=float32
if False: return audio with raw data, dtype=int16
channels_first:
if True: return audio with shape (channels, time)
Return:
Tuple[paddle.Tensor, int]: (audio_content, sample rate)
Examples:
.. code-block:: pycon
>>> import os
>>> import paddle
>>> sample_rate = 16000
>>> wav_duration = 0.5
>>> num_channels = 1
>>> num_frames = sample_rate * wav_duration
>>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
>>> waveform = wav_data.tile([num_channels, 1])
>>> base_dir = os.getcwd()
>>> filepath = os.path.join(base_dir, "test.wav")
>>> paddle.audio.save(filepath, waveform, sample_rate)
>>> wav_data_read, sr = paddle.audio.load(filepath)
"""
# for API doc
raise NotImplementedError("please set audio backend")
def save(
filepath: str,
src: Tensor,
sample_rate: int,
channels_first: bool = True,
encoding: str | None = None,
bits_per_sample: int | None = 16,
) -> None:
"""
Save audio tensor to file.
Args:
filepath: saved path
src: the audio tensor
sample_rate: the number of samples of audio per second.
channels_first: src channel information
if True, means input tensor is (channels, time)
if False, means input tensor is (time, channels)
encoding:encoding format, wave_backend only support PCM16 now.
bits_per_sample: bits per sample, wave_backend only support 16 bits now.
Returns:
None
Examples:
.. code-block:: pycon
>>> import paddle
>>> sample_rate = 16000
>>> wav_duration = 0.5
>>> num_channels = 1
>>> num_frames = sample_rate * wav_duration
>>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
>>> waveform = wav_data.tile([num_channels, 1])
>>> filepath = "./test.wav"
>>> paddle.audio.save(filepath, waveform, sample_rate)
"""
# for API doc
raise NotImplementedError("please set audio backend")
@@ -0,0 +1,192 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import sys
import warnings
import paddle
from . import backend, wave_backend
def _check_version(version: str) -> bool:
# require paddleaudio >= 1.0.2
ver_arr = version.split('.')
v0 = int(ver_arr[0])
v1 = int(ver_arr[1])
v2 = int(ver_arr[2])
if v0 < 1:
return False
if v0 == 1 and v1 == 0 and v2 <= 1:
return False
return True
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.
Examples:
.. code-block:: pycon
>>> import paddle
>>> sample_rate = 16000
>>> wav_duration = 0.5
>>> num_channels = 1
>>> num_frames = sample_rate * wav_duration
>>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
>>> waveform = wav_data.tile([num_channels, 1])
>>> wav_path = "./test.wav"
>>> current_backend = paddle.audio.backends.get_current_backend()
>>> print(current_backend)
wave_backend
>>> backends = paddle.audio.backends.list_available_backends()
>>> # default backends is ['wave_backend']
>>> # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2
>>> if 'soundfile' in backends:
... paddle.audio.backends.set_backend('soundfile')
>>> paddle.audio.save(wav_path, waveform, sample_rate)
"""
backends = []
try:
import paddleaudio
except ImportError:
package = "paddleaudio"
warn_msg = (
f"Failed importing {package}. \n"
"only wave_backend(only can deal with PCM16 WAV) supported.\n"
"if want soundfile_backend(more audio type supported),\n"
f"please manually installed (usually with `pip install {package} >= 1.0.2`). "
)
warnings.warn(warn_msg)
if "paddleaudio" in sys.modules:
version = paddleaudio.__version__
if not _check_version(version):
err_msg = (
f"the version of paddleaudio installed is {version},\n"
"please ensure the paddleaudio >= 1.0.2."
)
raise ImportError(err_msg)
backends = paddleaudio.backends.list_audio_backends()
backends.append("wave_backend")
return backends
def get_current_backend() -> str:
"""Get the name of the current audio backend
Returns:
str: The name of the current backend,
the wave_backend or backend imported from paddleaudio
Examples:
.. code-block:: pycon
>>> import paddle
>>> sample_rate = 16000
>>> wav_duration = 0.5
>>> num_channels = 1
>>> num_frames = sample_rate * wav_duration
>>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
>>> waveform = wav_data.tile([num_channels, 1])
>>> wav_path = "./test.wav"
>>> current_backend = paddle.audio.backends.get_current_backend()
>>> print(current_backend)
wave_backend
>>> backends = paddle.audio.backends.list_available_backends()
>>> # default backends is ['wave_backend']
>>> # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2
>>> if 'soundfile' in backends:
... paddle.audio.backends.set_backend('soundfile')
>>> paddle.audio.save(wav_path, waveform, sample_rate)
"""
current_backend = None
if "paddleaudio" in sys.modules:
import paddleaudio
current_backend = paddleaudio.backends.get_audio_backend()
if paddle.audio.load == paddleaudio.load:
return current_backend
return "wave_backend"
def set_backend(backend_name: str) -> None:
"""Set the backend by one of the list_audio_backend return.
Args:
backend (str): one of the list_audio_backend. "wave_backend" is the default. "soundfile" imported from paddleaudio.
Returns:
None
Examples:
.. code-block:: pycon
>>> import paddle
>>> sample_rate = 16000
>>> wav_duration = 0.5
>>> num_channels = 1
>>> num_frames = sample_rate * wav_duration
>>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
>>> waveform = wav_data.tile([num_channels, 1])
>>> wav_path = "./test.wav"
>>> current_backend = paddle.audio.backends.get_current_backend()
>>> print(current_backend)
wave_backend
>>> backends = paddle.audio.backends.list_available_backends()
>>> # default backends is ['wave_backend']
>>> # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2
>>> if 'soundfile' in backends:
... paddle.audio.backends.set_backend('soundfile')
>>> paddle.audio.save(wav_path, waveform, sample_rate)
"""
if backend_name not in list_available_backends():
raise NotImplementedError
if backend_name == "wave_backend":
module = wave_backend
else:
import paddleaudio
paddleaudio.backends.set_audio_backend(backend_name)
module = paddleaudio
for func in ["save", "load", "info"]:
setattr(backend, func, getattr(module, func))
setattr(paddle.audio, func, getattr(module, func))
def _init_set_audio_backend() -> None:
# init the default wave_backend.
for func in ["save", "load", "info"]:
setattr(backend, func, getattr(wave_backend, func))
@@ -0,0 +1,236 @@
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import annotations
import wave
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"
warn_msg = (
"only PCM16 WAV supported. \n"
"if want support more other audio types, please "
f"manually installed (usually with `pip install {package}`). \n "
"and use paddle.audio.backends.set_backend('soundfile') to set audio backend"
)
return warn_msg
def info(filepath: str | BinaryIO) -> AudioInfo:
"""Get signal information of input audio file.
Args:
filepath: audio path or file object.
Returns:
AudioInfo: info of the given audio.
Example:
.. code-block:: pycon
>>> import os
>>> import paddle
>>> sample_rate = 16000
>>> wav_duration = 0.5
>>> num_channels = 1
>>> num_frames = sample_rate * wav_duration
>>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
>>> waveform = wav_data.tile([num_channels, 1])
>>> base_dir = os.getcwd()
>>> filepath = os.path.join(base_dir, "test.wav")
>>> paddle.audio.save(filepath, waveform, sample_rate)
>>> wav_info = paddle.audio.info(filepath)
"""
if hasattr(filepath, 'read'):
file_obj = filepath
else:
file_obj = open(filepath, 'rb')
try:
file_ = wave.open(file_obj)
except wave.Error:
file_obj.seek(0)
file_obj.close()
err_msg = _error_message()
raise NotImplementedError(err_msg)
channels = file_.getnchannels()
sample_rate = file_.getframerate()
sample_frames = file_.getnframes() # audio frame
bits_per_sample = file_.getsampwidth() * 8
encoding = "PCM_S" # default WAV encoding, only support
file_obj.close()
return AudioInfo(
sample_rate, sample_frames, channels, bits_per_sample, encoding
)
def load(
filepath: str | Path,
frame_offset: int = 0,
num_frames: int = -1,
normalize: bool = True,
channels_first: bool = True,
) -> tuple[Tensor, int]:
"""Load audio data from file. load the audio content start form frame_offset, and get num_frames.
Args:
frame_offset: from 0 to total frames,
num_frames: from -1 (means total frames) or number frames which want to read,
normalize:
if True: return audio which norm to (-1, 1), dtype=float32
if False: return audio with raw data, dtype=int16
channels_first:
if True: return audio with shape (channels, time)
Return:
Tuple[paddle.Tensor, int]: (audio_content, sample rate)
Examples:
.. code-block:: pycon
>>> import os
>>> import paddle
>>> sample_rate = 16000
>>> wav_duration = 0.5
>>> num_channels = 1
>>> num_frames = sample_rate * wav_duration
>>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
>>> waveform = wav_data.tile([num_channels, 1])
>>> base_dir = os.getcwd()
>>> filepath = os.path.join(base_dir, "test.wav")
>>> paddle.audio.save(filepath, waveform, sample_rate)
>>> wav_data_read, sr = paddle.audio.load(filepath)
"""
if hasattr(filepath, 'read'):
file_obj = filepath
else:
file_obj = open(filepath, 'rb')
try:
file_ = wave.open(file_obj)
except wave.Error:
file_obj.seek(0)
file_obj.close()
err_msg = _error_message()
raise NotImplementedError(err_msg)
channels = file_.getnchannels()
sample_rate = file_.getframerate()
frames = file_.getnframes() # audio frame
audio_content = file_.readframes(frames)
file_obj.close()
# default_subtype = "PCM_16", only support PCM16 WAV
audio_as_np16 = np.frombuffer(audio_content, dtype=np.int16)
audio_as_np32 = audio_as_np16.astype(np.float32)
if normalize:
# dtype = "float32"
audio_norm = audio_as_np32 / (2**15)
else:
# dtype = "int16"
audio_norm = audio_as_np32
waveform = np.reshape(audio_norm, (frames, channels))
if num_frames != -1:
waveform = waveform[frame_offset : frame_offset + num_frames, :]
waveform = paddle.to_tensor(waveform)
if channels_first:
waveform = paddle.transpose(waveform, perm=[1, 0])
return waveform, sample_rate
def save(
filepath: str,
src: Tensor,
sample_rate: int,
channels_first: bool = True,
encoding: str | None = None,
bits_per_sample: int | None = 16,
) -> None:
"""
Save audio tensor to file.
Args:
filepath: saved path
src: the audio tensor
sample_rate: the number of samples of audio per second.
channels_first: src channel information
if True, means input tensor is (channels, time)
if False, means input tensor is (time, channels)
encoding: audio encoding format, wave_backend only support PCM16 now.
bits_per_sample: bits per sample, wave_backend only support 16 bits now.
Returns:
None
Examples:
.. code-block:: pycon
>>> import paddle
>>> sample_rate = 16000
>>> wav_duration = 0.5
>>> num_channels = 1
>>> num_frames = sample_rate * wav_duration
>>> wav_data = paddle.linspace(-1.0, 1.0, int(num_frames)) * 0.1
>>> waveform = wav_data.tile([num_channels, 1])
>>> filepath = "./test.wav"
>>> paddle.audio.save(filepath, waveform, sample_rate)
"""
assert src.ndim == 2, "Expected 2D tensor"
audio_numpy = src.numpy()
# change src shape to (time, channels)
if channels_first:
audio_numpy = np.transpose(audio_numpy)
channels = audio_numpy.shape[1]
# only support PCM16
if bits_per_sample not in (None, 16):
raise ValueError("Invalid bits_per_sample, only support 16 bit")
sample_width = int(bits_per_sample / 8) # 2
if src.dtype == paddle.float32:
audio_numpy = (audio_numpy * (2**15)).astype("<h")
with wave.open(filepath, 'w') as f:
f.setnchannels(channels)
f.setsampwidth(sample_width)
f.setframerate(sample_rate)
f.writeframes(audio_numpy.tobytes())