File size: 8,158 Bytes
aa8e154
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import numpy as np
import librosa
import sounddevice as sd
import queue
import threading
import time
import argparse
import sys
from collections import deque

SAMPLE_RATE = 22050
CHUNK_DURATION = 2
CHUNK_SAMPLES = SAMPLE_RATE * CHUNK_DURATION
SILENCE_THRESHOLD = 0.01
SCORE_WINDOW = 5

score_history = deque(maxlen=SCORE_WINDOW)
audio_queue = queue.Queue()


def extract_features(audio: np.ndarray, sr: float = SAMPLE_RATE) -> dict:
    if len(audio) < 512:
        return None

    features = {}

    # RMS energy - volume/projection
    rms = librosa.feature.rms(y=audio)[0]
    features["rms_mean"] = float(np.mean(rms))
    features["rms_std"] = float(np.std(rms))

    # Zero crossing rate - voice steadiness
    zcr = librosa.feature.zero_crossing_rate(audio)[0]
    features["zcr_mean"] = float(np.mean(zcr))

    # Pitch (F0) - monotone vs varied pitch
    pitches, magnitudes = librosa.piptrack(y=audio, sr=sr)
    pitch_values = pitches[magnitudes > np.median(magnitudes)]
    if len(pitch_values) > 0:
        features["pitch_mean"] = float(np.mean(pitch_values))
        features["pitch_std"] = float(np.std(pitch_values))
    else:
        features["pitch_mean"] = 0.0
        features["pitch_std"] = 0.0

    # Speech rate proxy - number of energy bursts
    onset_frames = librosa.onset.onset_detect(y=audio, sr=sr)
    features["speech_rate"] = len(onset_frames) / CHUNK_DURATION

    # Pause detection - ratio of silent frames
    silent_frames = np.sum(rms < SILENCE_THRESHOLD)
    features["pause_ratio"] = float(silent_frames / len(rms))

    return features


def compute_audio_score(features: dict) -> dict:
    if features is None:
        return {"score": 0, "tips": ["No audio detected"], "breakdown": {}}

    score = 100
    tips = []
    breakdown = {}

    # 1. Volume/Energy (25 pts)
    rms = features["rms_mean"]
    if rms < 0.02:
        vol_score = 10
        tips.append("Speak louder — your voice is too soft")
    elif rms < 0.05:
        vol_score = 18
        tips.append("Try projecting your voice more confidently")
    elif rms > 0.3:
        vol_score = 18
        tips.append("Slightly lower your volume for a calmer tone")
    else:
        vol_score = 25
    breakdown["volume"] = vol_score

    # 2. Pitch variation (25 pts) - monotone = low confidence
    pitch_std = features["pitch_std"]
    if pitch_std < 10:
        pitch_score = 10
        tips.append("Avoid monotone — vary your pitch to sound engaging")
    elif pitch_std < 30:
        pitch_score = 18
    else:
        pitch_score = 25
    breakdown["pitch_variation"] = pitch_score

    # 3. Speech rate (25 pts)
    rate = features["speech_rate"]
    if rate < 1.5:
        rate_score = 12
        tips.append("You're speaking too slowly — pick up the pace slightly")
    elif rate > 6:
        rate_score = 12
        tips.append("Slow down — speaking too fast signals nervousness")
    else:
        rate_score = 25
    breakdown["speech_rate"] = rate_score

    # 4. Pause ratio (25 pts)
    pause = features["pause_ratio"]
    if pause > 0.6:
        pause_score = 10
        tips.append("Too many pauses - try to maintain a steady flow")
    elif pause > 0.4:
        pause_score = 18
    else:
        pause_score = 25
    breakdown["pauses"] = pause_score

    score = sum(breakdown.values())
    score_history.append(score)
    smoothed = round(float(np.mean(score_history)), 1)

    if not tips:
        tips.append("Voice confidence is good - keep it up!")

    return {
        "score": smoothed,
        "raw_score": score,
        "breakdown": breakdown,
        "tips": tips,
        "features": {
            "rms": round(rms, 4),
            "pitch_std": round(features["pitch_std"], 2),
            "speech_rate": round(features["speech_rate"], 2),
            "pause_ratio": round(features["pause_ratio"], 2),
        },
    }


def get_label(score: float) -> str:
    if score >= 75:
        return "Confident"
    elif score >= 50:
        return "Moderate"
    else:
        return "Needs Improvement"


def analyze_file(path: str):
    print(f"\nLoading: {path}")
    try:
        audio, sr = librosa.load(path, sr=SAMPLE_RATE, mono=True)
    except Exception as e:
        print(f"Error loading file: {e}")
        sys.exit(1)

    total_chunks = len(audio) // CHUNK_SAMPLES
    if total_chunks == 0:
        print("Audio too short (need at least 2 seconds)")
        sys.exit(1)

    print(f"Duration: {len(audio)/sr:.1f}s | Chunks: {total_chunks}\n")
    all_scores = []

    for i in range(total_chunks):
        chunk = audio[i * CHUNK_SAMPLES : (i + 1) * CHUNK_SAMPLES]
        features = extract_features(chunk, sr)
        result = compute_audio_score(features)
        all_scores.append(result["score"])

        print(f"[Chunk {i+1}/{total_chunks}]")
        print(f"  Score     : {result['score']}{get_label(result['score'])}")
        print(f"  Volume    : {result['breakdown'].get('volume', 0)}/25")
        print(f"  Pitch Var : {result['breakdown'].get('pitch_variation', 0)}/25")
        print(f"  Rate      : {result['breakdown'].get('speech_rate', 0)}/25")
        print(f"  Pauses    : {result['breakdown'].get('pauses', 0)}/25")
        print(f"  Tip       : {result['tips'][0]}")
        print()

    final = round(float(np.mean(all_scores)), 1)
    print("=" * 45)
    print(f"FINAL AUDIO CONFIDENCE SCORE: {final}/100")
    print(f"Overall: {get_label(final)}")
    print("=" * 45)


def audio_callback(indata, frames, time_info, status):
    audio_queue.put(indata.copy())


def analyze_mic():
    print("\nMic mode started. Press Ctrl+C to stop.\n")
    buffer = np.array([], dtype=np.float32)

    with sd.InputStream(
        samplerate=SAMPLE_RATE,
        channels=1,
        dtype="float32",
        callback=audio_callback,
    ):
        try:
            while True:
                chunk_data = audio_queue.get()
                buffer = np.append(buffer, chunk_data.flatten())

                if len(buffer) >= CHUNK_SAMPLES:
                    chunk = buffer[:CHUNK_SAMPLES]
                    buffer = buffer[CHUNK_SAMPLES:]

                    features = extract_features(chunk)
                    result = compute_audio_score(features)

                    print(f"\rScore: {result['score']:5.1f} | {get_label(result['score']):<20} | Tip: {result['tips'][0][:50]}", end="", flush=True)

        except KeyboardInterrupt:
            print("\n\nSession ended.")
            if score_history:
                final = round(float(np.mean(score_history)), 1)
                print(f"Session Avg Score: {final}/100 — {get_label(final)}")


def get_latest_result() -> dict:
    """Called by fusion_scoring.py or Streamlit to get current audio score."""
    if not score_history:
        return {"score": 0, "tips": ["No audio data yet"], "breakdown": {}}
    return {"score": round(float(np.mean(score_history)), 1)}


def process_frame_audio(audio_chunk: np.ndarray) -> dict:
    """Called per-frame from main.py for real-time integration."""
    features = extract_features(audio_chunk)
    return compute_audio_score(features)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Audio Confidence Analyzer")
    parser.add_argument("--mic", action="store_true", help="Live mic analysis")
    parser.add_argument("--file", type=str, help="Path to audio/video file")
    args = parser.parse_args()

    if args.mic:
        analyze_mic()
    elif args.file:
        analyze_file(args.file)
    else:
        print("\nSelect mode:")
        print("1. Live Microphone")
        print("2. Audio File")
        choice = input("Enter choice (1/2): ").strip()

        if choice == "1":
            analyze_mic()
        elif choice == "2":
            path = input("Enter file path: ").strip()
            analyze_file(path)
        else:
            print("Invalid choice")
            sys.exit(1)