{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "214d92ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "# setup autoload\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5c26b5f0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-04T16:46:46.194725Z",
     "start_time": "2024-03-04T16:46:45.219446Z"
    }
   },
   "outputs": [],
   "source": [
    "import librosa\n",
    "import matplotlib.pyplot as plt\n",
    "import librosa.display\n",
    "import numpy as np\n",
    "\n",
    "\n",
    "from suno_utils.audio import Audio"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7ef4851d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-04T16:12:38.727269Z",
     "start_time": "2024-03-04T16:12:38.414882Z"
    }
   },
   "source": [
    "# Setup path"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "278a90c7",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-04T16:46:46.197682Z",
     "start_time": "2024-03-04T16:46:46.195974Z"
    }
   },
   "outputs": [],
   "source": [
    "test_audio_path = \"audios/halo.wav\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "7c64c82b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-04T16:46:46.852919Z",
     "start_time": "2024-03-04T16:46:46.198786Z"
    }
   },
   "outputs": [],
   "source": [
    "test_audio = Audio.from_file(test_audio_path).get_segment(to_s=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d92cf6d4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-04T16:46:46.859541Z",
     "start_time": "2024-03-04T16:46:46.854952Z"
    }
   },
   "outputs": [],
   "source": [
    "test_array = test_audio.array_float\n",
    "sample_rate = test_audio.sample_rate\n",
    "print(test_array.shape[0] / sample_rate)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "875c9d51",
   "metadata": {},
   "source": [
    "# Visualize the raw wave form"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "681baad8",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-04T16:46:48.636889Z",
     "start_time": "2024-03-04T16:46:46.860582Z"
    }
   },
   "outputs": [],
   "source": [
    "plt.figure().set_figwidth(12)\n",
    "librosa.display.waveshow(test_array, sr=sample_rate, color=\"blue\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c8c3f9b4",
   "metadata": {},
   "source": [
    "# Visualize the raw frenquency"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "426fe276",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-04T16:46:49.660418Z",
     "start_time": "2024-03-04T16:46:48.638437Z"
    }
   },
   "outputs": [],
   "source": [
    "start_bin = 2000\n",
    "dft_input = test_array[start_bin : start_bin + 4096]\n",
    "\n",
    "# calculate the DFT\n",
    "window = np.hanning(len(dft_input))\n",
    "windowed_input = dft_input * window\n",
    "dft = np.fft.rfft(windowed_input)\n",
    "\n",
    "# get the amplitude spectrum in decibels\n",
    "amplitude = np.abs(dft)\n",
    "amplitude_db = librosa.amplitude_to_db(amplitude, ref=np.max)\n",
    "\n",
    "# get the frequency bins\n",
    "frequency = librosa.fft_frequencies(sr=sample_rate, n_fft=len(dft_input))\n",
    "\n",
    "plt.figure().set_figwidth(12)\n",
    "plt.plot(frequency, amplitude_db)\n",
    "plt.xlabel(\"Frequency (Hz)\")\n",
    "plt.ylabel(\"Amplitude (dB)\")\n",
    "plt.xscale(\"log\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "adbd96a2",
   "metadata": {},
   "source": [
    "# Spectrogram"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "6ba6ad5f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-04T16:46:49.663568Z",
     "start_time": "2024-03-04T16:46:49.662053Z"
    }
   },
   "outputs": [],
   "source": [
    "# test_d = librosa.stft(test_array)\n",
    "# s_db = librosa.amplitude_to_db(np.abs(test_d), ref=np.max)\n",
    "\n",
    "# plt.figure().set_figwidth(12)\n",
    "# librosa.display.specshow(s_db, x_axis=\"time\", y_axis=\"hz\")\n",
    "# plt.colorbar()\n",
    "# plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "11742adb",
   "metadata": {},
   "source": [
    "# Mel"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "59ba8f6f",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-04T16:46:50.054477Z",
     "start_time": "2024-03-04T16:46:49.664749Z"
    }
   },
   "outputs": [],
   "source": [
    "s_mel = librosa.feature.melspectrogram(y=test_array, sr=sample_rate, n_mels=128, fmax=20000)\n",
    "s_mel_db = librosa.power_to_db(s_mel, ref=np.max)\n",
    "\n",
    "plt.figure().set_figwidth(12)\n",
    "librosa.display.specshow(s_mel_db, x_axis=\"time\", y_axis=\"mel\", sr=sample_rate, fmax=20000)\n",
    "plt.colorbar()\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e08ad811",
   "metadata": {},
   "source": [
    "# To wav conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "547ecfee",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_audio_path = \"audios/halo.wav\"\n",
    "test_audio = Audio.from_file(test_audio_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "62440df8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "87.2 ms ± 1.46 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "test_audio.to_wav(\"audios/halo_test.wav\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "2a90d4bd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.61 s ± 2.79 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "test_audio.to_hq_mp3(\"audios/halo_test.mp3\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72f09c32",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_env_dev",
   "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
}
