truce-rack-au 0.9.0

AU v2 host implementation for the truce-rack audio-plugin framework (Apple platforms)
Documentation
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
//! Audio Unit v2 host for the truce-rack framework. Apple-only.
//!
//! Built on `objc2-audio-toolbox` — the modern Rust bindings
//! that replace the C++ `AudioToolbox` scanner the legacy rack 0.4
//! relied on. No cmake, no bridging-header step.
//!
//! # Status
//!
//! - **Scan** via `AudioComponentFindNext` across the AU v2 type
//!   families (Effect, `MusicDevice`, Generator, Mixer, `MusicEffect`,
//!   `MIDIProcessor`). Each match yields one [`PluginInfo`] with the
//!   AU's four-CC packed as a `"type:sub:mfr"` `unique_id`.
//! - **Load** via `AudioComponentInstanceNew`.
//! - **Process** via `AudioUnitRender` into a per-block
//!   `AudioBufferList` whose buffers point directly at the host's
//!   output planes (see [`AuPlugin::process`]).
//! - **MIDI** via `MusicDeviceMIDIEvent`. Routes channel-voice
//!   messages with sample-accurate offsets; raw bytes pass through
//!   the same call. Non-MIDI-accepting effects no-op silently.
//! - **Editor** via the `AUv2` Cocoa view (`kAudioUnitProperty_CocoaUI`).
//!
//! AU v3 plugins that set `kAudioComponentFlag_RequiresAsyncInstantiation`
//! still need the async path and are not yet loadable through this
//! crate; truce-rack-au3 forwards them here regardless.

#![cfg(target_vendor = "apple")]

use truce_rack_core::buffer::AudioBuffer;
use truce_rack_core::bus::BusLayout;
use truce_rack_core::error::{Error, Result};
use truce_rack_core::events::EventList;
use truce_rack_core::info::{ParameterInfo, PluginCategory, PluginInfo, PresetInfo};
use truce_rack_core::plugin::{Plugin, PluginCore, ProcessContext, ProcessStatus};
use truce_rack_core::scanner::PluginScanner;

use objc2_audio_toolbox::{
    AURenderCallbackStruct, AudioComponent, AudioComponentCopyName, AudioComponentDescription,
    AudioComponentFindNext, AudioComponentFlags, AudioComponentInstance,
    AudioComponentInstanceDispose, AudioComponentInstanceNew, AudioComponentInstantiate,
    AudioComponentInstantiationOptions, AudioUnitGetParameter,
    AudioUnitGetProperty, AudioUnitGetPropertyInfo, AudioUnitInitialize, AudioUnitParameterID,
    AudioUnitParameterInfo, AudioUnitRender, AudioUnitRenderActionFlags, AudioUnitSetParameter,
    AudioUnitSetProperty, AudioUnitUninitialize, AUPreset, MusicDeviceMIDIEvent,
    kAudioUnitProperty_ClassInfo,
    kAudioUnitProperty_FactoryPresets, kAudioUnitProperty_MaximumFramesPerSlice,
    kAudioUnitProperty_ParameterInfo, kAudioUnitProperty_ParameterList,
    kAudioUnitProperty_PresentPreset, kAudioUnitProperty_SetRenderCallback,
    kAudioUnitProperty_StreamFormat, kAudioUnitScope_Global, kAudioUnitScope_Input,
    kAudioUnitScope_Output, kAudioUnitType_Effect, kAudioUnitType_Generator,
    kAudioUnitType_MIDIProcessor, kAudioUnitType_Mixer, kAudioUnitType_MusicDevice,
    kAudioUnitType_MusicEffect,
};
use objc2_core_audio_types::{
    AudioBuffer as CAAudioBuffer, AudioBufferList, AudioStreamBasicDescription, AudioTimeStamp,
    AudioTimeStampFlags, kAudioFormatFlagIsFloat, kAudioFormatFlagIsNonInterleaved,
    kAudioFormatFlagIsPacked, kAudioFormatLinearPCM,
};
use objc2_core_foundation::CFString;

use std::path::Path;
use std::ptr;

/// Format identifier used on returned [`PluginInfo`].
pub const FORMAT: &str = "au";

/// AU type families we surface to hosts. Output / `FormatConverter`
/// are intentionally skipped — they're system plumbing, not
/// effects.
const SCAN_TYPES: &[u32] = &[
    kAudioUnitType_Effect,
    kAudioUnitType_MusicDevice,
    kAudioUnitType_Generator,
    kAudioUnitType_MusicEffect,
    kAudioUnitType_MIDIProcessor,
    kAudioUnitType_Mixer,
];

/// AU v2 scanner.
#[derive(Debug, Default)]
pub struct AuScanner;

impl AuScanner {
    /// Construct a default scanner.
    #[must_use]
    pub fn new() -> Self {
        Self
    }
}

impl PluginScanner for AuScanner {
    type Plugin = AuPlugin;

    fn scan(&self) -> Result<Vec<PluginInfo>> {
        let mut out = Vec::new();
        for &type_code in SCAN_TYPES {
            unsafe { scan_family(type_code, &mut out) };
        }
        Ok(out)
    }

    fn scan_path(&self, _path: &Path) -> Result<Vec<PluginInfo>> {
        // AU discovery is registry-based, not path-based.
        Err(Error::Other(
            "truce-rack-au path-bounded scan is not meaningful (AU uses a registry)".into(),
        ))
    }

    fn load(&self, info: &PluginInfo) -> Result<Self::Plugin> {
        AuPlugin::load_from(info)
    }
}

unsafe fn scan_family(component_type: u32, out: &mut Vec<PluginInfo>) {
    let mut desc = AudioComponentDescription {
        componentType: component_type,
        componentSubType: 0,
        componentManufacturer: 0,
        componentFlags: 0,
        componentFlagsMask: 0,
    };
    let mut component: AudioComponent = ptr::null_mut();
    let category = type_category(component_type);
    let accepts_midi =
        component_type != kAudioUnitType_Effect && component_type != kAudioUnitType_Generator;
    loop {
        let next = unsafe {
            AudioComponentFindNext(component, ptr::NonNull::new_unchecked(&raw mut desc))
        };
        if next.is_null() {
            break;
        }
        component = next;
        let mut comp_desc = AudioComponentDescription {
            componentType: 0,
            componentSubType: 0,
            componentManufacturer: 0,
            componentFlags: 0,
            componentFlagsMask: 0,
        };
        let _ = unsafe {
            objc2_audio_toolbox::AudioComponentGetDescription(
                component,
                ptr::NonNull::new_unchecked(&raw mut comp_desc),
            )
        };
        // Skip AUv3 (app-extension) components — they require
        // AudioComponentInstantiate (async / sandboxed) and are
        // owned by truce-rack-au3. AUv3 components advertise themselves
        // via the IsV3AudioUnit flag in componentFlags.
        if AudioComponentFlags::from_bits_retain(comp_desc.componentFlags)
            .contains(AudioComponentFlags::IsV3AudioUnit)
        {
            continue;
        }
        let name = unsafe { component_name(component) };
        let (vendor, display) = split_name(&name);
        out.push(PluginInfo {
            name: display,
            vendor,
            version: unsafe { component_version(component) },
            category,
            path: std::path::PathBuf::new(),
            unique_id: format_unique_id(&comp_desc),
            format: FORMAT,
            has_editor: false,
            accepts_midi,
        });
    }
}

