7

I have been searching for this since last week. Tried pyaudio also and when I used it and another fork, the system audio was mixed with microphone audio. I was not able to find any other module for this and thus finally asked the question.

import pyaudio
import wave

CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "output.wav"

p = pyaudio.PyAudio()
SPEAKERS = p.get_default_output_device_info()["hostApi"] #The modified part

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                frames_per_buffer=CHUNK,
                input_host_api_specific_stream_info=SPEAKERS,
                as_loopback = True) #The part I have modified

print("* recording")

frames = []

for i in range(0, int(RATE / CHUNK * RECORD_SECONDS) + 1):
    data = stream.read(CHUNK)
    frames.append(data)

print("* done recording")

stream.stop_stream()
stream.close()
p.terminate()

wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(CHANNELS)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes(b''.join(frames))
wf.close()

This code was taken from Stack Overflow. It records the speaker output, but the output is mixed with the microphone input. Also the pyaudio module used was from the fork: https://github.com/intxcc/pyaudio_portaudio.

2
  • What you have tried so far. Any code ? What are the errors you encountered ?
    – g10dras
    Mar 14, 2019 at 0:37
  • 1
    I added the code. Jul 6, 2019 at 15:18

3 Answers 3

4

using https://github.com/intxcc/pyaudio_portaudio
This only records the audio of the device specified by "device_id"

import pyaudio
import wave

chunk = 1024  # Record in chunks of 1024 samples
sample_format = pyaudio.paInt16  # 16 bits per sample
channels = 2
fs = 44100  # Record at 44100 samples per second
seconds = 3
filename = "output.wav"

p = pyaudio.PyAudio()  # Create an interface to PortAudio

#Select Device
print ( "Available devices:\n")
for i in range(0, p.get_device_count()):
    info = p.get_device_info_by_index(i)
    print ( str(info["index"]) +  ": \t %s \n \t %s \n" % (info["name"], p.get_host_api_info_by_index(info["hostApi"])["name"]))
    pass

#ToDo change to your device ID
device_id = 7
device_info = p.get_device_info_by_index(device_id)
channels = device_info["maxInputChannels"] if (device_info["maxOutputChannels"] < device_info["maxInputChannels"]) else device_info["maxOutputChannels"]
# https://people.csail.mit.edu/hubert/pyaudio/docs/#pyaudio.Stream.__init__
stream = p.open(format=sample_format,
                channels=channels,
                rate=int(device_info["defaultSampleRate"]),
                input=True,
                frames_per_buffer=chunk,
                input_device_index=device_info["index"],
                as_loopback=True
                )

frames = []  # Initialize array to store frames

print('\nRecording', device_id, '...\n')

# Store data in chunks for 3 seconds
for i in range(0, int(fs / chunk * seconds)):
    data = stream.read(chunk)
    frames.append(data)

# Stop and close the stream 
stream.stop_stream()
stream.close()
# Terminate the PortAudio interface
p.terminate()

print('Finished recording')

# Save the recorded data as a WAV file
wf = wave.open(filename, 'wb')
wf.setnchannels(channels)
wf.setsampwidth(p.get_sample_size(sample_format))
wf.setframerate(fs)
wf.writeframes(b''.join(frames))
wf.close()

P.S. check out https://github.com/intxcc/pyaudio_portaudio/tree/master/example

3
  • 1
    A reminder to those who see this later : 1, do not use the official pyaudio, install and use the released wheel from github.com/intxcc/pyaudio_portaudio, 2. Make sure the device_id is correct, otherwise the stream part will report an error
    – Wade Wang
    Mar 5, 2022 at 10:52
  • Sadly, this thing only has support for linux out of the box with the amd64 wheel that he made.
    – Ryan Glenn
    Aug 1, 2022 at 19:31
  • impossible to build the library
    – Jalle
    Mar 24 at 22:43
1

This can be done with soundcard. You will have to figure out which device index to use for your loopback. This code prints out the ones you will have to choose from. I found the correct one by looping over all of them and seeing which produced non zeros when speakers were playing.

pip install soundcard

import soundcard as sc
import time

# get a list of all speakers:
speakers = sc.all_speakers()
# get the current default speaker on your system:
default_speaker = sc.default_speaker()

# get a list of all microphones:v
mics = sc.all_microphones(include_loopback=True)
# get the current default microphone on your system:
default_mic = mics[index of your speaker loopback here]

for i in range(len(mics)):
    try:
        print(f"{i}: {mics[i].name}")
    except Exception as e:
        print(e)

with default_mic.recorder(samplerate=148000) as mic, \
            default_speaker.player(samplerate=148000) as sp:
    print("Recording...")
    data = mic.record(numframes=1000000)
    print("Done...Stop your sound so you can hear playback")
    time.sleep(5)
    sp.play(data)
3
  • What file format does this save to? .wav?
    – Ryan Glenn
    Aug 1, 2022 at 19:32
  • This package looks really nicely documented.
    – Ryan Glenn
    Aug 1, 2022 at 19:37
  • 1
    The soundcard package doesn't seem to have support for recording output. This code records input and plays it through an output device--the opposite of what the post was asking.
    – nscalf
    May 3 at 0:55
0

On Ubuntu, you can use 'pavucontrol' to change the recording source. An example of recording audio directly from the speakers (without using a microphone):

First you run a script like the one below:


import pyaudio
mic = pyaudio.PyAudio()
stream = mic.open(format=pyaudio.paInt16, channels=1, rate=44100, input=True, output=True, frames_per_buffer=2048)
stream.start_stream()

if __name__ == '__main__':
    while True:
        data = stream.read(1024)
        # Do something with sound

Then you can change the recording source (recording tab) from 'Built-in=Audio Analog Stereo' to 'Monitor of Built-in=Audio Analog Stereo'. With this approach, you can analyze the sound from the speakers during the video call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.