Metadata-Version: 2.5
Name: frequency-detector
Version: 0.1.0
Summary: Estimate fundamental frequency in Hz from audio buffers or files.
Project-URL: Homepage, https://github.com/sherotree/frequency-detector-py
Project-URL: Repository, https://github.com/sherotree/frequency-detector-py
Project-URL: Issues, https://github.com/sherotree/frequency-detector-py/issues
Project-URL: Documentation, https://github.com/sherotree/frequency-detector-py#readme
Author-email: Sherotree <sherotree@users.noreply.github.com>
License-Expression: MIT
License-File: LICENSE
Keywords: audio,frequency,fundamental-frequency,hz,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

# frequency-detector

[Frequency Detector](https://www.uwarp.design/frequency-detector)

Headless Python library that estimates fundamental frequency in Hz from audio buffers or files. Reports raw Hz plus **min / average / max** over the analyzed audio. No microphone access, waveform UI, or network I/O.

## Install

```bash
pip install frequency-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.

## Quick start

```python
from frequency_detector import analyze_file

result = analyze_file("take.wav")

print(result.frequency)           # primary Hz estimate (average of voiced frames)
print(result.min_frequency)       # Hz
print(result.average_frequency)   # Hz
print(result.max_frequency)       # Hz

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

Analyze a NumPy buffer directly:

```python
import numpy as np
from frequency_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 abs(result.average_frequency - 440.0) < 3.0
```

## CLI

```bash
frequency-detector take.wav
frequency-detector take.mp3 --json --contour
frequency-detector long.wav --no-max-duration
frequency-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. 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.

## Public API

| Symbol | Role |
| --- | --- |
| `analyze_file(path, **opts) -> FrequencyResult` | Analyze a file on disk |
| `analyze_buffer(samples, sample_rate, **opts) -> FrequencyResult` | Analyze a PCM array |
| `analyze(source, **opts) -> FrequencyResult` | File path, bytes, stream, or NumPy array |
| `FrequencyResult` | Contour + Hz summary (`frequency`, `min_frequency`, `average_frequency`, `max_frequency`) |
| `FrequencyFrame` | `time`, `frequency` |

Optional note-name helpers live in `frequency_detector.notes` and are not part of the primary API.

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

## Development

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

## License

MIT © Sherotree