/// Query `kAudioUnitProperty_ParameterInfo` for one parameter.
unsafe fn fetch_parameter_info(
    unit: AudioComponentInstance,
    id: AudioUnitParameterID,
) -> Result<AudioUnitParameterInfo> {
    let mut info = AudioUnitParameterInfo {
        name: [0; 52],
        unitName: ptr::null(),
        clumpID: 0,
        cfNameString: ptr::null(),
        unit: objc2_audio_toolbox::AudioUnitParameterUnit::Generic,
        minValue: 0.0,
        maxValue: 0.0,
        defaultValue: 0.0,
        flags: objc2_audio_toolbox::AudioUnitParameterOptions::empty(),
    };
    #[allow(clippy::cast_possible_truncation)]
    let mut size = std::mem::size_of::<AudioUnitParameterInfo>() as u32;
    let status = unsafe {
        AudioUnitGetProperty(
            unit,
            kAudioUnitProperty_ParameterInfo,
            kAudioUnitScope_Global,
            id,
            ptr::NonNull::new_unchecked((&raw mut info).cast()),
            ptr::NonNull::new_unchecked(&raw mut size),
        )
    };
    if status != 0 {
        return Err(Error::Other(format!(
            "kAudioUnitProperty_ParameterInfo failed: OSStatus {status}"
        )));
    }
    Ok(info)
}

#[allow(clippy::cast_sign_loss)]
fn au_parameter_to_rack(id: AudioUnitParameterID, info: &AudioUnitParameterInfo) -> ParameterInfo {
    // CFNameString takes precedence over the 52-byte ASCII name
    // field — AU plugins set the latter to empty more often than
    // not since the introduction of CFString-based names.
    let name = if info.cfNameString.is_null() {
        // SAFETY: `info.name` is a NUL-terminated 52-byte ASCII
        // buffer per AU spec.
        let bytes: Vec<u8> = info
            .name
            .iter()
            .take_while(|&&c| c != 0)
            .map(|&c| c as u8)
            .collect();
        String::from_utf8_lossy(&bytes).into_owned()
    } else {
        // SAFETY: `info.cfNameString` is a +0 reference per AU
        // spec — we don't release it.
        let s = unsafe { &*info.cfNameString };
        s.to_string()
    };
    let unit_name = if info.unitName.is_null() {
        String::new()
    } else {
        let s = unsafe { &*info.unitName };
        s.to_string()
    };
    let flags = au_param_flags_to_rack(info);
    ParameterInfo {
        id,
        name: name.clone(),
        short_name: name,
        unit: unit_name,
        min: f64::from(info.minValue),
        max: f64::from(info.maxValue),
        default: f64::from(info.defaultValue),
        step_count: 0,
        flags,
    }
}

fn au_param_flags_to_rack(
    info: &AudioUnitParameterInfo,
) -> truce_rack_core::info::ParameterFlags {
    use objc2_audio_toolbox::AudioUnitParameterOptions as Opt;
    let mut flags = truce_rack_core::info::ParameterFlags::empty();
    // AU treats most parameters as automatable by default; only
    // explicit MeterReadOnly takes that away.
    if info.flags.contains(Opt::Flag_MeterReadOnly) {
        flags |= truce_rack_core::info::ParameterFlags::READ_ONLY;
    } else {
        flags |= truce_rack_core::info::ParameterFlags::AUTOMATABLE;
    }
    if info.flags.contains(Opt::Flag_OmitFromPresets) {
        flags |= truce_rack_core::info::ParameterFlags::HIDDEN;
    }
    flags
}

/// Query `kAudioUnitProperty_FactoryPresets` and return one
/// [`PresetInfo`] per preset. The property hands us a `CFArrayRef`
/// of `AUPreset` structs — we copy the fields out and release the
/// array.
unsafe fn fetch_factory_presets(unit: AudioComponentInstance) -> Option<Vec<PresetInfo>> {
    use objc2_core_foundation::{CFArray, CFRetained};
    let mut presets: *const CFArray = ptr::null();
    let mut size =
        u32::try_from(std::mem::size_of::<*const CFArray>()).unwrap_or(0);
    let status = unsafe {
        AudioUnitGetProperty(
            unit,
            kAudioUnitProperty_FactoryPresets,
            kAudioUnitScope_Global,
            0,
            ptr::NonNull::new_unchecked((&raw mut presets).cast()),
            ptr::NonNull::new_unchecked(&raw mut size),
        )
    };
    if status != 0 || presets.is_null() {
        return None;
    }
    // CFArrayRef from FactoryPresets is owned per Apple docs —
    // we wrap in CFRetained so it's released on drop.
    let array = unsafe {
        CFRetained::from_raw(ptr::NonNull::new_unchecked(presets.cast_mut()))
    };
    let count = array.count();
    if count <= 0 {
        return Some(Vec::new());
    }
    #[allow(clippy::cast_sign_loss)]
    let count_usize = count as usize;
    let mut out = Vec::with_capacity(count_usize);
    for i in 0..count {
        // CFArrayGetValueAtIndex returns *const c_void — points
        // at an `AUPreset` struct living inside the CFArray.
        let value = unsafe { array.value_at_index(i) };
        if value.is_null() {
            continue;
        }
        let preset = unsafe { &*value.cast::<AUPreset>() };
        let name = if preset.presetName.is_null() {
            format!("Preset {}", preset.presetNumber)
        } else {
            unsafe { &*preset.presetName }.to_string()
        };
        #[allow(clippy::cast_sign_loss)]
        out.push(PresetInfo {
            index: i as usize,
            name,
            preset_number: preset.presetNumber,
        });
    }
    Some(out)
}

/// Route one truce-rack [`truce_rack_core::events::Event`] into the AU via
/// `MusicDeviceMIDIEvent`. Non-MIDI events (param automation,
/// transport flags) are skipped — AU parameter automation goes
/// through `AudioUnitSetParameter` with a sample offset, and
/// transport flips are surfaced via the render-callback context.
fn send_midi(unit: AudioComponentInstance, event: &truce_rack_core::events::Event) {
    use truce_rack_core::events::{EventBody, MidiData};
    let offset = event.sample_offset;
    let (status, d1, d2) = match event.body {
        EventBody::Midi(MidiData::NoteOn { channel, note, velocity }) => (
            0x90 | u32::from(channel & 0x0F),
            u32::from(note & 0x7F),
            u32::from(velocity & 0x7F),
        ),
        EventBody::Midi(MidiData::NoteOff { channel, note, velocity }) => (
            0x80 | u32::from(channel & 0x0F),
            u32::from(note & 0x7F),
            u32::from(velocity & 0x7F),
        ),
        EventBody::Midi(MidiData::ControlChange { channel, controller, value }) => (
            0xB0 | u32::from(channel & 0x0F),
            u32::from(controller & 0x7F),
            u32::from(value & 0x7F),
        ),
        EventBody::Midi(MidiData::ProgramChange { channel, program }) => {
            (0xC0 | u32::from(channel & 0x0F), u32::from(program & 0x7F), 0)
        }
        EventBody::Midi(MidiData::ChannelAftertouch { channel, pressure }) => {
            (0xD0 | u32::from(channel & 0x0F), u32::from(pressure & 0x7F), 0)
        }
        EventBody::Midi(MidiData::PolyAftertouch { channel, note, pressure }) => (
            0xA0 | u32::from(channel & 0x0F),
            u32::from(note & 0x7F),
            u32::from(pressure & 0x7F),
        ),
        EventBody::Midi(MidiData::PitchBend { channel, value }) => (
            0xE0 | u32::from(channel & 0x0F),
            u32::from(value & 0x7F),
            u32::from((value >> 7) & 0x7F),
        ),
        EventBody::Midi(MidiData::Raw { len, data }) if len >= 1 => {
            let s = u32::from(data[0]);
            let d1 = if len >= 2 { u32::from(data[1]) } else { 0 };
            let d2 = if len >= 3 { u32::from(data[2]) } else { 0 };
            (s, d1, d2)
        }
        _ => return,
    };
    // Non-MusicDevice / non-MIDIFX components return
    // kAudioUnitErr_InvalidProperty here; ignore.
    let _ = unsafe { MusicDeviceMIDIEvent(unit, status, d1, d2, offset) };
}

