Reputation: 41
I'm trying to play a song, make a audio visualizer for it while it's playing, and I want the visualizer to be based on output signals so that it more modular and easier to sync, but I don't know how to get access my computer's audio output without making a special hardware. How do I do it with just programming.
I've been researching pyaudio, and here is my code thus far. I think it's accessing both input and output signals, but I don't know how to remove the former. All my attempts have crashed the program.
Lastly, this code mostly comes from this fantastic article on the subject: https://www.swharden.com/wp/2016-07-19-realtime-audio-visualization-in-python/
import pyaudio
import numpy as np
maxValue = 2**16
bars = 35
p=pyaudio.PyAudio()
stream=p.open(format=pyaudio.paInt16,channels=2,rate=44100,
input=True, output=True, frames_per_buffer=256
)
while True:
data = np.fromstring(stream.read(1024),dtype=np.int16)*100
#print(type(data))
dataL = data[0::2]
dataR = data[1::2]
#print(dataR.shape)
peakL = np.abs(np.max(dataL)-np.min(dataL))/maxValue
peakR = np.abs(np.max(dataR)-np.min(dataR))/maxValue
lString = "#"*int(peakL*bars)+"-"*int(bars-peakL*bars)
rString = "#"*int(peakR*bars)+"-"*int(bars-peakR*bars)
#print(dataL)
print("L=[%s]\tR=[%s]"%(lString, rString))
#if lString != rString:
# print('here')
Upvotes: 4
Views: 7104
Reputation: 11407
Here are three ideas:
Mind that if your idea was to play music in some player and let Python somehow catch the stream, that would be rather challenging. The processes and streams are isolated for a good purpose. Accessing memory space that does not belong to you can easily result in segmentation fault.
Upvotes: 2