From b40c7292b755d0fc056ccb979488ef452a94a9ad Mon Sep 17 00:00:00 2001 From: Christophe Weis <christopheweis@hotmail.fr> Date: Mon, 20 Jan 2025 18:24:05 +0100 Subject: [PATCH] Add PythonExample_Recording_Startup.py --- PythonExample_Recording.py | 16 +++++++++++----- 1 files changed, 11 insertions(+), 5 deletions(-) diff --git a/PythonExample_Recording.py b/PythonExample_Recording.py index f780050..cb3669f 100644 --- a/PythonExample_Recording.py +++ b/PythonExample_Recording.py @@ -1,5 +1,10 @@ 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() @@ -15,6 +20,7 @@ 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 @@ -22,8 +28,8 @@ 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) @@ -52,16 +58,16 @@ 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") -- Gitblit v1.9.1