Widgets_audio

Widgets are a quick way to get interactivity in your Jupyter displays.

Here is a sample of code from github.com/jupyter-widgets. It creates an audio signal where you can adjust the frequencies with sliders.


In [6]:
# You need numpy, matplotlib and widgets for this to work. And some audio tools
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
from ipywidgets import interactive
from IPython.display import Audio, display

In [4]:
def beat_freq(f1=220.0, f2=224.0):
    max_time = 5
    rate = 8000
    times = np.linspace(0,max_time,rate*max_time)
    signal = np.sin(2*np.pi*f1*times) + np.sin(2*np.pi*f2*times)
    display(Audio(data=signal, rate=rate))
    return signal

In [5]:
v = interactive(beat_freq, f1=(200.0,300.0), f2=(200.0,300.0))
display(v)


array([  0.00000000e+00,   3.62844010e-01,   7.12946526e-01, ...,
         9.48975974e-02,   4.99673224e-02,   9.49649499e-13])

In [ ]:
v.kwargs

In [ ]:
f1, f2 = v.children[:2]
f1.value = 255
f2.value = 260
plt.plot(v.result[0:6000]);

In [ ]: