File size: 12,046 Bytes
8ad2ab3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386

import scipy.signal
import numpy as np
import librosa
import pyworld as pw

# def compute_pitch_variation(file_path):
#     # Step 1: Load audio
#     y, sr = librosa.load(file_path, sr=None)
#     y = y.astype(np.float64)  # pyworld expects float64

#     # Step 2: Extract pitch (F0)
#     _f0, t = pw.dio(y, sr)              # Fast initial pitch estimation
#     f0 = pw.stonemask(y, _f0, t, sr)    # Refinement step

#     # Step 3: Filter voiced frames
#     voiced_f0 = f0[f0 > 0]

#     # Handle empty case
#     if voiced_f0.size == 0:
#         return {
#             "pitch_mean": 0.0,
#             "pitch_std": 0.0,
#             "pitch_range": 0.0,
#             "semitone_std": 0.0,
#             "pitch_variation_score": 0.0
#         }

#     # Step 4: Basic statistics
#     pitch_mean = np.mean(voiced_f0)
#     pitch_std = np.std(voiced_f0)
#     pitch_range = np.max(voiced_f0) - np.min(voiced_f0)

#     print(pitch_mean)
#     print(f'voiced_f0: {voiced_f0}')
#     # Step 5: Compute semitone-based variation (better for human perception)
#     median_f0 = np.median(voiced_f0)
#     if median_f0 <= 0:
#         median_f0 = 1e-6  # Avoid division by zero
        
#     semitone_diffs = 12 * np.log2(voiced_f0 / median_f0)
#     semitone_std = np.std(semitone_diffs)
#     print(semitone_std)

#     # Step 6: Scale semitone_std to a 0–100 score (tunable)
#     # For example: semitone_std of 0 β†’ 0 score, β‰₯6 semitones β†’ 100 score
#     pitch_variation_score = np.clip((semitone_std / 6.0) * 100, 0, 100)

#     return {
#         "pitch_mean": pitch_mean,
#         "pitch_std": pitch_std,
#         "pitch_range": pitch_range,
#         "semitone_std": semitone_std,
#         "pitch_variation_score": pitch_variation_score
#     }
# def compute_intonation_range(file_path):
#     # Step 1: Load and prepare audio
#     y, sr = librosa.load(file_path, sr=None)
#     y = y.astype(np.float64)

#     # Step 2: Extract F0
#     _f0, t = pw.dio(y, sr)
#     f0 = pw.stonemask(y, _f0, t, sr)
    
   

#     # Step 3: Filter voiced frames
#     voiced_f0 = f0[f0 > 0]
#     if voiced_f0.size == 0:
#         return 0.0
    
#     voiced_f0 = voiced_f0[(voiced_f0 > np.percentile(voiced_f0, 5)) & 
#                       (voiced_f0 < np.percentile(voiced_f0, 95))]

#     # Step 4: Compute intonation range (in semitones)
#     f0_min = np.min(voiced_f0)
#     f0_max = np.max(voiced_f0)
#     if f0_min <= 0:
#         f0_min = 1e-6  # to avoid log error
#     intonation_range = 12 * np.log2(f0_max / f0_min)
    
#     # range into scores:
    
#     max_range = 12.0
#     normalized = min(intonation_range, max_range) / max_range
#     score = normalized * 100
#     return round(score, 2), intonation_range



# def compute_pitch_variation(file_path):
#     # Step 1: Load audio
#     y, sr = librosa.load(file_path, sr=None)

#     # Step 2: Extract pitch using librosa.pyin (YIN-based)
#     f0, voiced_flags, voiced_probs = librosa.pyin(
#         y,
#         sr=sr,
#         fmin=80,
#         fmax=400,
#         frame_length=1105,
#         hop_length=256,
#         fill_na=np.nan
#     )

#     # Step 3: Filter voiced frames
#     voiced_f0 = f0[~np.isnan(f0)]
 
 
#     voiced_f0 = voiced_f0[
#         (voiced_f0 > np.percentile(voiced_f0, 5)) &
#         (voiced_f0 < np.percentile(voiced_f0, 95))
#     ]

#     # Handle empty case
#     if voiced_f0.size == 0:
#         return {
#             "pitch_mean": 0.0,
#             "pitch_std": 0.0,
#             "pitch_range": 0.0,
#             "semitone_std": 0.0,
#             "pitch_variation_score": 0.0
#         }

#     # Step 4: Basic statistics
#     pitch_mean = float(np.mean(voiced_f0))
#     pitch_std = float(np.std(voiced_f0))
#     pitch_range = float(np.max(voiced_f0) - np.min(voiced_f0))


#     # Step 5: Compute semitone-based variation
#     median_f0 = np.median(voiced_f0)
#     if median_f0 <= 0:
#         median_f0 = 1e-6

