{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8fae8f9c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T02:20:24.219302Z",
     "start_time": "2023-11-10T02:20:20.651071Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"] = \"4\"\n",
    "\n",
    "import numpy as np\n",
    "from suno_utils.audio import Audio\n",
    "from suno_utils.gpt.chirp_v2_5 import (\n",
    "    GenerationConfig,\n",
    "    decode_stream,\n",
    "    preload_models,\n",
    "    prep_gconf,\n",
    ")\n",
    "from suno_utils.gpt.engine import Engine\n",
    "from suno_utils.gpt.generation_engine import align_codes, make_request\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "65b0d91a-1d19-4d67-b30d-9e2ef2038ea1",
   "metadata": {},
   "outputs": [],
   "source": [
    "gpt_ckpt_path = \"/app/suno/checkpoints/2024-12-17_14-56-36/last_ckpt_infer.pt\"  # all weights"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9108708b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T02:20:40.239982Z",
     "start_time": "2023-11-10T02:20:24.221206Z"
    }
   },
   "outputs": [],
   "source": [
    "preload_models(load_gpt=False, load_semantic=False, load_codec_device=\"cuda\")\n",
    "\n",
    "engine = Engine(\n",
    "    gpt_ckpt_path,\n",
    "    \"/app/suno/data/dpo/models/tokenizer_60k.json\",\n",
    "    max_sequences=12,\n",
    "    compile=False,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9f90f769-f017-440e-b91c-2f3527201917",
   "metadata": {},
   "outputs": [],
   "source": [
    "def generate_audio_with_engine(text, text_tags, personalize=None):\n",
    "\n",
    "    general_config = dict(\n",
    "        cfg_coef=1.0,  # no text cfg for dpo stream\n",
    "        min_eos_p=0.1,\n",
    "        eos_pad_duration_s=0,\n",
    "        # cfg_coef_tags=0.0,\n",
    "        # cfg_coef_tags_max_steps=None,  # collect the data for now\n",
    "        # temp_coarse=0.95,\n",
    "        cfg_coef_tags=2,\n",
    "        cfg_coef_neg_tags=-1,\n",
    "        text_neg_tags=\"repetitive, loop, noisy, distorted\",\n",
    "        n_repeat_tags=1,\n",
    "        use_whisper=False,\n",
    "        text_start_control_tags=\"{start} \",\n",
    "        random_seed=42,\n",
    "        n_batch=1,\n",
    "    )\n",
    "    \n",
    "    if personalize is not None:\n",
    "        general_config[\"text_start_control_tags\"] = \"{P: \" + str(personalize) + \"}\"\n",
    "    cfg = GenerationConfig(\n",
    "        text=text, text_tags=text_tags, max_gen_duration_s=240, **general_config\n",
    "    )\n",
    "    preped_cfg = prep_gconf(cfg)\n",
    "    model_conf = engine.model.config\n",
    "    requests = []\n",
    "    for i in range(1, 4):\n",
    "        request = make_request(\n",
    "            f\"{i}\", preped_cfg, engine.model.config, engine.tokenizer\n",
    "        )\n",
    "        requests.append(request)\n",
    "\n",
    "    jobs = engine.run_request(requests)\n",
    "\n",
    "    for job in jobs:\n",
    "        stream = engine.token_generator(job)\n",
    "        audios = []\n",
    "        for audio in decode_stream(align_codes(tqdm(stream), model_conf)):\n",
    "            audios.append(audio[0])\n",
    "        audio_continued = Audio.concatenate(audios)\n",
    "        audio_continued.play()\n",
    "    return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "072adcda",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T03:31:45.479329Z",
     "start_time": "2023-11-10T03:31:45.477303Z"
    }
   },
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "[intro]\n",
    "\n",
    "[verse]\n",
    "Walking down the street, feeling so alive\n",
    "Got my head in the clouds, got a gleam in my eye\n",
    "Every step I take, it's like a brand new start\n",
    "No matter where I'm going, I'll always find my part\n",
    "(oh-oh-oh)\n",
    "\n",
    "[chorus]\n",
    "Life is like a high-wire act, we're dancing in the sky\n",
    "No need to worry, no need to ask why\n",
    "With a little bit of courage, we can chase our dreams\n",
    "No matter what comes our way, we'll always be a team\n",
    "(we're unstoppable, yeah)\n",
    "\n",
    "[outro]\n",
    "\"\"\"\n",
    "text_tags =  \"orchestral film epic\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4bad19d9-b0ff-4660-b202-1e518c3aed63",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d8adb403-8b94-4c4a-959d-9190b40814c7",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags, personalize=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2e41b809-a7c2-4a74-a674-61a267439978",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags, personalize=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c5e8db8-7310-4cf5-9b07-f95b0fdca2b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "[Verse 1]\n",
    "Almost Heaven, West Virginia\n",
    "Blue Ridge Mountains, Shenandoah River\n",
    "Life is old there, older than the trees\n",
    "Younger than the mountains, growing like a breeze\n",
    "\n",
    "[Chorus]\n",
    "Country roads, take me home\n",
    "To the place I belong\n",
    "West Virginia, mountain mama\n",
    "Take me home, country roads\n",
    "\"\"\"\n",
    "text_tags = \"bluegrass, female\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1dc7f332-c168-4b1e-84ff-0e4055c218f9",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39a98305-82e4-4873-b79a-21b4f699279e",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags, personalize=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "91498861-8634-4ea7-832a-15a356ec12f4",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags, personalize=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "33b18edd-b6a9-4518-8140-d57056299850",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "[verse]\n",
    "A is for the amazing grace that we recieve\n",
    "B is for the blessings, every day we blieve\n",
    "C is for the chorus, we sing it loud and clear\n",
    "D is for the devotion that we hold dear\n",
    "\n",
    "[Chorus]\n",
    "B is for Buttocks, ripe and slightly damp\n",
    "Yum Yum, boy oh boy do I like God\n",
    "Every day is a gift, when you won a skateboard ramp.\n",
    "Yum yum, boy oh boy do I like God.\n",
    "\n",
    "[outro]\n",
    "\"\"\"\n",
    "text_tags = \"gregorian chant\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2e650b5-1a9a-495a-9e77-38538554014b",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d2da8dc7-89ec-4afb-8db0-8e204480339b",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags, personalize=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c2f7ecf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags, personalize=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5d1c27e9-c5ae-4a20-9d05-1de86a59862a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e37c954",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:26:39.097925Z",
     "start_time": "2023-11-10T01:26:39.095877Z"
    }
   },
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "[Verse 1]\n",
    "一盏离愁孤灯伫立在窗口\n",
    "我在门后假装你人还没走\n",
    "旧地如重游月圆更寂寞\n",
    "夜半清醒的烛火不忍苛责我\n",
    "\n",
    "[Verse 2]\n",
    "一壶漂泊浪迹天涯难入喉\n",
    "你走之后酒暖回忆思念瘦\n",
    "水向东流时间怎么偷\n",
    "花开就一次成熟我却错过\n",
    "\n",
    "[Chorus]\n",
    "谁在用琵琶弹奏一曲东风破\n",
    "岁月在墙上剥落看见小时候\n",
    "犹记得那年我们都还很年幼\n",
    "而如今琴声幽幽我的等候你没听过\n",
    "\n",
    "[Chorus][Chorus][Chorus]\n",
    "谁在用琵琶弹奏一曲东风破\n",
    "枫叶将故事染色结局我看透\n",
    "篱笆外的古道我牵着你走过\n",
    "荒烟蔓草的年头就连分手都很沉默\n",
    "\"\"\"\n",
    "text_tags = \"pop woman\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d03b4175-59d3-4bd2-bb12-13761aa17c1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6bd4c45f-7e9a-48f2-817b-e9b7f5278297",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags, personalize=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c14f7300-e29a-4eb2-8dfc-de7e1f0abe97",
   "metadata": {},
   "outputs": [],
   "source": [
    "generate_audio_with_engine(text, text_tags, personalize=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0f5278f4-a64e-4ac7-a22e-f13c3b335f88",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "61d27b65",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:27:48.390694Z",
     "start_time": "2023-11-10T01:26:39.530819Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=text,\n",
    "        text_tags=\"chinese pop\",\n",
    "        n_batch=4,\n",
    "        max_gen_duration_s=80,\n",
    "        # stream=False,\n",
    "    )\n",
    ")\n",
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c5ec317",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:27:48.394012Z",
     "start_time": "2023-11-10T01:27:48.392197Z"
    }
   },
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "[Verse 1]\n",
    "一盏离愁孤灯伫立在窗口\n",
    "我在门后假装你人还没走\n",
    "旧地如重游月圆更寂寞\n",
    "夜半清醒的烛火不忍苛责我\n",
    "\n",
    "[Verse 2]\n",
    "一壶漂泊浪迹天涯难入喉\n",
    "你走之后酒暖回忆思念瘦\n",
    "水向东流时间怎么偷\n",
    "花开就一次成熟我却错过\n",
    "\n",
    "[Chorus]\n",
    "谁在用琵琶弹奏一曲东风破\n",
    "岁月在墙上剥落看见小时候\n",
    "犹记得那年我们都还很年幼\n",
    "而如今琴声幽幽我的等候你没听过\n",
    "\n",
    "[Chorus][Chorus][Chorus]\n",
    "谁在用琵琶弹奏一曲东风破\n",
    "枫叶将故事染色结局我看透\n",
    "篱笆外的古道我牵着你走过\n",
    "荒烟蔓草的年头就连分手都很沉默\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0908af9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:28:57.571553Z",
     "start_time": "2023-11-10T01:27:48.395127Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=text,\n",
    "        text_tags=\"chinese pop\",\n",
    "        n_batch=4,\n",
    "        max_gen_duration_s=80,\n",
    "        # stream=False,\n",
    "    )\n",
    ")\n",
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "77d230e7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.565147Z",
     "start_time": "2023-11-10T01:24:57.565137Z"
    }
   },
   "outputs": [],
   "source": [
    "print(\"WTF\")\n",
    "BREAK"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "443438ac",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c3372767",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.566111Z",
     "start_time": "2023-11-10T01:24:57.566101Z"
    }
   },
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "{start:40;max-duration:120}\n",
    "[Verse 1]\n",
    "笑你我枉花光心計\n",
    "愛競逐鏡花那美麗\n",
    "怕幸運會轉眼遠逝\n",
    "為貪嗔喜惡怒著迷\n",
    "[Verse 2]\n",
    "責你我太貪功戀勢\n",
    "怪大地眾生太美麗\n",
    "悔舊日太執信約誓\n",
    "為悲歡哀怨妒著迷\n",
    "[Verse 3]\n",
    "啊 捨不得璀璨俗世\n",
    "啊 躲不開癡戀的欣慰\n",
    "啊 找不到色相代替\n",
    "啊 參一生參不透這條難題\n",
    "[Chorus]\n",
    "吞風吻雨葬落日未曾徬徨\n",
    "欺山趕海踐雪徑也未絕望\n",
    "拈花把酒偏折煞世人情狂\n",
    "憑這兩眼與百臂或千手不能防\n",
    "[Chorus]\n",
    "天闊闊雪漫漫共誰同航\n",
    "這沙滾滾水皺皺笑著浪蕩\n",
    "貪歡一餉偏教那女兒情長埋葬\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c12aceec",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.566933Z",
     "start_time": "2023-11-10T01:24:57.566924Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=text,\n",
    "        text_tags=\"chinese pop\",\n",
    "        n_batch=4,\n",
    "        max_gen_duration_s=120,\n",
    "        # stream=False,\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "73dd0216",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.567544Z",
     "start_time": "2023-11-10T01:24:57.567536Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "918ae244",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.568183Z",
     "start_time": "2023-11-10T01:24:57.568175Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=\"[christmas]\",\n",
    "        text_tags=\"christmas happy music\",\n",
    "        n_batch=2,\n",
    "        max_gen_duration_s=120,\n",
    "        # stream=False,\n",
    "    )\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d8bed82",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.568764Z",
     "start_time": "2023-11-10T01:24:57.568756Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8f9e0366",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.569411Z",
     "start_time": "2023-11-10T01:24:57.569403Z"
    }
   },
   "outputs": [],
   "source": [
    "input_audio = Audio.from_file(\"audios/Pachelbel - Canon In D Major. Best version. [NlprozGcs80].wav\").get_segment(from_s=60, to_s=80)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f9d3323",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.570127Z",
     "start_time": "2023-11-10T01:24:57.570119Z"
    }
   },
   "outputs": [],
   "source": [
    "input_audio.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "001ec800",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.571025Z",
     "start_time": "2023-11-10T01:24:57.571016Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=\"\",\n",
    "        text_tags=\"canon\",\n",
    "        n_batch=4,\n",
    "        max_gen_duration_s=30,\n",
    "        # stream=False,\n",
    "    ),\n",
    "    history_audio=input_audio\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6309815d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.571719Z",
     "start_time": "2023-11-10T01:24:57.571711Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2270b189",
   "metadata": {},
   "source": [
    "# Oracle"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "772a5d20",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.572288Z",
     "start_time": "2023-11-10T01:24:57.572281Z"
    }
   },
   "outputs": [],
   "source": [
    "input_audio = Audio.from_file(\"audios/test_swift.mp3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "29ecceb0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.572954Z",
     "start_time": "2023-11-10T01:24:57.572946Z"
    }
   },
   "outputs": [],
   "source": [
    "test_input_audio = input_audio.get_segment(from_s=180, to_s=200)\n",
    "test_input_audio.play(compress=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d4b5d48c",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.573511Z",
     "start_time": "2023-11-10T01:24:57.573504Z"
    }
   },
   "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": "808f5130",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.574250Z",
     "start_time": "2023-11-10T01:24:57.574243Z"
    }
   },
   "outputs": [],
   "source": [
    "input_oracle_arr = oracle_arr[:, :1]\n",
    "print(input_oracle_arr.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "752ba22f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.574895Z",
     "start_time": "2023-11-10T01:24:57.574887Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=\" \" * 20,\n",
    "        text_tags=\"\",\n",
    "        n_batch=4,\n",
    "        max_gen_duration_s=30,\n",
    "        oracle_arr=input_oracle_arr,\n",
    "    ),\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bba0d8c9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.575451Z",
     "start_time": "2023-11-10T01:24:57.575444Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()\n",
    "    print(e.array_float[-100:].mean(axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ad979c8b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.576070Z",
     "start_time": "2023-11-10T01:24:57.576063Z"
    }
   },
   "outputs": [],
   "source": [
    "input_pavarotti = Audio.from_file(\"audios/test_pavarotti.mp3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6f0f8785",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.576674Z",
     "start_time": "2023-11-10T01:24:57.576666Z"
    }
   },
   "outputs": [],
   "source": [
    "input_pavarotti_seg = input_pavarotti.get_segment(from_s=152, to_s=172)\n",
    "input_pavarotti_seg.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4e91f1d1",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.577313Z",
     "start_time": "2023-11-10T01:24:57.577306Z"
    }
   },
   "outputs": [],
   "source": [
    "pavarotti_oracle_semantic_arr = semantic_encode(input_pavarotti_seg, n_codebooks=1)\n",
    "pavarotti_oracle_coarse_arr = codec_encode(input_pavarotti_seg)[\n",
    "    : pavarotti_oracle_semantic_arr.shape[0], :\n",
    "]\n",
    "print(pavarotti_oracle_semantic_arr.shape, pavarotti_oracle_coarse_arr.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6502b74a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.577899Z",
     "start_time": "2023-11-10T01:24:57.577892Z"
    }
   },
   "outputs": [],
   "source": [
    "masked_oracle_semantic_arr = oracle_semantic_arr.copy().astype(np.int16)\n",
    "# masked_oracle_semantic_arr[0::5, :] = -1\n",
    "masked_oracle_coarse_arr = pavarotti_oracle_coarse_arr.copy().astype(np.int16)\n",
    "# masked_oracle_coarse_arr[20:, :] = -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b97481d6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.578550Z",
     "start_time": "2023-11-10T01:24:57.578543Z"
    }
   },
   "outputs": [],
   "source": [
    "mixed_oracle_arr =  np.concatenate(\n",
    "    [masked_oracle_semantic_arr], #, masked_oracle_coarse_arr[:, 0:1]],\n",
    "    axis=-1,\n",
    ")\n",
    "print(mixed_oracle_arr.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76dcc73a",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.579180Z",
     "start_time": "2023-11-10T01:24:57.579172Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=\" \" * 20,\n",
    "        text_tags=\"\",\n",
    "        n_batch=4,\n",
    "        max_gen_duration_s=30,\n",
    "        oracle_arr=mixed_oracle_arr,\n",
    "    ),\n",
    "    # history_audio=input_pavarotti_seg\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "84e04fd4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.579819Z",
     "start_time": "2023-11-10T01:24:57.579811Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()\n",
    "    print(e.array_float[-100:].mean(axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b79d93dc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "edc87efa",
   "metadata": {},
   "source": [
    "# continuation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "984e8c9b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.580455Z",
     "start_time": "2023-11-10T01:24:57.580448Z"
    }
   },
   "outputs": [],
   "source": [
    "out_audio = generate_audio(\n",
    "    GenerationConfig(\n",
    "        text=\"[chorus] purple rain\" * 20,\n",
    "        text_tags=\"\",\n",
    "        n_batch=4,\n",
    "        max_gen_duration_s=30,\n",
    "        # stream=False,\n",
    "    ),\n",
    "    history_audio=test_input_audio,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "296b34eb",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.581083Z",
     "start_time": "2023-11-10T01:24:57.581075Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "df83ed93",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-11-10T01:24:57.581739Z",
     "start_time": "2023-11-10T01:24:57.581732Z"
    }
   },
   "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-11-10T01:24:57.582934Z",
     "start_time": "2023-11-10T01:24:57.582925Z"
    }
   },
   "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-11-10T01:24:57.583480Z",
     "start_time": "2023-11-10T01:24:57.583473Z"
    }
   },
   "outputs": [],
   "source": [
    "for e in out_audio:\n",
    "    e.play()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa81e5c6",
   "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.15"
  },
  "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
}
