In [ ]:
In [3]:
import numpy as np
import pyaudio
import wave
import matplotlib.pyplot as plt
import matplotlib.animation as animation
data_type = 16 # mapped to 2 ** 15 possible values
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
audio = pyaudio.PyAudio()
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
def init():
line.set_visible(False)
return line,
fig, ax = plt.subplots()
line, = ax.plot(0, 0, color='DarkViolet')
def update(frame, line):
if frame==1:
line.set_visible(True)
data = stream.read(CHUNK)
decoded = np.fromstring(data, dtype=np.int16)
sound = decoded #/ (2.0 ** (data_type - 1))
t=np.arange(sound.size)
line.set_data(t, sound)
line.axes.axis([0, sound.size, -7000, 7000])
return line,
ani = animation.FuncAnimation(fig, update, init_func=init, fargs=[line],
interval=25, blit=True)
plt.show()
In [ ]: