Fixed bug

This commit is contained in:
2024-09-15 01:50:45 -04:00
parent 2fbca72f63
commit f369e09a0c

View File

@@ -41,7 +41,7 @@ def record_transmissions(device_id: int):
stream = audio.open(format=FORMAT, channels=CHANNELS, stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True, rate=RATE, input=True,
frames_per_buffer=CHUNK, input_device_index=device_id ) frames_per_buffer=CHUNK, input_device_index=device_id )
frames = [] frames = []
recording = False recording = False
@@ -63,16 +63,21 @@ def record_transmissions(device_id: int):
frames = [] # Reset frames to start fresh for this transmission frames = [] # Reset frames to start fresh for this transmission
elif recording and not nonsilent_chunks: elif recording and not nonsilent_chunks:
# Transmission has ended (silence detected for the required duration) # Check if there were valid nonsilent chunks before trimming
if len(frames) > 0: if len(nonsilent_chunks) > 0:
# Save recording without leading/trailing silence # Transmission has ended (silence detected for the required duration)
trimmed_audio = sound[nonsilent_chunks[0][0]:nonsilent_chunks[-1][1]] if len(frames) > 0:
filename = get_filename() # Save recording without leading/trailing silence
save_wave_file(filename, trimmed_audio) trimmed_audio = sound[nonsilent_chunks[0][0]:nonsilent_chunks[-1][1]]
filename = get_filename()
save_wave_file(filename, trimmed_audio)
recording = False recording = False
frames.clear() # Clear frames to prepare for the next transmission frames.clear() # Clear frames to prepare for the next transmission
print("Recording stopped, waiting for the next transmission...") print("Recording stopped, waiting for the next transmission...")
# Optionally: adjust silence threshold if needed
# E.g., try increasing silence_thresh to detect lower sounds
def parse_arguments(): def parse_arguments():
@@ -84,8 +89,8 @@ def parse_arguments():
if __name__ == "__main__": if __name__ == "__main__":
try: try:
args = parse_arguments() args = parse_arguments()
logger.info("Arguments: %s", args) print("Arguments: %s", args)
record_transmissions(device=args['device_id']) record_transmissions(args.device_id)
except KeyboardInterrupt: except KeyboardInterrupt:
print("Stopping...") print("Stopping...")
finally: finally: