{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torchaudio\n",
    "import numpy as np\n",
    "import IPython"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "audio_filepath = \"/home/christian/audio/reference-audio-wav/02 Dreams.wav\"\n",
    "audio, sr = torchaudio.load(audio_filepath)\n",
    "audio = audio[:, :int(10 * sr)]\n",
    "print(audio.shape)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "audio_corrupt = audio.clone()\n",
    "\n",
    "# 10% chance to add highpass\n",
    "if np.random.uniform() < 0.1:\n",
    "    hpf_cutoff = np.random.uniform(20, 1000)\n",
    "    audio_corrupt = torchaudio.functional.highpass_biquad(\n",
    "            audio_corrupt, sr, hpf_cutoff\n",
    "    )\n",
    "# 10% chance to add lowpass\n",
    "if np.random.uniform() < 0.1:   \n",
    "    lpf_cutoff = np.random.uniform(1000, 20000)\n",
    "    audio_corrupt = torchaudio.functional.lowpass_biquad(\n",
    "        audio_corrupt, sr, lpf_cutoff\n",
    "    )   \n",
    "\n",
    "# 10% chance to make mono \n",
    "audio_corrupt = audio_corrupt.mean(dim=0, keepdim=True)\n",
    "\n",
    "# 10% add codec artifacts\n",
    "#compression = np.random.choice([64, 128, 192, 256])\n",
    "#audio_corrupt = torchaudio.functional.apply_codec(audio_corrupt, sr, \"mp3\", compression)\n",
    "\n",
    "# 10% chance to add noise\n",
    "noise_level_db = -np.random.uniform(42, 96)\n",
    "noise_signal = torch.randn_like(audio_corrupt) * 10**(noise_level_db / 20)\n",
    "# apply filter to noise signal\n",
    "lpf_cutoff = np.random.uniform(1000, 20000)\n",
    "noise_signal = torchaudio.functional.lowpass_biquad(noise_signal, sr, lpf_cutoff)\n",
    "audio_corrupt = audio_corrupt + noise_signal\n",
    "\n",
    "\n",
    "# 5% chance to add contrast\n",
    "contrast = np.random.uniform(0, 100.0)\n",
    "audio_corrupt = torchaudio.functional.contrast(audio_corrupt, enhancement_amount=contrast)\n",
    "\n",
    "# 10% chance to add reverb\n",
    "#audio_corrupt = torchaudio.functional.reverb(audio_corrupt, sr, room_size=0.5)\n",
    "\n",
    "# 10% chance to add distortion\n",
    "drive_db = np.random.uniform(0, 10)\n",
    "drive_lin = 10**(drive_db / 20)\n",
    "audio_corrupt = torch.tanh(drive_lin * audio_corrupt)\n",
    "\n",
    "print(f\"noise level: {noise_level_db} dB\")\n",
    "print(f\"contrast: {contrast}\")\n",
    "print(audio_corrupt.abs().max())\n",
    "\n",
    "IPython.display.display(IPython.display.Audio(audio, rate=sr))\n",
    "IPython.display.display(IPython.display.Audio(audio_corrupt, rate=sr))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "suno_env2",
   "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"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
