-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
284 lines (211 loc) · 9.06 KB
/
main.py
File metadata and controls
284 lines (211 loc) · 9.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/usr/bin/env python3
import argparse
import asyncio
import logging
import sys
import textwrap
import time
from pathlib import Path
import mido
from pydantic_ai import Agent
from text2synth.state import JU06AState
DEFAULT_MIDI_IN = DEFAULT_MIDI_OUT = "USB MIDI Interface"
LOGGER = logging.getLogger(__name__)
DEFAULT_LLM_MODEL = "anthropic:claude-sonnet-4-5"
def list_ports_cli(args):
print("MIDI Input Ports:")
for name in mido.get_input_names():
print(" ", name)
print("\nMIDI Output Ports:")
for name in mido.get_output_names():
print(" ", name)
def control_change_cli(args):
outport_name = args.midi_out
number = args.cc_number
value = args.cc_value
LOGGER.debug("Sending CC %d with value %d", number, value)
with mido.open_output(outport_name) as outport:
LOGGER.debug("Ready to use port")
msg = mido.Message('control_change', control=number, value=value)
outport.send(msg)
def analyze_patch_ranges_cli(args):
"""
Recursively analyze all .PRM patch files to find min/max ranges for each parameter.
Parameters
----------
directory : Path
Directory to search for .PRM files (searches recursively)
Returns
-------
Dict[str, Tuple[int, int]]
Dictionary mapping attribute names to (min, max) tuples for all numerical attributes
"""
path = args.path
# Dictionary to track min/max for each attribute
ranges = {}
# Recursively find all .PRM files
prn_files = list(Path(path).rglob("*.PRM"))
if not prn_files:
print(f"Warning: No .PRM files found in {path}")
return ranges
print(f"Found {len(prn_files)} .PRM files")
for prn_file in prn_files:
try:
patch = JU06AState.from_path(str(prn_file))
for attr_name in JU06AState.model_fields:
value = getattr(patch, attr_name)
# Skip non-integer attributes (like patch_name)
if not isinstance(value, int):
continue
# Update min/max for this attribute
if attr_name not in ranges:
ranges[attr_name] = (value, value)
else:
current_min, current_max = ranges[attr_name]
ranges[attr_name] = (min(current_min, value), max(current_max, value))
except Exception as e:
print(f"Error processing {prn_file}: {e}")
continue
print("\nParameter Ranges:")
print("-" * 50)
if args.show_double_only:
for attr_name, (min_val, max_val) in sorted(ranges.items()):
if max_val > 128 and max_val <= 255:
print(attr_name)
else:
for attr_name, (min_val, max_val) in sorted(ranges.items()):
print(f"{attr_name:20s}: {min_val:3d} - {max_val:3d}")
def program_change_cli(args):
outport_name = args.midi_out
program = args.program
LOGGER.info("Changing program to %d", program)
assert program >= 1
with mido.open_output(outport_name) as outport:
LOGGER.debug("Ready to use port")
msg = mido.Message('program_change', program=program-1)
outport.send(msg)
def apply_state_to_synth(state, outport_name):
with mido.open_output(outport_name) as outport:
LOGGER.debug("Ready to use MIDI port %s", outport_name)
for msg in state.to_cc_messages():
outport.send(msg)
# Sleeping a bit to avoid flooding the MIDI connection
time.sleep(0.001)
def send_patch_cli(args):
outport_name = args.midi_out
path = args.path
LOGGER.info("Applying PRM file %s", path)
state = JU06AState.from_path(path)
apply_state_to_synth(state, outport_name)
def load_patches(patch_directory: str, max_examples=None) -> str:
"""
Load patches in their original text format.
Parameters
----------
patch_directory : str
Directory containing .PRM patch files
max_examples : int
Maximum number of examples to include
Returns
-------
str
Concatenated original patch files
"""
patches = []
paths = list(Path(patch_directory).rglob("*.PRM"))[:max_examples]
print(f"Loading {len(paths)} patches")
for path in paths:
try:
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
patches.append(f"=== {path.name} ===\n{content}\n")
except Exception:
continue
return "\n".join(patches)
async def text2patch_cmd(agent, description, outport_name, output_path="test-patch.prm", patch_name="TEST PATCH"):
result = await agent.run(description)
state = result.output
state.to_path(output_path, patch_name)
LOGGER.debug("Applying to synth")
apply_state_to_synth(state, "USB MIDI Interface")
def text2patch_cli(args):
outport_name = args.midi_out
patches_path = args.patches_path
max_patches = args.max_patches
description = args.description
llm_model = args.llm_model
if patches_path is not None:
patches = load_patches(patches_path, max_patches)
else:
patches = ""
# Create agent
agent = Agent(
llm_model,
output_type=JU06AState,
system_prompt=textwrap.dedent(f"""You are an expert sound designer for the Roland JU-06A synthesizer.
Create synthesizer patches based on user descriptions. Consider:
- Filter cutoff and resonance for brightness and character
- Envelope (ADSR) for shaping the sound over time
- LFO for modulation effects
- Oscillator settings for tone color
- Effects like chorus and delay for depth
Here are example patches from real JU-06A presets to learn from:
{patches}
Generate creative, musically useful patches that match the user's description.""",
)
)
LOGGER.debug("Agent %s is created", agent)
asyncio.run(
text2patch_cmd(agent, description, outport_name)
)
def main():
logging.basicConfig(level=logging.INFO,
format="%(levelname)s:%(module)s.%(funcName)s: %(message)s")
parser = argparse.ArgumentParser(description="Simple MIDI CLI")
parser.add_argument("--midi-in", type=str, default=DEFAULT_MIDI_IN,
help="MIDI input device")
parser.add_argument("--midi-out", type=str, default=DEFAULT_MIDI_OUT,
help="MIDI output device")
subparsers = parser.add_subparsers(dest="command", required=True)
pc_parser = subparsers.add_parser("program-change", aliases=["pc"],
help="Send a Program change")
pc_parser.add_argument("program", type=int, help="Program number")
pc_parser.add_argument("--channel", type=int, default=1,
help="MIDI channel (default: 1)")
pc_parser.set_defaults(func=program_change_cli)
send_patch_parser = subparsers.add_parser("send-patch",
help="Apply the given patch file to the synth through MIDI")
send_patch_parser.add_argument("path", type=str, help="Path to the PRM file")
send_patch_parser.set_defaults(func=send_patch_cli)
cc_parser = subparsers.add_parser("control-change", aliases=["cc"],
help="Send a MIDI control change message")
cc_parser.add_argument("cc_number", type=int, help="Control change number")
cc_parser.add_argument("cc_value", type=int, help="Control change value")
cc_parser.add_argument("--channel", type=int, default=1,
help="MIDI channel (default: 1)")
cc_parser.set_defaults(func=control_change_cli)
stats_parser = subparsers.add_parser("stats",
help="Parse patches to find range statistics")
stats_parser.add_argument("path", type=str, help="Directory to recursively walk")
stats_parser.add_argument("--show-double-only", action="store_true",
default=False,
help="If given, only print CC that go beyond 127")
stats_parser.set_defaults(func=analyze_patch_ranges_cli)
list_ports_parser = subparsers.add_parser("list-ports",
help="list ports")
list_ports_parser.set_defaults(func=list_ports_cli)
text2patch_parser = subparsers.add_parser("text2patch", aliases=["t2p"],
help="Create a new patch from description and apply it to the synth")
text2patch_parser.add_argument("description", type=str, help="Patch description")
text2patch_parser.add_argument("--max-patches", type=int, help="Max patches to load")
text2patch_parser.add_argument("--patches-path", type=str, help="Where to look for patches")
text2patch_parser.add_argument("--llm-model", type=str, help="The LLM to use", default=DEFAULT_LLM_MODEL)
text2patch_parser.set_defaults(func=text2patch_cli)
args = parser.parse_args()
if hasattr(args, "func"):
args.func(args)
else:
print("No operation implemented for this command yet.")
sys.exit(-1)
if __name__ == "__main__":
main()