#     semitone_diffs = 12 * np.log2(voiced_f0 / median_f0)
#     semitone_std = float(np.std(semitone_diffs))
  

#     # Step 6: Scale to 0–100 score
#     pitch_variation_score = float(np.clip((semitone_std / 6.0) * 100, 0, 100))
#     return {
#         "pitch_mean": pitch_mean,
#         "pitch_std": pitch_std,
#         "pitch_range": pitch_range,
#         "semitone_std": semitone_std,
#         "pitch_variation_score": pitch_variation_score
#     }

# def compute_intonation_range(file_path):
#     # Step 1: Load and prepare audio
#     y, sr = librosa.load(file_path, sr=None)

#     # Step 2: Extract F0 using librosa.pyin
#     f0, voiced_flags, voiced_probs = librosa.pyin(
#         y,
#         sr=sr,
#         fmin=80,
#         fmax=400,
#         frame_length=1105,  # ensures two periods of fmin fit
#         hop_length=256,
#         fill_na=np.nan
#     )

#     # Step 3: Filter voiced frames
#     voiced_f0 = f0[~np.isnan(f0)]
#     if voiced_f0.size == 0:
#         return 0.0, 0.0

#     # Optional: remove outliers (5th to 95th percentile)
#     voiced_f0 = voiced_f0[
#         (voiced_f0 > np.percentile(voiced_f0, 5)) &
#         (voiced_f0 < np.percentile(voiced_f0, 95))
#     ]

#     # Step 4: Compute intonation range in semitones
#     f0_min = np.min(voiced_f0)
#     f0_max = np.max(voiced_f0)
#     if f0_min <= 0:
#         f0_min = 1e-6

#     intonation_range = 12 * np.log2(f0_max / f0_min)

#     # Step 5: Normalize and convert to score out of 100
#     max_range = 12.0  # ~1 octave
#     normalized = min(intonation_range, max_range) / max_range
#     score = normalized * 100

#     return round(score, 2), float(intonation_range)



# def compute_speech_rhythm_variability(file_path):
#     """
#     Computes the speech rhythm variability score from an audio file.
#     The method estimates tempo consistency across time using onset intervals.

#     Returns:
#         score (float): Normalized rhythm variability score out of 100.
#         raw_std (float): Raw standard deviation of inter-onset intervals.
#     """
#     # Step 1: Load audio
#     y, sr = librosa.load(file_path, sr=None)

#     # Step 2: Onset detection 
#     onset_env = librosa.onset.onset_strength(y=y, sr=sr)
#     onsets = librosa.onset.onset_detect(onset_envelope=onset_env, sr=sr, units='time')

#     if len(onsets) < 2:
#         return 0.0, 0.0  # Not enough onsets to compute rhythm

#     # Step 3: Compute inter-onset intervals (IOIs) as rhythm proxy
#     iois = np.diff(onsets)

#     # Optional: Remove outliers (5th–95th percentile)
#     ioi_clean = iois[(iois > np.percentile(iois, 5)) & (iois < np.percentile(iois, 95))]
#     if len(ioi_clean) < 2:
#         return 0.0, 0.0

#     # Step 4: Compute variability β€” standard deviation of IOIs
#     raw_std = np.std(ioi_clean)

#     # Step 5: Normalize raw_std to 0–100 score
#     # Lower std = more consistent rhythm β†’ higher score
#     min_std = 0.05   # near-perfect rhythm (tight pacing)
#     max_std = 0.6    # highly irregular rhythm

#     # Clamp and reverse-score
#     clamped_std = np.clip(raw_std, min_std, max_std)
#     normalized = 1 - (clamped_std - min_std) / (max_std - min_std)
#     score = normalized * 100

#     return round(score, 2), round(float(raw_std), 4)


# def calc_sds(file_path):
    
#     # sds = 0.35 * pitch_variation + 0.35 * intonation_range + 0.3 * speech_rhythm_variability
    
#     pitch_variation = compute_pitch_variation(file_path)
#     intonation_range = compute_intonation_range(file_path)
#     speech_rhythm_variability = compute_speech_rhythm_variability(file_path)
#     # print(f"Speech Rhythm Variability Score: {speech_rhythm_variability}")
#     # print(f"Speech Rhythm Variability Score: {speech_rhythm_variability}")
#     # print(f"Speech Rhythm Variability Score: {speech_rhythm_variability}")
    
#     sds = 0.35 * pitch_variation['pitch_variation_score'] + 0.35 * intonation_range[0] + 0.3 * speech_rhythm_variability[0]
#     return round(sds, 2)

# path = r'D:\Intern\shankh\audio_samples\anga.wav'

# result = calc_sds(path)
# print(f"SDS: {result}")

