test_midi_action_bridge.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import copy
  2. import sys
  3. import tempfile
  4. import unittest
  5. from pathlib import Path
  6. import yaml
  7. ROOT = Path(__file__).resolve().parents[1]
  8. sys.path.insert(0, str(ROOT / "scripts"))
  9. from midi_action_bridge import (
  10. binding_index,
  11. handle_midi_message,
  12. load_configuration,
  13. select_nam_model,
  14. validate_bindings,
  15. )
  16. class MidiActionBridgeTests(unittest.TestCase):
  17. @classmethod
  18. def setUpClass(cls):
  19. cls.state, cls.bindings, cls.repo_root = load_configuration(
  20. ROOT / "config" / "desired-state.yaml",
  21. repo_root=ROOT,
  22. )
  23. def test_current_bindings_map_notes_six_through_nine_to_guitar_tb_models(self):
  24. self.assertEqual(validate_bindings(self.bindings, self.state), [])
  25. events = {
  26. entry["event"]["note"]: entry["action"]["model"]
  27. for entry in self.bindings["bindings"]
  28. }
  29. self.assertEqual(
  30. events,
  31. {
  32. 6: "crystal_clean_tb",
  33. 7: "edge_of_breakup_tb",
  34. 8: "crunch_tb",
  35. 9: "overdriven_tb",
  36. },
  37. )
  38. self.assertTrue(
  39. all(
  40. entry["action"]["fallback_model"] == "crystal_clean_tb"
  41. for entry in self.bindings["bindings"]
  42. )
  43. )
  44. def test_note_on_sends_model_patch_set_through_mod_ui(self):
  45. commands = []
  46. actions = binding_index(self.bindings)
  47. result = handle_midi_message(
  48. [0x90, 7, 127],
  49. actions,
  50. self.state,
  51. ROOT,
  52. command=lambda instance, uri, value: commands.append(
  53. ("127.0.0.1", 8888, f"rigforge_patch_set {instance} {uri} p {value}")
  54. ) or True,
  55. )
  56. model = ROOT / self.state["routing"]["mod_host_chains"]["guitar"]["processors"][0][
  57. "available_models"
  58. ]["edge_of_breakup_tb"]
  59. self.assertEqual(result["model"], "edge_of_breakup_tb")
  60. self.assertEqual(result["note"], 7)
  61. self.assertEqual(
  62. commands,
  63. [(
  64. "127.0.0.1",
  65. 8888,
  66. "rigforge_patch_set /graph/rigforge_nam "
  67. "http://github.com/mikeoliphant/neural-amp-modeler-lv2#model p "
  68. + str(model),
  69. )],
  70. )
  71. def test_note_off_zero_velocity_and_unbound_channel_do_nothing(self):
  72. commands = []
  73. command = lambda *_args: commands.append(_args) or True
  74. actions = binding_index(self.bindings)
  75. self.assertIsNone(handle_midi_message([0x80, 7, 0], actions, self.state, ROOT, command))
  76. self.assertIsNone(handle_midi_message([0x90, 7, 0], actions, self.state, ROOT, command))
  77. self.assertIsNone(handle_midi_message([0x91, 7, 127], actions, self.state, ROOT, command))
  78. self.assertEqual(commands, [])
  79. def test_missing_requested_model_uses_declared_clean_fallback(self):
  80. state = copy.deepcopy(self.state)
  81. models = state["routing"]["mod_host_chains"]["guitar"]["processors"][0][
  82. "available_models"
  83. ]
  84. models["edge_of_breakup_tb"] = "models/missing.nam"
  85. models["crystal_clean_tb"] = "models/clean.nam"
  86. action = copy.deepcopy(self.bindings["bindings"][1]["action"])
  87. with tempfile.TemporaryDirectory() as directory:
  88. root = Path(directory)
  89. clean_model = root / "models" / "clean.nam"
  90. clean_model.parent.mkdir()
  91. clean_model.write_text("test fixture", encoding="utf-8")
  92. commands = []
  93. def command(instance, uri, value):
  94. commands.append((instance, uri, value))
  95. return True
  96. selected_id, selected_path = select_nam_model(
  97. action, state, root, command=command
  98. )
  99. self.assertEqual(selected_id, "crystal_clean_tb")
  100. self.assertEqual(selected_path, clean_model)
  101. self.assertIn("models/clean.nam", commands[0][2])
  102. def test_missing_requested_and_fallback_model_sends_no_ui_command(self):
  103. state = copy.deepcopy(self.state)
  104. models = state["routing"]["mod_host_chains"]["guitar"]["processors"][0][
  105. "available_models"
  106. ]
  107. models["edge_of_breakup_tb"] = "models/missing-edge.nam"
  108. models["crystal_clean_tb"] = "models/missing-clean.nam"
  109. commands = []
  110. action = copy.deepcopy(self.bindings["bindings"][1]["action"])
  111. with tempfile.TemporaryDirectory() as directory:
  112. with self.assertRaises(FileNotFoundError):
  113. select_nam_model(
  114. action,
  115. state,
  116. Path(directory),
  117. command=lambda *args: commands.append(args) or True,
  118. )
  119. self.assertEqual(commands, [])
  120. def test_invalid_or_duplicate_binding_is_rejected(self):
  121. bindings = copy.deepcopy(self.bindings)
  122. bindings["bindings"][1]["event"]["note"] = 6
  123. errors = validate_bindings(bindings, self.state)
  124. self.assertTrue(any("duplicate MIDI note binding" in error for error in errors))
  125. bindings = copy.deepcopy(self.bindings)
  126. bindings["bindings"][0]["action"]["target"]["processor"] = "not-a-processor"
  127. errors = validate_bindings(bindings, self.state)
  128. self.assertTrue(any("must identify exactly one processor" in error for error in errors))
  129. if __name__ == "__main__":
  130. unittest.main()