{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:25.273685Z",
     "start_time": "2024-02-02T20:27:25.269954Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"3\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:31.135560Z",
     "start_time": "2024-02-02T20:27:25.275542Z"
    }
   },
   "outputs": [],
   "source": [
    "from suno_utils.gpt.chirp_v2 import preload_codec_models as preload_codec_models_8k, codec_decode as decode_8k\n",
    "preload_codec_models_8k(\"/home/victor/data/models/chirp_v2/dac_2c_25x8.pt\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 0.8859999999999999 GB for dac_2c_25x8.pt\n",
    "### 0.46599999999999997 GB for dac_2c_25x12.pt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:32.337358Z",
     "start_time": "2024-02-02T20:27:31.138603Z"
    }
   },
   "outputs": [],
   "source": [
    "from suno_utils.tasks.dac_2c_12cb import decode_stream, encode_files, decode\n",
    "import numpy as np\n",
    "from suno_utils.audio import Audio\n",
    "from suno_utils.gpt.chirp_v2_5 import (\n",
    "    preload_codec_models,\n",
    "    codec_decode,\n",
    "    GenerationConfig,\n",
    "    codec_encode,\n",
    "    codec_decode_stream_to_full_audio\n",
    ")\n",
    "import torch\n",
    "preload_codec_models(\"/home/victor/data/models/chirp_v2/dac_2c_25x12.pt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:39.550347Z",
     "start_time": "2024-02-02T20:27:32.344250Z"
    }
   },
   "outputs": [],
   "source": [
    "# test_wav_path = \"StarWars60.wav\"\n",
    "test_wav_path = \"/home/tony/test.mp3\"\n",
    "audio = Audio.from_file(test_wav_path)\n",
    "audio.play()\n",
    "\n",
    "encoded = encode_files([test_wav_path], max_duration_per_file_s=200)[0]\n",
    "encoded.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:39.555710Z",
     "start_time": "2024-02-02T20:27:39.552415Z"
    }
   },
   "outputs": [],
   "source": [
    "input_array = np.load(\"audios/d9fb84e4-3ca4-4321-9b9d-5d8f4d097a77.npz\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:39.619570Z",
     "start_time": "2024-02-02T20:27:39.557827Z"
    }
   },
   "outputs": [],
   "source": [
    "input_array[\"v1_raw\"].shape\n",
    "encoded = input_array[\"v1_raw\"][:, 1:]\n",
    "print(encoded.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:47.801809Z",
     "start_time": "2024-02-02T20:27:39.622253Z"
    }
   },
   "outputs": [],
   "source": [
    "normal_decoded = decode_8k(encoded)\n",
    "normal_decoded.play()\n",
    "# normal_decoded.write_wav(\"sw_1.wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:51.783376Z",
     "start_time": "2024-02-02T20:27:47.803856Z"
    }
   },
   "outputs": [],
   "source": [
    "encoded = codec_encode(normal_decoded)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:51.788033Z",
     "start_time": "2024-02-02T20:27:51.785000Z"
    }
   },
   "outputs": [],
   "source": [
    "encoded.shape[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:51.851649Z",
     "start_time": "2024-02-02T20:27:51.790434Z"
    }
   },
   "outputs": [],
   "source": [
    "decode_diffs = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:27:51.913468Z",
     "start_time": "2024-02-02T20:27:51.853072Z"
    }
   },
   "outputs": [],
   "source": [
    "n_stride_tokens = 20\n",
    "n_overlap_tokens = 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-02T20:28:02.329419Z",
     "start_time": "2024-02-02T20:27:51.914689Z"
    }
   },
   "outputs": [],
   "source": [
    "decoded = decode_stream(\n",
    "    (encoded[i] for i in range(encoded.shape[0])),\n",
    "    n_stride_tokens=n_stride_tokens,\n",
    "    n_overlap_tokens=n_overlap_tokens,\n",
    ")\n",
    "audios = []\n",
    "for x in decoded:\n",
    "    audios.append(x[0])\n",
    "audio = Audio.concatenate(audios)\n",
    "audio.play()\n",
    "# audio.write_wav(\"sw_2.wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-29T15:10:15.100870Z",
     "start_time": "2024-01-29T15:10:05.316103Z"
    }
   },
   "outputs": [],
   "source": [
    "audio = codec_decode_stream_to_full_audio(encoded)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-29T15:10:15.128930Z",
     "start_time": "2024-01-29T15:10:15.102579Z"
    }
   },
   "outputs": [],
   "source": [
    "print(normal_decoded.array_float[0].shape, audio.array_float[0].shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-29T15:10:18.985455Z",
     "start_time": "2024-01-29T15:10:15.130188Z"
    }
   },
   "outputs": [],
   "source": [
    "differences = []\n",
    "max_diff = 0\n",
    "for track_id in [0, 1]:\n",
    "    for i, (v_normal, v_stream) in enumerate(\n",
    "        zip(\n",
    "            audio.array_float[track_id].tolist(),\n",
    "            normal_decoded.array_float[track_id].tolist(),\n",
    "        )\n",
    "    ):\n",
    "        differences.append(abs(v_normal - v_stream))\n",
    "        if abs(v_normal - v_stream) > 0.01:\n",
    "            # print(i, abs(v_normal - v_stream))\n",
    "            if max_diff < abs(v_normal - v_stream):\n",
    "                max_index = i\n",
    "                max_diff = abs(v_normal - v_stream)\n",
    "decode_diffs.append([differences, n_stride_tokens, n_overlap_tokens])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-24T03:50:41.635561Z",
     "start_time": "2024-01-24T03:48:13.173295Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "\n",
    "plt.clf()\n",
    "for differences, n_stride_tokens, n_overlap_tokens in decode_diffs:\n",
    "    plt.hist(\n",
    "        differences,\n",
    "        alpha=0.5,\n",
    "        bins=np.linspace(0, 0.008, 200),\n",
    "        label=f\"stride: {n_stride_tokens}, overlap: {n_overlap_tokens}, mean {np.mean(differences):.4f}, std {np.std(differences):.4f} \",\n",
    "    )\n",
    "plt.xlabel(\"audio wav differences\")\n",
    "plt.ylabel(\"number of counts (48k per sec)\")\n",
    "plt.legend()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-24T03:46:46.803725Z",
     "start_time": "2024-01-24T03:46:45.171442Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.plot(differences)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-23T18:51:35.262819Z",
     "start_time": "2024-01-23T18:51:34.482937Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.plot(differences)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-01-23T18:50:46.029566Z",
     "start_time": "2024-01-23T18:50:44.876951Z"
    }
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "plt.plot(differences)\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "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": 2
}
