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

add paddle audio dataset && backend #45939

Merged
merged 131 commits into from Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 125 commits
Commits
Show all changes
131 commits
Select commit Hold shift + click to select a range
f792267
add audio feature dataset
SmileGoat Aug 25, 2022
6a678ae
fix coding style
SmileGoat Aug 25, 2022
ad8e05d
fix coding style2
SmileGoat Aug 25, 2022
725506d
rm librosa
SmileGoat Aug 26, 2022
e768d9b
rm voxceleb
SmileGoat Aug 26, 2022
cf2a687
rm librosa in test
SmileGoat Aug 26, 2022
b1833ba
add scipy fftpack
SmileGoat Aug 26, 2022
417b5a5
add functional
SmileGoat Aug 26, 2022
9d6b69c
fix setup
SmileGoat Aug 26, 2022
0a98960
fix setup2
SmileGoat Aug 26, 2022
6da8584
rm colorlog
SmileGoat Aug 26, 2022
0b55b7a
refactor dataset __init__.py
SmileGoat Aug 27, 2022
0f1344d
fix converage
SmileGoat Aug 27, 2022
9219a55
fix librosa import error
SmileGoat Aug 27, 2022
2198443
fix windows test
SmileGoat Aug 27, 2022
fcfa6be
fix windows ci
SmileGoat Aug 27, 2022
7318db4
rm datasets
SmileGoat Aug 28, 2022
d446d92
fix setup
SmileGoat Aug 28, 2022
c4b34a6
Merge branch 'develop' of github.com:SmileGoat/Paddle into dataset_pa…
SmileGoat Aug 28, 2022
eaa5ceb
remove testdata
SmileGoat Aug 29, 2022
b83cc0c
add librosa in requirement
SmileGoat Aug 29, 2022
057711b
add librosa in requirement2
SmileGoat Aug 29, 2022
93ff919
change librosa to 0.8.1
SmileGoat Aug 29, 2022
f308d4d
update ci docker
SmileGoat Aug 30, 2022
6b95712
fix ci error
SmileGoat Aug 30, 2022
5fbae14
fix ci error2
SmileGoat Aug 30, 2022
1782cb7
fix ci coverage
SmileGoat Aug 31, 2022
57c77d4
fix converage
SmileGoat Aug 31, 2022
1b1d84a
fix coverage
SmileGoat Aug 31, 2022
b7e3d4d
rm audio_base in test, notest,test=coverage
SmileGoat Sep 1, 2022
cf071c2
fix copyright
SmileGoat Sep 1, 2022
8ae3226
rm backend
SmileGoat Sep 1, 2022
5dab485
add datast in __init__
SmileGoat Sep 2, 2022
15b789d
rm compliance&&add function test
SmileGoat Sep 4, 2022
3ac7c7b
fix setup
SmileGoat Sep 4, 2022
cce788b
fix windows
SmileGoat Sep 4, 2022
ba67011
fix windows2
SmileGoat Sep 5, 2022
38247c4
rm compliance
SmileGoat Sep 5, 2022
edb0872
fix test timeout
SmileGoat Sep 5, 2022
e5d9ef5
Merge branch 'dataset_paddle' into audio_backend_paddle2
SmileGoat Sep 5, 2022
54da906
add backend & datasets
SmileGoat Sep 5, 2022
8b5ab74
fix bugs
SmileGoat Sep 5, 2022
ede2da7
fix ci time issue
SmileGoat Sep 5, 2022
e698099
Merge branch 'dataset_paddle' into audio_backend_paddle2
SmileGoat Sep 5, 2022
85fd94c
add dataset test
SmileGoat Sep 5, 2022
bc23946
rm test_audio_feature
SmileGoat Sep 5, 2022
c2a86d9
avoid windows isssue, tmp
SmileGoat Sep 6, 2022
4914c02
note windows isssue
SmileGoat Sep 6, 2022
e5fa7eb
Merge branch 'dataset_paddle' into audio_backend_paddle2
SmileGoat Sep 6, 2022
0f5f928
skip windows issue
SmileGoat Sep 6, 2022
97c2e93
refactor dataset test
SmileGoat Sep 6, 2022
a9de571
Merge branch 'dataset_paddle' into audio_backend_paddle2
SmileGoat Sep 6, 2022
2c5ad2b
Merge branch 'develop' of github.com:SmileGoat/Paddle into audio_back…
SmileGoat Sep 6, 2022
b194c09
add dataset.py
SmileGoat Sep 6, 2022
5dfd340
fix dtype in layers.mfcc
SmileGoat Sep 6, 2022
d0ad3cb
Merge branch 'dataset_paddle' into audio_backend_paddle2
SmileGoat Sep 6, 2022
abd2df6
fix ci-static-check
SmileGoat Sep 6, 2022
e604a0f
fix dtype in layers.mfcc && fix ci-static-check
SmileGoat Sep 6, 2022
79c387a
Merge branch 'develop' of github.com:SmileGoat/Paddle into dataset_pa…
SmileGoat Sep 6, 2022
7e17b54
add relative accuracy
SmileGoat Sep 7, 2022
ca562be
modity API.spec
SmileGoat Sep 8, 2022
0678a3c
skip cuda11.2 test
SmileGoat Sep 8, 2022
ab40ff0
skip cuda11.2 test2
SmileGoat Sep 8, 2022
2faf87d
skip cuda11.2
SmileGoat Sep 8, 2022
0f9f531
rm dataset test
SmileGoat Sep 8, 2022
ac4c6bc
change dataset name
SmileGoat Sep 8, 2022
4cc9a8d
merge develop
SmileGoat Sep 9, 2022
dbaa398
fix format
SmileGoat Sep 9, 2022
d6a027d
update api.spec
SmileGoat Sep 9, 2022
ba611f3
update api.spec2
SmileGoat Sep 9, 2022
a7fdd78
fix coverage
SmileGoat Sep 9, 2022
c0aeb92
Merge branch 'develop' of github.com:SmileGoat/Paddle into audio_back…
SmileGoat Sep 9, 2022
0469385
add dataset test
SmileGoat Sep 9, 2022
3592058
rm download load dict
SmileGoat Sep 9, 2022
1f9bff7
rm download load dict in init
SmileGoat Sep 9, 2022
0e8fd14
update api.spec3
SmileGoat Sep 9, 2022
1b88963
fix dataset coverage
SmileGoat Sep 10, 2022
d82dea9
fix coverage
SmileGoat Sep 10, 2022
050dea1
fix coverage2
SmileGoat Sep 10, 2022
bb66649
restore api.spec
SmileGoat Sep 10, 2022
e3dcce6
restore api.spec2
SmileGoat Sep 10, 2022
1479346
Merge branch 'develop' of github.com:SmileGoat/Paddle into audio_data…
SmileGoat Sep 10, 2022
c332d78
fix api-spec 3
SmileGoat Sep 10, 2022
048fe8e
fix api-spec 4
SmileGoat Sep 10, 2022
4ffe233
fix api.spec
SmileGoat Sep 13, 2022
13efe0b
fix api.spec6
SmileGoat Sep 13, 2022
b3c4837
refactor init_backend
SmileGoat Sep 13, 2022
0726233
fix typo
SmileGoat Sep 13, 2022
009ee33
change paddleaudio backend set
SmileGoat Sep 19, 2022
9e896bd
fix get_current_audio_backend()
SmileGoat Sep 19, 2022
a7d0fb8
fix format
SmileGoat Sep 19, 2022
dca47da
fix format2
SmileGoat Sep 19, 2022
707578a
remove format in parameters
SmileGoat Sep 20, 2022
40963a7
fix format2
SmileGoat Sep 20, 2022
5499d32
add warning massage in wave_backend && remove redundant audio util
SmileGoat Sep 21, 2022
122b832
merge develop
SmileGoat Sep 21, 2022
ab4175a
rm audio util in print_signatures
SmileGoat Sep 21, 2022
55fe432
Merge branch 'develop' of github.com:SmileGoat/Paddle into audio_data…
SmileGoat Sep 21, 2022
c25c9cd
fix format3
SmileGoat Sep 21, 2022
d442072
add tess dataset license
SmileGoat Sep 21, 2022
b56e3ca
format warning
SmileGoat Sep 21, 2022
87a58c8
add more info in warning msg
SmileGoat Sep 22, 2022
39a93f0
add paddleaudio version check
SmileGoat Sep 22, 2022
86e0196
replace dataset esc50 with tess
SmileGoat Sep 27, 2022
ef5b921
add tess dataset && rm numpy transform in dataset.py
SmileGoat Sep 27, 2022
f5e070c
fix set audio backend bug
SmileGoat Sep 28, 2022
79d5464
fix equal error
SmileGoat Sep 28, 2022
a687516
fix format && coverage error
SmileGoat Sep 29, 2022
35d465a
add api example
SmileGoat Oct 14, 2022
4bf22b0
Merge branch 'develop' of github.com:SmileGoat/Paddle into audio_data…
SmileGoat Oct 14, 2022
ee97ba5
fix format
SmileGoat Oct 14, 2022
5c66e88
fix error
SmileGoat Oct 14, 2022
e6f5008
fix typo
SmileGoat Oct 14, 2022
f681319
add noqa in __init__
SmileGoat Oct 17, 2022
7b9be7d
fix backend doc example error
SmileGoat Oct 17, 2022
17b493b
rm seed in dataset
SmileGoat Oct 17, 2022
7ace395
update bakcend example
SmileGoat Oct 17, 2022
40566c0
fix typo
SmileGoat Oct 18, 2022
b0aae4e
fix typo
SmileGoat Oct 18, 2022
4238663
fix example err
SmileGoat Oct 18, 2022
1c20ff2
fix typo
SmileGoat Oct 18, 2022
7e359a6
fix ci dataset test
SmileGoat Oct 18, 2022
2a0a3bf
fix example fil
SmileGoat Oct 18, 2022
d1da85a
try to fix ci
SmileGoat Oct 18, 2022
8cbcff0
clean dataset doc
SmileGoat Oct 19, 2022
7ee4a58
change get_current_audio_backend to get_current_backend
SmileGoat Oct 20, 2022
b33893d
creplace paddle.audio.backends.info with paddle.audio.info, same with…
SmileGoat Oct 20, 2022
3a40c83
fix ci error
SmileGoat Oct 20, 2022
1ca215a
Merge branch 'develop' of github.com:SmileGoat/Paddle into audio_data…
SmileGoat Oct 20, 2022
160b50e
repalce api in test_audio_backend
SmileGoat Oct 20, 2022
2c6742f
fix save&&set_backend exmaple
SmileGoat Oct 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions paddle/fluid/API.spec
Expand Up @@ -21,3 +21,13 @@ paddle.audio.functional.functional.mel_frequencies (ArgSpec(args=['n_mels', 'f_m
paddle.audio.functional.functional.mel_to_hz (ArgSpec(args=['mel', 'htk'], varargs=None, varkw=None, defaults=(False,), kwonlyargs=[], kwonlydefaults=None, annotations={'return': typing.Union[float, paddle.Tensor], 'mel': typing.Union[float, paddle.Tensor], 'htk': <class 'bool'>}), ('document', 'e93b432d382f98c60d7c7599489e7072'))
paddle.audio.functional.functional.power_to_db (ArgSpec(args=['spect', 'ref_value', 'amin', 'top_db'], varargs=None, varkw=None, defaults=(1.0, 1e-10, 80.0), kwonlyargs=[], kwonlydefaults=None, annotations={'return': <class 'paddle.Tensor'>, 'spect': <class 'paddle.Tensor'>, 'ref_value': <class 'float'>, 'amin': <class 'float'>, 'top_db': typing.Union[float, NoneType]}), ('document', '28bbb1973e8399e856bfaea0415cecb9'))
paddle.audio.functional.window.get_window (ArgSpec(args=['window', 'win_length', 'fftbins', 'dtype'], varargs=None, varkw=None, defaults=(True, 'float64'), kwonlyargs=[], kwonlydefaults=None, annotations={'return': <class 'paddle.Tensor'>, 'window': typing.Union[str, typing.Tuple[str, float]], 'win_length': <class 'int'>, 'fftbins': <class 'bool'>, 'dtype': <class 'str'>}), ('document', '2418d63da10c0cd5da9ecf0a88ddf783'))
paddle.audio.backends (ArgSpec(), ('document', 'd41d8cd98f00b204e9800998ecf8427e'))
paddle.audio.backends.init_backend.get_current_audio_backend (ArgSpec(args=[], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'return': <class 'str'>}), ('document', '3ff9fd62e8be1f3dc7e34afaf50e1645'))
paddle.audio.backends.init_backend.list_available_backends (ArgSpec(args=[], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'return': typing.List[str]}), ('document', '8eba49f1b69f7ec7fa139a0714a2724e'))
paddle.audio.backends.init_backend.set_backend (ArgSpec(args=['backend_name'], varargs=None, varkw=None, defaults=None, kwonlyargs=[], kwonlydefaults=None, annotations={'backend_name': <class 'str'>}), ('document', '9680247dd97274d345dee415e2787527'))
paddle.audio.backends.wave_backend.info (ArgSpec(args=['filepath', 'format'], varargs=None, varkw=None, defaults=(None,), kwonlyargs=[], kwonlydefaults=None, annotations={'return': <class 'paddle.audio.backends.backend.AudioInfo'>, 'filepath': <class 'str'>, 'format': typing.Union[str, NoneType]}), ('document', 'e0ffd3accd942a9b0a4c08463a9f60f6'))
paddle.audio.backends.wave_backend.load (ArgSpec(args=['filepath', 'frame_offset', 'num_frames', 'normalize', 'channels_first', 'format'], varargs=None, varkw=None, defaults=(0, -1, True, True, None), kwonlyargs=[], kwonlydefaults=None, annotations={'return': typing.Tuple[paddle.Tensor, int], 'filepath': typing.Union[str, pathlib.Path], 'frame_offset': <class 'int'>, 'num_frames': <class 'int'>, 'normalize': <class 'bool'>, 'channels_first': <class 'bool'>, 'format': typing.Union[str, NoneType]}), ('document', '4de50575ca516b4b7c7c82c7fdec808f'))
paddle.audio.backends.wave_backend.save (ArgSpec(args=['filepath', 'src', 'sample_rate', 'channels_first', 'compression', 'format', 'encoding', 'bits_per_sample'], varargs=None, varkw=None, defaults=(True, None, None, None, None), kwonlyargs=[], kwonlydefaults=None, annotations={'filepath': <class 'str'>, 'src': <class 'paddle.Tensor'>, 'sample_rate': <class 'int'>, 'channels_first': <class 'bool'>, 'compression': typing.Union[float, NoneType], 'format': typing.Union[str, NoneType], 'encoding': typing.Union[str, NoneType], 'bits_per_sample': typing.Union[int, NoneType]}), ('document', '4c85cfcd29a0dcdfc32e74db8c0c3961'))
paddle.audio.datasets (ArgSpec(), ('document', 'd41d8cd98f00b204e9800998ecf8427e'))
paddle.audio.datasets.TESS (ArgSpec(), ('document', '3605f3aa2191ede7ddbe594cd27bb067'))
paddle.audio.datasets.TESS.meta_info (ArgSpec(), ('document', '60d548a6f71629c3b69bcda3a30d4819'))
1 change: 1 addition & 0 deletions python/paddle/__init__.py
Expand Up @@ -80,6 +80,7 @@
import paddle.reader # noqa: F401
import paddle.static # noqa: F401
import paddle.vision # noqa: F401
import paddle.audio # noqa: F401
import paddle.geometric # noqa: F401

from .tensor.attribute import is_complex # noqa: F401
Expand Down
4 changes: 3 additions & 1 deletion python/paddle/audio/__init__.py
Expand Up @@ -14,5 +14,7 @@

from . import features
from . import functional
from . import datasets
from . import backends

__all__ = ["functional", "features"]
__all__ = ["functional", "features", "datasets", "backends"]
32 changes: 32 additions & 0 deletions python/paddle/audio/backends/__init__.py
@@ -0,0 +1,32 @@
# 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_audio_backend # noqa: F401
from .init_backend import list_available_backends # noqa: F401
from .init_backend import set_backend

init_backend._init_set_audio_backend()

from .backend import load # noqa: F401
from .backend import info # noqa: F401
from .backend import save # noqa: F401

__all__ = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

有几个疑问

  1. 这几个函数名称信息有冗余,比如,audio和beckend都出现了两次
    paddle.audio.backends.get_current_audio_backend()
    是否可以考虑简洁的方式,比如
    paddle.audio.get_current_beckend()
    paddle.audio.list_available_beckends()
    paddle.audio.set_backend()
  2. paddle.audio.backends.save&load&info,backends的save接口表示什么含义呢?
    表示save的对象是一个backend吗?
    backend是通过set_backend()全局设置的一个状态
    如果是表示保存文件的话,是不是paddle.audio.save就可以了?

'get_current_audio_backend',
'list_available_backends',
'set_backend',
'info',
'load',
'save',
]
143 changes: 143 additions & 0 deletions python/paddle/audio/backends/backend.py
@@ -0,0 +1,143 @@
# 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

