{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import os\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import soundfile as sf\n",
    "import pyloudnorm as pyln"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_pickle(\"/home/tony/Data/Preference/up_diff2_v1/interesting_clips_upv2_1_6_20250414_sample.pkl\")\n",
    "print(df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# unique model_name\n",
    "model_names = df[\"model_name\"].unique()\n",
    "print(model_names)\n",
    "# unique tags\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tqdm\n",
    "\n",
    "# copy all s3 ids to local\n",
    "out_dir = \"/home/christian/dpo/interesting_clips_upv2_1_6_20250414_sample\"\n",
    "if not os.path.exists(out_dir):\n",
    "    os.makedirs(out_dir)\n",
    "\n",
    "# create a list of s3 ids \n",
    "s3_ids = df[\"s3_id\"].unique()\n",
    "print(len(s3_ids))\n",
    "\n",
    "base_url = f\"s3://suno-data-uploads/studio/uploads\"\n",
    "\n",
    "from joblib import Parallel, delayed\n",
    "\n",
    "def download_file(s3_id):\n",
    "    s3_filepath = f\"{base_url}/{s3_id}.mp3\"\n",
    "    out_filepath = os.path.join(out_dir, f\"{s3_id}.mp3\")\n",
    "    os.system(f\"aws s3 cp {s3_filepath} {out_filepath}\")\n",
    "\n",
    "# Use parallel processing with joblib\n",
    "Parallel(n_jobs=-1)(delayed(download_file)(s3_id) for s3_id in tqdm(s3_ids))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def measure_loudness(filepath, s3_id):\n",
    "    audio, rate = sf.read(filepath)\n",
    "    meter = pyln.Meter(rate)\n",
    "    loudness = meter.integrated_loudness(audio)\n",
    "    return {\"s3_id\": s3_id, \"loudness\": loudness}\n",
    "\n",
    "# measure the loudness of all the files\n",
    "filepaths = [os.path.join(out_dir, f\"{s3_id}.mp3\") for s3_id in s3_ids]\n",
    "\n",
    "# create input pairs for parallel processing\n",
    "input_pairs = [(filepaths[i], s3_ids[i]) for i in range(len(filepaths))]\n",
    "\n",
    "# process in parallel\n",
    "loudness_data = Parallel(n_jobs=-1)(\n",
    "    delayed(measure_loudness)(filepath, s3_id) \n",
    "    for filepath, s3_id in tqdm(input_pairs)\n",
    ")\n",
    "\n",
    "# save the results\n",
    "results_df = pd.DataFrame(loudness_data)\n",
    "print(results_df.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# now merge the results with the original df\n",
    "df = df.merge(results_df, on=\"s3_id\", how=\"left\")\n",
    "print(df.head())\n",
    "# save the results\n",
    "#df.to_pickle(\"/home/christian/dpo/interesting_clips_upv2_1_6_20250414_sample/results.pkl\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# take the average and standard deviation of the loudness for each of the model_name\n",
    "model_loudness_mean = df.groupby(\"model_name\")[\"loudness\"].mean()\n",
    "model_loudness_std = df.groupby(\"model_name\")[\"loudness\"].std()\n",
    "print(\"Mean loudness by model:\")\n",
    "print(model_loudness_mean)\n",
    "\n",
    "# save the results\n",
    "#model_loudness_mean.to_pickle(\"/home/christian/dpo/interesting_clips_upv2_1_6_20250414_sample/model_loudness_mean.pkl\")\n",
    "#model_loudness_std.to_pickle(\"/home/christian/dpo/interesting_clips_upv2_1_6_20250414_sample/model_loudness_std.pkl\")\n",
    "# also print the max and min loudness for each of the model_name\n",
    "model_loudness_max = df.groupby(\"model_name\")[\"loudness\"].max()\n",
    "model_loudness_min = df.groupby(\"model_name\")[\"loudness\"].min()\n",
    "print(\"\\nMax loudness by model:\")\n",
    "print(model_loudness_max)\n",
    "print(\"\\nMin loudness by model:\")\n",
    "print(model_loudness_min)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_env",
   "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.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