/// Synchronous `AudioComponentInstanceNew` wrapper. Used for AU v2
/// and for AU v3 plugins that don't set `RequiresAsyncInstantiation`.
fn instantiate_sync(
    component: AudioComponent,
    info_path: &std::path::Path,
) -> Result<AudioComponentInstance> {
    let mut instance: AudioComponentInstance = ptr::null_mut();
    let status = unsafe {
        AudioComponentInstanceNew(component, ptr::NonNull::new_unchecked(&raw mut instance))
    };
    if status != 0 || instance.is_null() {
        return Err(Error::LoadFailed {
            path: info_path.to_path_buf(),
            reason: format!("AudioComponentInstanceNew failed with OSStatus {status}"),
        });
    }
    Ok(instance)
}

/// Asynchronous instantiation via `AudioComponentInstantiate`,
/// required for AU v3 plugins flagged `RequiresAsyncInstantiation`
/// (sandboxed App-Extension-hosted units). The completion block
/// can be dispatched from any thread; we pump the main thread's
/// `CFRunLoop` until the result lands or a 10-second deadline
/// elapses.
fn instantiate_async(
    component: AudioComponent,
    info_path: &std::path::Path,
) -> Result<AudioComponentInstance> {
    use std::sync::Arc;
    use std::sync::atomic::{AtomicBool, AtomicI32, AtomicPtr, Ordering};
    use std::time::{Duration, Instant};

    let done = Arc::new(AtomicBool::new(false));
    let inst_ptr: Arc<AtomicPtr<std::ffi::c_void>> =
        Arc::new(AtomicPtr::new(ptr::null_mut()));
    let status_atom = Arc::new(AtomicI32::new(0));

    let done_clone = Arc::clone(&done);
    let inst_clone = Arc::clone(&inst_ptr);
    let status_clone = Arc::clone(&status_atom);

    // RcBlock heap-allocates the block and refcounts via Apple's
    // blocks runtime; AudioComponentInstantiate retains it for the
    // duration of the call, releases after firing. As long as our
    // Arc holders outlive the block (which we ensure by holding
    // them until `done == true`), the closure's captures stay live.
    let block = block2::RcBlock::new(
        move |inst: AudioComponentInstance, status: i32| {
            inst_clone.store(inst.cast(), Ordering::Release);
            status_clone.store(status, Ordering::Release);
            done_clone.store(true, Ordering::Release);
        },
    );
    unsafe {
        AudioComponentInstantiate(
            component,
            AudioComponentInstantiationOptions::empty(),
            &block,
        );
    }

    // Pump the main runloop in 50ms slices until the completion
    // block writes done=true. The runloop dispatches the block to
    // the main queue; without pumping, it'd never fire.
    let deadline = Instant::now() + Duration::from_secs(10);
    while !done.load(Ordering::Acquire) {
        if Instant::now() > deadline {
            return Err(Error::LoadFailed {
                path: info_path.to_path_buf(),
                reason: "AudioComponentInstantiate timed out after 10s".into(),
            });
        }
        let mode = unsafe { objc2_core_foundation::kCFRunLoopDefaultMode };
        objc2_core_foundation::CFRunLoop::run_in_mode(mode, 0.05, true);
    }

    let status = status_atom.load(Ordering::Acquire);
    let raw = inst_ptr.load(Ordering::Acquire);
    if status != 0 || raw.is_null() {
        return Err(Error::LoadFailed {
            path: info_path.to_path_buf(),
            reason: format!("AudioComponentInstantiate failed with OSStatus {status}"),
        });
    }
    Ok(raw.cast::<objc2_audio_toolbox::OpaqueAudioComponentInstance>())
}

fn type_category(type_code: u32) -> PluginCategory {
    match type_code {
        t if t == kAudioUnitType_MusicDevice => PluginCategory::Instrument,
        t if t == kAudioUnitType_MIDIProcessor => PluginCategory::NoteEffect,
        t if t == kAudioUnitType_Mixer => PluginCategory::Tool,
        _ => PluginCategory::Effect,
    }
}

/// AU `AudioComponentCopyName` returns `"<vendor>: <name>"`. Split
/// it so the host's browser shows them in separate columns.
fn split_name(full: &str) -> (String, String) {
    full.split_once(": ").map_or_else(
        || (String::new(), full.to_string()),
        |(v, n)| (v.to_string(), n.to_string()),
    )
}

unsafe fn component_name(component: AudioComponent) -> String {
    let mut cf_str: *const CFString = ptr::null();
    let status =
        unsafe { AudioComponentCopyName(component, ptr::NonNull::new_unchecked(&raw mut cf_str)) };
    if status != 0 || cf_str.is_null() {
        return String::new();
    }
    // SAFETY: AudioComponentCopyName follows the "Copy" rule —
    // we own the +1 retain on cf_str. Wrap it in CFRetained so it
    // gets released on drop.
    let retained = unsafe {
        objc2_core_foundation::CFRetained::from_raw(ptr::NonNull::new_unchecked(cf_str.cast_mut()))
    };
    retained.to_string()
}

unsafe fn component_version(component: AudioComponent) -> u32 {
    let mut version: u32 = 0;
    let _ = unsafe {
        objc2_audio_toolbox::AudioComponentGetVersion(
            component,
            ptr::NonNull::new_unchecked(&raw mut version),
        )
    };
    version
}

fn format_unique_id(desc: &AudioComponentDescription) -> String {
    format!(
        "{}:{}:{}",
        four_cc(desc.componentType),
        four_cc(desc.componentSubType),
        four_cc(desc.componentManufacturer),
    )
}

/// Render a four-CC as its printable ASCII representation if all
/// bytes are printable, otherwise its hex. AU manufacturer codes
/// are conventionally ASCII (`"appl"`, `"vlse"`, …).
fn four_cc(code: u32) -> String {
    let bytes = code.to_be_bytes();
    if bytes.iter().all(|b| b.is_ascii_graphic() && *b != b':') {
        String::from_utf8_lossy(&bytes).into_owned()
    } else {
        format!("{code:08x}")
    }
}

/// Parse a `"type:sub:mfr"` unique id back into a description for
/// `AudioComponentFindNext`.
fn parse_unique_id(id: &str) -> Option<AudioComponentDescription> {
    let mut parts = id.split(':');
    let t = parse_four_cc(parts.next()?)?;
    let s = parse_four_cc(parts.next()?)?;
    let m = parse_four_cc(parts.next()?)?;
    Some(AudioComponentDescription {
        componentType: t,
        componentSubType: s,
        componentManufacturer: m,
        componentFlags: 0,
        componentFlagsMask: 0,
    })
}

fn parse_four_cc(s: &str) -> Option<u32> {
    if s.len() == 4 && s.bytes().all(|b| b.is_ascii_graphic()) {
        let b = s.as_bytes();
        Some(u32::from_be_bytes([b[0], b[1], b[2], b[3]]))
    } else if s.len() == 8 {
        u32::from_str_radix(s, 16).ok()
    } else {
        None
    }
}

