{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Check for corrupted audio files in dataset\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import os\n",
        "import torchaudio\n",
        "import concurrent.futures\n",
        "\n",
        "i_dir = \"path/to/your/dataset\""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def check_wav(file_path):\n",
        "    \"\"\"Load a .wav file and return if it's corrupted or not\"\"\"\n",
        "    try:\n",
        "        waveform, sample_rate = torchaudio.load(file_path)\n",
        "        return (file_path, True)\n",
        "    except Exception as e:\n",
        "        return (file_path, False)\n",
        "\n",
        "\n",
        "def find_wavs(directory):\n",
        "    \"\"\"Find all .wav files in a directory\"\"\"\n",
        "    for foldername, subfolders, filenames in os.walk(directory):\n",
        "        for filename in filenames:\n",
        "            if filename.endswith(\".wav\"):\n",
        "                yield os.path.join(foldername, filename)\n",
        "\n",
        "\n",
        "def main(directory):\n",
        "    \"\"\"Check all .wav files in a directory and its subdirectories\"\"\"\n",
        "    with concurrent.futures.ThreadPoolExecutor() as executor:\n",
        "        wav_files = list(find_wavs(directory))\n",
        "        future_to_file = {executor.submit(check_wav, wav): wav for wav in wav_files}\n",
        "\n",
        "        done_count = 0\n",
        "        for future in concurrent.futures.as_completed(future_to_file):\n",
        "            file_path = future_to_file[future]\n",
        "            try:\n",
        "                is_valid = future.result()\n",
        "            except Exception as exc:\n",
        "                print(f\"{file_path} generated an exception: {exc}\")\n",
        "            else:\n",
        "                if not is_valid[1]:\n",
        "                    print(f\"Corrupted file: {file_path}\")\n",
        "\n",
        "            done_count += 1\n",
        "            if done_count % 5000 == 0:\n",
        "                print(f\"Processed {done_count} files...\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "main(i_dir)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "language_info": {
      "name": "python"
    },
    "orig_nbformat": 4
  },
  "nbformat": 4,
  "nbformat_minor": 2
}
