使用PyViz Panel audio.time同步HoloViews VLine

问题描述 投票:0回答:1

我想在图中显示当前音频的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()的回调,从而使Line在图上移动。这不会发生,并且该行仅保持默认值0.0(或代码执行时audio.time具有的其他任何值)。

如何更新VLine以保持跟踪audio.time

绿线应与音频窗格的时间匹配

holoviews panel audio-sync

我想在图中显示当前音频的HoloViews图中可视化。当PyViz的pn.pane.Audio.time值更改时(播放音频或...

python audio panel holoviews pyviz
1个回答
0
投票
由于将时间设置为param.Number(),所以我希望它可以跟踪audio.time。
© www.soinside.com 2019 - 2024. All rights reserved.