{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ffdbda4b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:16:50.543968Z",
     "start_time": "2023-09-06T17:16:50.542484Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "dd41c10c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:16:53.667092Z",
     "start_time": "2023-09-06T17:16:50.544832Z"
    }
   },
   "outputs": [],
   "source": [
    "import random\n",
    "import json\n",
    "import numpy as np\n",
    "import tqdm\n",
    "import torch\n",
    "import funcy\n",
    "import time\n",
    "import gc\n",
    "from scipy.io import wavfile\n",
    "import tempfile\n",
    "import collections\n",
    "from collections import defaultdict\n",
    "from joblib import Parallel, delayed\n",
    "\n",
    "from suno_utils.utils.s3 import _apply_mp\n",
    "from suno_utils.audio import Audio\n",
    "from suno_utils.tasks.data_loader import load_audio_mp\n",
    "from suno_utils.utils.text import write_jsonl, read_jsonl, write_json, read_json\n",
    "from suno_utils.utils.s3 import read_from_s3, check_s3_file_exists, open_from_s3\n",
    "from suno_utils.audio.conversion import convert_audio_files\n",
    "\n",
    "SAMPLE_RATE = 24_000\n",
    "EMBEDDING_RATE = 25\n",
    "N_CODEBOOKS = 8\n",
    "\n",
    "IN_DATA_DIR = \"/app/suno/data/mert_25hz\"\n",
    "IN_AUDIO_DIR = os.path.join(IN_DATA_DIR, \"audio\")\n",
    "IN_TSV_DIR = os.path.join(IN_DATA_DIR, \"audio_tsv\")\n",
    "IN_LABEL_DIR = os.path.join(IN_DATA_DIR, \"label\")\n",
    "\n",
    "OUT_DATA_DIR = \"/app/suno/data/mert_25hz_short\"\n",
    "OUT_AUDIO_DIR = os.path.join(OUT_DATA_DIR, \"audio\")\n",
    "OUT_TSV_DIR = os.path.join(OUT_DATA_DIR, \"audio_tsv\")\n",
    "OUT_LABEL_DIR = os.path.join(OUT_DATA_DIR, \"label\")\n",
    "OUT_TEMP_DIR = os.path.join(OUT_DATA_DIR, \"temp\")\n",
    "\n",
    "os.makedirs(OUT_DATA_DIR, exist_ok=True)\n",
    "os.makedirs(OUT_AUDIO_DIR, exist_ok=True)\n",
    "os.makedirs(OUT_TSV_DIR, exist_ok=True)\n",
    "os.makedirs(OUT_LABEL_DIR, exist_ok=True)\n",
    "os.makedirs(OUT_TEMP_DIR, exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "195c03f2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:16:53.670018Z",
     "start_time": "2023-09-06T17:16:53.668304Z"
    }
   },
   "outputs": [],
   "source": [
    "MIN_SIZE = SAMPLE_RATE * 5\n",
    "MAX_SIZE = SAMPLE_RATE * 12"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c729d86d",
   "metadata": {},
   "source": [
    "# File loading"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "66631638",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:16:54.300102Z",
     "start_time": "2023-09-06T17:16:53.671659Z"
    }
   },
   "outputs": [],
   "source": [
    "# load current data\n",
    "tsv_info = []\n",
    "with open(os.path.join(IN_TSV_DIR, \"train.tsv\"), \"r\") as f:\n",
    "    for line in f.read().strip().split(\"\\n\"):\n",
    "        if len(line.strip()) == 0:\n",
    "            continue\n",
    "        tsv_info.append(line.strip().split(\"\\t\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "cad52faa",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:16:54.303887Z",
     "start_time": "2023-09-06T17:16:54.301923Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "436804\n"
     ]
    }
   ],
   "source": [
    "print(len(tsv_info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "741d5c23",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:16:54.354280Z",
     "start_time": "2023-09-06T17:16:54.304983Z"
    }
   },
   "outputs": [],
   "source": [
    "tsv_data_path = tsv_info[0]\n",
    "tsv_data = tsv_info[1:]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d17fd56f",
   "metadata": {},
   "source": [
    "## Slice the TSV files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "c4906e01",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:17:06.346433Z",
     "start_time": "2023-09-06T17:16:54.355730Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|████████████████████████████████████████████████████████████████████████████████████████████████████| 436803/436803 [00:11<00:00, 36572.24it/s]\n"
     ]
    }
   ],
   "source": [
    "new_tsv_data = []\n",
    "# list of list of tuples,\n",
    "# each index is the audio's codec spllit list (start, end)\n",
    "split_times = []\n",
    "for n_row, (audio_fn, audio_size) in tqdm.tqdm(\n",
    "    enumerate(tsv_data), total=len(tsv_data)\n",
    "):\n",
    "    new_splits = []\n",
    "    for n in range(int(np.ceil(int(audio_size) / MAX_SIZE))):\n",
    "        start_idx = n * MAX_SIZE\n",
    "        end_idx = min(int(audio_size), (n + 1) * MAX_SIZE)\n",
    "        if end_idx - start_idx < MIN_SIZE:\n",
    "            continue\n",
    "        out_audio_fn = f\"{n_row}_{n}.wav\"\n",
    "        new_tsv_data.append((audio_fn, out_audio_fn, start_idx, end_idx))\n",
    "        start_idx_embed = int(round(start_idx / SAMPLE_RATE * EMBEDDING_RATE))\n",
    "        end_idx_embed = int(round(end_idx / SAMPLE_RATE * EMBEDDING_RATE))\n",
    "        new_splits.append((start_idx_embed, end_idx_embed))\n",
    "    split_times.append(new_splits)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4defb74e",
   "metadata": {},
   "source": [
    "## Slice the codebook labels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a0b384f7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:17:06.351314Z",
     "start_time": "2023-09-06T17:17:06.347717Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "436803"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(split_times)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "c7a427db",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:17:06.401042Z",
     "start_time": "2023-09-06T17:17:06.352357Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(0, 300), (300, 600), (600, 900), (900, 1200), (1200, 1500), (1500, 1800), (1800, 2100), (2100, 2400), (2400, 2700), (2700, 3000), (3000, 3300), (3300, 3437)]\n"
     ]
    }
   ],
   "source": [
    "print(split_times[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9065a223",
   "metadata": {
    "ExecuteTime": {
     "start_time": "2023-09-07T04:20:15.949Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "codebook: 0\n"
     ]
    }
   ],
   "source": [
    "for codebook in range(N_CODEBOOKS):\n",
    "    print(\"codebook:\", codebook)\n",
    "    bunchsize = 100\n",
    "    bunch = []\n",
    "    with open(os.path.join(OUT_LABEL_DIR, f\"train.codec_{codebook}\"), \"w\") as f_out:\n",
    "        with open(os.path.join(IN_LABEL_DIR, f\"train.codec_{codebook}\"), \"r\") as f:\n",
    "            n_curr = 0\n",
    "            for n_row, line in tqdm.tqdm(enumerate(f.readlines())):\n",
    "                line = line.split()\n",
    "                split_time_row = split_times[n_row]\n",
    "                for (start_idx_embed, end_idx_embed) in split_time_row:\n",
    "                    bunch.append(\" \".join(line[start_idx_embed:end_idx_embed]) + \"\\n\")\n",
    "                n_curr += 1\n",
    "                if n_curr == bunchsize:\n",
    "                    n_curr = 0\n",
    "                    f_out.writelines(bunch)\n",
    "                    bunch = []\n",
    "        # J also do the last one\n",
    "        f_out.writelines(bunch)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "id": "5e34ae67",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T04:03:17.895825Z",
     "start_time": "2023-09-07T04:03:17.828079Z"
    }
   },
   "outputs": [],
   "source": [
    "for codebook in range(N_CODEBOOKS):\n",
    "    with open(os.path.join(OUT_LABEL_DIR, f\"dict.codec_{codebook}.txt\"), \"w\") as f:\n",
    "        for n in range(N_CODEBOOKS):\n",
    "            f.write(f\"{n} 1\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "id": "32fa51c5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T04:04:45.218614Z",
     "start_time": "2023-09-07T04:04:45.215554Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['0 1\\n', '1 1\\n', '2 1\\n', '3 1\\n', '4 1\\n', '5 1\\n', '6 1\\n', '7 1\\n']\n"
     ]
    }
   ],
   "source": [
    "with open(os.path.join(OUT_LABEL_DIR, f\"dict.codec_{codebook}.txt\"), \"r\") as f:\n",
    "    print(f.readlines())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "58b4c431",
   "metadata": {},
   "source": [
    "## Write the train TSV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d7231209",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:42:46.340970Z",
     "start_time": "2023-09-06T17:42:43.897349Z"
    }
   },
   "outputs": [],
   "source": [
    "with open(os.path.join(OUT_TSV_DIR, f\"train.tsv\"), \"w\") as f:\n",
    "    f.write(OUT_AUDIO_DIR + \"\\n\")\n",
    "    f.write(\n",
    "        \"\\n\".join(\n",
    "            [\n",
    "                fn + \"\\t\" + str(int(idx_end - idx_start))\n",
    "                for _, fn, idx_start, idx_end in new_tsv_data\n",
    "            ]\n",
    "        )\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7a85ab3f",
   "metadata": {},
   "source": [
    "## Generate the split audios"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "8aed8c10",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:42:58.446167Z",
     "start_time": "2023-09-06T17:42:52.603704Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "436803 work_items\n"
     ]
    }
   ],
   "source": [
    "# write new audios to disk\n",
    "def _write_new_wav_files(work_item):\n",
    "    from_fn, to_work_items = work_item\n",
    "    _, audio_arr = wavfile.read(os.path.join(IN_AUDIO_DIR, from_fn))\n",
    "    for to_fn, (start_idx, end_idx) in to_work_items:\n",
    "        wavfile.write(\n",
    "            os.path.join(OUT_AUDIO_DIR, to_fn),\n",
    "            SAMPLE_RATE,\n",
    "            audio_arr[start_idx:end_idx],\n",
    "        )\n",
    "\n",
    "\n",
    "work_items = defaultdict(list)\n",
    "for from_fn, to_fn, start_idx, end_idx in new_tsv_data:\n",
    "    work_items[from_fn].append((to_fn, (start_idx, end_idx)))\n",
    "work_items = [(k, v) for k, v in work_items.items()]\n",
    "print(len(work_items), \"work_items\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7a3b3f60",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T18:09:37.251591Z",
     "start_time": "2023-09-06T18:09:37.248430Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7085918"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(new_tsv_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "5e941403",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T18:10:31.486681Z",
     "start_time": "2023-09-06T18:10:15.743945Z"
    }
   },
   "outputs": [],
   "source": [
    "# import json\n",
    "# with open(\"train_origin.tsv\", \"w\") as fp:\n",
    "#     json.dump(new_tsv_data, fp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9cab4abe",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T18:09:32.086642Z",
     "start_time": "2023-09-06T18:09:32.084300Z"
    }
   },
   "outputs": [],
   "source": [
    "# _ = _apply_mp(\n",
    "#     _write_new_wav_files,\n",
    "#     work_items,\n",
    "#     chunksize=640,\n",
    "#     n_cores=32,\n",
    "#     joblib_backend=\"threads\",\n",
    "# )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "738138b8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T17:42:30.677232Z",
     "start_time": "2023-09-06T17:42:30.625842Z"
    }
   },
   "outputs": [],
   "source": [
    "# codec_data = defaultdict(list)\n",
    "# for codebook in range(N_CODEBOOKS):\n",
    "#     # print(i)\n",
    "#     with open(os.path.join(IN_LABEL_DIR, f\"train.codec_{codebook}\"), \"r\") as f:\n",
    "#         for line in f.read().strip().split(\"\\n\"):\n",
    "#             if len(line.strip()) == 0:\n",
    "#                 continue\n",
    "#             codec_data[codebook].append(line.strip().split())\n",
    "#     # break\n",
    "# for codebook in range(N_CODEBOOKS):\n",
    "#     with open(os.path.join(OUT_LABEL_DIR, f\"train.codec_{codebook}\"), \"w\") as f:\n",
    "#         f.write(\"\\n\".join(new_codec_data[codebook]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "id": "7428b9fc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:31:34.064279Z",
     "start_time": "2023-09-07T02:24:03.889046Z"
    }
   },
   "outputs": [],
   "source": [
    "files = os.listdir(\"/app/suno/data/mert_25hz_short/audio\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "4baa2a98",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:31:34.069021Z",
     "start_time": "2023-09-07T02:31:34.066514Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5603230"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(files)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "id": "e2b8431c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:31:34.324238Z",
     "start_time": "2023-09-07T02:31:34.070111Z"
    }
   },
   "outputs": [],
   "source": [
    "selected = [f for f in files if \"_\" in f]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "id": "5e5eb810",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:53:07.574317Z",
     "start_time": "2023-09-07T02:53:07.571549Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5229011, 7085918)"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(selected), len(new_tsv_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "id": "3f38d6d5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:54:28.014439Z",
     "start_time": "2023-09-07T02:54:28.011765Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('1.wav', '0_4.wav', 1152000, 1440000)"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_tsv_data[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "id": "ad7d2b4e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:55:04.584294Z",
     "start_time": "2023-09-07T02:55:03.087804Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "436803\n"
     ]
    }
   ],
   "source": [
    "must_unique_f = set(int(i[0].split(\".wav\")[0]) for i in new_tsv_data)\n",
    "print(len(must_unique_f))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "id": "ea9c7030",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T03:49:41.953599Z",
     "start_time": "2023-09-07T03:49:41.939992Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(1, 463758)"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "min(must_unique_f), max(must_unique_f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "id": "9d88ad2f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:55:24.369334Z",
     "start_time": "2023-09-07T02:55:22.836407Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "436803\n"
     ]
    }
   ],
   "source": [
    "must_unique_f_1 = set(int(i[1].split(\"_\")[0]) for i in new_tsv_data)\n",
    "print(len(must_unique_f_1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "id": "0859be0f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:57:25.211055Z",
     "start_time": "2023-09-07T02:57:25.208342Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5229011, 7085918)"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(selected), len(new_tsv_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "ceb1c434",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:57:39.399387Z",
     "start_time": "2023-09-07T02:57:38.296763Z"
    }
   },
   "outputs": [],
   "source": [
    "unique_f = set(int(f.split(\"_\")[0]) for f in selected)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "id": "8b093aed",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:57:39.403453Z",
     "start_time": "2023-09-07T02:57:39.401030Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "337756"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(unique_f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "id": "67dab7b1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:46:11.437039Z",
     "start_time": "2023-09-07T02:46:11.391474Z"
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "8ee0c8f2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T03:50:41.738230Z",
     "start_time": "2023-09-07T03:50:41.680965Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "112756 184900\n",
      "247918 274821\n",
      "162244 500000\n"
     ]
    }
   ],
   "source": [
    "c = 0\n",
    "s = None\n",
    "for i in range(500000):\n",
    "    if i not in unique_f:\n",
    "        c += 1\n",
    "        if s is None:\n",
    "            s = i\n",
    "        else:\n",
    "            continue\n",
    "    if i in unique_f:\n",
    "        if s is not None:\n",
    "            print(s, i)\n",
    "            s = None\n",
    "        # print(i)\n",
    "print(c, c + len(unique_f))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "4541b7ba",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T21:59:37.014277Z",
     "start_time": "2023-09-06T21:59:37.012201Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "112756"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(unique_f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "53769df5",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-06T21:59:37.236355Z",
     "start_time": "2023-09-06T21:59:37.232184Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[112736,\n",
       " 112737,\n",
       " 112738,\n",
       " 112739,\n",
       " 112740,\n",
       " 112741,\n",
       " 112742,\n",
       " 112743,\n",
       " 112744,\n",
       " 112745,\n",
       " 112746,\n",
       " 112747,\n",
       " 112748,\n",
       " 112749,\n",
       " 112750,\n",
       " 112751,\n",
       " 112752,\n",
       " 112753,\n",
       " 112754,\n",
       " 112755]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(sorted(unique_f))[-20:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "2f7dd898",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T00:51:23.568034Z",
     "start_time": "2023-09-07T00:51:23.564721Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('1.wav', '0_0.wav', 0, 288000)"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_tsv_data[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "7b26ee0b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T00:51:54.245019Z",
     "start_time": "2023-09-07T00:51:53.512686Z"
    }
   },
   "outputs": [],
   "source": [
    "new_wav = wavfile.read(os.path.join(OUT_AUDIO_DIR, '0_0.wav'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "id": "b9e62de4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T00:54:22.565356Z",
     "start_time": "2023-09-07T00:54:22.562256Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(288000, 1)"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "new_wav[1].reshape(-1, 1).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "3d73fab0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T00:52:29.427411Z",
     "start_time": "2023-09-07T00:52:27.396720Z"
    }
   },
   "outputs": [],
   "source": [
    "from nnAudio import features as nnAudioFeatures"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "id": "1722728f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T00:53:28.148373Z",
     "start_time": "2023-09-07T00:53:27.640337Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CQT kernels created, time used = 0.5050 seconds\n"
     ]
    }
   ],
   "source": [
    "spec_layer = nnAudioFeatures.cqt.CQT(sr=new_wav[0], hop_length=new_wav[0]//EMBEDDING_RATE, fmin=32.7, \n",
    "                                           fmax=None, n_bins=336, bins_per_octave=336//7, \n",
    "                                           filter_scale=1, norm=1, window='hann', center=True, \n",
    "                                           pad_mode='constant', trainable=False, \n",
    "                                           output_format='Magnitude', verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "id": "8fcbc412",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T01:00:35.908643Z",
     "start_time": "2023-09-07T01:00:35.904899Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([288000])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "input_tensor = torch.from_numpy(new_wav[1]).float()\n",
    "input_tensor.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "id": "85f56f93",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T01:00:37.935224Z",
     "start_time": "2023-09-07T01:00:37.932492Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "300"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "288000 // new_wav[0] * EMBEDDING_RATE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "id": "6ad79910",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T01:00:38.572993Z",
     "start_time": "2023-09-07T01:00:38.459900Z"
    }
   },
   "outputs": [],
   "source": [
    "output_tensor = spec_layer(input_tensor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "8e0f8508",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T01:00:39.115600Z",
     "start_time": "2023-09-07T01:00:39.112815Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 336, 301])"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_tensor.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "id": "2b405932",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T01:00:43.598526Z",
     "start_time": "2023-09-07T01:00:43.540744Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[  49.1279,   52.6179,   55.2570,  ..., 2552.8076, 1880.0348,\n",
       "          1353.1643],\n",
       "         [  52.0929,   58.9065,   66.4197,  ..., 1992.2830, 1287.6638,\n",
       "           714.6923],\n",
       "         [  51.8104,   59.5420,   68.0058,  ..., 2252.4263, 1565.3777,\n",
       "          1002.5173],\n",
       "         ...,\n",
       "         [   0.0000,    0.0000,    0.0000,  ...,  330.8419,  308.4335,\n",
       "           925.4127],\n",
       "         [   0.0000,    0.0000,    0.0000,  ...,  837.1494,  576.7127,\n",
       "           765.4628],\n",
       "         [   0.0000,    0.0000,    0.0000,  ...,  868.1017,  926.3876,\n",
       "           564.1667]]])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "75d4bfab",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T01:01:44.413557Z",
     "start_time": "2023-09-07T01:01:44.349371Z"
    }
   },
   "outputs": [],
   "source": [
    "output_tensor_half = spec_layer(input_tensor[:144000])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "id": "36a04b60",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T01:01:46.820207Z",
     "start_time": "2023-09-07T01:01:46.816609Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 49.1279,  52.6179,  55.2570,  ..., 149.2399, 147.4749, 144.6790],\n",
       "         [ 52.0929,  58.9065,  66.4197,  ..., 125.9033, 125.5422, 124.2411],\n",
       "         [ 51.8104,  59.5420,  68.0058,  ...,  95.9982,  95.1028,  93.6845],\n",
       "         ...,\n",
       "         [  0.0000,   0.0000,   0.0000,  ...,  45.0109,  18.7750,  76.7034],\n",
       "         [  0.0000,   0.0000,   0.0000,  ...,  26.5843,  46.0399,  61.8331],\n",
       "         [  0.0000,   0.0000,   0.0000,  ...,   6.8963,  67.4006,  36.9114]]])"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "output_tensor_half"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "7a576cfb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T01:05:44.769568Z",
     "start_time": "2023-09-07T01:05:44.764225Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 True\n",
      "1 True\n",
      "2 True\n",
      "3 True\n",
      "4 True\n",
      "5 True\n",
      "6 True\n",
      "7 True\n",
      "8 True\n",
      "9 True\n",
      "10 True\n",
      "11 True\n",
      "12 True\n",
      "13 True\n",
      "14 True\n",
      "15 True\n",
      "16 True\n",
      "17 True\n",
      "18 True\n",
      "19 True\n",
      "20 True\n",
      "21 True\n",
      "22 True\n",
      "23 True\n",
      "24 True\n",
      "25 True\n",
      "26 True\n",
      "27 True\n",
      "28 True\n",
      "29 True\n",
      "30 True\n",
      "31 True\n",
      "32 True\n",
      "33 True\n",
      "34 True\n",
      "35 True\n",
      "36 True\n",
      "37 True\n",
      "38 True\n",
      "39 True\n",
      "40 True\n",
      "41 True\n",
      "42 True\n",
      "43 True\n",
      "44 True\n",
      "45 True\n",
      "46 True\n",
      "47 True\n",
      "48 True\n",
      "49 True\n",
      "50 True\n",
      "51 True\n",
      "52 True\n",
      "53 True\n",
      "54 True\n",
      "55 True\n",
      "56 True\n",
      "57 True\n",
      "58 True\n",
      "59 True\n",
      "60 True\n",
      "61 True\n",
      "62 True\n",
      "63 True\n",
      "64 True\n",
      "65 True\n",
      "66 True\n",
      "67 True\n",
      "68 True\n",
      "69 True\n",
      "70 True\n",
      "71 True\n",
      "72 True\n",
      "73 True\n",
      "74 True\n",
      "75 True\n",
      "76 True\n",
      "77 True\n",
      "78 True\n",
      "79 True\n",
      "80 True\n",
      "81 True\n",
      "82 True\n",
      "83 True\n",
      "84 True\n",
      "85 True\n",
      "86 True\n",
      "87 True\n",
      "88 True\n",
      "89 True\n",
      "90 True\n",
      "91 True\n",
      "92 True\n",
      "93 True\n",
      "94 True\n",
      "95 True\n",
      "96 True\n",
      "97 True\n",
      "98 True\n",
      "99 True\n",
      "100 True\n",
      "101 True\n",
      "102 True\n",
      "103 True\n",
      "104 True\n",
      "105 True\n",
      "106 True\n",
      "107 True\n",
      "108 True\n",
      "109 True\n",
      "110 True\n",
      "111 True\n",
      "112 True\n",
      "113 True\n",
      "114 True\n",
      "115 True\n",
      "116 True\n",
      "117 True\n",
      "118 True\n",
      "119 True\n",
      "120 True\n",
      "121 True\n",
      "122 True\n",
      "123 True\n",
      "124 False\n",
      "125 False\n",
      "126 False\n",
      "127 False\n",
      "128 False\n",
      "129 False\n",
      "130 False\n",
      "131 False\n",
      "132 False\n",
      "133 False\n",
      "134 False\n",
      "135 False\n",
      "136 False\n",
      "137 False\n",
      "138 False\n",
      "139 False\n",
      "140 False\n",
      "141 False\n",
      "142 False\n",
      "143 False\n",
      "144 False\n",
      "145 False\n",
      "146 False\n",
      "147 False\n",
      "148 False\n",
      "149 False\n",
      "150 False\n"
     ]
    }
   ],
   "source": [
    "for i in range(151):\n",
    "    print(i, torch.equal(output_tensor_half[:, :, i], output_tensor[:, :, i]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "id": "91ca160e",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-09-07T02:04:23.054565Z",
     "start_time": "2023-09-07T02:04:23.051847Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "7085918"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(new_tsv_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e319cb90",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.10.12"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
