{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Audio using the Base Overlay\n",
    "\n",
    "The PYNQ-Z1 board contains an integrated MIC, and line out connected to a 3.5mm jack. Both these interfaces are connected to the FPGA fabric of the Zynq chip. The Microphone has a PDM interface, and the line out is a PWM driven mono output. \n",
    "\n",
    "It is possible to play back audio from the board in a notebook, and to capture audio from other interfaces like HDMI, or a USB audio capture device. This notebook will only consider the MIC and line out interfaces on the board. \n",
    "\n",
    "The Microphone is integrated onto the board, as indicated in the image below. The MIC hole should not be covered when capturing audio. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![](images/pynqz1_audio.JPG)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Audio IP in base overlay\n",
    "\n",
    "To use audio on the PYNQ-Z1, audio controllers must be included in a hardware library or overlay. The *base* overlay contains a the PDM capture and PWM driver for the two audio interfaces as indicated in the image below:\n",
    "\n",
    "![](images/audio_block_diagram.jpg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The Audio IP in the *base* overlay consists of a PDM block to interface the MIC, and an *Audio Direct* IP block to drive the line out (PWM). There are three multiplexors. This allows the line out to be driven from the PS, or the MIC can be streamed directly to the output. The line out can also be disabled. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Using the MIC\n",
    "To use the MIC, first create an instance of the Audio class. The audio class can be used to access both the MIC and the line out. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from pynq.drivers import Audio\n",
    "audio = Audio()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Capture audio\n",
    "\n",
    "Capture a 4 second sample from the microphone, and save the raw pdm file to disk:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Record a sample\n",
    "audio.record(4)\n",
    "# Save recorded sample\n",
    "audio.save(\"Recording_1.pdm\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Playback on the board\n",
    "Connect headphones, or speakers to the 3.5mm line out and playback the captured audio:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Play recorded sample\n",
    "audio.play()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also playback from a pre-recorded pdm file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Load a sample\n",
    "audio.load(\"/home/xilinx/pynq/drivers/tests/pynq_welcome.pdm\")\n",
    "# Play loaded sample\n",
    "audio.play()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "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.4.3+"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
