Audio#
Open this notebook in Jupyterlite | 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, raw bytes, a NumPy ndarray or a Torch Tensor.
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 file formats.
If SciPy is installed, 1- or 2-dim NumPy ndarrays and Torch Tensors are also supported, where a 2-dim input of shape (samples, channels) is rendered as multi-channel audio. The dtype must be one of the following:
numpy: np.int16, np.uint16, np.float32, np.float64
torch: torch.short, torch.int16, torch.half, torch.float16, torch.float, torch.float32, torch.double, torch.float64
The array or tensor input will be converted to 16-bit integers and encoded as a wav file by SciPy.
Parameters:#
For details on other options for customizing the component see the layout and styling how-to guides.
autoplay(boolean, default=False): When True, it specifies that the output will play automatically. In Chromium browsers this requires the user to click play once.loop(boolean, default=False): Whether to loop when reaching the end of playback.muted(boolean, default=False): When True, it specifies that the output should be muted.object(str, pathlib.Path, bytes, BytesIO, np.ndarray or torch.Tensor): Local file path, remote URL pointing to an audio file, raw audio bytes, or a 1- or 2-dim NumPy array or Torch tensor.paused(boolean, default=True): Whether the player is paused.sample_rate(int, default=44100): The sample rate of the audio when given a NumPy array or Torch tensor.throttle(int, default=250): 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('https://ccrma.stanford.edu/~jos/mp3/pno-cs.mp3', name='Audio')
audio
The player can be controlled using its own widgets, as well as from Python. To start or stop playback in code, set the paused parameter (not executed here, so the page does not start playing on load):
audio.paused = False
The current player time can be read and set with the time parameter (in seconds):
audio.time
The volume may also be read and set:
audio.volume = 50
When providing a NumPy Array or Torch Tensor, you should specify the sample_rate.
In this example we generate 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
pn.pane.Audio(waveform_quiet, sample_rate=sps)
Controls#
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:
pn.Row(audio.controls(jslink=True), audio)
Open this notebook in Jupyterlite | Download this notebook from GitHub (right-click to download).