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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput, Error, Fields, Result as SynResult};
mod utils;
use utils::{determine_control_type, parse_midi_attributes, ControlType, RangeSpec};
/// Derive macro for MIDI parameter mapping
#[proc_macro_derive(MidiParams, attributes(midi))]
pub fn derive_midi_params(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
match impl_midi_params(&input) {
Ok(tokens) => tokens.into(),
Err(err) => err.to_compile_error().into(),
}
}
fn impl_midi_params(input: &DeriveInput) -> SynResult<proc_macro2::TokenStream> {
let name = &input.ident;
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
let fields = match &input.data {
Data::Struct(data) => match &data.fields {
Fields::Named(fields) => &fields.named,
_ => {
return Err(Error::new_spanned(
name,
"MidiParams only supports structs with named fields",
))
}
},
_ => {
return Err(Error::new_spanned(
name,
"MidiParams can only be derived for structs",
))
}
};
let mut midi_mappings = Vec::new();
let mut midi_updates = Vec::new();
for field in fields {
let field_name = field.ident.as_ref().unwrap();
let field_name_str = field_name.to_string();
let field_type = &field.ty;
// Parse all midi attributes on this field
let mappings = parse_midi_attributes(field)?;
if !mappings.is_empty() {
// Determine control type based on field type and mappings
let control_type = determine_control_type(field_type, &mappings)?;
match control_type {
ControlType::Range { min, max } => {
// Single range control
let mapping = &mappings[0];
let cc = mapping.cc;
// MIDI mapping
midi_mappings.push(quote! {
bevy_midi_params::MidiMapping::range(#cc, #field_name_str, #min, #max)
});
// MIDI update logic
midi_updates.push(quote! {
#cc => {
let new_value = #min + value * (#max - #min);
if (self.#field_name - new_value).abs() > f32::EPSILON {
self.#field_name = new_value;
changed = true;
}
}
});
}
ControlType::VectorRange { components } => {
// Multiple component controls for vector types
let component_names = ["x", "y", "z", "w"];
for (i, (cc, min, max)) in components.iter().enumerate() {
let comp_name = component_names[i];
let field_comp_name = format!("{}.{}", field_name_str, comp_name);
// MIDI mapping
midi_mappings.push(quote! {
bevy_midi_params::MidiMapping::range(#cc, #field_comp_name, #min, #max)
});
// MIDI update logic - access component by index
let idx = i;
midi_updates.push(quote! {
#cc => {
let new_value = #min + value * (#max - #min);
if (self.#field_name[#idx] - new_value).abs() > f32::EPSILON {
self.#field_name[#idx] = new_value;
changed = true;
}
}
});
}
}
ControlType::Toggle => {
let mapping = &mappings[0];
let cc = mapping.cc;
// MIDI mapping
midi_mappings.push(quote! {
bevy_midi_params::MidiMapping::button(#cc, #field_name_str)
});
// MIDI update logic
midi_updates.push(quote! {
#cc => {
if value > 0.5 {
self.#field_name = !self.#field_name;
changed = true;
}
}
});
}
ControlType::IntRange { min, max } => {
let mapping = &mappings[0];
let cc = mapping.cc;
// MIDI mapping
midi_mappings.push(quote! {
bevy_midi_params::MidiMapping::range(#cc, #field_name_str, #min as f32, #max as f32)
});
// MIDI update logic
midi_updates.push(quote! {
#cc => {
let new_value = (#min as f32 + value * (#max - #min) as f32).round() as i32;
if self.#field_name != new_value {
self.#field_name = new_value;
changed = true;
}
}
});
}
ControlType::LinearRgba => {
// LinearRgba has red, green, blue, alpha fields
if mappings.len() != 4 {
return Err(Error::new_spanned(
field_type,
"LinearRgba requires exactly 4 #[midi] attributes (r, g, b, a)",
));
}
let component_idents = [
quote::format_ident!("red"),
quote::format_ident!("green"),
quote::format_ident!("blue"),
quote::format_ident!("alpha"),
];
let component_labels = ["r", "g", "b", "a"];
for (i, mapping) in mappings.iter().enumerate() {
let cc = mapping.cc;
let (min, max) = if let Some(ref range) = mapping.range {
match range {
RangeSpec::Float(min, max) => (*min, *max),
RangeSpec::Int(min, max) => (*min as f32, *max as f32),
}
} else {
(0.0, 1.0)
};
let comp_ident = &component_idents[i];
let comp_label = component_labels[i];
let field_comp_name = format!("{}.{}", field_name_str, comp_label);
// MIDI mapping
midi_mappings.push(quote! {
bevy_midi_params::MidiMapping::range(#cc, #field_comp_name, #min, #max)
});
// MIDI update logic
midi_updates.push(quote! {
#cc => {
let new_value = #min + value * (#max - #min);
if (self.#field_name.#comp_ident - new_value).abs() > f32::EPSILON {
self.#field_name.#comp_ident = new_value;
changed = true;
}
}
});
}
}
ControlType::Srgba => {
// Srgba has red, green, blue, alpha fields
if mappings.len() != 4 {
return Err(Error::new_spanned(
field_type,
"Srgba requires exactly 4 #[midi] attributes (r, g, b, a)",
));
}
let component_idents = [
quote::format_ident!("red"),
quote::format_ident!("green"),
quote::format_ident!("blue"),
quote::format_ident!("alpha"),
];
let component_labels = ["r", "g", "b", "a"];
for (i, mapping) in mappings.iter().enumerate() {
let cc = mapping.cc;
let (min, max) = if let Some(ref range) = mapping.range {
match range {
RangeSpec::Float(min, max) => (*min, *max),
RangeSpec::Int(min, max) => (*min as f32, *max as f32),
}
} else {
(0.0, 1.0)
};
let comp_ident = &component_idents[i];
let comp_label = component_labels[i];
let field_comp_name = format!("{}.{}", field_name_str, comp_label);
// MIDI mapping
midi_mappings.push(quote! {
bevy_midi_params::MidiMapping::range(#cc, #field_comp_name, #min, #max)
});
// MIDI update logic
midi_updates.push(quote! {
#cc => {
let new_value = #min + value * (#max - #min);
if (self.#field_name.#comp_ident - new_value).abs() > f32::EPSILON {
self.#field_name.#comp_ident = new_value;
changed = true;
}
}
});
}
}
ControlType::Hsla => {
// Hsla has hue, saturation, lightness, alpha fields
if mappings.len() != 4 {
return Err(Error::new_spanned(
field_type,
"Hsla requires exactly 4 #[midi] attributes (h, s, l, a)",
));
}
let component_idents = [
quote::format_ident!("hue"),
quote::format_ident!("saturation"),
quote::format_ident!("lightness"),
quote::format_ident!("alpha"),
];
let component_labels = ["h", "s", "l", "a"];
for (i, mapping) in mappings.iter().enumerate() {
let cc = mapping.cc;
let (min, max) = if let Some(ref range) = mapping.range {
match range {
RangeSpec::Float(min, max) => (*min, *max),
RangeSpec::Int(min, max) => (*min as f32, *max as f32),
}
} else {
// Default ranges for HSL
match i {
0 => (0.0, 360.0), // hue
_ => (0.0, 1.0), // saturation, lightness, alpha
}
};
let comp_ident = &component_idents[i];
let comp_label = component_labels[i];
let field_comp_name = format!("{}.{}", field_name_str, comp_label);
// MIDI mapping
midi_mappings.push(quote! {
bevy_midi_params::MidiMapping::range(#cc, #field_comp_name, #min, #max)
});
// MIDI update logic
midi_updates.push(quote! {
#cc => {
let new_value = #min + value * (#max - #min);
if (self.#field_name.#comp_ident - new_value).abs() > f32::EPSILON {
self.#field_name.#comp_ident = new_value;
changed = true;
}
}
});
}
}
ControlType::Hsva => {
// Hsva has hue, saturation, value, alpha fields
if mappings.len() != 4 {
return Err(Error::new_spanned(
field_type,
"Hsva requires exactly 4 #[midi] attributes (h, s, v, a)",
));
}
let component_idents = [
quote::format_ident!("hue"),
quote::format_ident!("saturation"),
quote::format_ident!("value"),
quote::format_ident!("alpha"),
];
let component_labels = ["h", "s", "v", "a"];
for (i, mapping) in mappings.iter().enumerate() {
let cc = mapping.cc;
let (min, max) = if let Some(ref range) = mapping.range {
match range {
RangeSpec::Float(min, max) => (*min, *max),
RangeSpec::Int(min, max) => (*min as f32, *max as f32),
}
} else {
// Default ranges for HSV
match i {
0 => (0.0, 360.0), // hue
_ => (0.0, 1.0), // saturation, value, alpha
}
};
let comp_ident = &component_idents[i];
let comp_label = component_labels[i];
let field_comp_name = format!("{}.{}", field_name_str, comp_label);
// MIDI mapping
midi_mappings.push(quote! {
bevy_midi_params::MidiMapping::range(#cc, #field_comp_name, #min, #max)
});
// MIDI update logic
midi_updates.push(quote! {
#cc => {
let new_value = #min + value * (#max - #min);
if (self.#field_name.#comp_ident - new_value).abs() > f32::EPSILON {
self.#field_name.#comp_ident = new_value;
changed = true;
}
}
});
}
}
}
}
}
let type_name_str = name.to_string();
let expanded = quote! {
impl #impl_generics bevy_midi_params::MidiControllable for #name #ty_generics #where_clause {
fn update_from_midi(&mut self, cc: u8, value: f32) -> bool {
let mut changed = false;
match cc {
#(#midi_updates)*
_ => {}
}
changed
}
fn get_midi_mappings() -> Vec<bevy_midi_params::MidiMapping> {
vec![#(#midi_mappings),*]
}
fn get_type_name() -> &'static str {
#type_name_str
}
}
// Auto-register this type when it's used
bevy_midi_params::inventory::submit! {
bevy_midi_params::MidiParamsRegistration {
type_name: #type_name_str,
register_fn: |app: &mut bevy::prelude::App| {
bevy_midi_params::register_midi_type::<#name #ty_generics>(app);
},
}
}
};
Ok(expanded)
}