I want to make audio spectrum analyzer in Python. I used pyaudio library and I'm reading the stream from the microphone. For every read, I get 4410 values, that I convert to numbers using numpy and then draw them onto pygame screen. It looks like this: https://photos.google.com/share/AF1QipMCWVk1pR0dmrrsTlpE3gHQ9GTUV25MqwUxw4JuW8TrItkGkuU9X3ZpY2ZQ-RLHew?key=UE9Id19IU1dtSHZfUk43TjB3SWxFcVhRRTFYOWFB (the graph is upside down) The code I have for it is this:
import pyaudio, math, struct,pygame, numpy
pa = pyaudio.PyAudio()
#open audio stream
stream = pa.open(input_device_index=1,rate=44100,format=pyaudio.paInt16,channels=2,input=True)
#read bytes from stream and convert to numbers
def get_data():
data = stream.read(int(44100*0.05))
s = numpy.fromstring(data, numpy.int16)
return struct.unpack('h'*4410, data)
pygame.init()
screen = pygame.display.set_mode((4000,1000))
def redraw():
data = get_data()
#draw every number as a bar onto pygame windows
#last 4410 values are missin
for x in range(4000):
val = data[x]
pygame.draw.rect(screen,(0,0,0),(x,0,1,1000),0)
pygame.draw.rect(screen,(255,255,255),(x,0,1,val),0)
pygame.display.update()
pygame.event.clear()
while 1:
redraw()
Is there any fancy way to merge these 4410 values into just 15, so I can have the nice & cool green & red bars in reasonable-sized window, instead of this ugly thing that needs 3 screens?