未验证 提交 e1eb52e1 编写于 作者: L LoneRanger 提交者: GitHub

[xdoctest] reformat example code with google style in No.12-No.15 (#56210)

* fix sample codes

* fix bug

* Update tess.py
上级 702efe0f
...@@ -43,25 +43,27 @@ def list_available_backends() -> List[str]: ...@@ -43,25 +43,27 @@ def list_available_backends() -> List[str]:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
wav_path = "./test.wav" >>> wav_path = "./test.wav"
current_backend = paddle.audio.backends.get_current_backend() >>> current_backend = paddle.audio.backends.get_current_backend()
print(current_backend) # wave_backend, the default backend. >>> print(current_backend)
backends = paddle.audio.backends.list_available_backends() wave_backend
# default backends is ['wave_backend']
# backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2 >>> backends = paddle.audio.backends.list_available_backends()
if 'soundfile' in backends: >>> # default backends is ['wave_backend']
paddle.audio.backends.set_backend('soundfile') >>> # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2
>>> if 'soundfile' in backends:
paddle.audio.save(wav_path, waveform, sample_rate) ... paddle.audio.backends.set_backend('soundfile')
...
>>> paddle.audio.save(wav_path, waveform, sample_rate)
""" """
backends = [] backends = []
...@@ -100,26 +102,28 @@ def get_current_backend() -> str: ...@@ -100,26 +102,28 @@ def get_current_backend() -> str:
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
wav_path = "./test.wav" >>> wav_path = "./test.wav"
current_backend = paddle.audio.backends.get_current_backend() >>> current_backend = paddle.audio.backends.get_current_backend()
print(current_backend) # wave_backend, the default backend. >>> print(current_backend)
backends = paddle.audio.backends.list_available_backends() wave_backend
# default backends is ['wave_backend']
# backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2
if 'soundfile' in backends: >>> backends = paddle.audio.backends.list_available_backends()
paddle.audio.backends.set_backend('soundfile') >>> # default backends is ['wave_backend']
>>> # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2
paddle.audio.save(wav_path, waveform, sample_rate) >>> if 'soundfile' in backends:
... paddle.audio.backends.set_backend('soundfile')
...
>>> paddle.audio.save(wav_path, waveform, sample_rate)
""" """
current_backend = None current_backend = None
...@@ -144,26 +148,28 @@ def set_backend(backend_name: str): ...@@ -144,26 +148,28 @@ def set_backend(backend_name: str):
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
wav_path = "./test.wav" >>> wav_path = "./test.wav"
current_backend = paddle.audio.backends.get_current_backend() >>> current_backend = paddle.audio.backends.get_current_backend()
print(current_backend) # wave_backend, the default backend. >>> print(current_backend)
backends = paddle.audio.backends.list_available_backends() wave_backend
# default backends is ['wave_backend']
# backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2
if 'soundfile' in backends: >>> backends = paddle.audio.backends.list_available_backends()
paddle.audio.backends.set_backend('soundfile') >>> # default backends is ['wave_backend']
>>> # backends is ['wave_backend', 'soundfile'], if have installed paddleaudio >= 1.0.2
paddle.audio.save(wav_path, waveform, sample_rate) >>> 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(): if backend_name not in list_available_backends():
......
...@@ -46,20 +46,20 @@ def info(filepath: str) -> AudioInfo: ...@@ -46,20 +46,20 @@ def info(filepath: str) -> AudioInfo:
Example: Example:
.. code-block:: python .. code-block:: python
import os >>> import os
import paddle >>> import paddle
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
base_dir = os.getcwd() >>> base_dir = os.getcwd()
filepath = os.path.join(base_dir, "test.wav") >>> filepath = os.path.join(base_dir, "test.wav")
paddle.audio.save(filepath, waveform, sample_rate) >>> paddle.audio.save(filepath, waveform, sample_rate)
wav_info = paddle.audio.info(filepath) >>> wav_info = paddle.audio.info(filepath)
""" """
if hasattr(filepath, 'read'): if hasattr(filepath, 'read'):
...@@ -111,20 +111,20 @@ def load( ...@@ -111,20 +111,20 @@ def load(
Examples: Examples:
.. code-block:: python .. code-block:: python
import os >>> import os
import paddle >>> import paddle
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
base_dir = os.getcwd() >>> base_dir = os.getcwd()
filepath = os.path.join(base_dir, "test.wav") >>> filepath = os.path.join(base_dir, "test.wav")
paddle.audio.save(filepath, waveform, sample_rate) >>> paddle.audio.save(filepath, waveform, sample_rate)
wav_data_read, sr = paddle.audio.load(filepath) >>> wav_data_read, sr = paddle.audio.load(filepath)
""" """
if hasattr(filepath, 'read'): if hasattr(filepath, 'read'):
file_obj = filepath file_obj = filepath
...@@ -192,17 +192,17 @@ def save( ...@@ -192,17 +192,17 @@ def save(
Examples: Examples:
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
sample_rate = 16000 >>> sample_rate = 16000
wav_duration = 0.5 >>> wav_duration = 0.5
num_channels = 1 >>> num_channels = 1
num_frames = sample_rate * wav_duration >>> num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1 >>> wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1]) >>> waveform = wav_data.tile([num_channels, 1])
filepath = "./test.wav" >>> filepath = "./test.wav"
paddle.audio.save(filepath, waveform, sample_rate) >>> paddle.audio.save(filepath, waveform, sample_rate)
""" """
assert src.ndim == 2, "Expected 2D tensor" assert src.ndim == 2, "Expected 2D tensor"
......
...@@ -47,25 +47,36 @@ class ESC50(AudioClassificationDataset): ...@@ -47,25 +47,36 @@ class ESC50(AudioClassificationDataset):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
mode = 'dev' >>> mode = 'dev'
esc50_dataset = paddle.audio.datasets.ESC50(mode=mode, >>> esc50_dataset = paddle.audio.datasets.ESC50(mode=mode,
feat_type='raw') ... feat_type='raw')
for idx in range(5): >>> for idx in range(5):
audio, label = esc50_dataset[idx] ... audio, label = esc50_dataset[idx]
# do something with audio, label ... # do something with audio, label
print(audio.shape, label) ... print(audio.shape, label)
# [audio_data_length] , label_id ... # [audio_data_length] , label_id
[220500] 0
esc50_dataset = paddle.audio.datasets.ESC50(mode=mode, [220500] 14
feat_type='mfcc', [220500] 36
n_mfcc=40) [220500] 36
for idx in range(5): [220500] 19
audio, label = esc50_dataset[idx]
# do something with mfcc feature, label >>> esc50_dataset = paddle.audio.datasets.ESC50(mode=mode,
print(audio.shape, label) ... feat_type='mfcc',
# [feature_dim, length] , label_id ... n_mfcc=40)
>>> for idx in range(5):
... audio, label = esc50_dataset[idx]
... # do something with mfcc feature, label
... print(audio.shape, label)
... # [feature_dim, length] , label_id
[40, 1723] 0
[40, 1723] 14
[40, 1723] 36
[40, 1723] 36
[40, 1723] 19
""" """
archive = { archive = {
......
...@@ -49,25 +49,25 @@ class TESS(AudioClassificationDataset): ...@@ -49,25 +49,25 @@ class TESS(AudioClassificationDataset):
.. code-block:: python .. code-block:: python
import paddle >>> import paddle
mode = 'dev' >>> mode = 'dev'
tess_dataset = paddle.audio.datasets.TESS(mode=mode, >>> tess_dataset = paddle.audio.datasets.TESS(mode=mode,
feat_type='raw') ... feat_type='raw')
for idx in range(5): >>> for idx in range(5):
audio, label = tess_dataset[idx] ... audio, label = tess_dataset[idx]
# do something with audio, label ... # do something with audio, label
print(audio.shape, label) ... print(audio.shape, label)
# [audio_data_length] , label_id ... # [audio_data_length] , label_id
tess_dataset = paddle.audio.datasets.TESS(mode=mode, >>> tess_dataset = paddle.audio.datasets.TESS(mode=mode,
feat_type='mfcc', ... feat_type='mfcc',
n_mfcc=40) ... n_mfcc=40)
for idx in range(5): >>> for idx in range(5):
audio, label = tess_dataset[idx] ... audio, label = tess_dataset[idx]
# do something with mfcc feature, label ... # do something with mfcc feature, label
print(audio.shape, label) ... print(audio.shape, label)
# [feature_dim, num_frames] , label_id ... # [feature_dim, num_frames] , label_id
""" """
archive = { archive = {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册