authtransforms 0.1.0__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 AUTHENTA
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,438 @@
1
+ Metadata-Version: 2.4
2
+ Name: authtransforms
3
+ Version: 0.1.0
4
+ Summary: Audio augmentation pipeline for PyTorch — a torchvision-style Compose API for audio tensors.
5
+ License: MIT
6
+ License-File: LICENSE
7
+ Keywords: audio,augmentation,pytorch,torchaudio,deep-learning,speech
8
+ Requires-Python: >=3.11,<3.14
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: License :: OSI Approved :: MIT License
11
+ Classifier: Programming Language :: Python :: 3
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
16
+ Requires-Dist: ipython (>=8.0)
17
+ Requires-Dist: jupyterlab (>=4.5.5,<5.0.0)
18
+ Requires-Dist: librosa (>=0.10)
19
+ Requires-Dist: matplotlib (>=3.6)
20
+ Requires-Dist: numpy (>=1.24)
21
+ Requires-Dist: pandas (>=3.0.2,<4.0.0)
22
+ Requires-Dist: scipy (>=1.10)
23
+ Requires-Dist: soundfile (>=0.12)
24
+ Requires-Dist: torch (==2.5.0)
25
+ Requires-Dist: torchaudio (==2.5.0)
26
+ Description-Content-Type: text/markdown
27
+
28
+ # authtransforms
29
+
30
+ Audio augmentation pipeline for PyTorch — a `torchvision.transforms`-style API for audio tensors.
31
+
32
+ Inspired by [Simple Audio Augmentation with PyTorch](https://jonathanbgn.com/2021/08/30/audio-augmentation.html).
33
+
34
+ ---
35
+
36
+ ## Getting Started
37
+
38
+ ### What is Poetry?
39
+
40
+ Poetry is a tool that manages your Python dependencies and virtual environment for you — think of it like a smarter `pip` that also keeps track of exactly which package versions you have installed, and creates an isolated environment so nothing clashes with the rest of your system.
41
+
42
+ If you don't have it yet:
43
+
44
+ ```bash
45
+ curl -sSL https://install.python-poetry.org | python3 -
46
+ ```
47
+
48
+ Then restart your terminal and check it works:
49
+
50
+ ```bash
51
+ poetry --version
52
+ ```
53
+
54
+ ---
55
+
56
+ ### Step 1 — Clone the repo
57
+
58
+ ```bash
59
+ git clone https://github.com/phospheneai/audio-transforms.git
60
+ cd authtransforms
61
+ ```
62
+ ### step 2.a - install poetry shell
63
+ ```bash
64
+ poetry self add poetry-plugin-shell
65
+
66
+ poetry shell
67
+ ```
68
+
69
+ ### Step 2 — Install dependencies
70
+
71
+ This command reads `pyproject.toml` and installs everything into an isolated virtual environment automatically. You don't need to create one yourself.
72
+
73
+ ```bash
74
+ poetry install
75
+ ```
76
+
77
+ Want JupyterLab too? Add the `notebook` group:
78
+
79
+ ```bash
80
+ poetry install --with notebook
81
+ ```
82
+
83
+ ### Step 3 — Activate the environment
84
+
85
+ ```bash
86
+ poetry shell
87
+ ```
88
+
89
+ You're now inside the virtual environment. Your terminal prompt will change. From here you can run Python normally — all the installed packages are available.
90
+
91
+ To leave the environment later, just type `exit`.
92
+
93
+ ### Step 4 — Open JupyterLab
94
+
95
+ ```bash
96
+ jupyter lab
97
+ ```
98
+
99
+ The `notebooks/demo.ipynb` notebook is the best place to start. Open it and select the **Python (authtransforms)** kernel from the kernel picker in the top-right corner.
100
+
101
+ > If the kernel doesn't appear, run this once and then refresh JupyterLab:
102
+ > ```bash
103
+ > python -m ipykernel install --user --name authtransforms --display-name "Python (authtransforms)"
104
+ > ```
105
+
106
+ ---
107
+
108
+ ### Project structure
109
+
110
+ ```
111
+ authtransforms/
112
+ ├── src/
113
+ │ └── authtransforms/
114
+ │ ├── __init__.py — public API (imports everything)
115
+ │ ├── transforms.py — all the audio transforms
116
+ │ ├── pipeline.py — Compose, OneOf, SomeOf, …
117
+ │ └── utils.py — plotting and playback helpers
118
+ ├── notebooks/
119
+ │ └── demo.ipynb — interactive demo (start here)
120
+ ├── demo.py — same demo as a plain Python script
121
+ ├── pyproject.toml — project config and dependency list
122
+ ├── .gitignore
123
+ └── README.md
124
+ ```
125
+
126
+ All the source code lives in `src/authtransforms/`. If you want to add a new transform, add it to `transforms.py` and export it from `__init__.py`.
127
+
128
+ ---
129
+
130
+ ### Troubleshooting
131
+
132
+ #### `libsox.dylib` not found (macOS)
133
+
134
+ Some torchaudio transforms use SoX under the hood. If you see an error about `libsox.dylib`, install it via Homebrew and symlink it into the base miniconda lib directory (that's where torchaudio's rpath looks, regardless of which virtualenv is active):
135
+
136
+ ```bash
137
+ brew install sox
138
+ ln -sf /opt/homebrew/lib/libsox.dylib ~/miniconda3/lib/libsox.dylib
139
+ ```
140
+
141
+ This is a one-time fix per machine.
142
+
143
+ #### `ModuleNotFoundError: No module named 'authtransforms'`
144
+
145
+ The package wasn't installed into the environment yet. Run:
146
+
147
+ ```bash
148
+ poetry install
149
+ ```
150
+
151
+ #### Audio file not found from the notebook
152
+
153
+ The notebook runs from the `notebooks/` folder, so relative paths like `./audio.wav` look inside `notebooks/`. Use `Path.cwd().parent` to get the project root:
154
+
155
+ ```python
156
+ from pathlib import Path
157
+ PROJECT_ROOT = Path.cwd().parent
158
+ audio_path = PROJECT_ROOT / "sample-audio-multilingual/english/real/my_file.wav"
159
+ ```
160
+
161
+ ---
162
+
163
+ ### With pip (no Poetry)
164
+
165
+ If you just want to use the package without Poetry:
166
+
167
+ ```bash
168
+ pip install torch torchaudio matplotlib numpy soundfile ipython jupyterlab
169
+ pip install -e . # install authtransforms itself in editable mode
170
+ ```
171
+
172
+ ---
173
+
174
+ ## Quick Start
175
+
176
+ ```python
177
+ import torchaudio
178
+ from authtransforms.pipeline import Compose, OneOf
179
+ from authtransforms.transforms import (
180
+ ToMono, RandomClip, RandomSpeedChange,
181
+ RandomPitchShift, AddGaussianNoise, Normalize, RandomApply,
182
+ )
183
+ from authtransforms.utils import compare_audio, compare_play
184
+ import matplotlib.pyplot as plt
185
+
186
+ audio, sr = torchaudio.load("speech.wav")
187
+
188
+ pipeline = Compose([
189
+ ToMono(),
190
+ RandomClip(sample_rate=sr, clip_length=sr * 5),
191
+ OneOf([
192
+ RandomSpeedChange(sample_rate=sr),
193
+ RandomPitchShift(sample_rate=sr, semitones=(-2, 2)),
194
+ ]),
195
+ RandomApply(AddGaussianNoise(), p=0.5),
196
+ Normalize(target_db=-3.0),
197
+ ])
198
+
199
+ augmented = pipeline(audio)
200
+
201
+ compare_audio(audio, augmented, sr)
202
+ plt.show()
203
+
204
+ compare_play(audio, augmented, sr)
205
+ ```
206
+
207
+ ---
208
+
209
+ ## Transforms
210
+
211
+ All transforms are callable objects that accept a `(channels, samples)` `torch.Tensor` and return a transformed tensor of the same shape (unless noted).
212
+
213
+ ### `RandomClip(sample_rate, clip_length, vad=True, vad_trigger_level=7.0)`
214
+
215
+ Extract a random fixed-length segment from the audio. Optionally applies torchaudio's Voice Activity Detector to trim leading/trailing silence after clipping.
216
+
217
+ ```python
218
+ clip = RandomClip(sample_rate=16000, clip_length=16000 * 4) # 4-second clip
219
+ ```
220
+
221
+ ### `RandomSpeedChange(sample_rate, speed_factors=(0.9, 1.0, 1.1))`
222
+
223
+ Randomly perturb playback speed via SoX, then resample back to the original rate. Using 0.9× and 1.1× alongside the original has been shown to significantly improve speech recognition models.
224
+
225
+ ```python
226
+ speed = RandomSpeedChange(sample_rate=16000)
227
+ ```
228
+
229
+ ### `RandomBackgroundNoise(sample_rate, noise_dir, min_snr_db=0, max_snr_db=15)`
230
+
231
+ Mix in a randomly chosen `.wav` file from `noise_dir` (searched recursively) at a random signal-to-noise ratio. Loops or crops the noise to match the audio length.
232
+
233
+ ```python
234
+ noise = RandomBackgroundNoise(sample_rate=16000, noise_dir="./musan/noise")
235
+ ```
236
+
237
+ > Recommended noise source: [MUSAN](http://www.openslr.org/17/) — an 11 GB collection of music, speech, and noise recordings.
238
+
239
+ ### `RandomPitchShift(sample_rate, semitones=(-2, 2))`
240
+
241
+ Shift pitch by a random number of semitones within the given range, via SoX.
242
+
243
+ ```python
244
+ pitch = RandomPitchShift(sample_rate=16000, semitones=(-3, 3))
245
+ ```
246
+
247
+ ### `RandomGain(min_gain_db=-6, max_gain_db=6)`
248
+
249
+ Scale amplitude by a random gain factor in dB.
250
+
251
+ ```python
252
+ gain = RandomGain(min_gain_db=-6, max_gain_db=6)
253
+ ```
254
+
255
+ ### `AddGaussianNoise(min_amplitude=0.001, max_amplitude=0.015)`
256
+
257
+ Add Gaussian white noise at a random standard deviation.
258
+
259
+ ```python
260
+ noise = AddGaussianNoise(min_amplitude=0.002, max_amplitude=0.01)
261
+ ```
262
+
263
+ ### `TimeShift(max_shift=0.2, roll=False)`
264
+
265
+ Shift the waveform in time by up to `max_shift` fraction of the total length. Wraps circularly if `roll=True`, otherwise zero-pads.
266
+
267
+ ```python
268
+ shift = TimeShift(max_shift=0.15)
269
+ ```
270
+
271
+ ### `SpecAugment(sample_rate, n_fft, n_mels, freq_mask_param, time_mask_param, n_freq_masks, n_time_masks)`
272
+
273
+ Apply SpecAugment — frequency and time masking directly on a mel-spectrogram. Returns a spectrogram tensor rather than a waveform; use as the final step when your model consumes spectrograms.
274
+
275
+ ```python
276
+ spec_aug = SpecAugment(sample_rate=16000, freq_mask_param=27, time_mask_param=100)
277
+ ```
278
+
279
+ ### `RandomApply(transform, p=0.5)`
280
+
281
+ Apply any transform with probability `p`. Mirrors `torchvision.transforms.RandomApply`.
282
+
283
+ ```python
284
+ maybe_noise = RandomApply(AddGaussianNoise(), p=0.3)
285
+ ```
286
+
287
+ ### `Normalize(target_db=-3.0)`
288
+
289
+ Peak-normalize the audio to a target dBFS level.
290
+
291
+ ```python
292
+ norm = Normalize(target_db=-3.0)
293
+ ```
294
+
295
+ ### `ToMono()`
296
+
297
+ Average all channels to a single mono channel.
298
+
299
+ ---
300
+
301
+ ## Pipeline Combinators
302
+
303
+ ### `Compose(transforms)`
304
+
305
+ Apply a list of transforms sequentially — the core building block, identical in spirit to `torchvision.transforms.Compose`.
306
+
307
+ ```python
308
+ pipeline = Compose([
309
+ ToMono(),
310
+ RandomClip(sr, sr * 5),
311
+ RandomSpeedChange(sr),
312
+ Normalize(),
313
+ ])
314
+ augmented = pipeline(audio)
315
+ ```
316
+
317
+ ### `OneOf(transforms, weights=None)`
318
+
319
+ Pick exactly one transform at random each call. Optionally provide `weights` for non-uniform sampling.
320
+
321
+ ```python
322
+ augment = OneOf([
323
+ RandomSpeedChange(sr),
324
+ RandomPitchShift(sr),
325
+ TimeShift(),
326
+ ])
327
+ ```
328
+
329
+ ### `SomeOf(transforms, n=2, shuffle=True)`
330
+
331
+ Pick `n` transforms at random (without replacement) and apply them.
332
+
333
+ ```python
334
+ augment = SomeOf([
335
+ RandomGain(),
336
+ AddGaussianNoise(),
337
+ TimeShift(),
338
+ RandomPitchShift(sr),
339
+ ], n=2)
340
+ ```
341
+
342
+ ### `RandomOrder(transforms)`
343
+
344
+ Apply all transforms but in a random order each call.
345
+
346
+ ```python
347
+ augment = RandomOrder([
348
+ RandomGain(),
349
+ AddGaussianNoise(),
350
+ TimeShift(),
351
+ ])
352
+ ```
353
+
354
+ ### `Lambda(func, name="Lambda")`
355
+
356
+ Wrap any callable as a named transform.
357
+
358
+ ```python
359
+ double = Lambda(lambda x: x * 2, name="Double")
360
+ ```
361
+
362
+ ---
363
+
364
+ ## Utilities
365
+
366
+ ### Plotting
367
+
368
+ All plotting functions return a `matplotlib.Figure` and work in notebooks and scripts alike.
369
+
370
+ ```python
371
+ from authtransforms.utils import plot_waveform, plot_spectrogram, plot_audio, compare_audio
372
+ import matplotlib.pyplot as plt
373
+
374
+ # Single panel
375
+ plot_waveform(audio, sr, title="Waveform")
376
+ plot_spectrogram(audio, sr, title="Mel-Spectrogram")
377
+
378
+ # Stacked waveform + spectrogram
379
+ plot_audio(audio, sr, title="Original")
380
+
381
+ # 2×2 side-by-side comparison
382
+ compare_audio(audio, augmented, sr, title_before="Original", title_after="Augmented")
383
+ plt.show()
384
+ ```
385
+
386
+
387
+ ### Playback
388
+
389
+ ```python
390
+ from authtransforms.utils import play_audio, compare_play
391
+
392
+ # Play a single clip
393
+ play_audio(audio, sr, label="Original")
394
+
395
+ # Play before and after back-to-back
396
+ compare_play(audio, augmented, sr)
397
+ ```
398
+
399
+ In a **Jupyter notebook** this renders an interactive HTML5 audio widget.
400
+ In a **plain script** it writes a temporary WAV and opens it with the OS default player (`afplay` on macOS, `aplay` on Linux, `winsound` on Windows).
401
+
402
+ ### Info
403
+
404
+ ```python
405
+ from authtransforms.utils import audio_info
406
+
407
+ audio_info(audio, sr, label="Original")
408
+ # [Original]
409
+ # Shape : (1, 128000)
410
+ # Channels : 1
411
+ # Sample rate: 16000 Hz
412
+ # Duration : 8.000 s (128000 samples)
413
+ # Peak : 0.7231
414
+ # RMS : 0.0842 (-21.5 dBFS)
415
+ # dtype : torch.float32
416
+ ```
417
+
418
+ ---
419
+
420
+ ## Demo
421
+
422
+ ```bash
423
+ check notebooks/demo.ipynb to get to know how to use
424
+ ```
425
+
426
+ The demo runs the full pipeline, prints audio info before and after, displays a `compare_audio` figure, and plays both clips.
427
+
428
+ ---
429
+
430
+ ## References
431
+
432
+ - [Simple Audio Augmentation with PyTorch — Jonathan Boigne](https://jonathanbgn.com/2021/08/30/audio-augmentation.html)
433
+ - [torchaudio documentation](https://pytorch.org/audio/)
434
+ - [SpecAugment: A Simple Data Augmentation Method for ASR](https://arxiv.org/abs/1904.08779)
435
+ - [MUSAN noise dataset](http://www.openslr.org/17/)
436
+ - [audiomentations](https://github.com/iver56/audiomentations)
437
+ - [torch-audiomentations](https://github.com/asteroid-team/torch-audiomentations)
438
+