{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import IPython.display as ipd\n",
    "import torch\n",
    "from torch.utils.data import DataLoader\n",
    "\n",
    "from utils.task import load_checkpoint\n",
    "from utils.hparams import get_hparams_from_file\n",
    "from data_utils import TextAudioSpeakerLoader, TextAudioSpeakerCollate\n",
    "from model.models import SynthesizerTrn\n",
    "from text.symbols import symbols\n",
    "from text import tokenizer\n",
    "\n",
    "\n",
    "def get_text(text: str, hps) -> torch.LongTensor:\n",
    "    text_norm = tokenizer(text, hps.data.text_cleaners, language=hps.data.language)\n",
    "    text_norm = torch.LongTensor(text_norm)\n",
    "    return text_norm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## LJ Speech\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = \"ljs_base\"\n",
    "checkpoint = \"G_1000.pth\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:root:Loaded checkpoint './logs/ljs_base/G_2000.pth' (iteration 38)\n"
     ]
    }
   ],
   "source": [
    "hps = get_hparams_from_file(f\"./datasets/{model}/config.yaml\")\n",
    "filter_length = hps.data.n_mels if hps.data.use_mel else hps.data.n_fft // 2 + 1\n",
    "segment_size = hps.train.segment_size // hps.data.hop_length\n",
    "net_g = SynthesizerTrn(len(symbols), filter_length, segment_size, **hps.model).cuda()\n",
    "_ = net_g.eval()\n",
    "_ = load_checkpoint(f\"./datasets/{model}/logs/{checkpoint}\", net_g, None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Failed to create secure directory (/pulse): Permission denied\n",
      "Failed to create secure directory (/pulse): Permission denied\n",
      "Failed to create secure directory (/pulse): Permission denied\n",
      "Failed to create secure directory (/pulse): Permission denied\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "                <audio  controls=\"controls\" >\n",
       "                    <source src=\"data:audio/wav;base64,[encoded data, 68328 characters]\" type=\"audio/wav\" />\n",
       "                    Your browser does not support the audio element.\n",
       "                </audio>\n",
       "              "
      ],
      "text/plain": [
       "<IPython.lib.display.Audio object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "stn_tst = get_text(\"Single-stage text-to-speech models have been actively studied recently, and their results have outperformed two-stage pipeline systems.\", hps)\n",
    "with torch.no_grad():\n",
    "    x_tst = stn_tst.cuda().unsqueeze(0)\n",
    "    x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()\n",
    "\n",
    "    out = net_g.infer(x_tst, x_tst_lengths, noise_scale=0.667, noise_scale_w=0.8, length_scale=1)\n",
    "    audio = out[0][0, 0].data.cpu().float().numpy()\n",
    "ipd.display(ipd.Audio(audio, rate=hps.data.sample_rate, normalize=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## MADASR23\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = \"madasr23_base\"\n",
    "checkpoint = \"G_1000.pth\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:root:Loaded checkpoint './logs/madasr23_base/G_55000.pth' (iteration 31)\n"
     ]
    }
   ],
   "source": [
    "hps = get_hparams_from_file(f\"./datasets/{model}/config.yaml\")\n",
    "filter_length = hps.data.n_mels if hps.data.use_mel else hps.data.n_fft // 2 + 1\n",
    "segment_size = hps.train.segment_size // hps.data.hop_length\n",
    "net_g = SynthesizerTrn(len(symbols), filter_length, segment_size, n_speakers=hps.data.n_speakers, **hps.model).cuda()\n",
    "_ = net_g.eval()\n",
    "_ = load_checkpoint(f\"./datasets/{model}/logs/{checkpoint}\", net_g, None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "                <audio  controls=\"controls\" >\n",
       "                    <source src=\"data:audio/wav;base64,[encoded data, 264936 characters]\" type=\"audio/wav\" />\n",
       "                    Your browser does not support the audio element.\n",
       "                </audio>\n",
       "              "
      ],
      "text/plain": [
       "<IPython.lib.display.Audio object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "stn_tst = get_text(\"রোপক বা প্ল্যান্টার মেশিন দুই ধরনের হয় বৈদ্যুতিন এবং হাইড্রলিক যান্ত্রিক\", hps)\n",
    "with torch.no_grad():\n",
    "    x_tst = stn_tst.cuda().unsqueeze(0)\n",
    "    x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()\n",
    "    sid = torch.LongTensor([78]).cuda()\n",
    "\n",
    "    out = net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=0.667, noise_scale_w=0.8, length_scale=1)\n",
    "    audio = out[0][0, 0].data.cpu().float().numpy()\n",
    "ipd.display(ipd.Audio(audio, rate=hps.data.sample_rate, normalize=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## VCTK\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = \"vctk_base\"\n",
    "checkpoint = \"G_1000.pth\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hps = get_hparams_from_file(f\"./datasets/{model}/config.yaml\")\n",
    "filter_length = hps.data.n_mels if hps.data.use_mel else hps.data.n_fft // 2 + 1\n",
    "segment_size = hps.train.segment_size // hps.data.hop_length\n",
    "net_g = SynthesizerTrn(len(symbols), filter_length, segment_size, n_speakers=hps.data.n_speakers, **hps.model).cuda()\n",
    "_ = net_g.eval()\n",
    "_ = load_checkpoint(f\"./datasets/{model}/logs/{checkpoint}\", net_g, None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "stn_tst = get_text(\"VITS is Awesome!\", hps)\n",
    "with torch.no_grad():\n",
    "    x_tst = stn_tst.cuda().unsqueeze(0)\n",
    "    x_tst_lengths = torch.LongTensor([stn_tst.size(0)]).cuda()\n",
    "    sid = torch.LongTensor([4]).cuda()\n",
    "\n",
    "    out = net_g.infer(x_tst, x_tst_lengths, sid=sid, noise_scale=0.667, noise_scale_w=0.8, length_scale=1)\n",
    "    audio = out[0][0, 0].data.cpu().float().numpy()\n",
    "ipd.display(ipd.Audio(audio, rate=hps.data.sample_rate, normalize=False))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Voice Conversion\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset = TextAudioSpeakerLoader(hps.data.validation_files, hps.data)\n",
    "collate_fn = TextAudioSpeakerCollate()\n",
    "loader = DataLoader(dataset, num_workers=8, shuffle=False, batch_size=1, pin_memory=True, drop_last=True, collate_fn=collate_fn)\n",
    "data_list = list(loader)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "with torch.no_grad():\n",
    "    x, x_lengths, spec, spec_lengths, y, y_lengths, sid_src = [x.cuda() for x in data_list[0]]\n",
    "    sid_tgt1 = torch.LongTensor([1]).cuda()\n",
    "    sid_tgt2 = torch.LongTensor([2]).cuda()\n",
    "    sid_tgt3 = torch.LongTensor([4]).cuda()\n",
    "    audio1 = net_g.voice_conversion(spec, spec_lengths, sid_src=sid_src, sid_tgt=sid_tgt1)[0][0, 0].data.cpu().float().numpy()\n",
    "    audio2 = net_g.voice_conversion(spec, spec_lengths, sid_src=sid_src, sid_tgt=sid_tgt2)[0][0, 0].data.cpu().float().numpy()\n",
    "    audio3 = net_g.voice_conversion(spec, spec_lengths, sid_src=sid_src, sid_tgt=sid_tgt3)[0][0, 0].data.cpu().float().numpy()\n",
    "print(\"Original SID: %d\" % sid_src.item())\n",
    "ipd.display(ipd.Audio(y[0].cpu().numpy(), rate=hps.data.sample_rate, normalize=False))\n",
    "print(\"Converted SID: %d\" % sid_tgt1.item())\n",
    "ipd.display(ipd.Audio(audio1, rate=hps.data.sample_rate, normalize=False))\n",
    "print(\"Converted SID: %d\" % sid_tgt2.item())\n",
    "ipd.display(ipd.Audio(audio2, rate=hps.data.sample_rate, normalize=False))\n",
    "print(\"Converted SID: %d\" % sid_tgt3.item())\n",
    "ipd.display(ipd.Audio(audio3, rate=hps.data.sample_rate, normalize=False))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