import paddle

from pathlib import Path
from typing import Optional, Tuple, Union


class AudioInfo:
""" Audio info, return type of backend info function """

def __init__(self, sample_rate: int, num_samples: int, num_channels: int,
bits_per_sample: int, encoding: str):
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:
"""Get signal information of input audio file.

Args:
filepath: audio path or file object.

Returns:
AudioInfo: info of the given audio.

Example:
SmileGoat marked this conversation as resolved.
Show resolved Hide resolved
.. code-block:: python

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, 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.backends.save(filepath, waveform, sample_rate)
wav_info = paddle.audio.backends.info(filepath)
"""
# for API doc
raise NotImplementedError("please set audio backend")


def load(filepath: Union[str, Path],
frame_offset: int = 0,
num_frames: int = -1,
normalize: bool = True,
channels_first: bool = True) -> Tuple[paddle.Tensor, int]:
"""Load audio data from file.Load the audio content start form frame_offset, and get num_frames.

Args:
SmileGoat marked this conversation as resolved.
Show resolved Hide resolved
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)

Exampels:
.. code-block:: python

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, 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.backends.save(filepath, waveform, sample_rate)
wav_data_read, sr = paddle.audio.backends.load(filepath)
"""
# for API doc
raise NotImplementedError("please set audio backend")


