Metadata-Version: 2.5
Name: audio-file-pitch-detector
Version: 0.1.0
Summary: Detect monophonic pitch, notes, Hz, and cents from audio files (MP3, WAV, M4A, AAC).
Project-URL: Homepage, https://github.com/sherotree/audio-file-pitch-detector-py
Project-URL: Repository, https://github.com/sherotree/audio-file-pitch-detector-py
Project-URL: Issues, https://github.com/sherotree/audio-file-pitch-detector-py/issues
Project-URL: Documentation, https://github.com/sherotree/audio-file-pitch-detector-py#readme
Author-email: Sherotree <sherotree@users.noreply.github.com>
License-Expression: MIT
License-File: LICENSE
Keywords: audio,frequency,music,note,pitch,pitch-detection,yin
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Multimedia :: Sound/Audio :: Analysis
Classifier: Topic :: Scientific/Engineering
Classifier: Typing :: Typed
Requires-Python: >=3.10
Requires-Dist: numpy>=1.22
Provides-Extra: dev
Requires-Dist: pytest>=7.0; extra == 'dev'
Description-Content-Type: text/markdown

# audio-file-pitch-detector

[Audio File Pitch Detector](https://www.uwarp.design/audio-file-pitch-detector) as a portable Python library.

Detect monophonic pitch over time from audio files or PCM buffers. For each voiced frame the library reports the nearest 12-tone equal-temperament note name (A4 = 440 Hz), exact frequency in Hz, and cents deviation. It also returns a pitch contour plus summary stats: most common note, average frequency, detected range, and unique note count.

This package **diagnoses** pitch. It does not correct, retune, or rewrite audio. There is no web UI, microphone capture, or network I/O.

## Install

```bash
pip install audio-file-pitch-detector
```

From a local checkout:

```bash
pip install -e ".[dev]"
```

**Formats:** WAV is decoded with the Python standard library. **MP3, M4A, and AAC require [`ffmpeg`](https://ffmpeg.org/) on your `PATH`.** There is no hard file-size limit (unlike typical browser uploads).

## Quick start

```python
from audio_file_pitch_detector import analyze_file

result = analyze_file("take.wav")

print(result.most_common_note)      # e.g. "A4"
print(result.average_frequency)     # Hz
print(result.frequency_range)       # (min_hz, max_hz)
print(result.unique_note_count)

for frame in result.contour:
    if frame.voiced:
        print(f"{frame.time:.3f}s  {frame.note}  {frame.frequency:.2f} Hz  {frame.cents:+.1f} c")
```

Analyze a NumPy buffer directly:

```python
import numpy as np
from audio_file_pitch_detector import analyze

sr = 22050
t = np.arange(sr) / sr
tone = 0.4 * np.sin(2 * np.pi * 440.0 * t)
result = analyze(tone, sample_rate=sr)
assert result.most_common_note == "A4"
```

## CLI

```bash
audio-file-pitch-detector take.wav
audio-file-pitch-detector take.mp3 --json --contour
audio-file-pitch-detector long.wav --no-max-duration
audio-file-pitch-detector take.wav --frame-size 2048 --hop-size 512
```

## Analysis window and long files

By default the library analyzes the **first 120 seconds** of a file (the same practical cap used by the browser tool for responsiveness). Override it:

| API | Behavior |
| --- | --- |
| `max_duration=120` (default) | Cap at two minutes |
| `max_duration=None` | No duration cap |
| CLI `--no-max-duration` or `--max-duration 0` | No duration cap |

The analysis **frame size** and **hop size** are also configurable (`frame_size`, `hop_size`).

## Tips for better results

- Prefer **monophonic**, relatively dry recordings (one voice or one instrument). Full mixes confuse time-domain detectors.
- WAV / higher bitrates usually read cleaner than heavily compressed audio.
- Quiet rooms and less reverb yield more voiced frames.
- This is pitch **detection**, not pitch correction.

## Public API

| Symbol | Role |
| --- | --- |
| `analyze_file(path, **opts) -> PitchResult` | Analyze a file on disk |
| `analyze_buffer(samples, sample_rate, **opts) -> PitchResult` | Analyze a PCM array |
| `analyze(source, **opts) -> PitchResult` | File path, bytes, stream, or NumPy array |
| `PitchResult` | Contour + summary stats |
| `PitchFrame` | `time`, `frequency`, `note`, `cents` |

Algorithm: **YIN** (time-domain), after De Cheveigné & Kawahara (2002).

## Development

```bash
pip install -e ".[dev]"
pytest
```

## License

MIT © Sherotree
