What’s wrong with my python code?

Asked

Viewed 124 times

-3

I’m making an app to film the desktop with system audio and microphone. Initially it seemed simple but I found a problem on the way:I’m not able to record the sound of the system.Initially I thought it could be my computer that was having problems.But after downloading Camtasia I was able to record the sound of the system and the microphone. I had asked a similar question to this one and a user answered but his code does not work within my project curiously. So the only possibility is that something is killing the system audio recording. I’m going to leave his code and my project here for you to try and see what it could be.

Code that the user posted (I managed to get audio from the stereo mixer and microphone with it but it doesn’t work within my project):

import pyaudio
import wave
import numpy as np


CHUNK = 1024
FORMAT = pyaudio.paInt16
RATE = 44100
RECORD_SECONDS = 2
WAVE_OUTPUT_FILENAME = "tmp.wav"


p = pyaudio.PyAudio()


for i in range(0, p.get_device_count()):
    print(i, p.get_device_info_by_index(i)['name'])


#stream usando o as_loopback para pegar som do SO
stream = p.open(
    format = FORMAT,
    channels = 2,
    rate = RATE,
    input=True,
    frames_per_buffer=CHUNK,
    input_device_index=2,
    as_loopback=True)

##stream usando o iput device do meu Microphone
stream2 = p.open(
    format = FORMAT,
    channels = 1,
    rate = RATE,
    input=True,
    frames_per_buffer=CHUNK,
    input_device_index=1)
    #as_loopback=False)


frames = []
frames2 = []


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


#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))


#parar todos os streams e finalizar o pyaudio
stream.stop_stream()
stream.close()
stream2.stop_stream()
stream2.close()
p.terminate()


#gravando o áudio mixado em mono 
wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(FORMAT))
wf.setframerate(RATE)
wf.writeframes((encodecoded))
wf.close()

The code I made mixing mine and his:

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=2,
            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

1 answer

-1


No error returns? tries to change:WAVE_OUTPUT_FILENAME = "tmp.wav" for WAVE_OUTPUT_FILENAME = "tmp.wave"

  • 1

    Good morning Marcos.Worst that returns no error. I will try to change this.

  • 1

    Any help is welcome. I noticed an error appeared and put in the description of the question because here exceeded the maximum of characters.

  • 1

    in this mix = ... vc is trying to add the values, but the error occurs , because the numpy tries to make a Broadcasting already that sum of arrays of different dimensions and it is not succeeding, I suggest testing without the mix...

  • 1

    I already solved the problem. That wasn’t exactly the problem.But since you were the only one who tried to help me I’ll give you the best answer.

  • what exactly was the problem?

  • I don’t know. The important thing is that it worked.I think I deleted a module that was meant to matter unintentionally.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.