def save(
filepath: str,
src: paddle.Tensor,
sample_rate: int,
channels_first: bool = True,
encoding: Optional[str] = None,
bits_per_sample: Optional[int] = 16,
):
"""
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 infomation
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.

Examples:
SmileGoat marked this conversation as resolved.
Show resolved Hide resolved
.. code-block:: python

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, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1])
filepath = "./test.wav"

paddle.audio.backends.save(filepath, waveform, sample_rate)
"""
# for API doc
raise NotImplementedError("please set audio backend")
182 changes: 182 additions & 0 deletions python/paddle/audio/backends/init_backend.py
@@ -0,0 +1,182 @@
# 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.

import sys
import warnings
from . import wave_backend
from . import backend
from typing import List

import paddle


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:: python

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, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1])
wav_path = "./test.wav"

current_backend = paddle.audio.backends.get_current_audio_backend()
print(current_backend) # wave_backend, the default 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.backends.save(wav_path, waveform, sample_rate)

"""
backends = []
try:
import paddleaudio
except ImportError:
package = "paddleaudio"
warn_msg = (
"Failed importing {}. \n"
"only wave_banckend(only can deal with PCM16 WAV) supportted.\n"
"if want soundfile_backend(more audio type suppported),\n"
"please manually installed (usually with `pip install {} >= 1.0.2`). "
).format(package, package)
warnings.warn(warn_msg)

if "paddleaudio" in sys.modules:
version = paddleaudio.__version__
if _check_version(version) == False:
err_msg = (
"the version of paddleaudio installed is {},\n"
"please ensure the paddleaudio >= 1.0.2.").format(version)
raise ImportError(err_msg)
backends = paddleaudio.backends.list_audio_backends()
backends.append("wave_backend")
return backends


def get_current_audio_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:: python

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, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1])
wav_path = "./test.wav"

current_backend = paddle.audio.backends.get_current_audio_backend()
print(current_backend) # wave_backend, the default 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.backends.save(wav_path, waveform, sample_rate)

"""
current_backend = None
if "paddleaudio" in sys.modules:
import paddleaudio
current_backend = paddleaudio.backends.get_audio_backend()
if backend.load == paddleaudio.load:
return current_backend
return "wave_backend"


def set_backend(backend_name: str):
"""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.

Examples:
.. code-block:: python

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, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1])
wav_path = "./test.wav"

current_backend = paddle.audio.backends.get_current_audio_backend()
print(current_backend) # wave_backend, the default 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.backends.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.backends, func, getattr(module, func))


def _init_set_audio_backend():
# init the default wave_backend.
for func in ["save", "load", "info"]:
setattr(backend, func, getattr(wave_backend, func))