-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathrecord_handle.py
More file actions
101 lines (86 loc) · 3.13 KB
/
record_handle.py
File metadata and controls
101 lines (86 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import wave
import pyaudio
from tqdm import tqdm
from input_handle import cfg_write
def choose_rtx_output():
print(" --- RTX INPUT ---")
pyaudio_rtx_instance = pyaudio.PyAudio()
info = pyaudio_rtx_instance.get_host_api_info_by_index(0)
numdevices = info.get("deviceCount")
for i in range(0, numdevices):
if (
pyaudio_rtx_instance.get_device_info_by_host_api_device_index(0, i).get(
"maxInputChannels"
)
) > 0:
device = pyaudio_rtx_instance.get_device_info_by_host_api_device_index(
0, i
).get("name")
# print("Input ID ", i, " - ", device)
if "NVIDIA RTX Voice" in device:
print("\nUsing " + device)
pyaudio_rtx_instance.terminate()
cfg_write("mic_input", str(i))
return i
# Re-enable to have valid check for the RTX Mic
# valid = input("Is this correct (y/n)? ")
# if valid.lower() == "y":
# pyaudio_rtx_instance.terminate()
# return i
# If RTX not found then
print("\n Could not find NVIDIA RTX Microphone.\n")
print("Audio Options: ")
for i in range(0, numdevices):
if (
pyaudio_rtx_instance.get_device_info_by_host_api_device_index(0, i).get(
"maxInputChannels"
)
) > 0:
device = pyaudio_rtx_instance.get_device_info_by_host_api_device_index(
0, i
).get("name")
print( str(i)+ ".", device)
print("\nIf RTX Microphone is not found here, please check it is not disabled and is installed correctly.")
microphone_choice = input("\nPlease select the RTX Microphone input to record: ")
cfg_write("mic_input", str(microphone_choice))
return int(microphone_choice)
def record(length=10, user_output_name="file", mic_input='',
sample_rate=48000, channels=2):
FORMAT = pyaudio.paInt16
CHANNELS = channels
RATE = sample_rate
CHUNK = 1024
RECORD_SECONDS = length
if ".wav" not in user_output_name:
WAVE_OUTPUT_FILENAME = user_output_name + ".wav"
else:
WAVE_OUTPUT_FILENAME = user_output_name
pyaudio_inst = pyaudio.PyAudio()
# start Recording
stream = pyaudio_inst.open(
format=FORMAT,
channels=CHANNELS,
input_device_index=mic_input,
rate=sample_rate,
input=True,
frames_per_buffer=CHUNK,
)
print("Recording to RAM...")
frames = []
for i in tqdm(range(0, int(RATE / CHUNK * RECORD_SECONDS))):
data = stream.read(CHUNK)
frames.append(data)
print("\nWriting to .wav...")
# Stop recording
stream.stop_stream()
stream.close()
pyaudio_inst.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, "wb")
waveFile.setnchannels(channels)
waveFile.setsampwidth(pyaudio_inst.get_sample_size(FORMAT))
waveFile.setframerate(sample_rate)
waveFile.writeframes(b"".join(frames))
waveFile.close()
print("Complete!\n")
print("Written to " + WAVE_OUTPUT_FILENAME)
return None