accompy.protocols#

Protocols defining extensibility contracts for accompy.

These protocols (structural interfaces) enable dependency injection and allow users to provide custom implementations of core components.

Key protocols:

  • ChordResolver: Convert chord symbols to MIDI notes

  • PatternSource: Provide patterns for styles

  • MidiEventGenerator: Generate MIDI events from scores

  • AudioRenderer: Render MIDI to audio

See PEP 544 for more on Protocol types.

Classes

AudioRenderer(*args, **kwargs)

Renders MIDI events to audio.

ChordResolver(*args, **kwargs)

Convert chord symbols to MIDI note numbers.

MidiEventGenerator(*args, **kwargs)

Generates MIDI events from score and patterns.

PatternSource(*args, **kwargs)

Provides musical patterns for a given style.

SynthesizerBackend(*args, **kwargs)

Audio synthesis backend.

class accompy.protocols.AudioRenderer(*args, **kwargs)[source]#

Bases: Protocol

Renders MIDI events to audio.

This protocol abstracts audio synthesis, enabling:

  • Different synthesis backends (FluidSynth, Pyo, etc.)

  • Real-time vs. batch rendering

  • Alternative output formats

render(events, config)[source]#

Render MIDI events to PCM audio data.

Parameters:
Return type:

bytes

Returns:

Raw PCM audio bytes

render_to_file(events, config, output_path)[source]#

Render MIDI events directly to an audio file.

Parameters:
Return type:

Path

Returns:

Path to the created file

class accompy.protocols.ChordResolver(*args, **kwargs)[source]#

Bases: Protocol

Convert chord symbols to MIDI note numbers.

Example implementation:

>>> def my_resolver(symbol: str) -> list[int]:
...     # Return MIDI notes for chord
...     return [60, 64, 67]  # C major
class accompy.protocols.MidiEventGenerator(*args, **kwargs)[source]#

Bases: Protocol

Generates MIDI events from score and patterns.

This protocol enables alternative MIDI generation strategies (e.g., more sophisticated voicings, real-time adaptation).

generate(score, config, *, chord_resolver, pattern_source)[source]#

Generate MIDI events from a score.

Parameters:
  • score (Score) – Musical score with chord progression

  • config (AccompanimentConfig) – Accompaniment configuration

  • chord_resolver (ChordResolver) – Function to resolve chords to notes

  • pattern_source (PatternSource) – Source of musical patterns

Yields:

MidiEvent – MidiEvent objects in time order

Return type:

Iterator[MidiEvent]

class accompy.protocols.PatternSource(*args, **kwargs)[source]#

Bases: Protocol

Provides musical patterns for a given style.

Example

>>> class MyPatternSource:
...     def get_patterns(self, style: str) -> dict:
...         return {'drums': [...], 'bass': [...], 'comp': [...]}
...     def available_styles(self) -> list[str]:
...         return ['swing', 'bossa']
available_styles()[source]#

List all available style names.

Return type:

list[str]

Returns:

List of style names

get_patterns(style)[source]#

Get patterns for a style.

Parameters:

style (str) – Style name (e.g., “swing”, “bossa”, “rock”)

Return type:

dict[str, list]

Returns:

Dict with ‘drums’, ‘bass’, ‘comp’ keys containing pattern lists

class accompy.protocols.SynthesizerBackend(*args, **kwargs)[source]#

Bases: Protocol

Audio synthesis backend.

Simpler protocol than AudioRenderer, focused just on synthesis. Used by the synthesis module.

classmethod is_available()[source]#

Check if this backend’s dependencies are installed.

Return type:

bool

Returns:

True if the backend can be used

render_to_bytes(midi_path, *, sample_rate=44100)[source]#

Render a MIDI file to PCM bytes (for streaming).

Parameters:
  • midi_path (Path) – Path to MIDI file

  • sample_rate (int) – Audio sample rate

Return type:

bytes

Returns:

Raw PCM audio bytes

render_to_file(midi_path, output_path, *, sample_rate=44100)[source]#

Render a MIDI file to audio.

Parameters:
  • midi_path (Path) – Path to MIDI file

  • output_path (Path) – Where to save audio

  • sample_rate (int) – Audio sample rate

Return type:

Path

Returns:

Path to the created audio file