Skip to content

pn.pane.Audio fails with stereo numpy ndarray #7090

@AlonKellner

Description

@AlonKellner

Version Info

On the latest Colab (released on 2024-07-22) after installing jupyter_bokeh:

>>> pip install jupyter_bokeh

These are the relevant versions to reproduce this:

>>> pip freeze | grep -e bokeh -e panel -e numpy -e scipy
bokeh==3.4.3
jupyter_bokeh==4.0.5
numpy==1.26.4
panel==1.4.4
scipy==1.13.1

Behavior

Expected

When calling pn.pane.Audio with a two channel (stereo) numpy array, create an audio widget with the appropriate stereo channels.

Observed

Calling the pn.pane.Audio with a two channel (stereo) numpy array fails:

pn.pane.Audio(stereo, sample_rate=sr)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
[<ipython-input-22-2acaebc65c0f>](https://localhost:8080/#) in <cell line: 1>()
----> 1 pn.pane.Audio(stereo, sample_rate=sr)

1 frames
[/usr/local/lib/python3.10/dist-packages/panel/pane/base.py](https://localhost:8080/#) in __init__(self, object, **params)
    161         applies = self.applies(self.object, **(params if self._applies_kw else {}))
    162         if (isinstance(applies, bool) and not applies) and self.object is not None:
--> 163             self._type_error(self.object)
    164 
    165         kwargs = {

[/usr/local/lib/python3.10/dist-packages/panel/pane/base.py](https://localhost:8080/#) in _type_error(self, object)
    209 
    210     def _type_error(self, object):
--> 211         raise ValueError(f"{type(self).__name__} pane does not support objects of type '{type(object).__name__}'.")
    212 
    213     def __repr__(self, depth: int = 0) -> str:

ValueError: Audio pane does not support objects of type 'ndarray'.

This occurs due to a check in panel.pane.Audio.applies: _is_1dim_int_or_float_ndarray.
The same happens with tensorlikes.

This validation is overly restrictive, since scipy.io.wavfile is perfectly capable of handling 2d ndarrays and saving them as stereo files.

Reproduction

I reproduced this error and a possible solution in a Colab notebook.

import numpy as np
import panel as pn
pn.extension()

sr = 8000
beep_1 = np.sin(np.linspace(0, 2 * np.pi * 200, sr))
beep_2 = np.sin(np.linspace(0, 2 * np.pi * 400, sr))
stereo = np.c_[beep_1, beep_2]

# This is fine
pn.pane.Audio(beep_1, sample_rate=sr)

# This fails
pn.pane.Audio(stereo, sample_rate=sr)

The possible solution is currently implemented in WaloViz, it worked for me on multiple environments.

Summary

  • I may be interested in making a pull request to address this

Just give me the OK and I'd be more than happy to implement and document the solution, thanks :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions