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
|
Renders MIDI events to audio. |
|
Convert chord symbols to MIDI note numbers. |
|
Generates MIDI events from score and patterns. |
|
Provides musical patterns for a given style. |
|
Audio synthesis backend. |
- class accompy.protocols.AudioRenderer(*args, **kwargs)[source]#
Bases:
ProtocolRenders 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:
config (
AccompanimentConfig) – Rendering configuration (tempo, sample rate, etc.)
- Return type:
- Returns:
Raw PCM audio bytes
- render_to_file(events, config, output_path)[source]#
Render MIDI events directly to an audio file.
- Parameters:
config (
AccompanimentConfig) – Rendering configurationoutput_path (
Path) – Where to save the audio file
- Return type:
- Returns:
Path to the created file
- class accompy.protocols.ChordResolver(*args, **kwargs)[source]#
Bases:
ProtocolConvert 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:
ProtocolGenerates 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 progressionconfig (
AccompanimentConfig) – Accompaniment configurationchord_resolver (
ChordResolver) – Function to resolve chords to notespattern_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:
ProtocolProvides 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']
- class accompy.protocols.SynthesizerBackend(*args, **kwargs)[source]#
Bases:
ProtocolAudio 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:
- Returns:
True if the backend can be used