-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeature_extract.py
More file actions
executable file
·87 lines (65 loc) · 2.18 KB
/
Copy pathfeature_extract.py
File metadata and controls
executable file
·87 lines (65 loc) · 2.18 KB
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
import os
import random
import numpy as np
import librosa
from typing import Optional
from config import DEFAULT_MAX_LEN, SAMPLE_RATE
# ---------------------- Audio Processing Functions ---------------------- #
def pad(x: np.ndarray, max_len: int = DEFAULT_MAX_LEN) -> np.ndarray:
"""
Pad or truncate a 1D audio signal to a fixed length.
Args:
x: Input audio signal (1D numpy array).
max_len: Target length in samples.
Returns:
Padded or truncated audio signal.
"""
x_len = x.shape[0]
if x_len >= max_len:
return x[:max_len]
num_repeats = int(np.ceil(max_len / x_len))
padded_x = np.tile(x, num_repeats)[:max_len]
return padded_x
def pad_random(x: np.ndarray, max_len: int = DEFAULT_MAX_LEN) -> np.ndarray:
"""
Randomly pad or crop audio to a fixed length for data augmentation.
Args:
x: Input audio signal (1D numpy array).
max_len: Target length in samples.
Returns:
Padded or randomly cropped audio signal.
"""
x_len = x.shape[0]
if x_len > max_len:
start = np.random.randint(0, x_len - max_len)
return x[start:start + max_len]
num_repeats = int(np.ceil(max_len / x_len))
padded_x = np.tile(x, num_repeats)[:max_len]
return padded_x
def extract_fft(wav_path: str,) -> np.ndarray:
"""
Load an audio file and prepare it for training (with random padding/cropping).
Args:
wav_path: Path to the audio file.
Returns:
Audio signal as a numpy array.
"""
y, _ = librosa.load(wav_path, sr=16000)
y = pad_random(y)
return y
def extract_fft_eval_dev(wav_path: str) -> np.ndarray:
"""
Load an audio file for evaluation/dev (fixed padding).
Args:
wav_path: Path to the audio file.
Returns:
Padded audio signal as a numpy array.
"""
y, _ = librosa.load(wav_path, sr=16000)
y = pad(y)
return y
# ---------------------- Main ---------------------- #
def main():
print("Feature extraction module loaded successfully")
if __name__ == "__main__":
main()