{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5d0a6cfa",
   "metadata": {},
   "source": [
    "# Audio Handling (Using SciPy)\n",
    "\n",
    "For handling standard uncompressed audio (like WAV files), the scipy.io.wavfile module is simple and effective for a conceptual course.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bc85f90d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy.io import wavfile\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f023bc0",
   "metadata": {},
   "source": [
    "## 1. Reading and Seeing Audio Data\n",
    "\n",
    "Audio data is a sequence of samples over time.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f34af693",
   "metadata": {},
   "outputs": [],
   "source": [
    "def analyze_audio(audio_path):\n",
    "    # 1. Read the audio file\n",
    "    # rate: Sampling rate (samples/second, e.g., 44100 Hz)\n",
    "    # data: The actual audio samples (a NumPy array)\n",
    "    try:\n",
    "        rate, data = wavfile.read(audio_path)\n",
    "    except FileNotFoundError:\n",
    "        print(\"Error: Could not find audio file.\")\n",
    "        return\n",
    "\n",
    "    print(f\"--- Audio Metadata ---\")\n",
    "    print(f\"Sampling Rate (Hz): {rate}\")\n",
    "\n",
    "    # Check if stereo (2 channels) or mono (1 channel)\n",
    "    if data.ndim > 1:\n",
    "        channels = data.shape[1]\n",
    "        print(f\"Number of Channels: {channels}\")\n",
    "        # Use the first channel for plotting if stereo\n",
    "        audio_data = data[:, 0]\n",
    "    else:\n",
    "        channels = 1\n",
    "        print(f\"Number of Channels: {channels}\")\n",
    "        audio_data = data\n",
    "\n",
    "    # 2. Inspect the data (e.g., the first 10 samples)\n",
    "    print(f\"Total number of samples: {len(audio_data)}\")\n",
    "    print(\n",
    "        f\"Data type (Bit Depth): {audio_data.dtype} (e.g., int16 means 16-bit resolution)\"\n",
    "    )\n",
    "    print(f\"First 10 samples: {audio_data[:10]}\")\n",
    "\n",
    "    # 3. Visualize the signal (in the Spatial/Time Domain)\n",
    "    # Create a time vector\n",
    "    time = np.linspace(0.0, len(audio_data) / rate, len(audio_data))\n",
    "\n",
    "    plt.figure(figsize=(10, 4))\n",
    "    plt.plot(time, audio_data)\n",
    "    plt.title(f\"Audio Signal (Time Domain - {channels} Channel(s))\")\n",
    "    plt.xlabel(\"Time (seconds)\")\n",
    "    plt.ylabel(\"Amplitude (Sample Value)\")\n",
    "    plt.show()\n",
    "\n",
    "\n",
    "# Example Usage: analyze_audio('path/to/your/audio.wav')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d6e31ffe",
   "metadata": {},
   "source": [
    "## Key Audio Concepts:\n",
    "\n",
    "- Sampling Rate (rate): How many samples (digital amplitude values) are taken per second.\n",
    "- Amplitude/Bit Depth (data.dtype): The resolution of each sample (e.g., 8-bit, 16-bit). This relates directly to the maximum and minimum sample values (e.g., ±32767 for 16-bit signed integers).\n",
    "- Time Domain: The plot shows amplitude (y-axis) vs. time (x-axis), representing the sound wave.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
