| | |
| | | import pyaudio |
| | | import wave |
| | | from datetime import datetime |
| | | |
| | | # Generate a file name from current date and time |
| | | current_date_time = datetime.now().strftime("%Y-%m-%d--%H-%M-%S") |
| | | file_name = 'recording_' + current_date_time |
| | | |
| | | # Initialize PyAudio |
| | | p = pyaudio.PyAudio() |
| | |
| | | device_index = int(input("Please select the device ID to use for recording: ")) |
| | | n_channels = int(input("Please enter the number of channels to use for recording: ")) |
| | | duration_sec = int(input("Please enter the desired duration (in seconds) of the recording: ")) |
| | | #file_name = str(input("Please enter a name for the output file: ")) |
| | | |
| | | # Parameters |
| | | CHANNELS = n_channels # Number of channels |
| | |
| | | CHUNK = 32 # Number of frames per buffer |
| | | FORMAT = pyaudio.paInt16 # 16-bit audio |
| | | RECORD_SECONDS = duration_sec # Duration to record (in seconds) |
| | | OUTPUT_FILENAME = "output.wav" |
| | | MONITORING = True # To write to a text file as long as the device is recording |
| | | OUTPUT_FILENAME = file_name |
| | | MONITORING = False # To write to a text file as long as the device is recording |
| | | |
| | | # Get the chosen device's info to validate it can support the entered number of channels and 48kHz |
| | | device_info = p.get_device_info_by_index(device_index) |
| | |
| | | |
| | | for i in range(int(SAMPLE_RATE / CHUNK * RECORD_SECONDS)): |
| | | |
| | | print (i, f"RECORDING: Check status {stream.is_active()}") # This line is for testing |
| | | print (i, f"RECORDING: Check status {stream.is_active()}") # this line is for testing |
| | | |
| | | try: |
| | | # Record audio |
| | | # data = stream.read(CHUNK) |
| | | data = stream.read(CHUNK, exception_on_overflow = False) # This line (as an alternative to the line above) is to prevent overflows from terminating the recording |
| | | data = stream.read(CHUNK, exception_on_overflow = False) # this line is for testing |
| | | frames.append(data) |
| | | |
| | | if MONITORING: |
| | | if i%int(10*SAMPLE_RATE/CHUNK) == 0: # Writing current time into "current_recording.txt" every 10 seconds |
| | | if i%int(10*SAMPLE_RATE/CHUNK) == 0: # writing current time into "current_recording.txt" every 10 seconds |
| | | current_second = int(i*CHUNK/SAMPLE_RATE) |
| | | with open('current_recording.txt', 'a') as f: |
| | | f.write(f"{current_second}\n") |