gpt4 book ai didi

python - 将 HoloViews VLine 与 PyViz 面板 audio.time 同步

转载 作者:行者123 更新时间:2023-12-02 22:52:42 25 4
gpt4 key购买 nike

我想在 HoloViews 图中可视化当前音频在图中的位置。当 PyViz 的 pn.pane.Audio.time 时,此行应自动更新值改变(当正在播放音频或 Audio.time 改变时)。

我的尝试:

# Python 3.7 in JupyterLab
import numpy as np
import holoviews as hv # interactive plots
hv.notebook_extension("bokeh")
import panel as pn
pn.extension()
from holoviews.streams import Stream, param

# create sound
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)

# PyViz Panel's Audio widget to play sound
audio = pn.pane.Audio(waveform_int, sample_rate=sps)

# generated plotting data
x = np.arange(11.0)
y = np.arange(11.0, 0.0, -1) / 10
y[0::2] *= -1 # alternate positve-negative
# HoloViews line plot
line_plot = hv.Curve((x, y)).opts(width=500)

# should keep track of audio.time; DOES NOT WORK
Time = Stream.define('Time', t=param.Number(default=0.0, doc='A time parameter'))
time = Time(t=audio.time)

# callback to draw line when time value changes
def interactive_play(t):
return hv.VLine(t).opts(color='green')

# dynamic map plot of line for current audio time
dmap_time = hv.DynamicMap(interactive_play, streams=[time])

# display Audio pane
display(audio)
# combine plot with stream of audio.time
line_plot * dmap_time

为什么这不起作用?

由于时间设置为 param.Number() ,我希望这可以跟踪 audio.time .因此,在播放音频时,回调到 interactive_play()应该不断地被调用,导致一条线在情节上移动。
这不会发生,并且该行仅停留在默认值 0.0(或任何其他值 audio.time 在代码执行时具有)。

如何更新 VLine继续跟踪 audio.time ?

绿线应与音频 Pane 的时间匹配

holoviews panel audio-sync

最佳答案

Since time is set as a param.Number(), I expect this to keep track of audio.time.



在您的示例中,您没有以任何方式将 Panel Audio 对象链接到流。当你这样做时,你所做的一切:
time = Time(t=audio.time)

设置您的 Time 的初始值流到音频 Pane 的当前值。 audio.time不是对参数的引用,它只是该参数的当前值。

HoloViews DynamicMaps 支持监听其他对象参数的能力已经有一段时间了。有两种主要方法可以解决这个问题,或者通过执行以下操作:
@pn.depends(t=audio.param.time)
def interactive_play(t):
return hv.VLine(t).opts(color='green')

dmap_time = hv.DynamicMap(interactive_play)

在这里你正在装饰 interactive_play函数依赖于 audio.time参数,因此每当它更改时,DynamicMap 都会更新。执行此操作的更明确的方法以及内部实际发生的情况是:
from holoviews.streams import Params

def interactive_play(t):
return hv.VLine(t).opts(color='green')

stream = Params(parameters=[audio.param.time], rename={'time': 't'})
dmap_time = hv.DynamicMap(interactive_play, streams=[stream])

如果您需要使用新文件更新音频 Pane ,我强烈建议您使用回调来执行此操作,而不是使用交互或响应式(Reactive) API 不断创建新的音频 Pane 。这样,您也不必处理将流更新为不断变化的音频 Pane 。

关于python - 将 HoloViews VLine 与 PyViz 面板 audio.time 同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59063698/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com