forked from Steckdose007/Masterthesis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresample_data.py
More file actions
40 lines (30 loc) · 1.42 KB
/
resample_data.py
File metadata and controls
40 lines (30 loc) · 1.42 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
"""
Reamples the Data folder to any new sample rate
"""
import librosa
import soundfile as sf
import os
def resample_audio_folder(input_dir, output_dir, target_sr=16000):
"""Resample all .wav files in input_dir to target_sr and save in output_dir."""
os.makedirs(output_dir, exist_ok=True)
for filename in os.listdir(input_dir):
if filename.lower().endswith(".wav"):
input_path = os.path.join(input_dir, filename)
output_path = os.path.join(output_dir, filename)
# Load at original rate
audio, sr = librosa.load(input_path, sr=None) # sr=None keeps the original rate
# Resample only if needed
if sr != target_sr:
audio = librosa.resample(audio, orig_sr=sr, target_sr=target_sr)
# Save the resampled audio
sf.write(output_path, audio, target_sr)
# --- Main part of the script ---
base_dir = "data" # The folder containing 'normal' and 'sigmatism'
normal_input = os.path.join(base_dir, "normal")
sigmatism_input = os.path.join(base_dir, "sigmatism")
normal_output = os.path.join(base_dir, "normal16kHz")
sigmatism_output = os.path.join(base_dir, "sigmatism16kHz")
# Resample both folders
resample_audio_folder(normal_input, normal_output, target_sr=16000)
resample_audio_folder(sigmatism_input, sigmatism_output, target_sr=16000)
print("Resampling complete! Check normal16kHz and sigmatism16kHz folders for results.")