|
| 1 | +from pydantic_ai import RunContext |
| 2 | + |
| 3 | +from patchwork.deps import PatchworkDeps |
| 4 | + |
| 5 | + |
| 6 | +async def list_midi_ports(ctx: RunContext[PatchworkDeps]) -> str: |
| 7 | + """List available MIDI output ports.""" |
| 8 | + ports = ctx.deps.midi.list_ports() |
| 9 | + if not ports: |
| 10 | + return "No MIDI output ports found. Is your MIDI interface connected?" |
| 11 | + lines = [f"{i}: {name}" for i, name in enumerate(ports)] |
| 12 | + return "Available MIDI ports:\n" + "\n".join(lines) |
| 13 | + |
| 14 | + |
| 15 | +async def send_cc( |
| 16 | + ctx: RunContext[PatchworkDeps], |
| 17 | + synth: str, |
| 18 | + parameter: str, |
| 19 | + value: int, |
| 20 | +) -> str: |
| 21 | + """Send a MIDI CC value to a parameter on a synth. |
| 22 | +
|
| 23 | + Args: |
| 24 | + synth: Synth name (e.g. "minitaur", "tb-03") |
| 25 | + parameter: Parameter name from the synth's CC map (e.g. "filter_cutoff") |
| 26 | + value: CC value to send (0-127) |
| 27 | + """ |
| 28 | + synth_def = ctx.deps.synths.get(synth.lower()) |
| 29 | + if synth_def is None: |
| 30 | + available = ", ".join(ctx.deps.synths.keys()) |
| 31 | + return f"Unknown synth '{synth}'. Available: {available}" |
| 32 | + |
| 33 | + param_key = parameter.lower().replace(" ", "_") |
| 34 | + param = synth_def.cc_map.get(param_key) |
| 35 | + if param is None: |
| 36 | + available = ", ".join(synth_def.cc_map.keys()) |
| 37 | + return f"Unknown parameter '{parameter}' for {synth_def.name}. Available: {available}" |
| 38 | + |
| 39 | + low, high = param.value_range |
| 40 | + if not (low <= value <= high): |
| 41 | + return f"Value {value} out of range for {parameter} ({low}-{high})" |
| 42 | + |
| 43 | + if not ctx.deps.midi.is_connected: |
| 44 | + return f"MIDI not connected. Use list_midi_ports to see available ports, then ask me to connect." |
| 45 | + |
| 46 | + ctx.deps.midi.send_cc(synth_def.midi_channel, param.cc, value) |
| 47 | + return f"Sent CC {param.cc} = {value} to {synth_def.name} ch.{synth_def.midi_channel} ({parameter})" |
| 48 | + |
| 49 | + |
| 50 | +async def send_patch( |
| 51 | + ctx: RunContext[PatchworkDeps], |
| 52 | + synth: str, |
| 53 | + settings: dict[str, int], |
| 54 | +) -> str: |
| 55 | + """Send multiple CC values at once to set a full patch on a synth. |
| 56 | +
|
| 57 | + Args: |
| 58 | + synth: Synth name (e.g. "minitaur", "tb-03") |
| 59 | + settings: Dict of parameter name to CC value (e.g. {"filter_cutoff": 64, "resonance": 80}) |
| 60 | + """ |
| 61 | + synth_def = ctx.deps.synths.get(synth.lower()) |
| 62 | + if synth_def is None: |
| 63 | + available = ", ".join(ctx.deps.synths.keys()) |
| 64 | + return f"Unknown synth '{synth}'. Available: {available}" |
| 65 | + |
| 66 | + if not ctx.deps.midi.is_connected: |
| 67 | + return f"MIDI not connected. Use list_midi_ports to see available ports, then ask me to connect." |
| 68 | + |
| 69 | + results = [] |
| 70 | + for param_name, value in settings.items(): |
| 71 | + param_key = param_name.lower().replace(" ", "_") |
| 72 | + param = synth_def.cc_map.get(param_key) |
| 73 | + if param is None: |
| 74 | + results.append(f" ✗ Unknown parameter '{param_name}'") |
| 75 | + continue |
| 76 | + low, high = param.value_range |
| 77 | + if not (low <= value <= high): |
| 78 | + results.append(f" ✗ {param_name}: value {value} out of range ({low}-{high})") |
| 79 | + continue |
| 80 | + ctx.deps.midi.send_cc(synth_def.midi_channel, param.cc, value) |
| 81 | + results.append(f" ✓ {param_name} = {value} (CC {param.cc})") |
| 82 | + |
| 83 | + return f"Patch sent to {synth_def.name} ch.{synth_def.midi_channel}:\n" + "\n".join(results) |
| 84 | + |
| 85 | + |
| 86 | +async def list_synths(ctx: RunContext[PatchworkDeps]) -> str: |
| 87 | + """List all loaded synth definitions and their controllable parameters.""" |
| 88 | + if not ctx.deps.synths: |
| 89 | + return "No synth definitions loaded. Add YAML files to the synths/ directory." |
| 90 | + lines = [] |
| 91 | + for synth in ctx.deps.synths.values(): |
| 92 | + params = ", ".join(synth.cc_map.keys()) |
| 93 | + lines.append(f"- {synth.manufacturer} {synth.name} (ch.{synth.midi_channel}): {params}") |
| 94 | + return "Loaded synths:\n" + "\n".join(lines) |
| 95 | + |
| 96 | + |
| 97 | +async def connect_midi( |
| 98 | + ctx: RunContext[PatchworkDeps], |
| 99 | + port_index: int = 0, |
| 100 | +) -> str: |
| 101 | + """Connect to a MIDI output port by index. Use list_midi_ports first to see available ports. |
| 102 | +
|
| 103 | + Args: |
| 104 | + port_index: Index of the MIDI port to connect to (default: 0, the first port) |
| 105 | + """ |
| 106 | + try: |
| 107 | + port_name = ctx.deps.midi.open(port_index) |
| 108 | + return f"Connected to MIDI port: {port_name}" |
| 109 | + except (RuntimeError, ValueError) as e: |
| 110 | + return str(e) |
0 commit comments