Metadata-Version: 2.4
Name: musicbento-audio-to-midi
Version: 1.0.0
Summary: Audio to MIDI conversion for Python using Spotify Basic Pitch.
Author: MusicBento
License-Expression: MIT
Project-URL: Homepage, https://musicbento.com/audio-to-midi
Project-URL: Documentation, https://musicbento.com/audio-to-midi
Keywords: audio to midi,audio-to-midi,midi converter,mp3 to midi,music transcription,basic pitch,midi,musicbento
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: Topic :: Multimedia :: Sound/Audio :: MIDI
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Typing :: Typed
Requires-Python: <3.12,>=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: basic-pitch==0.4.0
Requires-Dist: librosa<0.12,>=0.8.0
Requires-Dist: setuptools<81,>=65
Provides-Extra: dev
Requires-Dist: build>=1.2.2; extra == "dev"
Requires-Dist: pytest>=8.3; extra == "dev"
Requires-Dist: twine>=5.1; extra == "dev"
Dynamic: license-file

# musicbento-audio-to-midi

Audio to MIDI conversion for Python using Spotify Basic Pitch.

Built by [MusicBento.com](https://musicbento.com/), with an interactive [Audio to MIDI Converter](https://musicbento.com/audio-to-midi).

## Install

```bash
pip install musicbento-audio-to-midi
```

Python 3.10 and 3.11 are supported. Spotify Basic Pitch 0.4.0 currently depends on runtime packages that do not support Python 3.12+ consistently, so this package declares the compatible range explicitly.

## Convert a file

```python
from musicbento_audio_to_midi import convert_audio_to_midi

result = convert_audio_to_midi(
    "input.wav",
    "output.mid",
)

print(result.note_count)
print(result.duration_seconds)
```

## Work with bytes

```python
from musicbento_audio_to_midi import convert_audio_to_midi

with open("input.mp3", "rb") as file:
    audio_bytes = file.read()

result = convert_audio_to_midi(
    audio_bytes,
    source_name="input.mp3",
)

with open(result.filename, "wb") as file:
    file.write(result.midi_bytes)
```

`str` paths, `pathlib.Path`, `bytes`, `bytearray`, `BytesIO`, and binary file-like objects are supported.

For raw bytes, pass `source_name` when you know the original file name. It is used for format validation, the temporary file suffix, and the generated MIDI filename.

## Reuse the loaded model

Loading the transcription model is relatively expensive. For multiple conversions, reuse one converter:

```python
from musicbento_audio_to_midi import AudioToMidiConverter

converter = AudioToMidiConverter()
converter.load()

first = converter.convert("first.wav")
second = converter.convert("second.wav")
```

The model is loaded once and reused by subsequent conversions.

## Settings

```python
from musicbento_audio_to_midi import (
    AudioToMidiConverter,
    AudioToMidiSettings,
)

settings = AudioToMidiSettings(
    onset_threshold=0.5,
    frame_threshold=0.3,
    minimum_note_length_ms=127.7,
    midi_tempo=120,
    minimum_frequency_hz=55,
    maximum_frequency_hz=1760,
)

converter = AudioToMidiConverter(settings)
result = converter.convert("input.wav")
```

Defaults include:

- maximum file size: 100 MB
- maximum duration: 8 minutes
- onset threshold: 0.5
- frame threshold: 0.3
- minimum note length: 127.7 ms
- MIDI tempo: 120 BPM

## Result

Every conversion returns an `AudioToMidiResult`:

```python
result.title
result.filename
result.note_count
result.duration_seconds
result.midi_bytes
result.mime_type
result.output_path
result.notes
```

Each item in `result.notes` contains start/end time, MIDI pitch, amplitude, and optional pitch bends.

## Supported audio

MusicBento recognizes these file extensions:

- MP3
- WAV
- M4A
- AAC
- OGG
- FLAC
- WebM

Actual decoding support depends on the audio libraries available in the Python environment. WAV is the most portable choice. Compressed formats can depend on libsndfile or FFmpeg support on the host system.

## Errors

Failures raise `AudioToMidiError` with a machine-readable `code`.

```python
from musicbento_audio_to_midi import AudioToMidiError, convert_audio_to_midi

try:
    convert_audio_to_midi("input.wav")
except AudioToMidiError as error:
    print(error.code)
```

Examples include `file-too-large`, `audio-too-long`, `decode-failed`, `model-load-failed`, `transcription-failed`, and `no-notes-detected`.

## Privacy

Audio transcription and MIDI generation run locally in the Python process. This package does not upload audio content to MusicBento.com.

For a ready-to-use browser tool, visit [MusicBento.com Audio to MIDI Converter](https://musicbento.com/audio-to-midi).

## Basic Pitch

This package uses [Spotify Basic Pitch](https://github.com/spotify/basic-pitch), distributed separately under the Apache License 2.0 through its own PyPI package.

## License

MIT