/// One loaded AU v2 instance. Holds the `AudioComponentInstance`
/// returned by `AudioComponentInstanceNew`. Disposes on `Drop`.
pub struct AuPlugin {
    info: PluginInfo,
    layouts: Vec<BusLayout>,
    active_layout: Option<BusLayout>,
    instance: AudioComponentInstance,
    /// Parameter id list, cached at load. AU exposes parameters
    /// by `AudioUnitParameterID` (u32); truce-rack-core's trait surface
    /// uses zero-based indices. This vec is the index → id map.
    param_ids: Vec<AudioUnitParameterID>,
    /// Stable-address render plumbing — the AU calls our render
    /// callback during `AudioUnitRender` with this Box's pointer as
    /// `inRefCon`. Kept boxed so its address survives moves of
    /// `AuPlugin` itself.
    render_ctx: Option<Box<RenderContext>>,
    /// Storage for the variable-length output `AudioBufferList`
    /// we pass to `AudioUnitRender`. Re-sized on activate based on
    /// channel count.
    output_buffer_storage: Vec<u8>,
    /// Running output sample counter for `AudioTimeStamp.mSampleTime`.
    sample_time: f64,
    /// Bus channel counts as set during activate (effects need
    /// matching input/output; instruments may have 0 input).
    input_channels: u32,
    output_channels: u32,
    /// Live editor state, populated by `PluginEditor::open`. The
    /// view-factory object (`AUCocoaUIBase`) and the resulting
    /// `NSView` are kept alive here; dropping the plugin tears them
    /// down via objc2's automatic retain/release.
    editor: AuEditorState,
}

/// Per-plugin Cocoa-view editor state. Lives on the main thread
/// only — never touched from the audio thread.
#[derive(Default)]
struct AuEditorState {
    factory: Option<objc2::rc::Retained<objc2::runtime::AnyObject>>,
    view: Option<objc2::rc::Retained<objc2_app_kit::NSView>>,
}

/// Audio-thread state shared with the AU render callback. The
/// callback fills the AU's input buffer from `input_planes` —
/// `process()` refreshes these pointers each block.
struct RenderContext {
    /// One pointer per channel into the host's input data for the
    /// current block. NULL when the plugin doesn't take input
    /// (instrument case).
    input_planes: Vec<*const f32>,
    /// Frames per block — must match `inNumberFrames` the AU
    /// passes to the callback. We assert and zero-fill on mismatch.
    frames: usize,
}

unsafe extern "C-unwind" fn render_input_callback(
    in_ref_con: std::ptr::NonNull<std::ffi::c_void>,
    _io_action_flags: std::ptr::NonNull<AudioUnitRenderActionFlags>,
    _in_time_stamp: std::ptr::NonNull<AudioTimeStamp>,
    _in_bus_number: u32,
    in_number_frames: u32,
    io_data: *mut AudioBufferList,
) -> i32 {
    if io_data.is_null() {
        return -1;
    }
    let ctx_ptr = in_ref_con.as_ptr().cast::<RenderContext>();
    let ctx = unsafe { &*ctx_ptr };
    let list = unsafe { &mut *io_data };
    let frames = in_number_frames as usize;
    let buffer_count = list.mNumberBuffers as usize;
    // The variable-length tail is reached via mBuffers as a [_; 1]
    // declared slice we extend via pointer arithmetic.
    let buffers_ptr: *mut CAAudioBuffer = list.mBuffers.as_mut_ptr();
    let bytes_u32 = u32::try_from(frames * std::mem::size_of::<f32>()).unwrap_or(0);
    let bytes_usize = bytes_u32 as usize;
    for i in 0..buffer_count {
        let buffer = unsafe { &mut *buffers_ptr.add(i) };
        buffer.mDataByteSize = bytes_u32;
        if let Some(&plane) = ctx.input_planes.get(i)
            && !plane.is_null()
            && frames <= ctx.frames
        {
            buffer.mData = plane.cast::<std::ffi::c_void>().cast_mut();
            continue;
        }
        // No input available for this channel — leave mData where
        // the caller put it and zero whatever bytes were there.
        if !buffer.mData.is_null() {
            unsafe {
                std::ptr::write_bytes(buffer.mData.cast::<u8>(), 0, bytes_usize);
            }
        }
    }
    0
}

// SAFETY: AU v2 instances are not movable across audio rendering
// threads while active, but we serialize all access through
// `&mut self` and never share the pointer.
unsafe impl Send for AuPlugin {}

impl AuPlugin {
    fn load_from(info: &PluginInfo) -> Result<Self> {
        let mut desc = parse_unique_id(&info.unique_id).ok_or_else(|| Error::LoadFailed {
            path: info.path.clone(),
            reason: format!("could not parse AU unique_id {:?}", info.unique_id),
        })?;
        let component = unsafe {
            AudioComponentFindNext(ptr::null_mut(), ptr::NonNull::new_unchecked(&raw mut desc))
        };
        if component.is_null() {
            return Err(Error::LoadFailed {
                path: info.path.clone(),
                reason: format!("no AU matching {}", info.unique_id),
            });
        }

        // Re-query the component's actual flags so we can detect
        // RequiresAsyncInstantiation — `desc` only carries our scan
        // mask, not the component's own.
        let mut comp_desc = AudioComponentDescription {
            componentType: 0,
            componentSubType: 0,
            componentManufacturer: 0,
            componentFlags: 0,
            componentFlagsMask: 0,
        };
        let _ = unsafe {
            objc2_audio_toolbox::AudioComponentGetDescription(
                component,
                ptr::NonNull::new_unchecked(&raw mut comp_desc),
            )
        };
        let flags = AudioComponentFlags::from_bits_retain(comp_desc.componentFlags);
        let needs_async = flags.contains(AudioComponentFlags::RequiresAsyncInstantiation);

        let instance = if needs_async {
            instantiate_async(component, &info.path)?
        } else {
            instantiate_sync(component, &info.path)?
        };
        let param_ids = unsafe { fetch_parameter_ids(instance) };
        let has_editor = unsafe { has_cocoa_ui(instance) };
        let mut updated = info.clone();
        updated.has_editor = has_editor;
        Ok(Self {
            info: updated,
            layouts: vec![BusLayout::stereo()],
            active_layout: None,
            instance,
            param_ids,
            render_ctx: None,
            output_buffer_storage: Vec::new(),
            sample_time: 0.0,
            input_channels: 0,
            output_channels: 0,
            editor: AuEditorState::default(),
        })
    }
}

/// True if the AU advertises an `AUv2` Cocoa view (one or more
/// classes implementing `AUCocoaUIBase`).
unsafe fn has_cocoa_ui(unit: AudioComponentInstance) -> bool {
    use objc2_audio_toolbox::kAudioUnitProperty_CocoaUI;
    let mut size: u32 = 0;
    let info_status = unsafe {
        AudioUnitGetPropertyInfo(
            unit,
            kAudioUnitProperty_CocoaUI,
            kAudioUnitScope_Global,
            0,
            &raw mut size,
            ptr::null_mut(),
        )
    };
    info_status == 0 && size as usize >= std::mem::size_of::<usize>() * 2
}

/// Allocate enough bytes to hold an `AudioBufferList` with
/// `n_buffers` `AudioBuffer`s tail-included. Returns a `Vec<u8>`
/// large enough; the caller casts the data pointer.
fn alloc_audio_buffer_list(n_buffers: usize) -> Vec<u8> {
    let n = n_buffers.max(1);
    let size = std::mem::size_of::<AudioBufferList>()
        + (n - 1) * std::mem::size_of::<CAAudioBuffer>();
    vec![0u8; size]
}

