Metadata-Version: 2.5
Name: twat-audio
Version: 2.7.12
Summary: Audio processing plugin for TWAT
Project-URL: Documentation, https://github.com/twardoch/twat-audio#readme
Project-URL: Issues, https://github.com/twardoch/twat-audio/issues
Project-URL: Source, https://github.com/twardoch/twat-audio
Author-email: Adam Twardoch <adam+github@twardoch.com>
License-Expression: MIT
License-File: LICENSE
Keywords: audio,processing,twat-plugin
Classifier: Development Status :: 4 - Beta
Classifier: Programming Language :: Python
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: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Requires-Python: >=3.10
Requires-Dist: fire>=0.6.0
Requires-Dist: pedalboard>=0.8.6
Requires-Dist: twat>=1.8.1
Provides-Extra: all
Requires-Dist: twat-mp[all]>=2.6.4; extra == 'all'
Requires-Dist: twat>=1.8.1; extra == 'all'
Provides-Extra: dev
Requires-Dist: absolufy-imports>=0.3.1; extra == 'dev'
Requires-Dist: mypy>=1.15.0; extra == 'dev'
Requires-Dist: pre-commit>=4.1.0; extra == 'dev'
Requires-Dist: pyproject-pyupgrade>=0.1.1; extra == 'dev'
Requires-Dist: pyupgrade-directories>=0.3.0; extra == 'dev'
Requires-Dist: pyupgrade>=3.19.1; extra == 'dev'
Requires-Dist: ruff>=0.9.7; extra == 'dev'
Provides-Extra: docs
Requires-Dist: mkdocs-material-extensions>=1.3.0; extra == 'docs'
Requires-Dist: mkdocs-material>=9.0.0; extra == 'docs'
Requires-Dist: mkdocs-minify-plugin>=0.7.0; extra == 'docs'
Requires-Dist: mkdocs>=1.5.0; extra == 'docs'
Requires-Dist: pymdown-extensions>=10.0; extra == 'docs'
Provides-Extra: parallel
Requires-Dist: twat-mp[all]>=2.6.4; extra == 'parallel'
Provides-Extra: test
Requires-Dist: coverage[toml]>=7.6.12; extra == 'test'
Requires-Dist: pytest-asyncio>=0.25.3; extra == 'test'
Requires-Dist: pytest-benchmark[histogram]>=5.1.0; extra == 'test'
Requires-Dist: pytest-cov>=6.0.0; extra == 'test'
Requires-Dist: pytest-xdist>=3.6.1; extra == 'test'
Requires-Dist: pytest>=8.3.4; extra == 'test'
Description-Content-Type: text/markdown

# twat-audio

**Audio processing utilities for the [twat](https://github.com/twardoch/twat) ecosystem.**

`twat-audio` provides deterministic, file-level audio helpers built on two backends:

- **[Pedalboard](https://github.com/spotify/pedalboard)** (Spotify) — fast, high-quality resampling via libsndfile/libresample.
- **[ffmpeg](https://ffmpeg.org/)** — normalization, trimming, audio extraction, muxing, and arbitrary filter application.

Speech-specific concerns (STT, TTS, dubbing orchestration, transcript handling) belong in `twat_speech`. This package stays focused on audio *files* and *signals*.

## Requirements

- Python 3.10+
- `ffmpeg` and `ffprobe` on `PATH` (for all operations except `resample_audio`)

## Installation

```bash
pip install twat-audio
```

Or with [uv](https://github.com/astral-sh/uv):

```bash
uv add twat-audio
```

## Python API

```python
from twat_audio import AudioProcessConfig, resample_audio, normalize_audio, trim_audio

# Resample to 16 kHz (common ML model input rate)
result = resample_audio(
    AudioProcessConfig("speech.wav", "speech_16k.wav", target_samplerate=16000.0)
)
print(result["status"])  # "success"

# Normalize loudness (EBU R128, target -16 LUFS)
normalize_audio("speech.wav", "speech_norm.wav", target_i=-16.0)

# Trim: start at 2 s, take 5 s
trim_audio("speech.wav", "clip.wav", start=2.0, duration=5.0)
```

### All Public Functions

| Function | Backend | Description |
|---|---|---|
| `resample_audio(config)` | Pedalboard | Resample to any sample rate |
| `normalize_audio(in, out, ...)` | ffmpeg | Loudness normalization (EBU R128) |
| `trim_audio(in, out, ...)` | ffmpeg | Trim by time range |
| `extract_audio(in, out, ...)` | ffmpeg | Extract audio from video |
| `replace_audio(video, audio, out)` | ffmpeg | Replace audio track in video |
| `simple_effect(in, out, filter)` | ffmpeg | Apply any ffmpeg `-af` filter |
| `audio_metadata(path)` | ffprobe | Read media metadata as dict |
| `generate_audio(prompt, ...)` | twat_genai | AI audio generation (optional) |

## CLI

```bash
# Show all subcommands
python -m twat_audio --help

# Resample
twat-audio resample input.wav output.wav --samplerate 16000

# Normalize (--dry-run prints the command without running it)
twat-audio normalize input.wav output.wav --dry-run

# Trim: 5-second clip starting at 2 s
twat-audio silence-trim input.wav clip.wav --start 2 --duration 5

# Extract audio from video
twat-audio transcode movie.mp4 voice.wav

# Replace audio track
twat-audio replace silent.mp4 voice.wav dubbed.mp4

# Apply ffmpeg filter
twat-audio effect input.wav loud.wav --audio-filter "volume=2.0"

# Read metadata
twat-audio info input.wav

# Print version
twat-audio version
```

## Supported Formats

Resampling (Pedalboard): **WAV, AIFF, FLAC, OGG, MP3**.

ffmpeg operations: all formats ffmpeg supports, including MP4/MKV/MOV/AVI/WebM containers for audio extraction.

## Role in the twat Ecosystem

`twat-audio` is a plugin for the [twat](https://github.com/twardoch/twat) host package. It registers under the `twat.plugins` entry point as `audio`. Other twat plugins can import it to gain audio file operations without depending on ffmpeg directly.

```python
# Via twat plugin discovery
import twat
twat.audio.normalize_audio("in.wav", "out.wav")
```

## Development

```bash
git clone https://github.com/twardoch/twat-audio
cd twat-audio
uv venv --python 3.12
uv pip install -e ".[dev,test]"
python -m pytest tests/ -v
```

Lint and format:

```bash
ruff check src/twat_audio tests
ruff format src/twat_audio tests
```

See [STYLE_GUIDE.md](STYLE_GUIDE.md) for coding conventions and [CONTRIBUTING](src_docs/development/contributing.md) for the full development workflow.

## License

MIT — see [LICENSE](LICENSE).
