{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import panel as pn\n",
    "\n",
    "pn.extension()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``Audio`` pane displays an audio player given a local or remote audio file, NumPy ndarray or a Torch Tensor.\n",
    "\n",
    "The pane also allows access and control over the player state including toggling of playing/``paused`` and ``loop`` state, the current ``time``, and the ``volume``.\n",
    "\n",
    "The audio player supports ``ogg``, ``mp3``, and ``wav`` file formats\n",
    "\n",
    "If SciPy is installed, 1-dim Numpy ndarrys and 1-dim Torch Tensors are also supported. The dtype must be one of the following\n",
    "\n",
    "- numpy: np.int16, np.uint16, np.float32, np.float64\n",
    "- torch: torch.short, torch.int16, torch.half, torch.float16, torch.float, torch.float32, torch.double, torch.float64\n",
    "\n",
    "The array or tensor input will be downsampled to 16bit and converted to a wav file by SciPy.\n",
    "\n",
    "#### Parameters:\n",
    "\n",
    "For details on other options for customizing the component see the [layout](../../how_to/layout/index.md) and [styling](../../how_to/styling/index.md) how-to guides.\n",
    "\n",
    "* **``autoplay``** (boolean): When True, it specifies that the output will play automatically. In Chromium browsers this requires the user to click play once\n",
    "* **``loop``** (boolean): Whether to loop when reaching the end of playback\n",
    "* **``muted``** (boolean): When True, it specifies that the output should be muted\n",
    "* **``name``** (str): The title of the pane\n",
    "* **``object``** (string): Local file path, remote URL pointing to audio file, 1-dim numpy array or 1-dim torch tensor\n",
    "* **``paused``** (boolean): Whether the player is paused\n",
    "* **``sample_rate``** (int): The sample_rate of the audio when given a NumPy array or Torch Tensor. Default is 44100.\n",
    "* **``throttle``** (int): How frequently to sample the current playback in milliseconds\n",
    "* **``time``** (float): Current playback time in seconds\n",
    "* **``volume``** (int): Volume in the range 0-100\n",
    "\n",
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Audio` pane can be constructed with a URL pointing to a remote audio file or a local audio file (in which case the data is embedded)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "audio = pn.pane.Audio('https://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3', name='Audio')\n",
    "\n",
    "audio"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The player can be controlled using its own widgets, as well as by using Python code as follows.  To pause or unpause it in code, use the ``paused`` property:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#audio.paused = False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The current player ``time`` can be read and set with the time variable (in seconds):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "audio.time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The ``volume`` may also be read and set:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "audio.volume = 50"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When providing a NumPy Array or Torch Tensor, you should specify the `sample_rate`.\n",
    "\n",
    "In this example we plot a frequency modulated signal:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sps = 44100 # Samples per second\n",
    "duration = 10 # Duration in seconds\n",
    "\n",
    "modulator_frequency = 2.0\n",
    "carrier_frequency = 120.0\n",
    "modulation_index = 2.0\n",
    "\n",
    "time = np.arange(sps*duration) / sps\n",
    "modulator = np.sin(2.0 * np.pi * modulator_frequency * time) * modulation_index\n",
    "carrier = np.sin(2.0 * np.pi * carrier_frequency * time)\n",
    "waveform = np.sin(2. * np.pi * (carrier_frequency * time + modulator))\n",
    "    \n",
    "waveform_quiet = waveform * 0.3\n",
    "\n",
    "pn.pane.Audio(waveform_quiet, sample_rate=sps)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Controls\n",
    "\n",
    "The `Audio` pane exposes a number of options which can be changed from both Python and Javascript. Try out the effect of these parameters interactively:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pn.Row(audio.controls(), audio)"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
