Interacting with audio#

import os
import numpy as np
import panel as pn
import holoviews as hv 
hv.extension("bokeh")
from holoviews.streams import Stream, Params
from splaat.audio.load import load_audio
from splaat.plot.spectrogram import compute_spectrogram
audio_directory = os.path.join(os.path.dirname(os.path.abspath("")), "_static", "audio")
audio, sr = load_audio(os.path.join(audio_directory, "mfa.wav"))
width = 1000
data = audio[0, :]
audio_widget = pn.pane.Audio(data, sample_rate=sr, name='Audio', throttle=500)
time = np.linspace(0, len(data) / sr, num=len(data))
line_plot = hv.Curve((time, data), ["time (s)","amplitude"]).opts(width=width)
f, t, sxx = compute_spectrogram(audio, sr)
spec_gram = hv.Image((t, f, np.log10(sxx)), ["time (s)","frequency (hz)"]).opts(width=width)

def interactive_play(x,y,t):
    if x is None:
        return hv.VLine(t).opts(color='green')
    else:
        audio_widget.time = x
        return hv.VLine(x).opts(color='green')

stream = Params(parameters=[audio_widget.param.time], rename={'time': 't'})
tap = hv.streams.SingleTap(transient=True)
dmap_time = hv.DynamicMap(interactive_play, streams=[stream, tap])

pn.Column( audio_widget, (spec_gram * dmap_time), (line_plot * dmap_time))
WARNING:param.Curve: Chart elements should only be supplied a single kdim