dlaiu commited on
Commit
176ce75
·
1 Parent(s): 26ba744

output as json with processing

Browse files
Files changed (1) hide show
  1. app.py +44 -1
app.py CHANGED
@@ -4,6 +4,45 @@ from parselmouth.praat import call
4
  import numpy as np
5
  import pandas as pd
6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  def get_pitch(audio_data):
8
  rate, data = audio_data
9
  if data.ndim > 1: # Check if the audio is stereo or multi-channel
@@ -21,7 +60,11 @@ def get_pitch(audio_data):
21
  df_pitch = pd.DataFrame(np.column_stack([pitch.xs(), pitch_values]),
22
  columns=['time', 'pitch'])
23
 
24
- return df_pitch
 
 
 
 
25
  except Exception as e:
26
  return "Error in pitch extraction: " + str(e)
27
 
 
4
  import numpy as np
5
  import pandas as pd
6
 
7
+ def find_rises_and_peaks_gradient(data, threshold=4):
8
+ data['rise_point'] = 0
9
+ data['peak_point'] = 0
10
+
11
+ pitch_values = data['pitch'].values
12
+ gradients = np.gradient(pitch_values)
13
+
14
+ in_rise = False
15
+ rise_start = 0
16
+ successive_rise_count = 0
17
+ min_successive_rise = 3 # Minimum successive values to qualify as a rise
18
+ checking_rise = False # Flag to start checking for rises after NaN
19
+
20
+ for i in range(1, len(gradients)):
21
+ if np.isnan(pitch_values[i]):
22
+ checking_rise = False # Reset flag when encountering NaN
23
+ in_rise = False
24
+ successive_rise_count = 0
25
+ continue
26
+
27
+ if not checking_rise:
28
+ checking_rise = True # Start checking for rises after NaN
29
+ continue
30
+
31
+ if gradients[i] >= threshold:
32
+ if not in_rise:
33
+ in_rise = True
34
+ rise_start = i-1
35
+ successive_rise_count += 1
36
+ else:
37
+ if in_rise:
38
+ if successive_rise_count >= min_successive_rise:
39
+ data.at[rise_start, 'rise_point'] = 1
40
+ data.at[i-1, 'peak_point'] = 1
41
+ in_rise = False
42
+ successive_rise_count = 0
43
+
44
+ return data
45
+
46
  def get_pitch(audio_data):
47
  rate, data = audio_data
48
  if data.ndim > 1: # Check if the audio is stereo or multi-channel
 
60
  df_pitch = pd.DataFrame(np.column_stack([pitch.xs(), pitch_values]),
61
  columns=['time', 'pitch'])
62
 
63
+ df_pitch = find_rises_and_peaks_gradient(df_pitch)
64
+
65
+ output = df_pitch.to_json(orient='records')
66
+
67
+ return output
68
  except Exception as e:
69
  return "Error in pitch extraction: " + str(e)
70