don't forget to preprocessing, it really important
Browse files- preprocessing.py +105 -0
preprocessing.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy as np
|
| 3 |
+
from pydub import AudioSegment
|
| 4 |
+
from scipy.io import wavfile
|
| 5 |
+
|
| 6 |
+
def trim_wav(input_file, output_file, target_duration_minutes=24, target_duration_seconds=10):
|
| 7 |
+
"""
|
| 8 |
+
Memotong file WAV sesuai durasi target
|
| 9 |
+
"""
|
| 10 |
+
try:
|
| 11 |
+
# Load file WAV
|
| 12 |
+
audio = AudioSegment.from_wav(input_file)
|
| 13 |
+
|
| 14 |
+
# Konversi target durasi ke milidetik
|
| 15 |
+
target_duration_ms = (target_duration_minutes * 60 + target_duration_seconds) * 1000
|
| 16 |
+
|
| 17 |
+
# Cek apakah durasi audio melebihi target
|
| 18 |
+
if len(audio) <= target_duration_ms:
|
| 19 |
+
print(f"File {input_file} memiliki durasi kurang dari target, diloncati")
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
+
# Potong audio sesuai durasi yang diinginkan
|
| 23 |
+
trimmed_audio = audio[:target_duration_ms]
|
| 24 |
+
|
| 25 |
+
# Export file yang sudah dipotong
|
| 26 |
+
trimmed_audio.export(output_file, format="wav")
|
| 27 |
+
|
| 28 |
+
print(f"File berhasil dipotong menjadi {target_duration_minutes} menit {target_duration_seconds} detik")
|
| 29 |
+
return output_file
|
| 30 |
+
|
| 31 |
+
except Exception as e:
|
| 32 |
+
print(f"Terjadi kesalahan saat memotong file: {str(e)}")
|
| 33 |
+
return None
|
| 34 |
+
|
| 35 |
+
def segment_wav_file(input_file, output_dir, segment_duration=5, overlap_percentage=0.5):
|
| 36 |
+
"""
|
| 37 |
+
Memotong file WAV menjadi segmen-segmen dengan overlap
|
| 38 |
+
"""
|
| 39 |
+
# Create output directory if not exists
|
| 40 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 41 |
+
|
| 42 |
+
# Read the input WAV file
|
| 43 |
+
sample_rate, audio_data = wavfile.read(input_file)
|
| 44 |
+
|
| 45 |
+
# Convert segment duration to samples
|
| 46 |
+
samples_per_segment = int(segment_duration * sample_rate)
|
| 47 |
+
samples_overlap = int(samples_per_segment * overlap_percentage)
|
| 48 |
+
|
| 49 |
+
# Calculate step size between segments
|
| 50 |
+
step_size = samples_per_segment - samples_overlap
|
| 51 |
+
|
| 52 |
+
# Segment the audio
|
| 53 |
+
segmented_files = []
|
| 54 |
+
for start in range(0, len(audio_data) - samples_per_segment + 1, step_size):
|
| 55 |
+
end = start + samples_per_segment
|
| 56 |
+
segment = audio_data[start:end]
|
| 57 |
+
|
| 58 |
+
# Generate output filename dengan format "sound_segment_X.wav"
|
| 59 |
+
segment_filename = f"ridho_segment_{start//step_size}.wav"
|
| 60 |
+
output_path = os.path.join(output_dir, segment_filename)
|
| 61 |
+
|
| 62 |
+
# Write segmented audio to file
|
| 63 |
+
wavfile.write(output_path, sample_rate, segment)
|
| 64 |
+
segmented_files.append(output_path)
|
| 65 |
+
|
| 66 |
+
print(f"Berhasil membuat {len(segmented_files)} segmen")
|
| 67 |
+
return segmented_files
|
| 68 |
+
|
| 69 |
+
def process_audio_pipeline(input_wav_file, output_dir):
|
| 70 |
+
"""
|
| 71 |
+
Fungsi utama untuk memproses single file WAV
|
| 72 |
+
"""
|
| 73 |
+
try:
|
| 74 |
+
# Buat direktori output jika belum ada
|
| 75 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 76 |
+
|
| 77 |
+
# Step 1: Trim to 24:10
|
| 78 |
+
trimmed_wav = os.path.join(output_dir, "trimmed.wav")
|
| 79 |
+
trimmed_result = trim_wav(input_wav_file, trimmed_wav)
|
| 80 |
+
if not trimmed_result:
|
| 81 |
+
return
|
| 82 |
+
|
| 83 |
+
# Step 2: Create segments
|
| 84 |
+
segments_dir = os.path.join(output_dir, "segments")
|
| 85 |
+
segmented_files = segment_wav_file(
|
| 86 |
+
trimmed_wav,
|
| 87 |
+
segments_dir,
|
| 88 |
+
segment_duration=5,
|
| 89 |
+
overlap_percentage=0.5
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
print("Proses selesai!")
|
| 93 |
+
return segmented_files
|
| 94 |
+
|
| 95 |
+
except Exception as e:
|
| 96 |
+
print(f"Terjadi kesalahan dalam pipeline: {str(e)}")
|
| 97 |
+
return None
|
| 98 |
+
|
| 99 |
+
# Contoh penggunaan
|
| 100 |
+
if __name__ == "__main__":
|
| 101 |
+
# Sesuaikan path file input dan direktori output
|
| 102 |
+
input_wav = "full recording.wav"
|
| 103 |
+
output_dir = "/nama/path/to/target/dataset"
|
| 104 |
+
|
| 105 |
+
process_audio_pipeline(input_wav, output_dir)
|