This commit is contained in:
TheGreyDiamond
2021-03-21 20:34:57 +01:00
parent d468436bc8
commit 9e4e58a5f4

View File

@ -1,29 +1,61 @@
import pyaudio import pyaudio, numpy, keyboard, time
import numpy
import scipy.io.wavfile as wav import scipy.io.wavfile as wav
import keyboard
from sys import getsizeof from sys import getsizeof
import time
RATE=48000 RATE=48000
RECORD_SECONDS = 20 RECORD_SECONDS = 20
CHUNKSIZE = 1024*4 ## 1024 CHUNKSIZE = 1024*4
SourcesToRecord = ["VoiceMeeter VAIO3 Output", "CABLE"]
print("Searching for audio sources")
Sources = []
AudioChannels = []
Frames = []
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
for i in range(0, numdevices):
if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
i2 = 0
while(i2 < len(SourcesToRecord)):
if(SourcesToRecord[i2] in p.get_device_info_by_host_api_device_index(0, i).get('name')):
print("Using " + p.get_device_info_by_host_api_device_index(0, i).get('name') + " for '" + SourcesToRecord[i2] + "' ID: " + str(i))
Sources.append(i)
AudioChannels.append(p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNKSIZE, input_device_index=i))
i2+=1
#streamLocal = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNKSIZE, input_device_index=localAudioChan)
#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))
## Populate frame buffer
i = 0
while(i <= len(SourcesToRecord)):
Frames.append([])
i += 1
samplesI = 0 samplesI = 0
saves = 0 saves = 0
def on_triggered(): #define your function to be executed on hot-key press def on_triggered(): #define your function to be executed on hot-key press
global cb, saves global cb, saves
print("Saved replay!") print("Saving replay..", end = "")
myBuf = frames[-320:] cutUpBuffers = []
myBufLocal = frames2[-320:] i = 0
numpydata = numpy.hstack(myBuf)
numpydata2 = numpy.hstack(myBufLocal)
saves+=1
name = time.strftime("%Y-%m-%d_%H-%M-%S-out-") name = time.strftime("%Y-%m-%d_%H-%M-%S-out-")
wav.write(name + str(saves) + '.wav',RATE,numpydata) while(i < len(Frames) - 1):
wav.write(name + str(saves) + '-local.wav',RATE,numpydata2) # cutUpBuffers.append(numpy.hstack(Frames[i][-320:]))
wav.write(name + str(saves) + str(i) + '.wav',RATE,numpy.hstack(Frames[i][-320:]))
i += 1
saves+=1
print("[DONE]")
# wav.write(name + str(saves) + '-local.wav',RATE,numpydata2)
#write_to_textfield(text_to_print) #<-- your function #write_to_textfield(text_to_print) #<-- your function
shortcut = 'alt+1' shortcut = 'alt+1'
@ -31,67 +63,57 @@ keyboard.add_hotkey(shortcut, on_triggered)
# initialize portaudio # initialize portaudio
cleanUp = False cleanUp = False
p = pyaudio.PyAudio()
info = p.get_host_api_info_by_index(0)
numdevices = info.get('deviceCount')
incomingAudioChan = -1 incomingAudioChan = -1
localAudioChan = -1 localAudioChan = -1
deviceWithId = {} deviceWithId = {}
for i in range(0, numdevices): #for i in range(0, numdevices):
if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0: # 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)) # print("Using " + p.get_device_info_by_host_api_device_index(0, i).get('name') + " for " + str(i))
for i in range(0, numdevices): #for i in range(0, numdevices):
if (p.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0: # 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') # 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')) # # 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')): # if("VoiceMeeter VAIO3 Output" in p.get_device_info_by_host_api_device_index(0, i).get('name')):
localAudioChan = i # localAudioChan = i
print("Using " + p.get_device_info_by_host_api_device_index(0, i).get('name') + " for local stream " + str(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')): # if("CABLE" in p.get_device_info_by_host_api_device_index(0, i).get('name')):
incomingAudioChan = i # incomingAudioChan = i
print("Using " + p.get_device_info_by_host_api_device_index(0, i).get('name') + " for Discord stream " + str(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): #if(incomingAudioChan == -1 or localAudioChan == -1):
print("FAILURE") # print("FAILURE")
exit # exit
stream = p.open(format=pyaudio.paInt16, channels=1, rate=RATE, input=True, frames_per_buffer=CHUNKSIZE, input_device_index=incomingAudioChan) print(" There are " + str(len(SourcesToRecord)) + " SourcesToRecord. Frames: " + str(len(Frames)))
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!") print("Running!")
try: try:
while True: while True:
data = stream.read(CHUNKSIZE) i = 0
frames.append(numpy.fromstring(data, dtype=numpy.int16)) while(i < len(SourcesToRecord)):
data = AudioChannels[i].read(CHUNKSIZE)
# print(i)
Frames[i].append(numpy.fromstring(data, dtype=numpy.int16))
i += 1
data = streamLocal.read(CHUNKSIZE)
frames2.append(numpy.fromstring(data, dtype=numpy.int16))
samplesI+=1 samplesI+=1
#print(getsizeof(frames)) if(len(Frames[i]) >= 900):
if(len(frames) >= 900):
cleanUp = True cleanUp = True
if(len(frames) <= 700): if(len(Frames[i]) <= 700):
cleanUp = False cleanUp = False
# print(len(frames))
if(cleanUp): if(cleanUp):
# print("Cleaning") while(i < len(SourcesToRecord)):
frames.pop(0) Frames[i] = Frames[i].pop(0)
frames.pop(0) i += 1
frames.pop(0)
frames2.pop(0)
frames2.pop(0)
frames2.pop(0)
except KeyboardInterrupt: except KeyboardInterrupt:
stream.stop_stream() i = 0
stream.close() while(i < len(AudioChannels)):
AudioChannels[i].stop_stream()
AudioChannels[i].close()
i += 1
p.terminate() p.terminate()
print("!!!!", end="") print("Quiting system, saved frame over lifetime: ", end="")
print(samplesI) print(samplesI)