This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
PYTHON 2 VERSION
You need the pydub package: you can install it with pip install pydub
. (https://github.com/jiaaro/pydub/)
This package requires the open-source multimedia library FFmpeg for the decompression of MP3 files. (http://www.ffmpeg.org)
import urllib
import urllib2
import cStringIO
import numpy as np
import scipy.signal as sg
import pydub
import matplotlib.pyplot as plt
from IPython.display import Audio, display
%matplotlib inline
def speak(sentence):
url = "http://translate.google.com/translate_tts?tl=en&q=" + \
urllib.quote_plus(sentence)
req = urllib2.Request(url, headers={'User-Agent': ''})
mp3 = urllib2.urlopen(req).read()
# We convert the mp3 bytes to wav.
audio = pydub.AudioSegment.from_mp3(cStringIO.StringIO(mp3))
wave = audio.export(cStringIO.StringIO(), format='wav')
wave.reset()
wave = wave.read()
# We get the raw data by removing the 24 first bytes
# of the header.
x = np.frombuffer(wave, np.int16)[24:] / 2.**15
return x, audio.frame_rate
Audio
class.def play(x, fr, autoplay=False):
display(Audio(x, rate=fr, autoplay=autoplay))
x, fr = speak("Hello world")
play(x, fr)
plt.figure(figsize=(6,3));
t = np.linspace(0., len(x)/fr, len(x))
plt.plot(t, x, lw=1);
b, a = sg.butter(4, 500./(fr/2.), 'low')
x_fil = sg.filtfilt(b, a, x)
play(x_fil, fr)
plt.figure(figsize=(6,3));
plt.plot(t, x, lw=1);
plt.plot(t, x_fil, lw=1);
We hear a muffled voice.
b, a = sg.butter(4, 1000./(fr/2.), 'high')
x_fil = sg.filtfilt(b, a, x)
play(x_fil, fr)
plt.figure(figsize=(6,3));
plt.plot(t, x, lw=1);
plt.plot(t, x_fil, lw=1);
It sounds like a phone call.
from IPython.html import widgets
@widgets.interact(t=(100., 5000., 100.))
def highpass(t):
b, a = sg.butter(4, t/(fr/2.), 'high')
x_fil = sg.filtfilt(b, a, x)
play(x_fil, fr, autoplay=True)
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).