FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
speechbrain/speechbrain/augment/codec.py at develop · speechbrain/speechbrain · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
speechbrain
/
speechbrain
Public
Notifications
You must be signed in to change notification settings
Fork
1.7k
Star
11.8k
Code
Issues
125
Pull requests
52
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
speechbrain
/
speechbrain
/
augment
/
codec.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
92 lines (74 loc) · 2.64 KB
Breadcrumbs
speechbrain
/
speechbrain
/
augment
/
codec.py
Copy path
File metadata and controls
92 lines (74 loc) · 2.64 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Codec Augmentation via torchaudio
This library provides codec augmentation techniques in torchaudio for enhanced
audio data processing.
For detailed guidance and usage examples, refer to the tutorial at:
https://pytorch.org/audio/stable/tutorials/audio_data_augmentation_tutorial.html
Note: This code is compatible with FFmpeg as the torchaudio backend.
When using FFmpeg2, the maximum number of samples for processing is limited to 16.
Authors
* Mirco Ravanelli 2023
"""
import
random
import
torch
import
torchaudio
class
CodecAugment
(
torch
.
nn
.
Module
):
"""
Apply random audio codecs to input waveforms using torchaudio.
This class provides an interface for applying codec augmentation techniques to audio data.
Arguments
---------
sample_rate: int
The sample rate of the input waveform.
Example
-------
>>> waveform = torch.rand(4, 16000)
>>> if torchaudio.list_audio_backends()[0] == "ffmpeg":
... augmenter = CodecAugment(16000)
... output_waveform = augmenter(waveform)
"""
def
__init__
(
self
,
sample_rate
=
16000
):
super
().
__init__
()
self
.
sample_rate
=
sample_rate
self
.
available_format_encoders
=
[
(
"wav"
,
"pcm_mulaw"
),
(
"mp3"
,
None
),
(
"g722"
,
None
),
]
def
apply_codec
(
self
,
waveform
,
format
=
None
,
encoder
=
None
):
"""
Apply the selected audio codec.
Arguments
----------
waveform: torch.Tensor
Input waveform of shape `[batch, time]`.
format: str
The audio format to use (e.g., "wav", "mp3"). Default is None.
encoder: str
The encoder to use for the format (e.g., "opus", "vorbis"). Default is None.
Returns
---------
torch.Tensor:
Coded version of the input waveform of shape `[batch, time]`.
"""
audio_effector
=
torchaudio
.
io
.
AudioEffector
(
format
=
format
,
encoder
=
encoder
)
waveform_aug
=
audio_effector
.
apply
(
waveform
.
transpose
(
0
,
1
).
to
(
"cpu"
),
self
.
sample_rate
)
return
waveform_aug
.
transpose
(
0
,
1
).
to
(
waveform
.
device
)
def
forward
(
self
,
waveform
):
"""
Apply a random audio codec from the available list.
Arguments
---------
waveform: torch.Tensor
Input waveform of shape `[batch, time]`.
Returns
-------
torch.Tensor
Coded version of the input waveform of shape `[batch, time]`.
"""
format
,
encoder
=
random
.
choice
(
self
.
available_format_encoders
)
return
self
.
apply_codec
(
waveform
,
format
=
format
,
encoder
=
encoder
)
Back
|
FazBrowse Home
|
New Git URL