简体   繁体   English

如何在Jupyter笔记本中布置IPython.display.Audio对象的列表?

[英]How can I layout a list of IPython.display.Audio objects in a Jupyter notebook?

I have a list of audio data that I want to display in a Jupyter notebook using IPython.display.Audio such that these controls are flowing from left to right next to one another. 我有一个音频数据列表,希望使用IPython.display.Audio在Jupyter笔记本中显示,以便这些控件从左到右彼此相邻流动。 I tried using ipywidgets.HBox but I get the an error since IPython.display.Audio is not an instance of a Widget . 我尝试使用ipywidgets.HBox但是由于IPython.display.Audio不是Widget的实例,所以出现错误。

My list of of audio data looks like the following, where y is the audio data and sr is the sampling rate. 我的音频数据列表如下所示,其中y是音频数据, sr是采样率。

data = [(y1, sr1), (y2, sr2), ..., (yN, srN)]

What I tried to do is something like the following. 我试图做的是如下所示。

audio_widgets = [display.Audio(audio[0], sr=audio[1]) for audio in data]
audio_hbox = widgets.HBox(audio_widgets) # TraitError happens here

I get the following error. 我收到以下错误。

TraitError: Element of the 'children' trait of a HBox instance must be a Widget, but a value of <IPython.lib.display.Audio object> <class 'IPython.lib.display.Audio'> was specified.

One way to get the audio displays to show is to simply show them one above the other (which takes up a lot of vertical space when there are a lot of audio data). 一种使音频显示得以显示的方法是简单地将它们一个显示在另一个之上(当有大量音频数据时,它将占用大量垂直空间)。

for audio in data:
     audio_widget = display.Audio(audio[0], sr=audio[1])
     display(audio_widget)

Any ideas on how I can control the layout display of audio controls? 关于如何控制音频控件的布局显示的任何想法?

You could display the Audio s into output widgets . 您可以将Audio显示到输出小部件中 Anything that displays nicely in a notebook will also display nicely in an output widget. 笔记本中显示良好的所有内容也将在输出窗口小部件中良好显示。 You can direct output to the widget using a context manager, see example code below. 您可以使用上下文管理器将输出定向到小部件,请参见下面的示例代码。

from IPython.display import Audio, display
from ipywidgets import widgets

audio_widgets = []
for (audio, sample_rate) in data:
    out = widgets.Output()
    with out:
        display(Audio(data=audio, rate=sample_rate))
    audio_widgets.append(out)
widgets.HBox(audio_widgets)

You could also print or display something else into the same output widget beside the Audio - possibly to have a label next to each audio piece. 您还可以在“ Audio旁边的同一输出小部件中printdisplay其他内容-可能在每个音频片段旁边有一个标签。

This is a very late answer but whatever)). 这是一个很晚的答案,但无论如何)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 IPython.display.audio 在 function 中使用时无法在 jupyter notebook 中播放音频 - Using IPython.display.audio to play audio in jupyter notebook not working when used inside a function 如何修复使用 IPython.Display.Audio (OSError [Errno22] Invalid argument:...) 引起的错误? - How do I fix an error caused in using IPython.Display.Audio (OSError [Errno22] Invalid argument:...)? 使用iPython.display.Audio生成一系列音频 - Generate series of audio tones using iPython.display.Audio IPython.display.Audio无法正确处理`.ogg`文件类型? - IPython.display.Audio cannot correctly handle `.ogg` file type? 为什么 IPython.display.Audio() 在 for 循环中不起作用? - Why doesn't IPython.display.Audio() work when in a for cycle? 如何以编程方式将单元格添加到 IPython 或 Jupyter 笔记本? - How can I programatically add cells to and IPython or Jupyter notebook? 如何调试一个死机的 jupyter notebook ipython kernel? - How can I debug a dieing jupyter notebook ipython kernel? 如何同时捕获和显示 ipython (jupyter notebook) 单元格输出? - How to simultaneously capture and display ipython (jupyter notebook) cell output? 如何在ipython / jupyter笔记本单元格中设置图片显示位置? - How to set picture display location in ipython/jupyter notebook cell? 我只能通过jupyter笔记本使用ipython吗? - Can I only use ipython through the jupyter notebook ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM