未验证 提交 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]:
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_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.save(wav_path, waveform, sample_rate)
>>> 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_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 = []
......@@ -100,26 +102,28 @@ def get_current_backend() -> str:
Examples:
.. code-block:: python
import paddle
>>> 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"
>>> 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_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
>>> current_backend = paddle.audio.backends.get_current_backend()
>>> print(current_backend)
wave_backend
if 'soundfile' in backends:
paddle.audio.backends.set_backend('soundfile')
>>> backends = paddle.audio.backends.list_available_backends()
>>> # 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
......@@ -144,26 +148,28 @@ def set_backend(backend_name: str):
Examples:
.. code-block:: python
import paddle
>>> 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"
>>> 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_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
>>> current_backend = paddle.audio.backends.get_current_backend()
>>> print(current_backend)
wave_backend
if 'soundfile' in backends:
paddle.audio.backends.set_backend('soundfile')
>>> backends = paddle.audio.backends.list_available_backends()
>>> # 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():
......
......@@ -46,20 +46,20 @@ def info(filepath: str) -> AudioInfo:
Example:
.. 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.save(filepath, waveform, sample_rate)
wav_info = paddle.audio.info(filepath)
>>> 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.save(filepath, waveform, sample_rate)
>>> wav_info = paddle.audio.info(filepath)
"""
if hasattr(filepath, 'read'):
......@@ -111,20 +111,20 @@ def load(
Examples:
.. code-block:: python
import os
import paddle
>>> 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")
>>> 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.save(filepath, waveform, sample_rate)
wav_data_read, sr = paddle.audio.load(filepath)
>>> paddle.audio.save(filepath, waveform, sample_rate)
>>> wav_data_read, sr = paddle.audio.load(filepath)
"""
if hasattr(filepath, 'read'):
file_obj = filepath
......@@ -192,17 +192,17 @@ def save(
Examples:
.. code-block:: python
import paddle
>>> 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"
>>> 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.save(filepath, waveform, sample_rate)
>>> paddle.audio.save(filepath, waveform, sample_rate)
"""
assert src.ndim == 2, "Expected 2D tensor"
......
......@@ -47,25 +47,36 @@ class ESC50(AudioClassificationDataset):
.. code-block:: python
import paddle
mode = 'dev'
esc50_dataset = paddle.audio.datasets.ESC50(mode=mode,
feat_type='raw')
for idx in range(5):
audio, label = esc50_dataset[idx]
# do something with audio, label
print(audio.shape, label)
# [audio_data_length] , label_id
esc50_dataset = paddle.audio.datasets.ESC50(mode=mode,
feat_type='mfcc',
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
>>> import paddle
>>> mode = 'dev'
>>> esc50_dataset = paddle.audio.datasets.ESC50(mode=mode,
... feat_type='raw')
>>> for idx in range(5):
... audio, label = esc50_dataset[idx]
... # do something with audio, label
... print(audio.shape, label)
... # [audio_data_length] , label_id
[220500] 0
[220500] 14
[220500] 36
[220500] 36
[220500] 19
>>> esc50_dataset = paddle.audio.datasets.ESC50(mode=mode,
... feat_type='mfcc',
... 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 = {
......
......@@ -49,25 +49,25 @@ class TESS(AudioClassificationDataset):
.. code-block:: python
import paddle
mode = 'dev'
tess_dataset = paddle.audio.datasets.TESS(mode=mode,
feat_type='raw')
for idx in range(5):
audio, label = tess_dataset[idx]
# do something with audio, label
print(audio.shape, label)
# [audio_data_length] , label_id
tess_dataset = paddle.audio.datasets.TESS(mode=mode,
feat_type='mfcc',
n_mfcc=40)
for idx in range(5):
audio, label = tess_dataset[idx]
# do something with mfcc feature, label
print(audio.shape, label)
# [feature_dim, num_frames] , label_id
>>> import paddle
>>> mode = 'dev'
>>> tess_dataset = paddle.audio.datasets.TESS(mode=mode,
... feat_type='raw')
>>> for idx in range(5):
... audio, label = tess_dataset[idx]
... # do something with audio, label
... print(audio.shape, label)
... # [audio_data_length] , label_id
>>> tess_dataset = paddle.audio.datasets.TESS(mode=mode,
... feat_type='mfcc',
... n_mfcc=40)
>>> for idx in range(5):
... audio, label = tess_dataset[idx]
... # do something with mfcc feature, label
... print(audio.shape, label)
... # [feature_dim, num_frames] , label_id
"""
archive = {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册