/// Query `kAudioUnitProperty_ParameterList` and return the
/// parameter ids in declaration order. Returns an empty vec on
/// any failure — a plugin with zero parameters is valid.
unsafe fn fetch_parameter_ids(unit: AudioComponentInstance) -> Vec<AudioUnitParameterID> {
    let mut size: u32 = 0;
    let info_status = unsafe {
        AudioUnitGetPropertyInfo(
            unit,
            kAudioUnitProperty_ParameterList,
            kAudioUnitScope_Global,
            0,
            &raw mut size,
            ptr::null_mut(),
        )
    };
    if info_status != 0 || size == 0 {
        return Vec::new();
    }
    let count = (size as usize) / std::mem::size_of::<AudioUnitParameterID>();
    let mut ids: Vec<AudioUnitParameterID> = vec![0; count];
    let mut io_size = size;
    let get_status = unsafe {
        AudioUnitGetProperty(
            unit,
            kAudioUnitProperty_ParameterList,
            kAudioUnitScope_Global,
            0,
            ptr::NonNull::new_unchecked(ids.as_mut_ptr().cast()),
            ptr::NonNull::new_unchecked(&raw mut io_size),
        )
    };
    if get_status != 0 {
        return Vec::new();
    }
    ids.truncate(io_size as usize / std::mem::size_of::<AudioUnitParameterID>());
    ids
}

impl Drop for AuPlugin {
    fn drop(&mut self) {
        if !self.instance.is_null() {
            unsafe { AudioComponentInstanceDispose(self.instance) };
        }
    }
}

impl PluginCore for AuPlugin {
    fn info(&self) -> &PluginInfo {
        &self.info
    }
    fn active_layout(&self) -> Option<&BusLayout> {
        self.active_layout.as_ref()
    }
    fn supported_layouts(&self) -> &[BusLayout] {
        &self.layouts
    }
    fn parameter_count(&self) -> usize {
        self.param_ids.len()
    }

    fn parameter_info(&self, index: usize) -> Result<ParameterInfo> {
        let id = *self
            .param_ids
            .get(index)
            .ok_or(Error::InvalidParameter(index))?;
        let info = unsafe { fetch_parameter_info(self.instance, id)? };
        Ok(au_parameter_to_rack(id, &info))
    }

    fn parameter_value(&self, index: usize) -> Result<f64> {
        let id = *self
            .param_ids
            .get(index)
            .ok_or(Error::InvalidParameter(index))?;
        let mut value: f32 = 0.0;
        let status = unsafe {
            AudioUnitGetParameter(
                self.instance,
                id,
                kAudioUnitScope_Global,
                0,
                ptr::NonNull::new_unchecked(&raw mut value),
            )
        };
        if status != 0 {
            return Err(Error::Other(format!(
                "AudioUnitGetParameter failed: OSStatus {status}"
            )));
        }
        Ok(f64::from(value))
    }

    fn parameter_value_string(&self, _index: usize, _value: f64) -> Result<String> {
        // AU exposes a parameter-value-to-string property via
        // kAudioUnitProperty_ParameterValueStrings (for indexed
        // params) and AUParameterStringFromValue (for everything
        // else). Neither maps cleanly to the truce-rack-core surface
        // yet; tracked as a follow-on. For now, fall back to the
        // unit-stripped numeric form so callers get *something*
        // they can display.
        Err(Error::Other(
            "au parameter_value_string not yet wired".into(),
        ))
    }

    fn set_parameter(&mut self, index: usize, value: f64) -> Result<()> {
        let id = *self
            .param_ids
            .get(index)
            .ok_or(Error::InvalidParameter(index))?;
        #[allow(clippy::cast_possible_truncation)]
        let v = value as f32;
        let status = unsafe {
            AudioUnitSetParameter(self.instance, id, kAudioUnitScope_Global, 0, v, 0)
        };
        if status != 0 {
            return Err(Error::Other(format!(
                "AudioUnitSetParameter failed: OSStatus {status}"
            )));
        }
        Ok(())
    }
    fn preset_count(&self) -> usize {
        unsafe { fetch_factory_presets(self.instance) }.map_or(0, |v| v.len())
    }

    fn preset_info(&self, index: usize) -> Result<PresetInfo> {
        let presets = unsafe { fetch_factory_presets(self.instance) }
            .ok_or_else(|| Error::Other("kAudioUnitProperty_FactoryPresets failed".into()))?;
        let preset = presets
            .get(index)
            .ok_or(Error::InvalidParameter(index))?;
        Ok(preset.clone())
    }

    fn load_preset(&mut self, preset_number: i32) -> Result<()> {
        let preset = AUPreset {
            presetNumber: preset_number,
            presetName: ptr::null(),
        };
        let status = unsafe {
            AudioUnitSetProperty(
                self.instance,
                kAudioUnitProperty_PresentPreset,
                kAudioUnitScope_Global,
                0,
                (&raw const preset).cast(),
                u32::try_from(std::mem::size_of::<AUPreset>()).unwrap_or(0),
            )
        };
        if status != 0 {
            return Err(Error::Other(format!(
                "AudioUnitSetProperty(PresentPreset) failed: OSStatus {status}"
            )));
        }
        Ok(())
    }
    fn save_state(&self) -> Result<Vec<u8>> {
        use objc2_core_foundation::{
            CFData, CFPropertyList, CFPropertyListCreateData, CFPropertyListFormat, CFRetained,
        };
        let mut class_info: *const CFPropertyList = ptr::null();
        let mut size = u32::try_from(std::mem::size_of::<*const CFPropertyList>())
            .unwrap_or(0);
        let status = unsafe {
            AudioUnitGetProperty(
                self.instance,
                kAudioUnitProperty_ClassInfo,
                kAudioUnitScope_Global,
                0,
                ptr::NonNull::new_unchecked((&raw mut class_info).cast()),
                ptr::NonNull::new_unchecked(&raw mut size),
            )
        };
        if status != 0 || class_info.is_null() {
            return Err(Error::Other(format!(
                "kAudioUnitProperty_ClassInfo failed: OSStatus {status}"
            )));
        }
        let plist = unsafe {
            CFRetained::<CFPropertyList>::from_raw(ptr::NonNull::new_unchecked(
                class_info.cast_mut(),
            ))
        };
        let mut error: *mut objc2_core_foundation::CFError = ptr::null_mut();
        let data: Option<CFRetained<CFData>> = unsafe {
            CFPropertyListCreateData(
                None,
                Some(&plist),
                CFPropertyListFormat::BinaryFormat_v1_0,
                0,
                &raw mut error,
            )
        };
        let data = data.ok_or_else(|| {
            Error::Other("CFPropertyListCreateData returned null".into())
        })?;
        let len = data.length();
        if len < 0 {
            return Err(Error::Other("CFData length negative".into()));
        }
        #[allow(clippy::cast_sign_loss)]
        let len_usize = len as usize;
        let mut out = vec![0u8; len_usize];
        unsafe {
            data.bytes(
                objc2_core_foundation::CFRange {
                    location: 0,
                    length: len,
                },
                out.as_mut_ptr(),
            );
        }
        Ok(out)
    }

