Sfoglia il codice sorgente

change gain with midi in IR when changing NAM

Johan Le Baut 2 giorni fa
parent
commit
fffe537a55
1 ha cambiato i file con 53 aggiunte e 3 eliminazioni
  1. 53 3
      scripts/midi_action_bridge.py

+ 53 - 3
scripts/midi_action_bridge.py

@@ -1,5 +1,5 @@
 #!/usr/bin/env python3
-"""Translate configured MIDI events into NAM model changes via mod-ui."""
+"""Translate configured MIDI events into guitar model and level changes via mod-ui."""
 
 import argparse
 import hashlib
@@ -13,6 +13,13 @@ from mod_ui_websocket import ModUiWebSocketClient
 from validate_desired_state import safe_relative_path, validate_state
 
 
+IR_OUTPUT_GAIN_PARAMETER = "g_out"
+DEFAULT_IR_OUTPUT_GAIN = 1.0
+IR_OUTPUT_GAIN_BY_MODEL = {
+    "edge_of_breakup_tb": 1.5,
+}
+
+
 def load_yaml(path):
     with path.open(encoding="utf-8") as stream:
         return yaml.safe_load(stream)
@@ -191,6 +198,15 @@ def mod_ui_command(config, instance, property_uri, value):
         return client.set_patch(instance, property_uri, value)
 
 
+def mod_ui_parameter_command(client, instance, symbol, value):
+    """Set a regular LV2 control port through mod-ui's WebSocket protocol."""
+    client.connect()
+    client.send_text(
+        f"param_set {instance}/{symbol} {float(value):.6f}"
+    )
+    return True
+
+
 def select_nam_model(action, state, repo_root, command=None, user_files_root=None):
     processor = resolve_processor(state, action["target"])
     models = processor["available_models"]
@@ -237,7 +253,13 @@ def binding_index(bindings):
 
 
 def handle_midi_message(
-    message, bindings, state, repo_root, command=None, user_files_root=None
+    message,
+    bindings,
+    state,
+    repo_root,
+    command=None,
+    user_files_root=None,
+    parameter_command=None,
 ):
     if not message or len(message) < 3:
         return None
@@ -256,13 +278,29 @@ def handle_midi_message(
         command=command,
         user_files_root=user_files_root,
     )
-    return {
+    result = {
         "binding": binding["id"],
         "channel": channel,
         "note": note,
         "model": model_id,
         "path": model_path,
     }
+    target = binding["action"]["target"]
+    if target.get("chain") == "guitar" and target.get("processor") == "amp-head":
+        ir_processor = resolve_processor(
+            state, {"chain": "guitar", "processor": "cabinet-ir"}
+        )
+        ir_output_gain = IR_OUTPUT_GAIN_BY_MODEL.get(
+            model_id, DEFAULT_IR_OUTPUT_GAIN
+        )
+        if parameter_command is not None and not parameter_command(
+            ir_processor["mod_ui_instance"],
+            IR_OUTPUT_GAIN_PARAMETER,
+            ir_output_gain,
+        ):
+            raise RuntimeError("mod-ui did not apply the cabinet IR output gain")
+        result["ir_output_gain"] = ir_output_gain
+    return result
 
 
 def list_midi_ports():
@@ -320,6 +358,11 @@ def run_bridge(state, bindings, repo_root, rtmidi_module=None, sleeper=time.slee
     def set_nam_model(instance, property_uri, value):
         return mod_ui_client.set_patch(instance, property_uri, value)
 
+    def set_plugin_parameter(instance, symbol, value):
+        return mod_ui_parameter_command(
+            mod_ui_client, instance, symbol, value
+        )
+
     print(f"Looking for ALSA MIDI input containing {name_contains!r}…")
     while True:
         midi_in = None
@@ -339,11 +382,18 @@ def run_bridge(state, bindings, repo_root, rtmidi_module=None, sleeper=time.slee
                         result = handle_midi_message(
                             received[0], actions, state, repo_root,
                             command=set_nam_model,
+                            parameter_command=set_plugin_parameter,
                         )
                         if result:
+                            gain_message = (
+                                f", IR gain {result['ir_output_gain']:.2f}"
+                                if "ir_output_gain" in result
+                                else ""
+                            )
                             print(
                                 f"{result['binding']}: MIDI ch {result['channel'] + 1} "
                                 f"note {result['note']} -> {result['model']}"
+                                f"{gain_message}"
                             )
                     except (OSError, RuntimeError, ValueError, KeyError) as error:
                         print(f"MIDI action failed: {error}", file=sys.stderr)