import pyaudio import numpy import scipy.io.wavfile as wav import keyboard from sys import getsizeof import time RATE=48000 RECORD_SECONDS = 20 CHUNKSIZE = 1024*4 ## 1024 samplesI = 0 saves = 0 def on_triggered(): #define your function to be executed on hot-key press global cb, saves print("Saved replay!") myBuf = frames[-320:] myBufLocal = frames2[-320:] numpydata = numpy.hstack(myBuf) numpydata2 = numpy.hstack(myBufLocal) saves+=1 name = time.strftime("%Y-%m-%d_%H-%M-%S-out-") wav.write(name + str(saves) + '.wav',RATE,numpydata) wav.write(name + str(saves) + '-local.wav',RATE,numpydata2) #write_to_textfield(text_to_print) #<-- your function shortcut = 'alt+1' keyboard.add_hotkey(shortcut, on_triggered) # initialize portaudio cleanUp = False p = pyaudio.PyAudio() info = p.get_host_api_info_by_index(0) numdevices = info.get('deviceCount') incomingAudioChan = -1 localAudioChan = -1 deviceWithId = {} for i in range(0, numdevices): if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0: print("Using " + p.get_device_info_by_host_api_device_index(0, i).get('name') + " for " + str(i)) for i in range(0, numdevices): if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0: deviceWithId[i] = p.get_device_info_by_host_api_device_index(0, i).get('name') # print("id ", i, " - ", p.get_device_info_by_host_api_device_index(0, i).get('name')) if("VoiceMeeter VAIO3 Output" in p.get_device_info_by_host_api_device_index(0, i).get('name')): localAudioChan = i print("Using " + p.get_device_info_by_host_api_device_index(0, i).get('name') + " for local stream " + str(i)) if("CABLE" in p.get_device_info_by_host_api_device_index(0, i).get('name')): incomingAudioChan = i print("Using " + p.get_device_info_by_host_api_device_index(0, i).get('name') + " for Discord stream " + str(i)) if(incomingAudioChan == -1 or localAudioChan == -1): print("FAILURE") exit stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNKSIZE, input_device_index=incomingAudioChan) streamLocal = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNKSIZE, input_device_index=localAudioChan) frames = [] # A python-list of chunks(numpy.ndarray) frames2 = [] #for _ in range(0, int(RATE / CHUNKSIZE * RECORD_SECONDS)): print("Running!") try: while True: data = stream.read(CHUNKSIZE) frames.append(numpy.fromstring(data, dtype=numpy.int16)) data = streamLocal.read(CHUNKSIZE) frames2.append(numpy.fromstring(data, dtype=numpy.int16)) samplesI+=1 #print(getsizeof(frames)) if(len(frames) >= 900): cleanUp = True if(len(frames) <= 700): cleanUp = False # print(len(frames)) if(cleanUp): # print("Cleaning") frames.pop(0) frames.pop(0) frames.pop(0) frames2.pop(0) frames2.pop(0) frames2.pop(0) except KeyboardInterrupt: stream.stop_stream() stream.close() p.terminate() print("!!!!", end="") print(samplesI)