modusa 0.4.31__tar.gz → 0.4.32__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: modusa
3
- Version: 0.4.31
3
+ Version: 0.4.32
4
4
  Summary: A modular signal analysis python library.
5
5
  Author-Email: Ankit Anand <ankit0.anand0@gmail.com>
6
6
  License: MIT
@@ -16,7 +16,7 @@ dependencies = [
16
16
  ]
17
17
  requires-python = ">=3.11"
18
18
  readme = "README.md"
19
- version = "0.4.31"
19
+ version = "0.4.32"
20
20
 
21
21
  [project.license]
22
22
  text = "MIT"
@@ -9,9 +9,9 @@ from modusa.tools import load_ann, save_ann
9
9
  from modusa.tools import dist_plot, hill_plot, plot, fig
10
10
 
11
11
  # Synthsizing related
12
- from modusa.tools import synth_f0
12
+ from modusa.tools import synth_f0, synth_clicks
13
13
 
14
14
  # Audio features related
15
15
  from modusa.tools import stft
16
16
 
17
- __version__ = "0.4.31" # This is dynamically used by the documentation, and pyproject.toml; Only need to change it here; rest gets taken care of.
17
+ __version__ = "0.4.32" # This is dynamically used by the documentation, and pyproject.toml; Only need to change it here; rest gets taken care of.
@@ -16,7 +16,7 @@ from .plotter import Fig as fig
16
16
  from .plotter import dist_plot, hill_plot, plot
17
17
 
18
18
  # Synthesizing related
19
- from .synth import synth_f0
19
+ from .synth import synth_f0, synth_clicks
20
20
 
21
21
  # Audio features
22
22
  from .audio_stft import stft
@@ -0,0 +1,112 @@
1
+ #!/usr/bin/env python3
2
+
3
+ #---------------------------------
4
+ # Author: Ankit Anand
5
+ # Date: 22/10/25
6
+ # Email: ankit0.anand0@gmail.com
7
+ #---------------------------------
8
+
9
+ import numpy as np
10
+
11
+ def synth_f0(f0, f0t, sr, nharm=0):
12
+ """
13
+ Synthesize f0 contour so that you can
14
+ hear it back.
15
+
16
+ Parameters
17
+ ----------
18
+ f0: ndarray
19
+ - Fundamental frequency (f0) contour in Hz.
20
+ f0t: ndarray
21
+ - Timestamps in seconds
22
+ sr: int
23
+ - Sampling rate in Hz for the synthesized audio.
24
+ nharm: int
25
+ - Number of harmonics
26
+ - Default: 0 => Only fundamental frequency (No harmonics)
27
+
28
+ Returns
29
+ -------
30
+ ndarray
31
+ - Syntesized audio.
32
+ sr
33
+ - Sampling rate of the synthesized audio
34
+ """
35
+
36
+ # Create new time axis
37
+ t = np.arange(0, f0t[-1], 1 / sr)
38
+
39
+ # Interpolate the f0 to match the sampling time points
40
+ f0_interp = np.interp(t, f0t, f0)
41
+
42
+ # Compute phase by integrating frequency over time
43
+ phase = 2 * np.pi * np.cumsum(f0_interp) / sr
44
+
45
+ # Start with fundamental
46
+ y = np.sin(phase)
47
+
48
+ # Add harmonics if requested
49
+ for n in range(2, nharm + 2): # from 2nd to (nharm+1)th harmonic
50
+ y += np.sin(n * phase) / n**2 # dividing by n to reduce harmonic amplitude
51
+
52
+ # Normalize output to avoid clipping
53
+ y /= np.max(np.abs(y))
54
+
55
+ return y, sr
56
+
57
+ def synth_clicks(event_times, sr, freq=500, nharm=4, click_duration_ms=5):
58
+ """
59
+ Generate a train of short sine wave clicks with harmonics at specified event times.
60
+
61
+ Parameters
62
+ ----------
63
+ event_times : array-like
64
+ - Times of events in seconds where clicks should be placed.
65
+ sr : int
66
+ - Sampling rate in Hz.
67
+ freq : float, optional
68
+ - Fundamental frequency of the sine click in Hz. Default is 500 Hz.
69
+ nharm : int | None
70
+ - Number of harmonics to include (including fundamental). Default is 4.
71
+ click_duration_ms : float | None
72
+ - Duration of each click in milliseconds. Default is 5 ms.
73
+
74
+ Returns
75
+ -------
76
+ np.ndarray
77
+ - Audio signal with sine wave clicks (with harmonics) at event times.
78
+ int
79
+ - Sampling rate of the generated click audio.
80
+ """
81
+
82
+ n_samples = int(np.ceil(sr * event_times[-1]))
83
+ y = np.zeros(n_samples, dtype=np.float32)
84
+
85
+ # Single click length
86
+ click_len = int(sr * click_duration_ms / 1000)
87
+ if click_len < 1:
88
+ click_len = 1
89
+
90
+ t = np.arange(click_len) / sr
91
+ window = np.hanning(click_len)
92
+
93
+ # Generate harmonic sine click
94
+ sine_click = np.zeros(click_len)
95
+ for n in range(1, nharm+2):
96
+ sine_click += (1 / n**2) * np.sin(2 * np.pi * freq * n * t)
97
+
98
+ # Apply window
99
+ sine_click = sine_click * window**2
100
+
101
+ for event_time in event_times:
102
+ start_sample = int(event_time * sr)
103
+ end_sample = start_sample + click_len
104
+ if end_sample > n_samples:
105
+ end_sample = n_samples
106
+ y[start_sample:end_sample] += sine_click[:end_sample - start_sample]
107
+
108
+ # Normalize to avoid clipping if clicks overlap
109
+ y /= np.max(np.abs(y))
110
+
111
+ return y, sr
112
+
@@ -1,55 +0,0 @@
1
- #!/usr/bin/env python3
2
-
3
- #---------------------------------
4
- # Author: Ankit Anand
5
- # Date: 22/10/25
6
- # Email: ankit0.anand0@gmail.com
7
- #---------------------------------
8
-
9
- import numpy as np
10
-
11
- def synth_f0(f0, f0t, sr, nharm=0):
12
- """
13
- Synthesize f0 contour so that you can
14
- hear it back.
15
-
16
- Parameters
17
- ----------
18
- f0: ndarray
19
- - Fundamental frequency (f0) contour in Hz.
20
- f0t: ndarray
21
- - Timestamps in seconds
22
- sr: int
23
- - Sampling rate in Hz for the synthesized audio.
24
- nharm: int
25
- - Number of harmonics
26
- - Default: 0 => Only fundamental frequency (No harmonics)
27
-
28
- Returns
29
- -------
30
- ndarray
31
- - Syntesized audio.
32
- sr
33
- - Sampling rate of the synthesized audio
34
- """
35
-
36
- # Create new time axis
37
- t = np.arange(0, f0t[-1], 1 / sr)
38
-
39
- # Interpolate the f0 to match the sampling time points
40
- f0_interp = np.interp(t, f0t, f0)
41
-
42
- # Compute phase by integrating frequency over time
43
- phase = 2 * np.pi * np.cumsum(f0_interp) / sr
44
-
45
- # Start with fundamental
46
- y = np.sin(phase)
47
-
48
- # Add harmonics if requested
49
- for n in range(2, nharm + 2): # from 2nd to (nharm+1)th harmonic
50
- y += np.sin(n * phase) / n**2 # dividing by n to reduce harmonic amplitude
51
-
52
- # Normalize output to avoid clipping
53
- y /= np.max(np.abs(y))
54
-
55
- return y, sr
File without changes
File without changes