0

I am creating a simple program that visualizes stereo sound. The program code below shows the visualization from mirophone (input) only. I want the visualization to work from the output (system sound).

import pyaudio
import numpy as np

maxValue = 2**16
p=pyaudio.PyAudio()
stream = p.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, output=True, frames_per_buffer=1024, output_device_index=3)


while True:
    data = np.fromstring(stream.read(1024),dtype=np.int16)
    dataL = data[0::2]
    dataR = data[1::2]
    peakL = np.abs(np.max(dataL)-np.min(dataL))/maxValue
    peakR = np.abs(np.max(dataR)-np.min(dataR))/maxValue

    print("left", round(peakL, 2))
    print("right", round(peakR, 2))
PawełeK
  • 33
  • 6

1 Answers1

1

If you're on Linux:

  1. Open PulseAudio Volume Control *
  2. Go to the Recording tab
  3. Start your script (or check it's already running).
  4. Change the input of the new stream named ALSA plug-in [python 3.8] (or something very similar) to Monitor of <your-audio-output-device>

* On Debian-based systems you can install PulseAudio Volume Control using sudo apt install pavucontrol

Also it may be worth checking out the sounddevice library, I switched over to it from pyaudio in my personal project a while ago and (personally) have found it's a little easier to work with. They are very, very similar in usage but sounddevice figures out a few things automatically that you need to figure out yourself with pyaudio.

I have a library that uses sounddevice that's part of a framework I'm developing that I think fulfills your needs (except it's non-blocking for real-time usage). Hopefully it's useful for reference: https://gitlab.com/volux/voluxaudio.

Let me know if you want clarification on anything, happy to help :)