# accompy.renderers.midi

MIDI event generation (stateless, iterator-based).

This module provides event-based MIDI generation that separates event creation
from file writing, enabling both batch file creation and future real-time streaming.

Key functions:

- generate_midi_events: Pure function that yields MIDI events
- events_to_midi_file: Side-effect function that writes events to a file

### Module Attributes

| [`NO_CHORD_SYMBOLS`](#accompy.renderers.midi.NO_CHORD_SYMBOLS)     | the renderer leaves their beats silent.                                                                                                                   |
|-----------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| [`FALLBACK_CHORD_NOTES`](#accompy.renderers.midi.FALLBACK_CHORD_NOTES) | a major triad on C3, moved to the symbol's root when it has a readable one (see [`fallback_chord_notes()`](#accompy.renderers.midi.fallback_chord_notes)). |

### Functions

| [`bar_chord_spans`](#accompy.renderers.midi.bar_chord_spans)(beats_per_bar, n_chords)           | `(offset, duration)` in beats of each of `n_chords` chords sharing one bar.         |
|-----------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------|
| [`bass_pitch`](#accompy.renderers.midi.bass_pitch)(root, chord_notes, pitch_offset)        | The bass note a pattern's `pitch_offset` plays over a chord.                        |
| [`chord_events`](#accompy.renderers.midi.chord_events)(chord_notes, \*[, style, ...])        | The drum, bass and piano events `style` plays over one chord.                       |
| [`comp_voicing`](#accompy.renderers.midi.comp_voicing)(chord_notes)                          | The notes the piano plays for a chord: its first four notes moved into C4-B4.       |
| [`events_to_midi_file`](#accompy.renderers.midi.events_to_midi_file)(events, path, tempo[, ...])    | Write MIDI events to a MIDI file.                                                   |
| [`fallback_chord_notes`](#accompy.renderers.midi.fallback_chord_notes)(symbol)                       | What the renderer plays for a symbol its resolver cannot read: a triad on its root. |
| [`generate_builtin_midi`](#accompy.renderers.midi.generate_builtin_midi)(score, config, \*, ...)      | Generate a MIDI file using the built-in renderer.                                   |
| [`generate_events`](#accompy.renderers.midi.generate_events)(chords, \*[, style, repeats, ...]) | The note events the builtin backend plays for `chords`, without writing a file.     |
| [`generate_midi_events`](#accompy.renderers.midi.generate_midi_events)(score, config, \*, ...)       | Generate MIDI events from score.                                                    |
| [`pitch_classes`](#accompy.renderers.midi.pitch_classes)(notes)                               | Return unique pitch classes (0-11) as a tuple.                                      |
| [`resolve_pitch_offset`](#accompy.renderers.midi.resolve_pitch_offset)(\*, root_pc, chord_pcs, ...)  | Resolve a pitch offset against a chord.                                             |
| [`select_patterns`](#accompy.renderers.midi.select_patterns)(patterns, beats_per_bar)           | The `(drum, bass, comp)` patterns the renderer plays from a style's pattern lists.  |
| [`style_time_signature`](#accompy.renderers.midi.style_time_signature)(pattern_source, style)        | The meter a style implies: its first drum pattern's beats per bar over 4, else 4/4. |

### accompy.renderers.midi.FALLBACK_CHORD_NOTES *= (48, 52, 55)*

a major triad on C3,
moved to the symbol’s root when it has a readable one (see [`fallback_chord_notes()`](#accompy.renderers.midi.fallback_chord_notes)).

* **Type:**
  The notes used when the chord resolver cannot read a symbol

### accompy.renderers.midi.NO_CHORD_SYMBOLS *= ('', 'n', 'N.C.', 'NC')*

the renderer leaves their beats silent.

* **Type:**
  Chord symbols that mean “no chord here”

### accompy.renderers.midi.bar_chord_spans(beats_per_bar, n_chords)

`(offset, duration)` in beats of each of `n_chords` chords sharing one bar.

The bar is always filled: with fewer chords than beats, the beats are split evenly and
the remainder goes to the first chords (3 chords in 4/4 get 2, 1 and 1 beats); with more
chords than beats, each gets `beats_per_bar / n_chords` beats.

```pycon
>>> bar_chord_spans(4, 2)
[(0, 2), (2, 2)]
>>> bar_chord_spans(4, 3)
[(0, 2), (2, 1), (3, 1)]
>>> bar_chord_spans(4, 5)[1]
(0.8, 0.8)
>>> bar_chord_spans(4, 0)
[]
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`float`](https://docs.python.org/3/builtins/functions.html#float), [`float`](https://docs.python.org/3/builtins/functions.html#float)]]

### accompy.renderers.midi.bass_pitch(root, chord_notes, pitch_offset)

The bass note a pattern’s `pitch_offset` plays over a chord.

The chord’s root is moved to C2-B2, then the offset is resolved against the chord
([`resolve_pitch_offset()`](#accompy.renderers.midi.resolve_pitch_offset)), so a pattern’s “3rd” follows the chord’s quality.

```pycon
>>> bass_pitch(57, [57, 60, 64], 4), bass_pitch(48, [48, 52, 55], 4)
(48, 40)
```

* **Return type:**
  [`int`](https://docs.python.org/3/builtins/functions.html#int)

### accompy.renderers.midi.chord_events(chord_notes, , style='swing', start_beat=0, duration=4, beats_per_bar=4, instruments=None, volumes=None, pattern_source=None)

The drum, bass and piano events `style` plays over one chord.

This is one step of [`generate_midi_events()`](#accompy.renderers.midi.generate_midi_events), which calls it for every chord of
a score: `chord_notes` are the chord’s MIDI notes, root first (what a chord
resolver returns), `start_beat` where the chord starts and `duration` how many
beats it lasts. It reads no tempo and keeps no state between chords.

```pycon
>>> [(e.time, e.channel, e.note) for e in chord_events([48, 52, 55], style="rock",
...     duration=1)]
[(0, 9, 36), (0, 9, 42), (0.5, 9, 42), (0, 0, 36), (0, 1, 60), (0, 1, 64),
 (0, 1, 67)]
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`MidiEvent`](accompy.base.html.md#accompy.base.MidiEvent)]

### accompy.renderers.midi.comp_voicing(chord_notes)

The notes the piano plays for a chord: its first four notes moved into C4-B4.

A chord with no notes is voiced as a C major triad.

```pycon
>>> comp_voicing([50, 53, 57, 60, 64]), comp_voicing([])
([62, 65, 69, 60], [60, 64, 67])
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`int`](https://docs.python.org/3/builtins/functions.html#int)]

### accompy.renderers.midi.events_to_midi_file(events, path, tempo, time_signature=(4, 4))

Write MIDI events to a MIDI file.

* **Parameters:**
  * **events** ([`Sequence`](https://docs.python.org/3/library/typing.html#typing.Sequence)[[`MidiEvent`](accompy.base.html.md#accompy.base.MidiEvent)]) – Sequence of MIDI events (must be subscriptable, not just iterable)
  * **path** ([`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path)) – Output file path
  * **tempo** ([`int`](https://docs.python.org/3/builtins/functions.html#int)) – Tempo in BPM
  * **time_signature** ([`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`int`](https://docs.python.org/3/builtins/functions.html#int), [`int`](https://docs.python.org/3/builtins/functions.html#int)]) – Time signature (numerator, denominator)
* **Return type:**
  [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path)
* **Returns:**
  Path to the created MIDI file

#### NOTE
Requires midiutil: pip install midiutil

### accompy.renderers.midi.fallback_chord_notes(symbol)

What the renderer plays for a symbol its resolver cannot read: a triad on its root.

The root is the leading note name, in either case and with `#`/`b` or `♯`/`♭`
(`C`, `F#`, `B♭`, `eb`); without one, the triad is on C. The triad is major
unless the symbol says minor (`-`, `m`, `min`, not `maj`): `E#m` plays F minor.

```pycon
>>> fallback_chord_notes("G#13(b9)"), fallback_chord_notes("?")
([56, 60, 63], [48, 52, 55])
>>> fallback_chord_notes("E#-7"), fallback_chord_notes("E#maj7")
([53, 56, 60], [53, 57, 60])
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`int`](https://docs.python.org/3/builtins/functions.html#int)]

### accompy.renderers.midi.generate_builtin_midi(score, config, , pattern_source, chord_resolver)

Generate a MIDI file using the built-in renderer.

This is a convenience function that combines event generation and file writing.

* **Parameters:**
  * **score** ([`Score`](accompy.base.html.md#accompy.base.Score)) – Musical score
  * **config** ([`AccompanimentConfig`](accompy.base.html.md#accompy.base.AccompanimentConfig)) – Configuration
  * **pattern_source** – Pattern provider
  * **chord_resolver** – Chord resolution function
* **Return type:**
  [`Path`](https://docs.python.org/3/library/pathlib.html#pathlib.Path)
* **Returns:**
  Path to the generated MIDI file

### accompy.renderers.midi.generate_events(chords, , style='swing', repeats=1, time_signature=None, instruments=None, volumes=None, pattern_source=None, chord_resolver=None)

The note events the builtin backend plays for `chords`, without writing a file.

`chords` is anything [`accompy.ensure_score()`](accompy.html.md#accompy.ensure_score) accepts (a chord string, an iReal
URL, measures, chord symbols, `(chord, beats)` pairs, a Score);
`time_signature` applies to the forms that do not carry one (default: the style’s,
see [`style_time_signature()`](#accompy.renderers.midi.style_time_signature): 3/4 for `waltz`, else 4/4). Times and durations
are in beats: the events do not depend on tempo. `repeats` defaults to 1, as in
[`accompy.generate_accompaniment()`](accompy.html.md#accompy.generate_accompaniment).

```pycon
>>> events = generate_events("| C | G |", style="bossa")
>>> events[0]
MidiEvent(time=0, channel=9, note=37, velocity=56, duration=0.25)
>>> max(e.time for e in events) < 8
True
```

* **Return type:**
  [`list`](https://docs.python.org/3/builtins/stdtypes.html#list)[[`MidiEvent`](accompy.base.html.md#accompy.base.MidiEvent)]

### accompy.renderers.midi.generate_midi_events(score, config, , pattern_source, chord_resolver)

Generate MIDI events from score.

Yields events in time order. Does NOT write files.
This enables real-time streaming or batch file creation.

* **Parameters:**
  * **score** ([`Score`](accompy.base.html.md#accompy.base.Score)) – Musical score with chord progression
  * **config** ([`AccompanimentConfig`](accompy.base.html.md#accompy.base.AccompanimentConfig)) – Accompaniment configuration
  * **pattern_source** – Source providing patterns (must have get_patterns method)
  * **chord_resolver** – Function to convert chord symbols to MIDI notes
* **Yields:**
  [`MidiEvent`](accompy.base.html.md#accompy.base.MidiEvent) – MidiEvent objects in chronological order
* **Return type:**
  [*Iterator*](https://docs.python.org/3/library/typing.html#typing.Iterator)[[*MidiEvent*](accompy.base.html.md#accompy.base.MidiEvent)]

### Example

```pycon
>>> from accompy.base import Score, AccompanimentConfig
>>> from accompy.patterns import get_patterns
>>> from accompy.chord_resolution import chord_to_notes
>>> score = Score.from_string("| C | Am | F | G |")
>>> config = AccompanimentConfig()
>>> events = generate_midi_events(score, config,
...     pattern_source=type('PS', (), {'get_patterns': get_patterns})(),
...     chord_resolver=chord_to_notes)
>>> first_event = next(events)
```

### accompy.renderers.midi.pitch_classes(notes)

Return unique pitch classes (0-11) as a tuple.

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`int`](https://docs.python.org/3/builtins/functions.html#int), [`...`](https://docs.python.org/3/builtins/constants.html#Ellipsis)]

### accompy.renderers.midi.resolve_pitch_offset(, root_pc, chord_pcs, target_offset)

Resolve a pitch offset against a chord.

The patterns use semitone offsets (0=root, 7=5th, etc.). When an offset
represents a chord tone which varies by quality (e.g. 3rd), we prefer a
matching chord tone from `chord_pcs`.

### Examples

For a minor chord, a pattern might request offset 4 (“3rd”); this
function will choose 3 if the chord contains a minor 3rd.

* **Return type:**
  [`int`](https://docs.python.org/3/builtins/functions.html#int)

### accompy.renderers.midi.select_patterns(patterns, beats_per_bar)

The `(drum, bass, comp)` patterns the renderer plays from a style’s pattern lists.

The drum pattern is the first one whose `beats_per_bar` matches (else the first);
bass and comping take the first of their list. A missing one is `None`.

```pycon
>>> from accompy.patterns import get_patterns
>>> [p.name for p in select_patterns(get_patterns("swing"), 4)]
['swing_basic', 'walking', 'swing_comp']
```

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)

### accompy.renderers.midi.style_time_signature(pattern_source, style)

The meter a style implies: its first drum pattern’s beats per bar over 4, else 4/4.

```pycon
>>> from accompy.patterns import get_pattern_registry
>>> style_time_signature(get_pattern_registry(), "waltz")
(3, 4)
>>> style_time_signature(get_pattern_registry(), "swing")
(4, 4)
```

* **Return type:**
  [`tuple`](https://docs.python.org/3/builtins/stdtypes.html#tuple)[[`int`](https://docs.python.org/3/builtins/functions.html#int), [`int`](https://docs.python.org/3/builtins/functions.html#int)]