    fn load_state(&mut self, bytes: &[u8]) -> Result<()> {
        use objc2_core_foundation::{
            CFData, CFPropertyList, CFPropertyListCreateWithData, CFPropertyListFormat,
            CFRetained,
        };
        if bytes.is_empty() {
            return Err(Error::Other("empty AU state".into()));
        }
        #[allow(clippy::cast_possible_wrap)]
        let cf_data: CFRetained<CFData> = unsafe {
            CFData::new(None, bytes.as_ptr(), bytes.len() as isize)
        }
        .ok_or_else(|| Error::Other("CFData::new returned null".into()))?;
        let mut error: *mut objc2_core_foundation::CFError = ptr::null_mut();
        let mut format = CFPropertyListFormat::BinaryFormat_v1_0;
        let plist: Option<CFRetained<CFPropertyList>> = unsafe {
            CFPropertyListCreateWithData(None, Some(&cf_data), 0, &raw mut format, &raw mut error)
        };
        let plist = plist.ok_or_else(|| {
            Error::Other("CFPropertyListCreateWithData returned null".into())
        })?;
        let plist_ptr: *const CFPropertyList = CFRetained::as_ptr(&plist).as_ptr().cast_const();
        let status = unsafe {
            AudioUnitSetProperty(
                self.instance,
                kAudioUnitProperty_ClassInfo,
                kAudioUnitScope_Global,
                0,
                (&raw const plist_ptr).cast(),
                u32::try_from(std::mem::size_of::<*const CFPropertyList>()).unwrap_or(0),
            )
        };
        if status != 0 {
            return Err(Error::Other(format!(
                "AudioUnitSetProperty(ClassInfo) failed: OSStatus {status}"
            )));
        }
        Ok(())
    }
    fn activate(
        &mut self,
        layout: BusLayout,
        sample_rate: f64,
        max_block_size: usize,
    ) -> Result<()> {
        // Match legacy rack 0.4's sequence: set planar f32 stream
        // format on both scopes (effects need input, instruments
        // don't), set MaximumFramesPerSlice, then
        // AudioUnitInitialize. Sample-rate-dependent parameter
        // ranges (e.g. Apple AUBandpass's Center Frequency cap at
        // Nyquist) only become correct after this point — we
        // refresh the parameter id cache as the last step.
        let channels: u32 = 2;
        #[allow(clippy::cast_possible_truncation)]
        let format = AudioStreamBasicDescription {
            mSampleRate: sample_rate,
            mFormatID: kAudioFormatLinearPCM,
            mFormatFlags: kAudioFormatFlagIsFloat
                | kAudioFormatFlagIsPacked
                | kAudioFormatFlagIsNonInterleaved,
            mBytesPerPacket: 4,
            mFramesPerPacket: 1,
            mBytesPerFrame: 4,
            mChannelsPerFrame: channels,
            mBitsPerChannel: 32,
            mReserved: 0,
        };
        let _ = unsafe {
            AudioUnitSetProperty(
                self.instance,
                kAudioUnitProperty_StreamFormat,
                kAudioUnitScope_Input,
                0,
                (&raw const format).cast(),
                u32::try_from(std::mem::size_of::<AudioStreamBasicDescription>()).unwrap_or(0),
            )
        };
        let _ = unsafe {
            AudioUnitSetProperty(
                self.instance,
                kAudioUnitProperty_StreamFormat,
                kAudioUnitScope_Output,
                0,
                (&raw const format).cast(),
                u32::try_from(std::mem::size_of::<AudioStreamBasicDescription>()).unwrap_or(0),
            )
        };
        let max_frames = u32::try_from(max_block_size).unwrap_or(u32::MAX);
        let _ = unsafe {
            AudioUnitSetProperty(
                self.instance,
                kAudioUnitProperty_MaximumFramesPerSlice,
                kAudioUnitScope_Global,
                0,
                (&raw const max_frames).cast(),
                u32::try_from(std::mem::size_of::<u32>()).unwrap_or(0),
            )
        };
        // Set the render callback before AudioUnitInitialize so
        // the AU sees its input source from frame 0.
        let mut render_ctx = Box::new(RenderContext {
            input_planes: vec![ptr::null(); channels as usize],
            frames: max_block_size,
        });
        let render_ctx_ptr: *mut RenderContext = render_ctx.as_mut();
        let callback_struct = AURenderCallbackStruct {
            inputProc: Some(render_input_callback),
            inputProcRefCon: render_ctx_ptr.cast(),
        };
        // Best-effort: instruments don't accept SetRenderCallback
        // on the input scope. We ignore the OSStatus here for
        // exactly the legacy reasons.
        let _ = unsafe {
            AudioUnitSetProperty(
                self.instance,
                kAudioUnitProperty_SetRenderCallback,
                kAudioUnitScope_Input,
                0,
                (&raw const callback_struct).cast(),
                u32::try_from(std::mem::size_of::<AURenderCallbackStruct>()).unwrap_or(0),
            )
        };

        let status = unsafe { AudioUnitInitialize(self.instance) };
        if status != 0 {
            return Err(Error::Other(format!(
                "AudioUnitInitialize failed: OSStatus {status}"
            )));
        }

        // Refresh the parameter id cache now that initialize has
        // settled the AU's sample-rate-dependent state.
        self.param_ids = unsafe { fetch_parameter_ids(self.instance) };
        self.render_ctx = Some(render_ctx);
        self.output_buffer_storage = alloc_audio_buffer_list(channels as usize);
        self.input_channels = channels;
        self.output_channels = channels;
        self.sample_time = 0.0;
        self.active_layout = Some(layout);
        Ok(())
    }
    fn deactivate(&mut self) {
        if self.is_active() {
            let _ = unsafe { AudioUnitUninitialize(self.instance) };
        }
        self.render_ctx = None;
        self.active_layout = None;
    }
    fn is_active(&self) -> bool {
        self.active_layout.is_some()
    }

    fn editor(&mut self) -> Option<&mut dyn truce_rack_core::editor::PluginEditor> {
        if self.info.has_editor {
            Some(self)
        } else {
            None
        }
    }
}

impl truce_rack_core::editor::PluginEditor for AuPlugin {
    fn open(&mut self, parent: truce_rack_core::editor::WindowHandle, _scale: f64) -> Result<()> {
        use truce_rack_core::editor::WindowHandle;
        let WindowHandle::NSView(parent_ptr) = parent else {
            return Err(Error::Other("AU editor requires an NSView parent".into()));
        };
        if parent_ptr.is_null() {
            return Err(Error::Other("AU editor: parent NSView is null".into()));
        }
        if self.editor.view.is_some() {
            return Ok(());
        }
        unsafe { open_cocoa_editor(self, parent_ptr) }
    }

    fn close(&mut self) {
        if let Some(view) = self.editor.view.take() {
            unsafe { remove_from_superview(&view) };
        }
        self.editor.factory = None;
    }

    fn is_open(&self) -> bool {
        self.editor.view.is_some()
    }

    fn size(&self) -> Option<(u32, u32)> {
        let view = self.editor.view.as_ref()?;
        let frame = unsafe { view_frame(view) };
        #[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
        Some((frame.2.max(0.0) as u32, frame.3.max(0.0) as u32))
    }

    fn is_resizable(&self) -> bool {
        // AUv2 Cocoa views are typically fixed-size: the factory
        // returns an NSView with a baked layout. Resizing is
        // accepted by setFrame: but most hosts treat AUv2 views as
        // non-resizable. We mirror that.
        false
    }

    fn set_size(&mut self, width: u32, height: u32) -> Option<(u32, u32)> {
        let view = self.editor.view.as_ref()?;
        unsafe { set_view_frame(view, f64::from(width), f64::from(height)) };
        Some((width, height))
    }

    fn show(&mut self) {
        if let Some(view) = self.editor.view.as_ref() {
            unsafe { set_hidden(view, false) };
        }
    }

    fn hide(&mut self) {
        if let Some(view) = self.editor.view.as_ref() {
            unsafe { set_hidden(view, true) };
        }
    }
}

/// Signature of `[AUCocoaUIBase uiViewForAudioUnit:withSize:]`.
/// Used to dispatch directly through the method's raw IMP, side-
/// stepping objc2's encoding check (Apple's AUs use one type
/// encoding for the `AudioUnit` argument, third-party AUs use the
/// older Carbon-era one — same ABI, but objc2 panics on mismatch).
type UiViewFn = unsafe extern "C-unwind" fn(
    *mut objc2::runtime::AnyObject,
    objc2::runtime::Sel,
    *mut std::ffi::c_void,
    objc2_foundation::NSSize,
) -> *mut objc2_app_kit::NSView;