import numpy as np
import librosa
import pyworld

def compute_pitch_variation(file_path):
    # Step 1: Load audio
    y, sr = librosa.load(file_path, sr=None)

    # Step 2: Extract pitch using pyworld
    _f0, t = pyworld.harvest(y.astype(np.float64), sr, f0_floor=80.0, f0_ceil=400.0, frame_period=1000 * 256 / sr)
    f0 = pyworld.stonemask(y.astype(np.float64), _f0, t, sr)

    # Step 3: Filter voiced frames
    voiced_f0 = f0[f0 > 0]

    # Remove outliers (5th to 95th percentile)
    voiced_f0 = voiced_f0[
        (voiced_f0 > np.percentile(voiced_f0, 5)) &
        (voiced_f0 < np.percentile(voiced_f0, 95))
    ]

    if voiced_f0.size == 0:
        return {
            "pitch_mean": 0.0,
            "pitch_std": 0.0,
            "pitch_range": 0.0,
            "semitone_std": 0.0,
            "pitch_variation_score": 0.0
        }

    # Step 4: Basic statistics
    pitch_mean = float(np.mean(voiced_f0))
    pitch_std = float(np.std(voiced_f0))
    pitch_range = float(np.max(voiced_f0) - np.min(voiced_f0))

    # Step 5: Semitone-based variation
    median_f0 = np.median(voiced_f0)
    if median_f0 <= 0:
        median_f0 = 1e-6
    semitone_diffs = 12 * np.log2(voiced_f0 / median_f0)
    semitone_std = float(np.std(semitone_diffs))

    # Step 6: Scaled variation score
    pitch_variation_score = float(np.clip((semitone_std / 6.0) * 100, 0, 100))

    return {
        "pitch_mean": pitch_mean,
        "pitch_std": pitch_std,
        "pitch_range": pitch_range,
        "semitone_std": semitone_std,
        "pitch_variation_score": pitch_variation_score
    }


def compute_intonation_range(file_path):
    # Step 1: Load audio
    y, sr = librosa.load(file_path, sr=None)

    # Step 2: Extract pitch using pyworld
    _f0, t = pyworld.harvest(y.astype(np.float64), sr, f0_floor=80.0, f0_ceil=400.0, frame_period=1000 * 256 / sr)
    f0 = pyworld.stonemask(y.astype(np.float64), _f0, t, sr)

    # Step 3: Filter voiced frames
    voiced_f0 = f0[f0 > 0]
    if voiced_f0.size == 0:
        return 0.0, 0.0

    # Remove outliers
    voiced_f0 = voiced_f0[
        (voiced_f0 > np.percentile(voiced_f0, 5)) &
        (voiced_f0 < np.percentile(voiced_f0, 95))
    ]
    if voiced_f0.size == 0:
        return 0.0, 0.0

    # Step 4: Compute intonation range
    f0_min = np.min(voiced_f0)
    f0_max = np.max(voiced_f0)
    if f0_min <= 0:
        f0_min = 1e-6
    intonation_range = 12 * np.log2(f0_max / f0_min)

    # Step 5: Normalize
    max_range = 12.0
    normalized = min(intonation_range, max_range) / max_range
    score = normalized * 100

    return round(score, 2), float(intonation_range)


def compute_speech_rhythm_variability(file_path):
    """
    Computes the speech rhythm variability score from an audio file.
    The method estimates tempo consistency across time using onset intervals.
    """
    y, sr = librosa.load(file_path, sr=None)

    # Step 2: Onset detection
    onset_env = librosa.onset.onset_strength(y=y, sr=sr)
    onsets = librosa.onset.onset_detect(onset_envelope=onset_env, sr=sr, units='time')

    if len(onsets) < 2:
        return 0.0, 0.0

    iois = np.diff(onsets)

    ioi_clean = iois[(iois > np.percentile(iois, 5)) & (iois < np.percentile(iois, 95))]
    if len(ioi_clean) < 2:
        return 0.0, 0.0

    raw_std = np.std(ioi_clean)

    min_std = 0.05
    max_std = 0.6
    clamped_std = np.clip(raw_std, min_std, max_std)
    normalized = 1 - (clamped_std - min_std) / (max_std - min_std)
    score = normalized * 100

    return round(score, 2), round(float(raw_std), 4)


def calc_sds(file_path):
    pitch_variation = compute_pitch_variation(file_path)
    intonation_range = compute_intonation_range(file_path)
    speech_rhythm_variability = compute_speech_rhythm_variability(file_path)

    sds = 0.35 * pitch_variation['pitch_variation_score'] + \
          0.35 * intonation_range[0] + \
          0.3 * speech_rhythm_variability[0]
    
    return round(sds, 2)