4

I've tinkered with PyAudio and I've only been able to record from the microphone. I want to be able to record what's played through the speakers. Is there a way to do this with PyAudio or should I be using something else?

  • Microphones take audio in and speakers give audio out. If there's sound playing through a speaker, and the microphone is attached to the computer and in a distance where it can capture the output from the speaker, you'll be able to "record what's playing through the speakers". – Wiggy A. Dec 23 '18 at 08:07

2 Answers2

3

The audio subsystem on Linux works around the concepts of sources and sinks. A source is a place where sound can some into the audio subsystem (microphone, app playing sound, etc), and a sink is a place where it can leave (speakers). You need to find the audio source that acts as a loopback monitor of the speaker sink. See b-ak's answer to the following post:

https://askubuntu.com/questions/229352/how-to-record-output-to-speakers

It explains the commands you can use from the command line to aid in identification of the appropriate identifier of this audio source. Then, you will need to follow Roland Smith's answer to the following post to load this into pyaudio:

voice recording using pyaudio

Here, it is explained that you can instantiate a pyaudio object and get a list of device ids at an interactive python prompt like this:

import pyaudio
p = pyaudio.PyAudio()
[p.get_device_info_by_index(i) for i in range(p.get_device_count())]

Find the appropriate device and then pass the input_device_index parameter to p.open with the appropriate device id when opening the stream.

lesthaeghet
  • 116
  • 6
1

I think this is a duplicate posting of this post 4 years ago

I did some research and found out, that WASAPI still works and seems to be your only option. Here's the Gitlab Link for the extended PyAudio lib. I found no solution for any Linux OS that works "out of the box". If it is really important, try to use any VirtualMachine with Windows on it.

As mentioned earlier the question is why do you want to achieve this and if there might be a more "elegant" way.

  • I am not an expert but I think the user needs two features: 1. Only want to record speaker output stream record/read or 2. record/read mic input and speaker output stream simultaneously. In Linux, maybe use some package and terminal command the problem can solve. I am still working on it. – ati ince Jun 29 '21 at 15:44