/// Fetch the AU's Cocoa view factory descriptor, load the bundle,
/// instantiate the factory, ask it for an `NSView`, and add it as a
/// subview of `parent_ptr`. Stores the live factory + view on
/// `self.editor` so they survive `open` returning.
unsafe fn open_cocoa_editor(plugin: &mut AuPlugin, parent_ptr: *mut std::ffi::c_void) -> Result<()> {
    use objc2::msg_send;
    use objc2::runtime::AnyObject;
    use objc2_audio_toolbox::{AudioUnitCocoaViewInfo, kAudioUnitProperty_CocoaUI};
    use objc2_core_foundation::{CFRetained, CFString, CFURL};

    let mut size: u32 = 0;
    let info_status = unsafe {
        AudioUnitGetPropertyInfo(
            plugin.instance,
            kAudioUnitProperty_CocoaUI,
            kAudioUnitScope_Global,
            0,
            &raw mut size,
            ptr::null_mut(),
        )
    };
    if info_status != 0 || (size as usize) < std::mem::size_of::<AudioUnitCocoaViewInfo>() {
        return Err(Error::Other(format!(
            "kAudioUnitProperty_CocoaUI not advertised (status {info_status}, size {size})"
        )));
    }
    // The CocoaUI struct is variable-length: 1 bundle URL +
    // N class names. We get the static-sized struct here and only
    // use the first class name (mCocoaAUViewClass[0]) — the
    // overwhelming majority of AUs publish exactly one view class.
    let mut storage = vec![0u8; size as usize];
    let mut io_size = size;
    let get_status = unsafe {
        AudioUnitGetProperty(
            plugin.instance,
            kAudioUnitProperty_CocoaUI,
            kAudioUnitScope_Global,
            0,
            ptr::NonNull::new_unchecked(storage.as_mut_ptr().cast()),
            ptr::NonNull::new_unchecked(&raw mut io_size),
        )
    };
    if get_status != 0 {
        return Err(Error::Other(format!(
            "kAudioUnitProperty_CocoaUI get failed: OSStatus {get_status}"
        )));
    }
    // SAFETY: The first sizeof(AudioUnitCocoaViewInfo) bytes form
    // the descriptor; we've verified size at the property-info
    // step. The pointers inside are +1 references owned by us —
    // we wrap them in CFRetained to release on drop.
    #[allow(clippy::cast_ptr_alignment)]
    let view_info = unsafe { &*storage.as_ptr().cast::<AudioUnitCocoaViewInfo>() };
    let bundle_url = unsafe { CFRetained::<CFURL>::from_raw(view_info.mCocoaAUViewBundleLocation) };
    let class_name = unsafe { CFRetained::<CFString>::from_raw(view_info.mCocoaAUViewClass[0]) };

    // CFURL ↔ NSURL and CFString ↔ NSString are toll-free
    // bridged, but objc2's msg_send! checks the type encoding —
    // raw CFType pointers register as struct pointers, not '@'.
    // Cast through the matching NSObject type so the encoding
    // matches `id` (and any future runtime type checks pass).
    // SAFETY: toll-free bridging guarantees identical layout.
    let bundle_url_ns: &objc2_foundation::NSURL = unsafe {
        &*CFRetained::as_ptr(&bundle_url).as_ptr().cast::<objc2_foundation::NSURL>()
    };
    let class_name_ns: &objc2_foundation::NSString = unsafe {
        &*CFRetained::as_ptr(&class_name).as_ptr().cast::<objc2_foundation::NSString>()
    };

    // [NSBundle bundleWithURL:url] → NSBundle*
    let ns_bundle_class = objc2::class!(NSBundle);
    let bundle: *mut AnyObject = unsafe {
        msg_send![ns_bundle_class, bundleWithURL: bundle_url_ns]
    };
    if bundle.is_null() {
        return Err(Error::Other("NSBundle bundleWithURL: returned nil".into()));
    }
    // [bundle classNamed:name] → Class
    let view_factory_class: *mut objc2::runtime::AnyClass = unsafe {
        msg_send![bundle, classNamed: class_name_ns]
    };
    if view_factory_class.is_null() {
        return Err(Error::Other(format!(
            "view factory class not found in bundle: {}",
            unsafe { (*CFRetained::as_ptr(&class_name).as_ptr()).to_string() }
        )));
    }
    // [[class alloc] init]
    let factory_alloc: *mut AnyObject = unsafe { msg_send![view_factory_class, alloc] };
    let factory: *mut AnyObject = unsafe { msg_send![factory_alloc, init] };
    if factory.is_null() {
        return Err(Error::Other("AU view factory init returned nil".into()));
    }
    let factory_retained = unsafe { objc2::rc::Retained::from_raw(factory) }
        .ok_or_else(|| Error::Other("could not retain AU view factory".into()))?;

    // Default preferred size — most factories ignore this and pick
    // their own.
    let pref = objc2_foundation::NSSize::new(0.0, 0.0);
    // `uiViewForAudioUnit:withSize:` was historically declared with
    // the Carbon-era `^{ComponentInstanceRecord=[1q]}` encoding;
    // Apple's own AU factories migrated to the newer
    // `^{OpaqueAudioComponentInstance=}` form. objc2's msg_send!
    // panics on encoding mismatch. Skip the check by dispatching
    // through the method's raw IMP directly — same ABI, no encoding
    // verification.
    let sel = objc2::sel!(uiViewForAudioUnit:withSize:);
    let factory_class = (*factory_retained).class();
    let Some(method) = factory_class.instance_method(sel) else {
        return Err(Error::Other(
            "AU view factory has no uiViewForAudioUnit:withSize: method".into(),
        ));
    };
    let imp = method.implementation();
    // SAFETY: AUv2 view factories all expose this exact signature;
    // we matched the selector above before transmuting the IMP.
    let typed: UiViewFn = unsafe { std::mem::transmute(imp) };
    let factory_obj: *mut objc2::runtime::AnyObject =
        objc2::rc::Retained::as_ptr(&factory_retained).cast_mut();
    let view_raw: *mut objc2_app_kit::NSView =
        unsafe { typed(factory_obj, sel, plugin.instance.cast(), pref) };
    if view_raw.is_null() {
        return Err(Error::Other(
            "uiViewForAudioUnit:withSize: returned nil".into(),
        ));
    }
    // The returned view is autoreleased; retain it.
    let view = unsafe { objc2::rc::Retained::retain(view_raw) }
        .ok_or_else(|| Error::Other("could not retain AU NSView".into()))?;

    // [parent addSubview:view]
    let parent_view: *mut objc2_app_kit::NSView = parent_ptr.cast();
    let _: () = unsafe { msg_send![parent_view, addSubview: &*view] };

    plugin.editor.factory = Some(factory_retained);
    plugin.editor.view = Some(view);
    Ok(())
}

unsafe fn remove_from_superview(view: &objc2_app_kit::NSView) {
    use objc2::msg_send;
    let _: () = unsafe { msg_send![view, removeFromSuperview] };
}

unsafe fn set_hidden(view: &objc2_app_kit::NSView, hidden: bool) {
    use objc2::msg_send;
    let flag: objc2::runtime::Bool = objc2::runtime::Bool::new(hidden);
    let _: () = unsafe { msg_send![view, setHidden: flag] };
}

