Skip to content

Commit

Permalink
Create pyplot_example.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Souloist committed Oct 29, 2015
1 parent bb5551a commit 4e595d7
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions Visualizing_with_pyplot/pyplot_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# plot_micinput.py
"""
Using Pyaudio, record sound from the audio device and plot,
for 8 seconds, and display it live in a Window.
Usage example: python pyrecplotanimation.py
Gerald Schuller, October 2014
Modified: Ivan Selesnick, September 2015
"""

import pyaudio
import struct
from matplotlib import pyplot as plt

plt.ion() # Turn on interactive mode so plot gets updated

WIDTH = 2 # bytes per sample
CHANNELS = 1 # mono
# RATE = 16000
RATE = 8000
BLOCKSIZE = 1024
DURATION = 10 # Duration in seconds

NumBlocks = int( DURATION * RATE / BLOCKSIZE )

print 'BLOCKSIZE =', BLOCKSIZE
print 'NumBlocks =', NumBlocks

# Initialize plot window:
plt.figure(1)
plt.ylim(-10000, 10000) # set y-axis limits

plt.xlim(0, BLOCKSIZE) # set x-axis limits
plt.xlabel('Time (n)')
t = range(0, BLOCKSIZE)

# # Time axis in units of milliseconds:
# plt.xlim(0, 1000.0*BLOCKSIZE/RATE) # set x-axis limits
# plt.xlabel('Time (msec)')
# t = [n*1000/float(RATE) for n in range(BLOCKSIZE)]

line, = plt.plot([], [], color = 'blue') # Create empty line
line.set_xdata(t) # x-data of plot (time)

# Open audio device:
p = pyaudio.PyAudio()
stream = p.open(format = p.get_format_from_width(WIDTH),
channels = CHANNELS,
rate = RATE,
input = True,
output = False)

for i in range(1, NumBlocks):
input_string = stream.read(BLOCKSIZE) # Read audio input stream
input_tuple = struct.unpack('h'*BLOCKSIZE, input_string) # Convert
line.set_ydata(input_tuple) # Update y-data of plot
# # Print block number:
# if i % 20 == 0:
# print i,
plt.draw()
plt.close()

stream.stop_stream()
stream.close()
p.terminate()

print '* Done'

0 comments on commit 4e595d7

Please sign in to comment.