{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a0d83737",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !nvidia-smi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "88c3355d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"7\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b59c8785",
   "metadata": {},
   "outputs": [],
   "source": [
    "from suno_utils.audio import Audio\n",
    "from generation import preload_models, generate, _retrieve_models\n",
    "\n",
    "CKPT_DIR = \"/home/christian/code/neon/stable-audio-tools/harmonai_train/\"\n",
    "_ = preload_models(\n",
    "    tokenizer_filepath=\"/home/georg/notebooks/gpu_nb/tmp/tokenizer_60k.json\",\n",
    "    semantic_model_filepath=\"/home/georg/notebooks/gpu_nb/tmp/mert_25.pt\",\n",
    "    semantic_clusters_filepath=\"/home/georg/notebooks/gpu_nb/tmp/mert_25_2x4k.npy\",\n",
    "    ### 25hz ###\n",
    "    codec_filepath=\"/home/georg/notebooks/gpu_nb/tmp/25hz_vae_peaq_kl_0.005.pth\",\n",
    "    #\n",
    "    #\n",
    "    # 8 nodes 5s\n",
    "    # dit_model_filepath=\"/app/suno/checkpoints/2025-01-13_22-30-17_s9391/last_ckpt_infer.pt\",\n",
    "    # 16 nodes 5s single chunks\n",
    "    # dit_model_filepath=\"/app/suno/checkpoints/2025-01-17_22-35-39_s2528/last_ckpt_infer.pt\",\n",
    "    # 16 nodes 5s\n",
    "    # dit_model_filepath=\"/app/suno/checkpoints/2025-01-15_04-45-00_s7938/last_ckpt_infer.pt\",\n",
    "    # 16 nodes 1s\n",
    "    # dit_model_filepath=\"/app/suno/checkpoints/2025-01-15_04-53-13_s8269/last_ckpt_infer.pt\",\n",
    "    # 16 nodes 5s no text pad\n",
    "    dit_model_filepath=\"/app/suno/checkpoints/2025-01-18_16-22-22_s4434/last_ckpt_infer.pt\",\n",
    "    # single chunks only\n",
    "    # dit_model_filepath=\"/app/suno/checkpoints/2025-01-21_00-14-22_s2829/last_ckpt_infer.pt\",\n",
    ")\n",
    "models = _retrieve_models()\n",
    "if models[\"dit_model\"].ctx_len is not None:\n",
    "    model_duration_s = 6 * 60\n",
    "else:\n",
    "    model_duration_s = models[\"dit_model\"].block_size // models[\"dit_model\"].io_hz\n",
    "duration_s = 2 * 60 if model_duration_s >= 2 * 60 else 30"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "61510416",
   "metadata": {},
   "outputs": [],
   "source": [
    "offset_s = 0\n",
    "\n",
    "samples = {\n",
    "    \"walking_down\": {\n",
    "        \"audio_fp\": \"/home/georg/notebooks/samples/walking_down.mp3\",\n",
    "        \"tags\": \"Country\",\n",
    "        \"lyrics\": \"Walking down the street\\nFeeling so alive\\nGot my head in the clouds\\nGot a gleam in my eye\\nEvery step that i take\\nIs like a brand new start\\nNo matter where I'm going\\nI'll always find my part\\n\\nOooo\\n\\nLife is like a high wire act\\nwe're dancing in the sky\\nIt's a worry no need to ask why\\nWith a little bit or courage\\nWe can chase our dreams\\nNo matter what comes away\\nWe'll always be a team\\n\\nDon't stop oooo, yeah\",\n",
    "    },\n",
    "    # \"norah\": {\n",
    "    #     \"audio_fp\": \"/home/georg/notebooks/samples/norah.wav\",\n",
    "    #     \"tags\": \"jazz, pop, female vocal\",\n",
    "    #     \"lyrics\": \"[Verse 1]\\nI waited 'til I saw the sun\\nI don't know why I didn't come\\nI left you by the house of fun\\nI don't know why I didn't come\\nI don't know why I didn't come\\n\\n[Verse 2]\\nWhen I saw the break of day\\nI wished that I could fly away\\nInstead of kneeling in the sand\\nCatching teardrops in my hand\\n\\n[Refrain]\\nMy heart is drenched in wine\\nBut you'll be on my mind forever\\n\\n[Verse 3]\\nOut across the endless sea\\nI will die in ecstasy\\nBut I'll be a bag of bones\\nDriving down the road alone\\n\\n[Refrain]\\nMy heart is drenched in wine\\nBut you'll be on my mind\",\n",
    "    # },\n",
    "    \"turkey_time\": {\n",
    "        \"audio_fp\": \"/home/georg/notebooks/samples/turkey_time.mp3\",\n",
    "        \"tags\": \"Country\",\n",
    "        \"lyrics\": \"[Verse]\\nGather 'round now, it's turkey time\\nCatchin' up with all your kin and dimes\\nFeastin' on that golden bird, so fine (so fine)\\nPass the gravy, pour some wine\\n\\n[Chorus]\\nTurkey time, it's a country rhyme\\nGrab your plate, fill it up, take your time\\nAnd when the sun starts settin', we'll gather 'round\\nSingin' songs, sippin' cider, dancin' all night sound (oooh)\\nTurkey time, it's a country rhyme\\n\\n[Verse]\\nWell, it's that time of year, with family near\\nGatherin' 'round the table, filled with cheer\\nTurkey's roasting, gravy's flowin'\\nMashed potatoes piled high, it's Thanksiving time\\n\\n[Chorus]\\nTurkey time, it's a country rhyme\\nGrab your plate, fill it up, take your time\\nAnd when the sun starts settin', we'll gather 'round\\nSingin' songs, sippin' cider, dancin' all night sound (oooh)\\nTurkey time, it's a country rhyme\\n\\n[Bridge]  \\nTurkey time is here again\\nTurkey time is here again\\nTurkey time is here again\\nTurkey tiiiiime!\",\n",
    "    },\n",
    "    # \"mistletoe\": {\n",
    "    #     \"audio_fp\": \"/home/georg/notebooks/samples/mistletoe.mp3\",\n",
    "    #     \"tags\": \"Country\",\n",
    "    #     \"lyrics\": \"[intro]\\nooooh\\n(what is life, what is love?)\\n[Verse]\\nAnother year has come and gone, and I'm not where I long to be\\nSnowfall on the city streets, but it ain't Christmas to me\",\n",
    "    # },\n",
    "    \"halo\": {\n",
    "        \"audio_fp\": \"/home/georg/notebooks/samples/halo.wav\",\n",
    "        \"tags\": \"R&B, Ballad, Pop\",\n",
    "        \"lyrics\": \"[Verse 1]\\nRemember those walls I built?\\nWell, baby, they're tumblin' down\\nAnd they didn't even put up a fight\\nThey didn't even make a sound\\nI found a way to let you in\\nBut I never really had a doubt\\nStandin' in the light of your halo\\nI got my angel now\\n\\n[Pre-Chorus]\\nIt's like I've been awakened\\nEvery rule, I had you breakin'\\nIt's the risk that I'm takin'\\nI ain't ever gonna shut you out\\n\\n[Chorus]\\nEverywhere I'm lookin' now\\nI'm surrounded by your embrace\\nBaby, I can see your halo\\nYou know you're my savin' grace\\nYou're everything I need and more\\nIt's written all over your face\\nBaby, I can feel your halo\\nPray it won't fade away\\n\\n[Post-Chorus]\\nI can feel your halo, halo, halo\\nI can see your halo, halo, halo\\nI can feel your halo, halo, halo\\nI can see your halo, halo, halo, ooh\\n\\n[Verse 2]\\nHit me like a ray of sun\\nBurnin' through my darkest night\\nYou're the only one that I want\\nThink I'm addicted to your light\\nI swore I'd never fall again\\nBut this don't even feel like fallin'\\nGravity can't begin\\nTo pull me back to\",\n",
    "    },\n",
    "}\n",
    "\n",
    "# # Select current sample\n",
    "# current_sample = \"turkey_time\"\n",
    "# audio_fp = samples[current_sample][\"audio_fp\"]\n",
    "# tags = samples[current_sample][\"tags\"]\n",
    "# lyrics = samples[current_sample][\"lyrics\"]\n",
    "\n",
    "# if current_sample == \"halo\":\n",
    "#     offset_s = 20 if duration_s <= 30.1 else 0\n",
    "\n",
    "# audio = (\n",
    "#     Audio.from_file(audio_fp, n_channels=2)\n",
    "#     .get_segment(from_s=offset_s, to_s=offset_s + duration_s + 0.01)\n",
    "#     .normalize_volume()\n",
    "# )\n",
    "\n",
    "# # TODO: pad with silence for integer multiple of sliding\n",
    "# if models[\"dit_model\"].ctx_len is not None:\n",
    "#     window_s = int(\n",
    "#         round((models[\"dit_model\"].block_size - models[\"dit_model\"].ctx_len) / models[\"dit_model\"].io_hz)\n",
    "#     )\n",
    "#     if audio.duration_s % window_s > 0.1:\n",
    "#         audio = Audio.concatenate(\n",
    "#             [\n",
    "#                 audio,\n",
    "#                 Audio.from_silence(\n",
    "#                     window_s - audio.duration_s % window_s + 0.01,\n",
    "#                     sample_rate=audio.sample_rate,\n",
    "#                     n_channels=audio.n_channels,\n",
    "#                 ),\n",
    "#             ]\n",
    "#         )\n",
    "# audio.normalize_volume().play()\n",
    "# print(lyrics)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9c6c8c80",
   "metadata": {},
   "outputs": [],
   "source": [
    "# steps = 16\n",
    "# cfg_coef = 1\n",
    "\n",
    "# audios = []\n",
    "# for n in range(1):\n",
    "#     pred_audio = generate(\n",
    "#         audio,\n",
    "#         lyrics=lyrics,\n",
    "#         tags=tags,\n",
    "#         text_cfg_coef=2.0,\n",
    "#         steps=steps,\n",
    "#         seed=n,\n",
    "#     ).normalize_volume()\n",
    "#     audios.append(pred_audio)\n",
    "#     pred_audio.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "acda5fe8",
   "metadata": {},
   "outputs": [],
   "source": [
    "for name, sample in samples.items():\n",
    "    audio = Audio.from_file(sample[\"audio_fp\"], n_channels=2)\n",
    "    lyrics = sample[\"lyrics\"]\n",
    "    # lyrics = \"cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat cat \"\n",
    "    tags = sample[\"tags\"]\n",
    "    # Generate once for each sample\n",
    "    pred_audio = generate(\n",
    "        audio,\n",
    "        lyrics=lyrics,\n",
    "        tags=tags,\n",
    "        text_cfg_coef=4.0,\n",
    "        steps=32,\n",
    "        seed=0,\n",
    "    ).normalize_volume()\n",
    "\n",
    "    print(name)\n",
    "    pred_audio.play()\n",
    "    print(lyrics)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5ea08dfd",
   "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
