{
 "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": [
    "<div style=\"background-color: lightcoral; width: 100%; border-radius:5px; padding: 10px; color: white\">Warning: The Audio widget is deprecated use the <a href=\"../panes/Audio.ipynb\">Audio pane</a> instead.</div><br>\n",
    "\n",
    "The ``Audio`` widget displays an audio player given a local or remote audio file. The widget also allows access and control over the player state including toggling of playing/``paused`` and ``loop`` state, the current ``time``, and the ``volume``. The audio player supports ``ogg``, ``mp3``, and ``wav`` files.\n",
    "\n",
    "For more information about listening to widget events and laying out widgets refer to the [widgets user guide](../../user_guide/Widgets.ipynb). Alternatively you can learn how to build GUIs by declaring parameters independently of any specific widgets in the [param user guide](../../user_guide/Param.ipynb). To express interactivity entirely using Javascript without the need for a Python server take a look at the [links user guide](../../user_guide/Param.ipynb).\n",
    "\n",
    "#### Parameters:\n",
    "\n",
    "For layout and styling related parameters see the [customization user guide](../../user_guide/Customization.ipynb).\n",
    "\n",
    "* **``name``** (str): The title of the widget\n",
    "* **``loop``** (boolean): Whether to loop when reaching the end of playback\n",
    "* **``paused``** (boolean): Whether the player is paused\n",
    "* **``throttle``** (int): How frequently to sample the current playback time in milliseconds\n",
    "* **``time``** (float): Current playback time in seconds\n",
    "* **``value``** (string): Local file path or remote URL pointing to audio file\n",
    "* **``volume``** (int): Volume in the range 0-100\n",
    "\n",
    "___"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Audio` widget 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": [
    "pn.widgets.Audio(name='Audio', value='http://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternatively, if SciPy is available, the widget may also be constructed from a NumPy array containing int16 values and a `sample_rate`, e.g. 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",
    "waveform_int = np.int16(waveform_quiet * 32767)\n",
    "\n",
    "audio = pn.widgets.Audio(value=waveform_int, sample_rate=sps)\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"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