unsafe fn view_frame(view: &objc2_app_kit::NSView) -> (f64, f64, f64, f64) {
    use objc2::msg_send;
    use objc2_foundation::NSRect;

    // Force any pending Auto Layout pass so the descendant frames
    // we're about to walk are post-layout. Apple's stock AUv2 views
    // (AULowpass, AUMultibandCompressor, …) only finalize their
    // child-view positions on the first layout pass.
    let _: () = unsafe { msg_send![view, layoutSubtreeIfNeeded] };

    let r: NSRect = unsafe { msg_send![view, frame] };

    // AUGenericView and friends report a `frame` that's smaller
    // than the union of their descendant subviews — macOS doesn't
    // clip children to a parent's bounds by default, so the visible
    // UI extends past the parent's frame. Walk the subtree and use
    // the bounding box of every descendant frame as the real
    // preferred size.
    let (extent_w, extent_h) = unsafe { subtree_extent(view) };
    let w = extent_w.max(r.size.width);
    let h = extent_h.max(r.size.height);
    (r.origin.x, r.origin.y, w, h)
}

/// Recursively walk `view`'s subviews, computing the bounding box
/// (in `view`'s coordinate system) of every descendant's frame.
/// Returns `(max_x, max_y)` — i.e. the max width/height the view
/// would need so no descendant overflows.
unsafe fn subtree_extent(view: &objc2_app_kit::NSView) -> (f64, f64) {
    use objc2::msg_send;
    use objc2_foundation::{NSArray, NSRect};

    let subviews: *mut NSArray<objc2_app_kit::NSView> =
        unsafe { msg_send![view, subviews] };
    if subviews.is_null() {
        return (0.0, 0.0);
    }
    let count: usize = unsafe { msg_send![subviews, count] };
    let mut max_x = 0.0_f64;
    let mut max_y = 0.0_f64;
    for i in 0..count {
        let sub: *mut objc2_app_kit::NSView =
            unsafe { msg_send![subviews, objectAtIndex: i] };
        if sub.is_null() {
            continue;
        }
        let frame: NSRect = unsafe { msg_send![sub, frame] };
        max_x = max_x.max(frame.origin.x + frame.size.width);
        max_y = max_y.max(frame.origin.y + frame.size.height);
        // Recurse into the descendant, translating its extent into
        // `view`'s coordinate space.
        let (sub_w, sub_h) = unsafe { subtree_extent(&*sub) };
        max_x = max_x.max(frame.origin.x + sub_w);
        max_y = max_y.max(frame.origin.y + sub_h);
    }
    (max_x, max_y)
}

unsafe fn set_view_frame(view: &objc2_app_kit::NSView, w: f64, h: f64) {
    use objc2::msg_send;
    use objc2_foundation::{NSPoint, NSRect, NSSize};
    let r = NSRect::new(NSPoint::new(0.0, 0.0), NSSize::new(w, h));
    let _: () = unsafe { msg_send![view, setFrame: r] };
}

impl Plugin<f32> for AuPlugin {
    fn process(
        &mut self,
        buffer: &mut AudioBuffer<'_, f32>,
        events: &EventList,
        _context: &mut ProcessContext<'_>,
    ) -> Result<ProcessStatus> {
        if !self.is_active() {
            return Err(Error::NotActivated);
        }
        let frames = buffer.num_frames();
        let frames_u32 = u32::try_from(frames).unwrap_or(u32::MAX);

        // Feed MIDI to the AU before AudioUnitRender so the
        // plugin sees the events at the correct sample offset
        // within this block. MusicDeviceMIDIEvent works for both
        // MusicDevice (instruments) and AUMIDIFX effects that
        // accept MIDI; for plain audio effects the call no-ops
        // with `kAudioUnitErr_InvalidProperty`, which we ignore.
        for event in events {
            send_midi(self.instance, event);
        }

        // Refresh the render context's input pointers so the
        // callback hands the AU the right plane addresses for this
        // block.
        let main_inputs = buffer.main_inputs();
        if let Some(ctx) = self.render_ctx.as_deref_mut() {
            ctx.frames = frames;
            ctx.input_planes.clear();
            for chan in main_inputs.iter().take(self.input_channels as usize) {
                ctx.input_planes.push(chan.as_ptr());
            }
            while ctx.input_planes.len() < self.input_channels as usize {
                ctx.input_planes.push(ptr::null());
            }
        }

        // Build the output AudioBufferList in-place over our
        // pre-allocated bytes. Each entry's `mData` points directly
        // at the corresponding truce-rack-core output channel.
        let main_outputs = buffer.main_outputs();
        // SAFETY: alloc_audio_buffer_list returned a Vec<u8>
        // sized to hold an AudioBufferList plus its variable
        // tail; the Vec is heap-allocated to 8-byte alignment via
        // the global allocator, which suffices for AudioBufferList.
        #[allow(clippy::cast_ptr_alignment)]
        let list_ptr = self.output_buffer_storage.as_mut_ptr().cast::<AudioBufferList>();
        unsafe {
            (*list_ptr).mNumberBuffers = self.output_channels;
            let buffers_start: *mut CAAudioBuffer = (*list_ptr).mBuffers.as_mut_ptr();
            for ch in 0..self.output_channels as usize {
                let buf = &mut *buffers_start.add(ch);
                buf.mNumberChannels = 1;
                buf.mDataByteSize =
                    u32::try_from(frames * std::mem::size_of::<f32>()).unwrap_or(0);
                buf.mData = main_outputs
                    .get_mut(ch)
                    .map_or(ptr::null_mut(), |c| c.as_mut_ptr().cast());
            }
        }

        let mut flags = AudioUnitRenderActionFlags::empty();
        let timestamp = AudioTimeStamp {
            mSampleTime: self.sample_time,
            mHostTime: 0,
            mRateScalar: 1.0,
            mWordClockTime: 0,
            mSMPTETime: objc2_core_audio_types::SMPTETime {
                mSubframes: 0,
                mSubframeDivisor: 0,
                mCounter: 0,
                mType: objc2_core_audio_types::SMPTETimeType(0),
                mFlags: objc2_core_audio_types::SMPTETimeFlags(0),
                mHours: 0,
                mMinutes: 0,
                mSeconds: 0,
                mFrames: 0,
            },
            mFlags: AudioTimeStampFlags::SampleTimeValid,
            mReserved: 0,
        };
        let status = unsafe {
            AudioUnitRender(
                self.instance,
                &raw mut flags,
                ptr::NonNull::new_unchecked((&raw const timestamp).cast_mut()),
                0, // output bus 0
                frames_u32,
                ptr::NonNull::new_unchecked(list_ptr),
            )
        };
        #[allow(clippy::cast_precision_loss)]
        let frames_f = frames as f64;
        self.sample_time += frames_f;
        if status != 0 {
            return Ok(ProcessStatus::Error);
        }
        Ok(ProcessStatus::Continue)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn four_cc_roundtrip() {
        let aufx = u32::from_be_bytes(*b"aufx");
        assert_eq!(four_cc(aufx), "aufx");
        assert_eq!(parse_four_cc("aufx"), Some(aufx));
    }

    #[test]
    fn unique_id_roundtrip() {
        let desc = AudioComponentDescription {
            componentType: u32::from_be_bytes(*b"aufx"),
            componentSubType: u32::from_be_bytes(*b"dely"),
            componentManufacturer: u32::from_be_bytes(*b"appl"),
            componentFlags: 0,
            componentFlagsMask: 0,
        };
        let id = format_unique_id(&desc);
        assert_eq!(id, "aufx:dely:appl");
        let parsed = parse_unique_id(&id).unwrap();
        assert_eq!(parsed.componentType, desc.componentType);
        assert_eq!(parsed.componentSubType, desc.componentSubType);
        assert_eq!(parsed.componentManufacturer, desc.componentManufacturer);
    }
}