This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
import numpy as np
import matplotlib.pyplot as plt
from IPython.display import Audio, display, clear_output
from IPython.html import widgets
from functools import partial
import matplotlib as mpl
%matplotlib inline
rate = 16000.
duration = .5
t = np.linspace(0., duration, rate * duration)
Audio
class.def synth(f):
x = np.sin(f * 2. * np.pi * t)
display(Audio(x, rate=rate, autoplay=True))
synth(440)
keys = 'C,C#,D,D#,E,F,F#,G,G#,A,A#,B,C'.split(',')
notes = zip(keys, 440. * 2 ** (np.arange(3, 3 + len(keys)) / 12.))
container = widgets.ContainerWidget()
buttons = []
for note, f in notes:
button = widgets.ButtonWidget(description=note)
def on_button_clicked(f, b):
clear_output()
synth(f)
button.on_click(partial(on_button_clicked, f))
button.set_css({'width': '30px',
'height': '60px',
'padding': '0',
'color': ('black', 'white')['#' in note],
'background': ('white', 'black')['#' in note],
'border': '1px solid black',
'float': 'left'})
buttons.append(button)
container.children = buttons
display(container)
container.remove_class('vbox')
container.add_class('hbox')
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).