1
I tried to record the sound of the desktop with the pyaudio
, but I can only hear the sound of the microphone or speaker.
I also downloaded the portaudio, but I’m not sure I can get the sound of the speaker and the sound of the microphone with it simultaneously
I noticed that when I changed the input device to stereo mixer I was able to record the audio from the speaker but I stopped recording the microphone.
This is the code I’m using:
import pyaudio
import wave
import threading
import time
import subprocess
CHUNK = 1024
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "tmp/tmp.wav"
class recorder:
def __init__(self):
self.going = False
self.process = None
self.filename = "ScreenCapture.mpg"
def record(self,filename):
try:
if self.process.is_alive():
self.going = False
except AttributeError:
print("test")
self.process = threading.Thread(target=self._record)
self.process.start()
self.filename = filename
def _record(self):
p = pyaudio.PyAudio()
stream = p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
as_loopback=True)
#print("* recording")
frames = []
self.going = True
while self.going:
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()
def stop_recording(self):
self.going = False
What should I do?
Code updated as per the advice of ederwander but has not yet worked. I don’t know if the problem is the code or my notebook :
import pyaudio
import wave
import threading
import time
import subprocess
#from pydub import AudioSegment
import numpy as np
CHUNK = 1024
FORMAT = pyaudio.paInt16
#CHANNELS = 2
RATE = 44100
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "tmp/tmp.wav"
p = pyaudio.PyAudio()
for i in range(0, p.get_device_count()):
print(i, p.get_device_info_by_index(i)['name'])
class recorder:
def __init__(self):
self.going = False
self.process = None
self.filename = "ScreenCapture.mpg"
def record(self, filename):
try:
if self.process.is_alive():
self.going = False
except AttributeError:
print("test")
self.process = threading.Thread(target=self._record)
self.process.start()
self.filename = filename
def _record(self):
#p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,
channels=2,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
input_device_index=0,
as_loopback=True)
stream2 = p.open(
format=FORMAT,
channels=1,
rate=RATE,
input=True,
frames_per_buffer=CHUNK,
input_device_index=1)
# as_loopback=False)
print("* recording")
frames = []
frames2= []
self.going = True
while self.going:
data = stream.read(CHUNK)
data2 = stream2.read(CHUNK)
frames.append(data)
frames2.append(data)
# frames = dados do som as_loopback (Speakers)
frames = b''.join(frames);
# frames2 = dados do som Microfone
frames2 = b''.join(frames2);
# decodificando os dados do Speaker
Sdecoded = np.frombuffer(frames, 'int16')
# decodificando o microfone
Mdecoded = np.frombuffer(frames2, 'int16')
# convertendo os dados do Speaker em um vetor do tipo Numpy (facilitando a vida na hora de pegar os canais de áudio)
Sdecoded = np.array(Sdecoded, dtype='int16')
# pegando os dados do lado direito
direito = Sdecoded[1::2]
# pegando os dados do lado esquerdo
esquerdo = Sdecoded[::2]
# mixando tudo para mono = somar lado direito + lado esquerdo + os dados decofificados do Microfone q já estão em mono
mix = (direito + esquerdo + Mdecoded)
# garantindo que nenhum valor extrapole os limites do short int
signal = np.clip(mix, -32767, 32766)
# codificar os dados novamente
encodecoded = wave.struct.pack("%dh" % (len(signal)), *list(signal))
print("* done recording")
stream.stop_stream()
stream.close()
stream2.stop_stream()
stream2.close()
p.terminate()
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
#wf.writeframes(b''.join(frames))
wf.writeframes((encodecoded))
wf.close()
def stop_recording(self):
self.going = False
Please avoid long discussions in the comments; your talk was moved to the chat
– Maniero