{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fae8f9c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:02:47.721113Z",
     "start_time": "2023-12-21T16:02:44.658896Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0\"\n",
    "\n",
    "import numpy as np\n",
    "from suno_utils.audio import Audio\n",
    "from suno_utils.gpt.chirp_v2 import (\n",
    "    generate_audio,\n",
    "    generate_audio_stream,\n",
    "    preload_models,\n",
    "    codec_decode,\n",
    "    GenerationConfig,\n",
    "    semantic_encode,\n",
    "    codec_encode,\n",
    "    hoot_encode,\n",
    "    hoot_encode_and_align,\n",
    ")\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "01a25410",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:02:47.863398Z",
     "start_time": "2023-12-21T16:02:47.722342Z"
    }
   },
   "outputs": [],
   "source": [
    "!ls /app/suno/checkpoints/2023-12-18_13-32-12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d7778d50",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:02:47.866377Z",
     "start_time": "2023-12-21T16:02:47.864756Z"
    }
   },
   "outputs": [],
   "source": [
    "# 2023-12-02_23-30-13 fine tune v4 # 2023-12-01_17-14-49 fine tune v3 # 2023-10-17_16-01-32 is the default one "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9108708b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:02:58.459894Z",
     "start_time": "2023-12-21T16:02:47.867982Z"
    }
   },
   "outputs": [],
   "source": [
    "_ = preload_models(\n",
    "    gpt_ckpt_path=\"/app/suno/checkpoints/2023-12-18_13-32-12/last_ckpt_infer.pt\",\n",
    "    # gpt_ckpt_path=\"/app/suno/checkpoints/2023-10-17_16-01-32/last_ckpt_infer.pt\",\n",
    "    tokenizer_path=\"/home/victor/data/models/chirp_v2/tokenizer_60k.json\",\n",
    "    semantic_ckpt_path=\"/home/victor/data/models/chirp_v2/mert_25.pt\",\n",
    "    semantic_centroids_path=\"/home/victor/data/models/chirp_v2/mert_25_2x4k.npy\",\n",
    "    codec_ckpt_path=\"/home/victor/data/models/chirp_v2/dac_2c_25x8.pt\",\n",
    "    fasttext_ckpt_path=\"/home/victor/data/models/lid.176.bin\",\n",
    "    # hoot_ckpt_path=\"/home/tony/Data/Hoot/v1_models/hoot_ckpt.pt\",\n",
    "    hoot_ckpt_path=\"/home/tony/Data/checkpoints/hoot/2023-11-22_07-04-38/50k_ckpt.pt\",\n",
    "    hoot_tokenizer_path=\"/home/tony/Data/Hoot/v1_models/tokenizer.model\",\n",
    "    load_whisper=True,\n",
    "    use_gpu=True,\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "edc87efa",
   "metadata": {},
   "source": [
    "# continuation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7ae437e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:18.065783Z",
     "start_time": "2023-12-21T16:03:18.022815Z"
    }
   },
   "outputs": [],
   "source": [
    "input_audio = Audio.from_file(\"audios/test_jingle_martin.wav\")\n",
    "# input_audio = Audio.from_file(\"audios/test_jingle_martin_2.wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8416f6a2",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:18.537080Z",
     "start_time": "2023-12-21T16:03:18.509761Z"
    }
   },
   "outputs": [],
   "source": [
    "input_audio.play(compress=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f29880e0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:36.678275Z",
     "start_time": "2023-12-21T16:03:36.663909Z"
    }
   },
   "outputs": [],
   "source": [
    "test_input_audio = input_audio.get_segment(from_s=0, to_s=10.01) # input_audio.get_segment(from_s=0, to_s=18)\n",
    "test_input_audio.play(compress=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9bd670c6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:39.058150Z",
     "start_time": "2023-12-21T16:03:38.883881Z"
    }
   },
   "outputs": [],
   "source": [
    "oracle_semantic_arr = semantic_encode(test_input_audio, n_codebooks=1)\n",
    "oracle_coarse_arr = codec_encode(test_input_audio)[:oracle_semantic_arr.shape[0], :]\n",
    "print(oracle_semantic_arr.shape, oracle_coarse_arr.shape)\n",
    "oracle_arr = np.concatenate(\n",
    "    [oracle_semantic_arr, oracle_coarse_arr],\n",
    "    axis=-1,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "69c5b6bb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:39.090769Z",
     "start_time": "2023-12-21T16:03:39.089367Z"
    }
   },
   "outputs": [],
   "source": [
    "# np.save(\"/home/tony/Data/Prompts/jingle2.npy\", oracle_arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ae3c932",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:39.274175Z",
     "start_time": "2023-12-21T16:03:39.272857Z"
    }
   },
   "outputs": [],
   "source": [
    "# oracle_arr = np.load(\"/home/tony/Data/Prompts/jingle.npy\")\n",
    "# print(oracle_arr.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "508d72e3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:23:18.273716Z",
     "start_time": "2023-12-21T16:23:18.271502Z"
    }
   },
   "outputs": [],
   "source": [
    "prompt_text = \"\"\"\n",
    "[start]\n",
    "[Verse]\n",
    "Another year has come and gone, and I'm not where I long to be\n",
    "Snowfall on the city streets, but it ain't Christmas to me\n",
    "My loved ones gathered 'round, but there's an empty space nearby\n",
    "I'm a thousand miles away, and I'm not by their side\n",
    "\n",
    "[Chorus]\n",
    "I'm away from home for the Yuletide (Yuletide)\n",
    "Missing heaps of snow and grandma's cherry pie\n",
    "I'm away from home for the Yuletide (Yuletide)\n",
    "Seeking mistletoe and missing you by my side\n",
    "[end]\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "984e8c9b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:36:20.060222Z",
     "start_time": "2023-12-21T16:35:40.811453Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=prompt_text,\n",
    "        text_tags=\"pop christmas\",\n",
    "        text_neg_control_tags=\"instrumental noise ameteur\",\n",
    "        n_batch=4,\n",
    "        cfg_coef=1.5,\n",
    "        temp_semantic=0.99,\n",
    "        temp_coarse=0.99,\n",
    "        max_gen_duration_s=50,\n",
    "        history_arr=oracle_arr,\n",
    "        history_do_asr=False,\n",
    "        use_whisper=False\n",
    "    ),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7c04f09a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:36:23.260118Z",
     "start_time": "2023-12-21T16:36:20.061822Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c21f252",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:05.110690Z",
     "start_time": "2023-12-21T16:03:04.956321Z"
    }
   },
   "outputs": [],
   "source": [
    "BREAK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "296b34eb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:05.112288Z",
     "start_time": "2023-12-21T16:03:05.112279Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df83ed93",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:05.112911Z",
     "start_time": "2023-12-21T16:03:05.112902Z"
    }
   },
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "On a vibrant summer morning, curiosity drew me to the innovative realm of Suno AI. Their latest\n",
    "masterpiece, Chirp, known for its unique ability to generate music from words, promised a bold\n",
    "step forward in music creation. I dived into their platform, typing in familiar lyrics. As I clicked\n",
    "'Create,' the ensuing symphony seamlessly blended human emotion and machine precision,\n",
    "heralding a new era in the convergence of music, words, and machine learning\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b5a2270",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:05.113715Z",
     "start_time": "2023-12-21T16:03:05.113706Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=text,\n",
    "        text_tags=\"hifi rap\",\n",
    "        n_batch=2,\n",
    "        max_gen_duration_s=120,\n",
    "        # stream=False,\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7b5656bb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:05.114481Z",
     "start_time": "2023-12-21T16:03:05.114473Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84046437",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-12-21T16:03:05.115150Z",
     "start_time": "2023-12-21T16:03:05.115141Z"
    }
   },
   "outputs": [],
   "source": [
    "[1, 1, 1].any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f1f4118f",
   "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.14"
  },
  "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
}
