from pynq.drivers import Audio
audio = Audio()

"""
Explanation: Audio using the Base Overlay
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. 
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. 
The Microphone is integrated onto the board, as indicated in the image below. The MIC hole should not be covered when capturing audio. 

Audio IP in base overlay
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:

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. 
Using the MIC
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.
End of explanation
"""


# Record a sample
audio.record(4)
# Save recorded sample
audio.save("Recording_1.pdm")

"""
Explanation: Capture audio
Capture a 4 second sample from the microphone, and save the raw pdm file to disk:
End of explanation
"""


# Play recorded sample
audio.play()

"""
Explanation: Playback on the board
Connect headphones, or speakers to the 3.5mm line out and playback the captured audio:
End of explanation
"""


# Load a sample
audio.load("/home/xilinx/pynq/drivers/tests/pynq_welcome.pdm")
# Play loaded sample
audio.play()

"""
Explanation: You can also playback from a pre-recorded pdm file
End of explanation
"""


