Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python audio graph live stream

import numpy as np
import pyaudio
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
plt.style.use('bmh')

SAMPLESIZE = 4096 # number of data points to read at a time
SAMPLERATE = 44100 # time resolution of the recording device (Hz)

p = pyaudio.PyAudio() # instantiate PyAudio
stream=p.open(format=pyaudio.paInt16,channels=1,rate=SAMPLERATE,input=True,
              frames_per_buffer=SAMPLESIZE) # use default input device to open audio stream

# set up plotting
fig = plt.figure()
ax = plt.axes(xlim=(0, SAMPLESIZE-1), ylim=(-9999, 9999))
line, = ax.plot([], [], lw=1)

# x axis data points
x = np.linspace(0, SAMPLESIZE-1, SAMPLESIZE)

# methods for animation
def init():
    line.set_data([], [])
    return line,
def animate(i):
    y = np.frombuffer(stream.read(SAMPLESIZE), dtype=np.int16)
    line.set_data(x, y)
    return line,

FuncAnimation(fig, animate, init_func=init, frames=200, interval=20, blit=True)

plt.show()

# stop and close the audio stream
stream.stop_stream()
stream.close()
p.terminate()
Comment

PREVIOUS NEXT
Code Example
Python :: how do you change a class variable in python 
Python :: pandas iloc range 
Python :: move to next iteration of for loop python 
Python :: make setup file for cython 
Python :: timedistributed pytorch 
Python :: backslashing in an interactive session in python 
Python :: python requests-session for websites wihout login 
Python :: function with parameters python 
Python :: plotly showing routes 
Python :: python subclass with extra arguments 
Python :: Create Tables From Migration 
Python :: threading pass keyword args example 
Python :: double linked list python 
Python :: Python Tkinter Frame Widget Syntax 
Python :: move a file in python 
Python :: Move Mouse Every Minute Using Python 3 & PyAutoGUI 
Python :: pygame lerp 
Python :: Mirror Inverse Program in python 
Python :: How to swapcase of string in python 
Python :: Function argument unpacking in python 
Python :: python keyerror 0 
Python :: print(((x//y)+1)*z) means in python 
Python :: python set xticks to int not float 
Python :: python find occurance of item 
Python :: how to run another python file in python 
Python :: preventing players from changing existing entries in tic tac toe game 
Python :: python array_combine 
Python :: cos2x 
Python :: find location of a class in python 
Python :: 218922995834555169026 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =