commit | author | age
|
b40c72
|
1 |
import pyaudio |
CW |
2 |
import wave |
|
3 |
from datetime import datetime |
|
4 |
|
|
5 |
# Generate a file name from current date and time |
|
6 |
current_date_time = datetime.now().strftime("%Y-%m-%d--%H-%M-%S") |
|
7 |
file_name = 'recording_' + current_date_time |
|
8 |
|
|
9 |
# Initialize PyAudio |
|
10 |
p = pyaudio.PyAudio() |
|
11 |
|
|
12 |
# No prompts needed for the startup version of this script |
|
13 |
''' |
|
14 |
# List available input devices |
|
15 |
print("Available audio input devices:") |
|
16 |
for i in range(p.get_device_count()): |
|
17 |
device_info = p.get_device_info_by_index(i) |
|
18 |
if device_info['maxInputChannels'] > 0: |
|
19 |
print(f"Device ID {i}: {device_info['name']} (Max channels: {device_info['maxInputChannels']})") |
|
20 |
|
|
21 |
# Prompt user to select a device |
|
22 |
device_index = int(input("Please select the device ID to use for recording: ")) |
|
23 |
n_channels = int(input("Please enter the number of channels to use for recording: ")) |
|
24 |
duration_sec = int(input("Please enter the desired duration (in seconds) of the recording: ")) |
|
25 |
#file_name = str(input("Please enter a name for the output file: ")) |
|
26 |
''' |
|
27 |
|
|
28 |
# Parameters |
|
29 |
CHANNELS = 19 # Number of channels |
|
30 |
SAMPLE_RATE = 48000 |
|
31 |
CHUNK = 32 # Number of frames per buffer |
|
32 |
FORMAT = pyaudio.paInt16 # 16-bit audio |
|
33 |
RECORD_SECONDS = 10 # Duration to record (in seconds) |
|
34 |
OUTPUT_FILENAME = "/home/rpi1/Desktop/" + file_name + ".wav" |
|
35 |
MONITORING = True # To write to a text file as long as the device is recording |
|
36 |
|
|
37 |
# Get the chosen device's info to validate it can support the entered number of channels and 48kHz |
|
38 |
device_info = p.get_device_info_by_index(2) |
|
39 |
max_channels = device_info['maxInputChannels'] |
|
40 |
if max_channels < CHANNELS: |
|
41 |
print(f"Error: The selected device only supports {max_channels} channels, but {CHANNELS} channels are required.") |
|
42 |
p.terminate() |
|
43 |
exit(1) |
|
44 |
|
|
45 |
# Open stream |
|
46 |
stream = p.open(format=FORMAT, |
|
47 |
channels=CHANNELS, |
|
48 |
rate=SAMPLE_RATE, |
|
49 |
input=True, |
|
50 |
input_device_index=2, |
|
51 |
frames_per_buffer=CHUNK) |
|
52 |
|
|
53 |
print(f"Recording {RECORD_SECONDS} seconds of audio from device ID {2}...") |
|
54 |
|
|
55 |
frames = [] |
|
56 |
|
|
57 |
print (f"Check status {stream.is_active()}") |
|
58 |
|
|
59 |
if MONITORING: |
|
60 |
open("/home/rpi1/Desktop/current_recording_" + current_date_time + ".txt","w+") |
|
61 |
|
|
62 |
for i in range(int(SAMPLE_RATE / CHUNK * RECORD_SECONDS)): |
|
63 |
|
|
64 |
# print (i, f"RECORDING: Check status {stream.is_active()}") # this line is for testing |
|
65 |
|
|
66 |
try: |
|
67 |
# Record audio |
|
68 |
# data = stream.read(CHUNK) |
|
69 |
data = stream.read(CHUNK, exception_on_overflow = False) # this line is for testing |
|
70 |
frames.append(data) |
|
71 |
|
|
72 |
if MONITORING: |
|
73 |
if i%int(10*SAMPLE_RATE/CHUNK) == 0: # writing current time into "current_recording.txt" every 10 seconds |
|
74 |
current_second = int(i*CHUNK/SAMPLE_RATE) |
|
75 |
with open('current_recording_' + current_date_time + '.txt', 'a') as f: |
|
76 |
f.write(f"{current_second}\n") |
|
77 |
|
|
78 |
except: |
|
79 |
print ("Error") |
|
80 |
|
|
81 |
print("Finished recording.") |
|
82 |
|
|
83 |
|
|
84 |
print (f"Check status {stream.is_active()}") |
|
85 |
# Stop and close the stream |
|
86 |
stream.stop_stream() |
|
87 |
stream.close() |
|
88 |
|
|
89 |
# Terminate PyAudio |
|
90 |
p.terminate() |
|
91 |
|
|
92 |
# Save the recorded audio to a file |
|
93 |
wf = wave.open(OUTPUT_FILENAME, 'wb') |
|
94 |
wf.setnchannels(CHANNELS) |
|
95 |
wf.setsampwidth(p.get_sample_size(FORMAT)) |
|
96 |
wf.setframerate(SAMPLE_RATE) |
|
97 |
wf.writeframes(b''.join(frames)) |
|
98 |
wf.close() |
|
99 |
|
|
100 |
print(f"Audio saved to {OUTPUT_FILENAME}.") |
|
101 |
|