{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a9e8d527-2e3b-4f5b-9808-8af7b83313f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#download dataset and resample\n",
    "\n",
    "import os\n",
    "from glob import glob\n",
    "import torchaudio\n",
    "from pathlib import Path\n",
    "from tqdm import tqdm\n",
    "import csv\n",
    "import shutil\n",
    "import torch\n",
    "\n",
    "     \n",
    "target_rate=22050\n",
    "download_dir=\"download_data/\"\n",
    "save_dir=\"resample1/\"\n",
    "zeroth_dir=\"zeroth_korean/\"\n",
    "tedxhr_dir=\"pansori_tedxkr/\"\n",
    "kss_dir=\"kss/\"\n",
    "\n",
    "data_dir_list=[zeroth_dir,tedxhr_dir,kss_dir]\n",
    "\n",
    "for i in [download_dir,save_dir]:\n",
    "    for j in data_dir_list:\n",
    "        os.makedirs(i+j, exist_ok=True)\n",
    "\n",
    "\n",
    "#create resampled mono wav,\n",
    "#dir          save_dir/wav/*.wav\n",
    "def saveResampleWav(wavList,save_dir,working_dir,target_rate):\n",
    "    for itemPath in tqdm(wavList):\n",
    "        savePath=os.path.join(save_dir,working_dir,\"wavs\",Path(itemPath).stem+\".wav\")\n",
    "        speech_array, sample_rate = torchaudio.load(itemPath)\n",
    "        speech_array = torchaudio.functional.resample(speech_array, sample_rate, target_rate)\n",
    "        speech_array = torch.unsqueeze(torch.mean(speech_array,axis=0),dim=0) \n",
    "        os.makedirs(os.path.dirname(savePath), exist_ok=True)\n",
    "        torchaudio.save(savePath, speech_array, target_rate)\n",
    "        \n",
    "\n",
    "#create csv,\n",
    "#dir           save_dir/metadata.csv\n",
    "#csv format    file_name | text | text | speaker_name\n",
    "def saveCsv(textList,save_dir,working_dir,csvReadLine):\n",
    "    with open(os.path.join(save_dir,working_dir,\"metadata.csv\"), \"w\", encoding=\"utf-8\") as file:\n",
    "        wr = csv.writer(file, delimiter='|')\n",
    "        for textPath in textList:\n",
    "            with open(textPath, \"r\", encoding=\"utf-8\") as f:\n",
    "                lines = f.readlines()\n",
    "            for line in lines:\n",
    "                file_name,text,speaker_name=csvReadLine(line,textPath)\n",
    "                if not os.path.exists(os.path.join(save_dir,working_dir,file_name)):\n",
    "                    continue\n",
    "                wr.writerow([file_name,text,text,speaker_name])\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "71ffd8c8-7bad-4c98-9c7e-8096ce18ac60",
   "metadata": {},
   "outputs": [],
   "source": [
    "#download kss\n",
    "import os\n",
    "os.environ['KAGGLE_USERNAME'] = \"\"\n",
    "os.environ['KAGGLE_KEY'] = \"\"\n",
    "#!pip install --user kaggle\n",
    "#!conda install -c conda-forge kaggle -y\n",
    "!kaggle datasets download -d bryanpark/korean-single-speaker-speech-dataset\n",
    "!unzip korean-single-speaker-speech-dataset.zip -d $download_dir$kss_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe299998-2769-4567-a50f-e59d8a499cdd",
   "metadata": {},
   "outputs": [],
   "source": [
    "#download zeroth and tedxkr\n",
    "!wget https://www.openslr.org/resources/40/zeroth_korean.tar.gz\n",
    "!wget https://www.openslr.org/resources/58/pansori-tedxkr-corpus-1.0.tar.gz\n",
    "\n",
    "!tar -xvf zeroth_korean.tar.gz -C $download_dir$zeroth_dir\n",
    "!tar -xvf pansori-tedxkr-corpus-1.0.tar.gz -C $download_dir$tedxhr_dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "75e82bfa-4f7d-4f1e-a904-2dcf7f8b5992",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████████████████████████████████| 2989/2989 [01:27<00:00, 34.20it/s]\n"
     ]
    }
   ],
   "source": [
    "#preprocess tedxhr\n",
    "textList=glob(download_dir+tedxhr_dir+\"**/*.txt\", recursive=True)\n",
    "wavList=glob(download_dir+tedxhr_dir+\"**/*.flac\", recursive=True)\n",
    "working_dir=tedxhr_dir\n",
    "\n",
    "\n",
    "def readLine(line,textPath):\n",
    "    lineSplit = line.split(' ', 1)\n",
    "    file_name=os.path.join(\"wavs\", Path(lineSplit[0]).stem+ \".wav\")\n",
    "    text=lineSplit[1].strip()\n",
    "    speaker_name=\"tedxhr_\"+Path(textPath).parent.name\n",
    "    return [file_name,text,speaker_name]\n",
    "\n",
    "\n",
    "\n",
    "saveResampleWav(wavList,save_dir,working_dir,target_rate)\n",
    "saveCsv(textList,save_dir,working_dir,readLine)         "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aedc256c-61cf-41f0-86ed-2ed7c8b6030c",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████| 22720/22720 [12:41<00:00, 29.85it/s]\n"
     ]
    }
   ],
   "source": [
    "#preprocess zeroth\n",
    "textList=glob(download_dir+zeroth_dir+\"**/*.txt\", recursive=True)\n",
    "wavList=glob(download_dir+zeroth_dir+\"**/*.flac\", recursive=True)\n",
    "working_dir=zeroth_dir\n",
    "\n",
    "\n",
    "def readLine(line,textPath):\n",
    "    lineSplit = line.split(' ', 1)\n",
    "    file_name=os.path.join(\"wavs\", Path(lineSplit[0]).stem+ \".wav\")\n",
    "    text=lineSplit[1].strip()\n",
    "    speaker_name=\"zeroth_\"+Path(textPath).parent.name\n",
    "    return [file_name,text,speaker_name]\n",
    "\n",
    "\n",
    "saveResampleWav(wavList,save_dir,working_dir,target_rate)\n",
    "saveCsv(textList,save_dir,working_dir,readLine)         \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "94a6e180-02d6-4521-b2fa-31697947b860",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 12854/12854 [02:53<00:00, 74.11it/s]\n"
     ]
    }
   ],
   "source": [
    "#preprocess kss\n",
    "\n",
    "textList=glob(\"kss/**/*.txt\", recursive=True)\n",
    "wavList=glob(\"kss/**/*.wav\", recursive=True)\n",
    "working_dir=kss_dir\n",
    "\n",
    "\n",
    "\n",
    "def readLine(line,textPath):\n",
    "    lineSplit = line.split('|')\n",
    "    file_name=os.path.join(\"wavs\", Path(lineSplit[0]).stem+ \".wav\")\n",
    "    text=lineSplit[3].strip()\n",
    "    speaker_name=\"kss\"\n",
    "    return [file_name,text,speaker_name]\n",
    "\n",
    "\n",
    "\n",
    "saveResampleWav(wavList,save_dir,working_dir,target_rate)\n",
    "saveCsv(textList,save_dir,working_dir,readLine)         \n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:tf2]",
   "language": "python",
   "name": "conda-env-tf2-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
