{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":73047,"databundleVersionId":8823072,"sourceType":"competition"}],"dockerImageVersionId":31154,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os, random, numpy as np, librosa, soundfile as sf, matplotlib.pyplot as plt\nfrom tqdm import tqdm\nfrom pathlib import Path\nfrom IPython.display import Audio as ipdAudio\n\n\nBASE_DIR = \"/kaggle/input/ben10/ben10\"\nTRAIN_DIR = os.path.join(BASE_DIR, \"16_kHz_train_audio\")\nVALID_DIR = os.path.join(BASE_DIR, \"16_kHz_valid_audio\")\n\nRAW_AUDIO_DIR = BASE_DIR  \nCLEAN_AUDIO_DIR = \"./clean_audio\"  \nos.makedirs(CLEAN_AUDIO_DIR, exist_ok=True)\n\nSAMPLE_RATE = 16000\nENERGY_THRESHOLD = 0.001  \n\nprint(\"Base folder:\", BASE_DIR)\nprint(\"Train folder:\", TRAIN_DIR)\nprint(\"Valid folder:\", VALID_DIR)\nprint(\"Output folder:\", CLEAN_AUDIO_DIR)\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-10-21T10:00:26.785642Z","iopub.execute_input":"2025-10-21T10:00:26.786282Z","iopub.status.idle":"2025-10-21T10:00:26.792118Z","shell.execute_reply.started":"2025-10-21T10:00:26.786245Z","shell.execute_reply":"2025-10-21T10:00:26.791480Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"files = []\nfor d in [TRAIN_DIR, VALID_DIR]:\n    for dp, _, fs in os.walk(d):\n        for f in fs:\n            if f.lower().endswith(\".wav\"):\n                files.append(os.path.join(dp, f))\n\nprint(f\"Total audio files found (train+valid): {len(files)}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-21T10:00:33.793057Z","iopub.execute_input":"2025-10-21T10:00:33.793626Z","iopub.status.idle":"2025-10-21T10:00:46.010699Z","shell.execute_reply.started":"2025-10-21T10:00:33.793605Z","shell.execute_reply":"2025-10-21T10:00:46.010097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import librosa.display\n\ndef inspect_random_audio():\n    f = random.choice(files)\n    y, sr = librosa.load(f, sr=None, mono=False)\n    print(f\"Example file: {f}\\nOriginal sr={sr}, shape={y.shape}\")\n    plt.figure(figsize=(12, 3))\n    librosa.display.waveshow(y, sr=sr)\n    plt.title(\"Raw waveform example\")\n    plt.show()\n    return ipdAudio(y, rate=sr)\n\ninspect_random_audio()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-21T10:02:23.191632Z","iopub.execute_input":"2025-10-21T10:02:23.192128Z","iopub.status.idle":"2025-10-21T10:02:36.060843Z","shell.execute_reply.started":"2025-10-21T10:02:23.192105Z","shell.execute_reply":"2025-10-21T10:02:36.060113Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def preprocess_audio(src_path, dst_path, target_sr=SAMPLE_RATE):\n    try:\n        y, sr = librosa.load(src_path, sr=None, mono=True)\n        if sr != target_sr:\n            y = librosa.resample(y, orig_sr=sr, target_sr=target_sr)\n        y = librosa.util.normalize(y)\n        y = y - np.mean(y)\n        sf.write(dst_path, y, target_sr)\n        return True\n    except Exception as e:\n        print(f\"Failed: {src_path} -> {e}\")\n        return False\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-21T10:02:46.943242Z","iopub.execute_input":"2025-10-21T10:02:46.944040Z","iopub.status.idle":"2025-10-21T10:02:46.948984Z","shell.execute_reply.started":"2025-10-21T10:02:46.944012Z","shell.execute_reply":"2025-10-21T10:02:46.948356Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for f in tqdm(files, desc=\"Preprocessing train+valid\"):\n    dst = os.path.join(CLEAN_AUDIO_DIR, Path(f).name)\n    preprocess_audio(f, dst)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-21T10:02:50.195596Z","iopub.execute_input":"2025-10-21T10:02:50.195887Z","iopub.status.idle":"2025-10-21T10:09:54.098579Z","shell.execute_reply.started":"2025-10-21T10:02:50.195830Z","shell.execute_reply":"2025-10-21T10:09:54.097730Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def is_silent(path):\n    try:\n        y, sr = librosa.load(path, sr=None)\n        return np.mean(y**2) < ENERGY_THRESHOLD\n    except Exception as e:\n        print(f\"Error reading {path}: {e}\")\n        return True  # if failed, treat as silent\n\nkept = []\nremoved = 0\nfor f in tqdm(os.listdir(CLEAN_AUDIO_DIR), desc=\"Filtering silent files\"):\n    full_path = os.path.join(CLEAN_AUDIO_DIR, f)\n    if is_silent(full_path):\n        os.remove(full_path)\n        removed += 1\n    else:\n        kept.append(full_path)\n\nprint(f\"✅ Kept {len(kept)} non-silent clips, removed {removed} silent ones.\")\nprint(f\"🎧 Cleaned dataset saved in: {CLEAN_AUDIO_DIR}\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-21T10:09:54.099692Z","iopub.execute_input":"2025-10-21T10:09:54.099937Z","iopub.status.idle":"2025-10-21T10:10:28.686050Z","shell.execute_reply.started":"2025-10-21T10:09:54.099920Z","shell.execute_reply":"2025-10-21T10:10:28.685370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nimport random\ndef preview_cleaned():\n    f = random.choice(os.listdir(CLEAN_AUDIO_DIR))\n    path = os.path.join(CLEAN_AUDIO_DIR, f)\n    y, sr = librosa.load(path, sr=None)\n    print(\"Preview:\", path)\n    plt.figure(figsize=(12,3))\n    librosa.display.waveshow(y, sr=sr)\n    plt.show()\n    return ipdAudio(y, rate=sr)\n\npreview_cleaned()\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-10-21T10:10:28.686771Z","iopub.execute_input":"2025-10-21T10:10:28.687084Z","iopub.status.idle":"2025-10-21T10:10:29.024585Z","shell.execute_reply.started":"2025-10-21T10:10:28.687066Z","shell.execute_reply":"2025-10-21T10:10:29.023865Z"}},"outputs":[],"execution_count":null}]}