Skip to content

Commit

Permalink
Update audio.py (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anushka-Pote authored Nov 8, 2024
1 parent 02020cd commit 5a3f89d
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions Backend/audio.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sounddevice as sd
import numpy as np
import logging
import argparse

# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
Expand All @@ -12,14 +13,33 @@
# Sound variables
CALLBACKS_PER_SECOND = 38 # Callbacks per sec (system dependent)
SUS_FINDING_FREQUENCY = 2 # Calculates SUS *n* times every sec
SOUND_AMPLITUDE_THRESHOLD = 20 # Amplitude considered for SUS calc
SOUND_AMPLITUDE_THRESHOLD = 20 # Default amplitude threshold

# Packing *n* frames to calculate SUS
FRAMES_COUNT = int(CALLBACKS_PER_SECOND/SUS_FINDING_FREQUENCY)
AMPLITUDE_LIST = list([0] * FRAMES_COUNT)
FRAMES_COUNT = int(CALLBACKS_PER_SECOND / SUS_FINDING_FREQUENCY)
AMPLITUDE_LIST = [0] * FRAMES_COUNT
SUS_COUNT = 0
count = 0

# Configure command-line argument parsing
parser = argparse.ArgumentParser(description="Configure sound detection sensitivity.")
parser.add_argument('--threshold', type=int, help="Set the amplitude threshold for sound detection.")
args = parser.parse_args()

# Prompt user for threshold if not provided via command-line
if args.threshold is not None:
SOUND_AMPLITUDE_THRESHOLD = args.threshold
else:
try:
user_input = input("Enter amplitude threshold for sound detection (default is 20): ")
if user_input.strip():
SOUND_AMPLITUDE_THRESHOLD = int(user_input)
else:
logging.info("Using default amplitude threshold of 20.")
except ValueError:
logging.error("Invalid input. Using default amplitude threshold of 20.")
SOUND_AMPLITUDE_THRESHOLD = 20

def print_sound(indata, outdata, frames, time, status):
avg_amp = 0
global SOUND_AMPLITUDE, SUS_COUNT, count, SOUND_AMPLITUDE_THRESHOLD, AUDIO_CHEAT
Expand Down Expand Up @@ -47,7 +67,6 @@ def print_sound(indata, outdata, frames, time, status):
count = 0
except Exception as e:
logging.error(f"Error in print_sound: {e}")
# Optionally notify the user
print("An error occurred while processing audio input. Please check the logs.")

def sound():
Expand Down

0 comments on commit 5a3f89d

Please sign in to comment.