Audio#

Download this notebook from GitHub (right-click to download).


import numpy as np
import panel as pn

pn.extension()

The Audio pane displays an audio player given a local or remote audio file or a numpy array.

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.

The audio player supports ogg, mp3, and wav files as well as Numpy arrays.

Parameters:#

For layout and styling related parameters see the customization user guide.

  • name (str): The title of the pane

  • loop (boolean): Whether to loop when reaching the end of playback

  • object (string): Local file path or remote URL pointing to audio file

  • paused (boolean): Whether the player is paused

  • autoplay (boolean): When True, it specifies that the output will play automatically. In Chromium browsers this requires the user to click play once

  • muted (boolean): When True, it specifies that the output should be muted

  • throttle (int): How frequently to sample the current playback time in milliseconds

  • time (float): Current playback time in seconds

  • volume (int): Volume in the range 0-100


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).

audio = pn.pane.Audio('http://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3', name='Audio')

audio

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:

#audio.paused = False

The current player time can be read and set with the time variable (in seconds):

audio.time
0

The volume may also be read and set:

audio.volume = 50

Alternatively, if SciPy is available, the Pane 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:

sps = 44100 # Samples per second
duration = 10 # Duration in seconds

modulator_frequency = 2.0
carrier_frequency = 120.0
modulation_index = 2.0

time = np.arange(sps*duration) / sps
modulator = np.sin(2.0 * np.pi * modulator_frequency * time) * modulation_index
carrier = np.sin(2.0 * np.pi * carrier_frequency * time)
waveform = np.sin(2. * np.pi * (carrier_frequency * time + modulator))
    
waveform_quiet = waveform * 0.3
waveform_int = np.int16(waveform_quiet * 32767)

pn.pane.Audio(waveform_int, sample_rate=sps)