{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f6132018",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import random\n",
    "import torchaudio\n",
    "from torch import Tensor\n",
    "from pathlib import Path\n",
    "from IPython.display import Audio\n",
    "from torch_audiomentations import Gain, PitchShift, LowPassFilter, HighPassFilter, ApplyImpulseResponse, Compose, BandPassFilter, PolarityInversion\n",
    "workspace_dir = Path.cwd().parent.parent\n",
    "sample_rate = 16000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "a238e4ab",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([1, 132800])\n",
      "原始音频: shape=torch.Size([1, 1, 132800]), sample_rate=16000\n",
      "orginal\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "                <audio  controls=\"controls\" >\n",
       "                    <source src=\"data:audio/wav;base64,[encoded data, 354192 characters]\" type=\"audio/wav\" />\n",
       "                    Your browser does not support the audio element.\n",
       "                </audio>\n",
       "              "
      ],
      "text/plain": [
       "<IPython.lib.display.Audio object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "                <audio  controls=\"controls\" >\n",
       "                    <source src=\"data:audio/wav;base64,[encoded data, 354192 characters]\" type=\"audio/wav\" />\n",
       "                    Your browser does not support the audio element.\n",
       "                </audio>\n",
       "              "
      ],
      "text/plain": [
       "<IPython.lib.display.Audio object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "transforms = {\n",
    "    \"gain_up\": Gain(min_gain_in_db=4, max_gain_in_db=8, p=1.0, output_type='tensor'),\n",
    "    \"gain_down\": Gain(min_gain_in_db=-15, max_gain_in_db=-8, p=1.0, output_type='tensor'),\n",
    "    \"pitch_up\": PitchShift(min_transpose_semitones=1, max_transpose_semitones=4, p=1.0, sample_rate=sample_rate, output_type='tensor'),\n",
    "    \"pitch_down\": PitchShift(min_transpose_semitones=-4, max_transpose_semitones=-1, p=1.0, sample_rate=sample_rate, output_type='tensor'),\n",
    "    \"lowpass\": LowPassFilter(min_cutoff_freq=600, max_cutoff_freq=2000, p=1.0, output_type='tensor'),\n",
    "    \"highpass\": HighPassFilter(min_cutoff_freq=800, max_cutoff_freq=2000, p=1.0, output_type='tensor')\n",
    "}\n",
    "\n",
    "audio_path = workspace_dir / 'data/test/F001_001.wav'\n",
    "\n",
    "waveform, sample_rate = torchaudio.load_with_torchcodec(audio_path)\n",
    "print(waveform.shape)\n",
    "if waveform.dim() == 2:\n",
    "    waveform = waveform.unsqueeze(0)\n",
    "\n",
    "print(f\"原始音频: shape={waveform.shape}, sample_rate={sample_rate}\")\n",
    "print('orginal')\n",
    "display(Audio(waveform.squeeze(0), rate=sample_rate))\n",
    "\n",
    "noise_audio = workspace_dir / 'data/test/output.wav'\n",
    "noise_audio = workspace_dir / 'data/corridor'\n",
    "test_transform = ApplyImpulseResponse(ir_paths=noise_audio,convolve_mode='same', p=1, output_type=\"tensor\")\n",
    "dry_waveform = waveform.clone()\n",
    "wet_waveform = test_transform(waveform, sample_rate=sample_rate)\n",
    "out: Tensor = 0.8 * dry_waveform + 0.2 * wet_waveform\n",
    "\n",
    "max_amp = out.abs().max()\n",
    "if max_amp > 1.0:\n",
    "    print(max_amp)\n",
    "    out = out / max_amp\n",
    "display(Audio(out.squeeze(0), rate=sample_rate))\n",
    "# for name, transform in transforms.items():\n",
    "#     out: Tensor = transform(waveform, sample_rate=sample_rate)\n",
    "#     out.squeeze(0)\n",
    "#     max_val = out.abs().max()\n",
    "#     if max_val > 1.0:\n",
    "#         print(f\"max_val: {max_val}\")\n",
    "#         waveform_3d = waveform / max_val\n",
    "#     print(f\"name: {name}, shepe: {out.shape}\")\n",
    "#     display(Audio(out.squeeze(0), rate=sample_rate))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "aadb05aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.19999999999999996\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "final_prob = 0.8\n",
    "warmup_epochs = 12\n",
    "current_prob = 0.0\n",
    "epoch = 4\n",
    "current_prob = final_prob * (1 - math.cos(math.pi * epoch / warmup_epochs)) / 2\n",
    "print(current_prob)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "study-asr (3.11.12)",
   "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.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
