test_midi_action_bridge.py 6.4 KB

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