-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudio_pause_safe.py
More file actions
42 lines (32 loc) · 1 KB
/
Copy pathaudio_pause_safe.py
File metadata and controls
42 lines (32 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import sounddevice as sd
import numpy as np
from scipy.signal import find_peaks
DURATION = 10
FS = 44100
print("🎤 Speak naturally for", DURATION, "seconds...")
# BLOCKING RECORD (no callbacks → no crash)
audio = sd.rec(
int(DURATION * FS),
samplerate=FS,
channels=1,
blocking=True
)
signal = audio.flatten()
energy = np.abs(signal)
# Normalize safely
max_val = np.max(energy)
if max_val > 0:
energy = energy / max_val
# Detect speech peaks
peaks, _ = find_peaks(energy, height=0.05)
speech_events = len(peaks)
speech_rate = speech_events / DURATION
# Pause estimation
silence_threshold = 0.02
pause_ratio = np.mean(energy < silence_threshold)
print("\n--- Audio Behavior Metrics ---")
print("Speech events:", speech_events)
print("Speech rate (events/sec):", round(speech_rate, 2))
print("Silence ratio:", round(pause_ratio, 2))
audio_score = int(min(100, (pause_ratio * 100) + (speech_rate * 10)))
print("Audio Behavioral Load Score:", audio_score)