rack 0.4.8

A modern Rust library for hosting audio plugins
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
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
use crate::{Error, MidiEvent, MidiEventKind, ParameterInfo, PluginInfo, PluginInstance, PresetInfo, Result};
use smallvec::SmallVec;
use std::ffi::CString;
use std::marker::PhantomData;
use std::ptr::NonNull;

use super::ffi;
use super::util::map_error;

/// An instantiated AudioUnit plugin
///
/// # Thread Safety
///
/// This type is `Send` but not `Sync`:
/// - `Send`: The plugin can be moved between threads safely
/// - NOT `Sync`: Multiple threads should not access the plugin simultaneously
///   without synchronization. Wrap in `Arc<Mutex<>>` if shared access is needed.
pub struct AudioUnitPlugin {
    inner: NonNull<ffi::RackAUPlugin>,
    info: PluginInfo,
    // Pre-allocated pointer arrays for zero-allocation process() calls
    input_ptrs: Vec<*const f32>,
    output_ptrs: Vec<*mut f32>,
    // Channel configuration (queried from AudioUnit during initialize)
    input_channels: usize,
    output_channels: usize,
    // PhantomData<*const ()> makes this type !Sync while keeping it Send
    _not_sync: PhantomData<*const ()>,
}

// Safety: AudioUnitPlugin can be sent between threads because:
// 1. Each plugin instance owns its C++ state exclusively
// 2. The plugin doesn't share mutable state with other instances
unsafe impl Send for AudioUnitPlugin {}

// Note: AudioUnitPlugin is NOT Sync due to PhantomData<*const ()>
// This is intentional - AudioUnit instances require synchronization for shared access

impl AudioUnitPlugin {
    /// Create a new AudioUnit plugin instance
    pub(crate) fn new(info: &PluginInfo) -> Result<Self> {
        unsafe {
            // Convert unique_id to CString
            let unique_id = CString::new(info.unique_id.as_str())
                .map_err(|_| Error::Other("Invalid unique_id (contains null byte)".to_string()))?;

            // Create plugin instance via FFI
            let ptr = ffi::rack_au_plugin_new(unique_id.as_ptr());
            if ptr.is_null() {
                return Err(Error::PluginNotFound(format!(
                    "Failed to create AudioUnit instance for {}",
                    info.name
                )));
            }

            Ok(Self {
                inner: NonNull::new_unchecked(ptr),
                info: info.clone(),
                input_ptrs: Vec::new(),
                output_ptrs: Vec::new(),
                input_channels: 0,
                output_channels: 0,
                _not_sync: PhantomData,
            })
        }
    }
}

impl PluginInstance for AudioUnitPlugin {
    fn initialize(&mut self, sample_rate: f64, max_block_size: usize) -> Result<()> {
        unsafe {
            let result = ffi::rack_au_plugin_initialize(
                self.inner.as_ptr(),
                sample_rate,
                max_block_size as u32,
            );

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            // Query actual channel configuration
            let input_channels = ffi::rack_au_plugin_get_input_channels(self.inner.as_ptr());
            let output_channels = ffi::rack_au_plugin_get_output_channels(self.inner.as_ptr());

            if input_channels < 0 || output_channels < 0 {
                return Err(Error::Other("Failed to query channel configuration".to_string()));
            }

            self.input_channels = input_channels as usize;
            self.output_channels = output_channels as usize;

            // Pre-allocate pointer arrays for zero-allocation process() calls
            // Reserve capacity to avoid reallocation even if channel counts are unusual
            self.input_ptrs = Vec::with_capacity(self.input_channels.max(8));
            self.output_ptrs = Vec::with_capacity(self.output_channels.max(8));

            // Initialize with null pointers (will be filled in process())
            self.input_ptrs.resize(self.input_channels, std::ptr::null());
            self.output_ptrs.resize(self.output_channels, std::ptr::null_mut());

            Ok(())
        }
    }

    fn reset(&mut self) -> Result<()> {
        unsafe {
            let result = ffi::rack_au_plugin_reset(self.inner.as_ptr());

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            Ok(())
        }
    }

    fn process(
        &mut self,
        inputs: &[&[f32]],
        outputs: &mut [&mut [f32]],
        num_frames: usize,
    ) -> Result<()> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        // Validate channel counts match plugin configuration
        if inputs.len() != self.input_channels {
            return Err(Error::Other(format!(
                "Input channel count mismatch: plugin expects {}, got {}",
                self.input_channels, inputs.len()
            )));
        }
        if outputs.len() != self.output_channels {
            return Err(Error::Other(format!(
                "Output channel count mismatch: plugin expects {}, got {}",
                self.output_channels, outputs.len()
            )));
        }

        // Validate inputs (channel counts are now guaranteed to be correct)
        if inputs.is_empty() || outputs.is_empty() {
            return Err(Error::Other("Empty input or output channels".to_string()));
        }

        // Validate all channels have the same length
        for (i, input) in inputs.iter().enumerate() {
            if input.len() < num_frames {
                return Err(Error::Other(format!(
                    "Input channel {} has {} samples, need at least {}",
                    i,
                    input.len(),
                    num_frames
                )));
            }
        }

        for (i, output) in outputs.iter().enumerate() {
            if output.len() < num_frames {
                return Err(Error::Other(format!(
                    "Output channel {} has {} samples, need at least {}",
                    i,
                    output.len(),
                    num_frames
                )));
            }
        }

        // Reuse pre-allocated pointer arrays (zero-allocation hot path)
        // Fill with current buffer pointers
        for (i, input_ch) in inputs.iter().enumerate() {
            self.input_ptrs[i] = input_ch.as_ptr();
        }
        for (i, output_ch) in outputs.iter_mut().enumerate() {
            self.output_ptrs[i] = output_ch.as_mut_ptr();
        }

        unsafe {
            let result = ffi::rack_au_plugin_process(
                self.inner.as_ptr(),
                self.input_ptrs.as_ptr(),
                inputs.len() as u32,
                self.output_ptrs.as_ptr(),
                outputs.len() as u32,
                num_frames as u32,
            );

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            Ok(())
        }
    }

    fn parameter_count(&self) -> usize {
        unsafe {
            let count = ffi::rack_au_plugin_parameter_count(self.inner.as_ptr());
            if count < 0 {
                0
            } else {
                count as usize
            }
        }
    }

    fn parameter_info(&self, index: usize) -> Result<ParameterInfo> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        unsafe {
            let mut name = vec![0i8; 256];
            let mut unit = vec![0i8; 32];
            let mut min = 0.0f32;
            let mut max = 0.0f32;
            let mut default_value = 0.0f32;

            let result = ffi::rack_au_plugin_parameter_info(
                self.inner.as_ptr(),
                index as u32,
                name.as_mut_ptr(),
                name.len(),
                &mut min,
                &mut max,
                &mut default_value,
                unit.as_mut_ptr(),
                unit.len(),
            );

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            // Convert name to String
            let name_cstr = std::ffi::CStr::from_ptr(name.as_ptr());
            let name_str = name_cstr
                .to_str()
                .map_err(|e| Error::Other(format!("Invalid UTF-8 in parameter name: {}", e)))?
                .to_string();

            // Convert unit to String
            let unit_cstr = std::ffi::CStr::from_ptr(unit.as_ptr());
            let unit_str = unit_cstr
                .to_str()
                .map_err(|e| Error::Other(format!("Invalid UTF-8 in parameter unit: {}", e)))?
                .to_string();

            Ok(ParameterInfo {
                index,
                name: name_str,
                min,
                max,
                default: default_value,
                unit: unit_str,
            })
        }
    }

    fn get_parameter(&self, index: usize) -> Result<f32> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        unsafe {
            let mut value = 0.0f32;
            let result =
                ffi::rack_au_plugin_get_parameter(self.inner.as_ptr(), index as u32, &mut value);

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            Ok(value)
        }
    }

    fn set_parameter(&mut self, index: usize, value: f32) -> Result<()> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        unsafe {
            let result =
                ffi::rack_au_plugin_set_parameter(self.inner.as_ptr(), index as u32, value);

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            Ok(())
        }
    }

    fn send_midi(&mut self, events: &[MidiEvent]) -> Result<()> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        // Convert Rust MIDI events to FFI events
        // Use SmallVec to avoid heap allocation for typical use cases (1-16 events)
        let ffi_events: SmallVec<[ffi::RackAUMidiEvent; 16]> = events
            .iter()
            .map(|event| {
                let (status, data1, data2, channel) = match event.kind {
                    MidiEventKind::NoteOn { note, velocity, channel } => {
                        (ffi::RackAUMidiEventType::NoteOn as u8, note, velocity, channel)
                    }
                    MidiEventKind::NoteOff { note, velocity, channel } => {
                        (ffi::RackAUMidiEventType::NoteOff as u8, note, velocity, channel)
                    }
                    MidiEventKind::PolyphonicAftertouch { note, pressure, channel } => {
                        (ffi::RackAUMidiEventType::PolyphonicAftertouch as u8, note, pressure, channel)
                    }
                    MidiEventKind::ControlChange { controller, value, channel } => {
                        (ffi::RackAUMidiEventType::ControlChange as u8, controller, value, channel)
                    }
                    MidiEventKind::ProgramChange { program, channel } => {
                        // Program Change only has 1 data byte (program number)
                        // data2 is 0 because MIDI Program Change messages don't use it
                        (ffi::RackAUMidiEventType::ProgramChange as u8, program, 0, channel)
                    }
                    MidiEventKind::ChannelAftertouch { pressure, channel } => {
                        // Channel Aftertouch only has 1 data byte (pressure value)
                        (ffi::RackAUMidiEventType::ChannelAftertouch as u8, pressure, 0, channel)
                    }
                    MidiEventKind::PitchBend { value, channel } => {
                        // Pitch bend uses 14-bit value split into two 7-bit bytes
                        // LSB (least significant 7 bits) in data1, MSB (most significant 7 bits) in data2
                        let lsb = (value & 0x7F) as u8;
                        let msb = ((value >> 7) & 0x7F) as u8;
                        (ffi::RackAUMidiEventType::PitchBend as u8, lsb, msb, channel)
                    }
                    // System Real-Time messages (no channel or data bytes)
                    MidiEventKind::TimingClock => {
                        (ffi::RackAUMidiEventType::TimingClock as u8, 0, 0, 0)
                    }
                    MidiEventKind::Start => {
                        (ffi::RackAUMidiEventType::Start as u8, 0, 0, 0)
                    }
                    MidiEventKind::Continue => {
                        (ffi::RackAUMidiEventType::Continue as u8, 0, 0, 0)
                    }
                    MidiEventKind::Stop => {
                        (ffi::RackAUMidiEventType::Stop as u8, 0, 0, 0)
                    }
                    MidiEventKind::ActiveSensing => {
                        (ffi::RackAUMidiEventType::ActiveSensing as u8, 0, 0, 0)
                    }
                    MidiEventKind::SystemReset => {
                        (ffi::RackAUMidiEventType::SystemReset as u8, 0, 0, 0)
                    }
                };

                ffi::RackAUMidiEvent {
                    sample_offset: event.sample_offset,
                    status,
                    data1,
                    data2,
                    channel,
                }
            })
            .collect();

        unsafe {
            let result = ffi::rack_au_plugin_send_midi(
                self.inner.as_ptr(),
                ffi_events.as_ptr(),
                ffi_events.len() as u32,
            );

            if result != ffi::RACK_AU_OK {
                let err = map_error(result);

                // If the plugin is an effect, provide more specific error context
                if matches!(self.info.plugin_type, crate::PluginType::Effect) {
                    return Err(Error::Other(format!(
                        "Effect plugin '{}' does not support MIDI (only instrument plugins typically respond to MIDI)",
                        self.info.name
                    )));
                }

                return Err(err);
            }

            Ok(())
        }
    }

    fn preset_count(&self) -> Result<usize> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        unsafe {
            let count = ffi::rack_au_plugin_get_preset_count(self.inner.as_ptr());
            if count < 0 {
                Ok(0)
            } else {
                Ok(count as usize)
            }
        }
    }

    fn preset_info(&self, index: usize) -> Result<PresetInfo> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        unsafe {
            let mut name = vec![0i8; 256];
            let mut preset_number: i32 = 0;

            let result = ffi::rack_au_plugin_get_preset_info(
                self.inner.as_ptr(),
                index as u32,
                name.as_mut_ptr(),
                name.len(),
                &mut preset_number,
            );

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            // Convert name to String
            let name_cstr = std::ffi::CStr::from_ptr(name.as_ptr());
            let name_str = name_cstr
                .to_str()
                .map_err(|e| Error::Other(format!("Invalid UTF-8 in preset name: {}", e)))?
                .to_string();

            Ok(PresetInfo {
                index,
                name: name_str,
                preset_number,
            })
        }
    }

    fn load_preset(&mut self, preset_number: i32) -> Result<()> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        unsafe {
            let result = ffi::rack_au_plugin_load_preset(self.inner.as_ptr(), preset_number);

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            Ok(())
        }
    }

    fn get_state(&self) -> Result<Vec<u8>> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        unsafe {
            // Get state size
            let size = ffi::rack_au_plugin_get_state_size(self.inner.as_ptr());
            if size <= 0 {
                return Err(Error::Other("Failed to get plugin state size".to_string()));
            }

            // Allocate buffer
            let mut data = vec![0u8; size as usize];
            let mut actual_size = data.len();

            // Get state data
            let result = ffi::rack_au_plugin_get_state(
                self.inner.as_ptr(),
                data.as_mut_ptr(),
                &mut actual_size,
            );

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            // Resize to actual size (in case it's smaller than allocated)
            data.resize(actual_size, 0);

            Ok(data)
        }
    }

    fn set_state(&mut self, data: &[u8]) -> Result<()> {
        if !self.is_initialized() {
            return Err(Error::NotInitialized);
        }

        if data.is_empty() {
            return Err(Error::Other("State data is empty".to_string()));
        }

        unsafe {
            let result = ffi::rack_au_plugin_set_state(
                self.inner.as_ptr(),
                data.as_ptr(),
                data.len(),
            );

            if result != ffi::RACK_AU_OK {
                return Err(map_error(result));
            }

            Ok(())
        }
    }

    fn info(&self) -> &PluginInfo {
        &self.info
    }

    fn is_initialized(&self) -> bool {
        unsafe {
            let result = ffi::rack_au_plugin_is_initialized(self.inner.as_ptr());
            result != 0
        }
    }
}

// Additional methods not in PluginInstance trait
impl AudioUnitPlugin {
    /// Get the number of input channels
    ///
    /// Returns the actual number of input channels the plugin was configured with
    /// after initialization. This may differ from what was requested if the plugin
    /// doesn't support the requested configuration.
    ///
    /// # Returns
    ///
    /// - Number of input channels (e.g., 1 for mono, 2 for stereo)
    /// - 0 if not initialized
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use rack::prelude::*;
    /// # fn example() -> Result<()> {
    /// # let scanner = Scanner::new()?;
    /// # let plugins = scanner.scan()?;
    /// # let mut plugin = scanner.load(&plugins[0])?;
    /// plugin.initialize(48000.0, 512)?;
    /// let channels = plugin.input_channels();
    /// println!("Plugin has {} input channels", channels);
    /// # Ok(())
    /// # }
    /// ```
    pub fn input_channels(&self) -> usize {
        self.input_channels
    }

    /// Get the number of output channels
    ///
    /// Returns the actual number of output channels the plugin was configured with
    /// after initialization. This may differ from what was requested if the plugin
    /// doesn't support the requested configuration.
    ///
    /// # Returns
    ///
    /// - Number of output channels (e.g., 1 for mono, 2 for stereo)
    /// - 0 if not initialized
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use rack::prelude::*;
    /// # fn example() -> Result<()> {
    /// # let scanner = Scanner::new()?;
    /// # let plugins = scanner.scan()?;
    /// # let mut plugin = scanner.load(&plugins[0])?;
    /// plugin.initialize(48000.0, 512)?;
    /// let channels = plugin.output_channels();
    /// println!("Plugin has {} output channels", channels);
    ///
    /// // Allocate buffers with correct channel count
    /// let mut inputs: Vec<Vec<f32>> = (0..plugin.input_channels())
    ///     .map(|_| vec![0.0f32; 512])
    ///     .collect();
    /// let mut outputs: Vec<Vec<f32>> = (0..plugin.output_channels())
    ///     .map(|_| vec![0.0f32; 512])
    ///     .collect();
    /// # Ok(())
    /// # }
    /// ```
    pub fn output_channels(&self) -> usize {
        self.output_channels
    }

    /// Create GUI asynchronously
    ///
    /// Creates the plugin's graphical user interface. This function tries multiple
    /// strategies in order:
    /// 1. AUv3 modern GUI (requestViewController)
    /// 2. AUv2 legacy GUI (kAudioUnitProperty_CocoaUI)
    /// 3. Generic parameter UI (fallback)
    ///
    /// The callback is invoked on the main thread when the GUI is ready.
    ///
    /// # Parameters
    ///
    /// - `callback`: Closure invoked when GUI creation completes. Receives
    ///   `Ok(AudioUnitGui)` on success or `Err(Error)` on failure.
    ///
    /// # Thread Safety
    ///
    /// **MUST be called from the main thread.** This is a macOS/AppKit requirement.
    /// The callback will also be invoked on the main thread.
    ///
    /// # Errors
    ///
    /// The callback receives an error if:
    /// - Plugin is not initialized
    /// - GUI creation fails (rare - generic UI is always available as fallback)
    ///
    /// # Example
    ///
    /// ```no_run
    /// # use rack::prelude::*;
    /// # fn example() -> Result<()> {
    /// # let scanner = Scanner::new()?;
    /// # let plugins = scanner.scan()?;
    /// # let mut plugin = scanner.load(&plugins[0])?;
    /// plugin.initialize(48000.0, 512)?;
    ///
    /// plugin.create_gui(|result| {
    ///     match result {
    ///         Ok(gui) => {
    ///             println!("GUI created!");
    ///             gui.show_window(Some("My Plugin"))?;
    ///         }
    ///         Err(e) => {
    ///             eprintln!("GUI creation failed: {}", e);
    ///         }
    ///     }
    ///     Ok(())
    /// });
    /// # Ok(())
    /// # }
    /// ```
    pub fn create_gui<F>(&mut self, callback: F)
    where
        F: FnOnce(Result<super::gui::AudioUnitGui>) -> Result<()> + Send + 'static,
    {
        if !self.is_initialized() {
            // Invoke callback with error immediately
            let _ = callback(Err(Error::NotInitialized));
            return;
        }

        // Box the callback so we can pass it through C
        let boxed_callback = Box::new(callback);
        let user_data = Box::into_raw(boxed_callback) as *mut std::ffi::c_void;

        // Define the C callback trampoline
        extern "C" fn trampoline<F>(
            user_data: *mut std::ffi::c_void,
            gui: *mut ffi::RackAUGui,
            error_code: std::os::raw::c_int,
        ) where
            F: FnOnce(Result<super::gui::AudioUnitGui>) -> Result<()> + Send + 'static,
        {
            // Safety: user_data is the boxed callback we created above
            let callback = unsafe {
                Box::from_raw(user_data as *mut F)
            };

            let result = if gui.is_null() {
                Err(map_error(error_code))
            } else {
                // Safety: gui is a valid pointer from C++
                Ok(unsafe { super::gui::AudioUnitGui::from_raw(gui) })
            };

            // Invoke the user's callback
            let _ = callback(result);
        }

        // Call the FFI function with our trampoline
        unsafe {
            ffi::rack_au_gui_create_async(
                self.inner.as_ptr(),
                trampoline::<F>,
                user_data,
            );
        }
    }
}

impl Drop for AudioUnitPlugin {
    fn drop(&mut self) {
        unsafe {
            ffi::rack_au_plugin_free(self.inner.as_ptr());
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{PluginScanner, PluginType};

    // Helper to get a real plugin for testing
    fn get_test_plugin() -> Option<PluginInfo> {
        use super::super::scanner::AudioUnitScanner;

        let scanner = AudioUnitScanner::new().ok()?;
        let plugins = scanner.scan().ok()?;

        // Find an effect or instrument plugin
        plugins
            .into_iter()
            .find(|p| p.plugin_type == PluginType::Effect || p.plugin_type == PluginType::Instrument)
    }

    #[test]
    fn test_plugin_creation_with_invalid_id() {
        use std::path::PathBuf;

        // Try to create plugin with non-existent unique_id
        let info = PluginInfo::new(
            "Fake Plugin".to_string(),
            "Fake Vendor".to_string(),
            1,
            PluginType::Effect,
            PathBuf::from("/fake/path"),
            "ffffffff-ffffffff-ffffffff".to_string(),
        );

        let result = AudioUnitPlugin::new(&info);
        assert!(result.is_err());
    }

    #[test]
    fn test_plugin_lifecycle() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        println!("Testing with plugin: {}", info.name);

        // Create plugin
        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        assert!(!plugin.is_initialized(), "Plugin should not be initialized");

        // Initialize plugin
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");
        assert!(plugin.is_initialized(), "Plugin should be initialized");

        // Re-initialize should succeed
        plugin
            .initialize(48000.0, 512)
            .expect("Re-initialization should succeed");
        assert!(plugin.is_initialized());
    }

    #[test]
    fn test_plugin_info() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        let retrieved_info = plugin.info();

        assert_eq!(retrieved_info.name, info.name);
        assert_eq!(retrieved_info.unique_id, info.unique_id);
    }

    #[test]
    fn test_drop_cleanup() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        // Create and drop plugin in a scope
        {
            let _plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        }
        // Plugin should be cleaned up here without crashing
    }

    #[test]
    fn test_audio_processing() {
        use super::super::scanner::AudioUnitScanner;

        let scanner = AudioUnitScanner::new().expect("Failed to create scanner");
        let plugins = scanner.scan().expect("Failed to scan plugins");

        // Find an effect plugin for processing test
        let Some(info) = plugins
            .into_iter()
            .find(|p| p.plugin_type == PluginType::Effect)
        else {
            println!("No effect plugins available, skipping test");
            return;
        };

        println!("Testing audio processing with: {}", info.name);

        // Create and initialize plugin
        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Create test buffers (planar format - separate buffer per channel)
        let frames = 512;
        let mut left_in = vec![0.0f32; frames];
        let mut right_in = vec![0.0f32; frames];
        let mut left_out = vec![0.0f32; frames];
        let mut right_out = vec![0.0f32; frames];

        // Fill input with a simple sine wave (440 Hz)
        let frequency = 440.0f32;
        let sample_rate = 48000.0f32;
        for i in 0..frames {
            let sample = (2.0 * std::f32::consts::PI * frequency * i as f32 / sample_rate).sin() * 0.5;
            left_in[i] = sample;  // Left channel
            right_in[i] = sample; // Right channel
        }

        // Process audio (planar format)
        plugin
            .process(
                &[&left_in, &right_in],
                &mut [&mut left_out, &mut right_out],
                frames
            )
            .expect("Audio processing failed");

        // Verify output is not all zeros (plugin did something)
        let has_signal = left_out.iter().chain(right_out.iter()).any(|&sample| sample != 0.0);
        assert!(
            has_signal,
            "Output should contain audio signal (not all zeros)"
        );

        println!("✓ Audio processing succeeded, output contains signal");
    }

    #[test]
    fn test_parameter_count() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        println!("Testing parameter count with: {}", info.name);

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.parameter_count();
        println!("  Found {} parameters", count);

        // Verify we can call it - parameter_count should never panic
        // (Some plugins might have 0 parameters, which is fine)
    }

    #[test]
    fn test_parameter_info() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        println!("Testing parameter info with: {}", info.name);

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.parameter_count();
        if count == 0 {
            println!("  Plugin has no parameters, skipping test");
            return;
        }

        // Get info for first parameter
        let param_info = plugin
            .parameter_info(0)
            .expect("Failed to get parameter info");

        println!("  Parameter 0: {}", param_info.name);
        println!("    Range: {} - {}", param_info.min, param_info.max);
        println!("    Default: {}", param_info.default);

        assert!(!param_info.name.is_empty(), "Parameter name should not be empty");
        assert!(param_info.index == 0);
    }

    #[test]
    fn test_get_set_parameter() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        println!("Testing get/set parameter with: {}", info.name);

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.parameter_count();
        if count == 0 {
            println!("  Plugin has no parameters, skipping test");
            return;
        }

        // Get original value
        let original_value = plugin
            .get_parameter(0)
            .expect("Failed to get parameter");

        println!("  Original value: {}", original_value);

        // Set to a different value (0.75, normalized)
        plugin
            .set_parameter(0, 0.75)
            .expect("Failed to set parameter");

        // Verify it changed
        let new_value = plugin
            .get_parameter(0)
            .expect("Failed to get parameter after set");

        println!("  New value: {}", new_value);

        // Value should be close to 0.75 (allowing for small floating point error)
        assert!(
            (new_value - 0.75).abs() < 0.01,
            "Parameter value should be ~0.75, got {}",
            new_value
        );

        // Restore original value
        plugin
            .set_parameter(0, original_value)
            .expect("Failed to restore parameter");
    }

    #[test]
    fn test_parameter_range_clamping() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.parameter_count();
        if count == 0 {
            return;
        }

        // Test setting values outside 0.0-1.0 range (should be clamped by C++ layer)
        plugin.set_parameter(0, 2.0).expect("Should handle > 1.0");
        let value = plugin.get_parameter(0).expect("Failed to get parameter");
        assert!(
            value <= 1.0,
            "Parameter should be clamped to <= 1.0, got {}",
            value
        );

        plugin.set_parameter(0, -1.0).expect("Should handle < 0.0");
        let value = plugin.get_parameter(0).expect("Failed to get parameter");
        assert!(
            value >= 0.0,
            "Parameter should be clamped to >= 0.0, got {}",
            value
        );
    }

    #[test]
    fn test_parameter_out_of_bounds() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.parameter_count();

        // Try to access parameter beyond count
        let result = plugin.get_parameter(count + 10);
        assert!(result.is_err(), "Should fail for out-of-bounds index");

        let result = plugin.set_parameter(count + 10, 0.5);
        assert!(result.is_err(), "Should fail for out-of-bounds index");

        let result = plugin.parameter_info(count + 10);
        assert!(result.is_err(), "Should fail for out-of-bounds index");
    }

    #[test]
    fn test_parameter_unit_strings() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.parameter_count();
        if count == 0 {
            println!("Plugin has no parameters, skipping test");
            return;
        }

        // Check that we can retrieve unit strings
        for i in 0..count {
            let param = plugin.parameter_info(i).expect("Failed to get parameter info");
            // Unit string may be empty (generic parameter) or contain a unit
            // Just verify it doesn't panic and returns a valid String
            println!("Parameter {} unit: '{}'", i, param.unit);
        }
    }

    #[test]
    fn test_parameter_operations_before_init() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        // Don't initialize - test pre-init behavior

        // All operations should fail gracefully before initialization
        let count = plugin.parameter_count();
        assert_eq!(count, 0, "Parameter count should be 0 before initialization");

        let result = plugin.parameter_info(0);
        assert!(result.is_err(), "parameter_info should fail before init");

        let result = plugin.get_parameter(0);
        assert!(result.is_err(), "get_parameter should fail before init");
    }

    #[test]
    fn test_parameter_value_round_trip() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.parameter_count();
        if count == 0 {
            return;
        }

        // Test round-tripping at various normalized values
        let test_values = vec![0.0, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0];

        for &test_value in &test_values {
            plugin.set_parameter(0, test_value).expect("Failed to set parameter");
            let read_value = plugin.get_parameter(0).expect("Failed to get parameter");

            // Allow small epsilon for floating-point precision
            let diff = (read_value - test_value).abs();
            assert!(
                diff < 0.01,
                "Value round-trip failed: set {}, got {} (diff: {})",
                test_value,
                read_value,
                diff
            );
        }
    }

    #[test]
    fn test_parameter_extreme_values() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.parameter_count();
        if count == 0 {
            return;
        }

        // Test extreme values (clamped by C++ layer before passing to AudioUnit)
        plugin.set_parameter(0, -1.0).expect("Should handle negative values");
        let value = plugin.get_parameter(0).expect("Failed to get parameter");
        assert!(value >= 0.0 && value <= 1.0, "Value should be clamped to 0.0-1.0");

        plugin.set_parameter(0, 2.0).expect("Should handle > 1.0 values");
        let value = plugin.get_parameter(0).expect("Failed to get parameter");
        assert!(value >= 0.0 && value <= 1.0, "Value should be clamped to 0.0-1.0");

        // Note: NaN and infinity are rejected by AudioUnit itself (not our code)
        // AudioUnit returns error -67743 (kAudioUnitErr_InvalidParameter)
        // This is correct behavior - we don't need to test these edge cases
        // as AudioUnit provides its own validation
    }

    // ============================================================================
    // MIDI Tests
    // ============================================================================

    // Helper to get an instrument plugin for MIDI testing
    fn get_instrument_plugin() -> Option<PluginInfo> {
        use super::super::scanner::AudioUnitScanner;

        let scanner = AudioUnitScanner::new().ok()?;
        let plugins = scanner.scan().ok()?;

        plugins
            .into_iter()
            .find(|p| p.plugin_type == PluginType::Instrument)
    }

    #[test]
    fn test_send_midi_note_on_off() {
        let Some(info) = get_instrument_plugin() else {
            println!("No instrument plugins available, skipping test");
            return;
        };

        println!("Testing MIDI with instrument: {}", info.name);

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Send a C major chord
        let events = vec![
            MidiEvent::note_on(60, 100, 0, 0), // Middle C
            MidiEvent::note_on(64, 100, 0, 0), // E
            MidiEvent::note_on(67, 100, 0, 0), // G
        ];

        let result = plugin.send_midi(&events);
        assert!(result.is_ok(), "Failed to send MIDI events");

        println!("✓ MIDI note on events sent successfully");

        // Process audio to render the notes (planar format)
        let left_in = vec![0.0f32; 512];
        let right_in = vec![0.0f32; 512];
        let mut left_out = vec![0.0f32; 512];
        let mut right_out = vec![0.0f32; 512];

        let result = plugin.process(
            &[&left_in, &right_in],
            &mut [&mut left_out, &mut right_out],
            512
        );
        assert!(result.is_ok(), "Failed to process audio after MIDI");

        println!("✓ Audio processed after MIDI events");

        // Send note off events
        let events = vec![
            MidiEvent::note_off(60, 64, 0, 0),
            MidiEvent::note_off(64, 64, 0, 0),
            MidiEvent::note_off(67, 64, 0, 0),
        ];

        let result = plugin.send_midi(&events);
        assert!(result.is_ok(), "Failed to send MIDI note off events");

        println!("✓ MIDI note off events sent successfully");
    }

    #[test]
    fn test_send_midi_control_change() {
        let Some(info) = get_instrument_plugin() else {
            println!("No instrument plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Send control change (modulation wheel)
        let events = vec![MidiEvent::control_change(1, 64, 0, 0)];

        let result = plugin.send_midi(&events);
        assert!(result.is_ok(), "Failed to send MIDI control change");

        println!("✓ MIDI control change sent successfully");
    }

    #[test]
    fn test_send_midi_program_change() {
        let Some(info) = get_instrument_plugin() else {
            println!("No instrument plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Send program change
        let events = vec![MidiEvent::program_change(5, 0, 0)];

        let result = plugin.send_midi(&events);
        assert!(result.is_ok(), "Failed to send MIDI program change");

        println!("✓ MIDI program change sent successfully");
    }

    #[test]
    fn test_send_midi_empty_events() {
        let Some(info) = get_instrument_plugin() else {
            println!("No instrument plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Send empty event array (should succeed)
        let events: Vec<MidiEvent> = vec![];
        let result = plugin.send_midi(&events);
        assert!(result.is_ok(), "Empty MIDI array should succeed");

        println!("✓ Empty MIDI event array handled correctly");
    }

    #[test]
    fn test_send_midi_before_init() {
        let Some(info) = get_instrument_plugin() else {
            println!("No instrument plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");

        // Try to send MIDI before initialization
        let events = vec![MidiEvent::note_on(60, 100, 0, 0)];
        let result = plugin.send_midi(&events);

        assert!(result.is_err(), "send_midi should fail before initialization");
        assert!(matches!(result, Err(Error::NotInitialized)));

        println!("✓ send_midi correctly fails before initialization");
    }

    #[test]
    fn test_send_midi_multi_channel() {
        let Some(info) = get_instrument_plugin() else {
            println!("No instrument plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Send notes on different MIDI channels (0, 5, 10, 15)
        let events = vec![
            MidiEvent::note_on(60, 100, 0, 0),   // Middle C on channel 0
            MidiEvent::note_on(64, 100, 5, 0),   // E on channel 5
            MidiEvent::note_on(67, 100, 10, 0),  // G on channel 10 (drums in GM)
            MidiEvent::note_on(72, 100, 15, 0),  // High C on channel 15
        ];

        let result = plugin.send_midi(&events);
        assert!(result.is_ok(), "Failed to send multi-channel MIDI: {:?}", result.err());

        // Process audio to verify notes rendered (planar format)
        let left_in = vec![0.0f32; 512];
        let right_in = vec![0.0f32; 512];
        let mut left_out = vec![0.0f32; 512];
        let mut right_out = vec![0.0f32; 512];

        plugin
            .process(
                &[&left_in, &right_in],
                &mut [&mut left_out, &mut right_out],
                512
            )
            .expect("Failed to process audio");

        // Check that we got some audio output
        let has_output = left_out.iter().chain(right_out.iter()).any(|&sample| sample.abs() > 1e-6);
        assert!(has_output, "Expected audio output from multi-channel MIDI notes");

        println!("✓ Multi-channel MIDI events sent successfully");
    }

    #[test]
    fn test_midi_with_effect_plugin() {
        use super::super::scanner::AudioUnitScanner;

        let scanner = AudioUnitScanner::new().expect("Failed to create scanner");
        let plugins = scanner.scan().expect("Failed to scan plugins");

        // Find an effect plugin
        let Some(info) = plugins
            .into_iter()
            .find(|p| p.plugin_type == PluginType::Effect)
        else {
            println!("No effect plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Send MIDI to effect plugin (should either fail or be ignored)
        let events = vec![MidiEvent::note_on(60, 100, 0, 0)];
        let result = plugin.send_midi(&events);

        // Effect plugins typically don't support MIDI, so this may fail
        // or succeed with the event being ignored
        match result {
            Ok(_) => println!("Effect plugin accepted MIDI (may be ignored)"),
            Err(_) => println!("✓ Effect plugin rejected MIDI as expected"),
        }
    }

    #[test]
    fn test_midi_value_clamping() {
        // Test that MidiEvent helper functions clamp values correctly
        let event = MidiEvent::note_on(200, 200, 20, 0);

        match event.kind {
            MidiEventKind::NoteOn { note, velocity, channel } => {
                assert_eq!(note, 127, "Note should be clamped to 127");
                assert_eq!(velocity, 127, "Velocity should be clamped to 127");
                assert_eq!(channel, 15, "Channel should be clamped to 15");
            }
            _ => panic!("Expected NoteOn event"),
        }

        println!("✓ MIDI value clamping works correctly");
    }

    #[test]
    fn test_midi_sample_offset() {
        let Some(info) = get_instrument_plugin() else {
            println!("No instrument plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Send events with different sample offsets
        let events = vec![
            MidiEvent::note_on(60, 100, 0, 0),
            MidiEvent::note_on(64, 100, 0, 128),
            MidiEvent::note_on(67, 100, 0, 256),
        ];

        let result = plugin.send_midi(&events);
        assert!(result.is_ok(), "Failed to send MIDI events with sample offsets");

        println!("✓ MIDI events with sample offsets sent successfully");
    }

    // ============================================================================
    // Preset Tests
    // ============================================================================

    #[test]
    fn test_preset_count() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        println!("Testing preset count with: {}", info.name);

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.preset_count().expect("Failed to get preset count");
        println!("  Found {} presets", count);

        // Verify preset_count doesn't panic (some plugins have 0 presets, which is valid)
    }

    #[test]
    fn test_preset_info() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        println!("Testing preset info with: {}", info.name);

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.preset_count().expect("Failed to get preset count");
        if count == 0 {
            println!("  Plugin has no presets, skipping test");
            return;
        }

        // Get info for first preset
        let preset_info = plugin
            .preset_info(0)
            .expect("Failed to get preset info");

        println!("  Preset 0: {}", preset_info.name);
        println!("    Preset number: {}", preset_info.preset_number);

        assert!(!preset_info.name.is_empty(), "Preset name should not be empty");
        assert_eq!(preset_info.index, 0);
    }

    #[test]
    fn test_load_preset() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        println!("Testing load preset with: {}", info.name);

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.preset_count().expect("Failed to get preset count");
        if count == 0 {
            println!("  Plugin has no presets, skipping test");
            return;
        }

        // Get first preset info
        let preset = plugin.preset_info(0).expect("Failed to get preset info");

        // Load the preset
        plugin
            .load_preset(preset.preset_number)
            .expect("Failed to load preset");

        println!("  ✓ Loaded preset: {}", preset.name);
    }

    #[test]
    fn test_state_round_trip() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        println!("Testing state round-trip with: {}", info.name);

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Save original state
        let original_state = plugin.get_state().expect("Failed to get state");
        println!("  Original state size: {} bytes", original_state.len());

        // Modify plugin state (if it has parameters)
        let param_count = plugin.parameter_count();
        let original_param_value = if param_count > 0 {
            let value = plugin.get_parameter(0).expect("Failed to get parameter");
            // Change parameter
            plugin
                .set_parameter(0, 0.99)
                .expect("Failed to set parameter");

            let new_value = plugin.get_parameter(0).expect("Failed to get parameter");
            println!("  Modified parameter 0: {} -> {}", value, new_value);
            Some(value)
        } else {
            None
        };

        // Restore state
        plugin
            .set_state(&original_state)
            .expect("Failed to restore state");

        println!("  ✓ State restored");

        // Verify parameter was restored (if plugin has parameters)
        if let Some(expected_value) = original_param_value {
            let restored_value = plugin.get_parameter(0).expect("Failed to get parameter");
            assert!(
                (restored_value - expected_value).abs() < 0.01,
                "Parameter not restored correctly (expected {}, got {})",
                expected_value,
                restored_value
            );
            println!("  ✓ Parameter restored to: {}", restored_value);
        }
    }

    #[test]
    fn test_preset_out_of_bounds() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.preset_count().expect("Failed to get preset count");
        if count == 0 {
            println!("Plugin has no presets, skipping test");
            return;
        }

        // Try to access preset beyond count
        let result = plugin.preset_info(count + 10);
        assert!(result.is_err(), "Should fail for out-of-bounds index");

        println!("✓ Out-of-bounds preset index rejected");
    }

    #[test]
    fn test_preset_before_init() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        // Don't initialize - test pre-init behavior

        // All operations should fail gracefully before initialization
        let result = plugin.preset_count();
        assert!(result.is_err(), "preset_count should fail before init");

        let result = plugin.preset_info(0);
        assert!(result.is_err(), "preset_info should fail before init");

        println!("✓ Preset operations correctly fail before initialization");
    }

    #[test]
    fn test_state_empty_data() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Try to set empty state
        let result = plugin.set_state(&[]);
        assert!(result.is_err(), "Should reject empty state data");

        println!("✓ Empty state data rejected");
    }

    #[test]
    fn test_state_before_init() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        // Don't initialize

        let result = plugin.get_state();
        assert!(result.is_err(), "get_state should fail before init");

        println!("✓ State operations correctly fail before initialization");
    }

    #[test]
    fn test_multiple_presets() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let count = plugin.preset_count().expect("Failed to get preset count");
        if count < 2 {
            println!("Plugin has {} presets, skipping multi-preset test", count);
            return;
        }

        println!("Testing with {} presets", count);

        // Load several presets and verify they each work
        for i in 0..count.min(3) {
            let preset = plugin.preset_info(i).expect("Failed to get preset info");
            plugin
                .load_preset(preset.preset_number)
                .expect("Failed to load preset");
            println!("  ✓ Loaded preset {}: {}", i, preset.name);
        }
    }

    #[test]
    fn test_channel_count_queries() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");

        // Before init, should be 0
        assert_eq!(plugin.input_channels(), 0, "Input channels should be 0 before init");
        assert_eq!(plugin.output_channels(), 0, "Output channels should be 0 before init");

        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // After init, should have valid channel counts
        let input_ch = plugin.input_channels();
        let output_ch = plugin.output_channels();

        assert!(input_ch > 0, "Input channels should be > 0 after init");
        assert!(output_ch > 0, "Output channels should be > 0 after init");

        println!("Plugin configured with {} input, {} output channels", input_ch, output_ch);
        println!("✓ Channel count queries work correctly");
    }

    #[test]
    fn test_process_with_wrong_channel_count() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let input_ch = plugin.input_channels();
        let output_ch = plugin.output_channels();

        // Create buffers with WRONG channel count (1 instead of actual)
        let left_in = vec![0.0f32; 512];
        let mut left_out = vec![0.0f32; 512];

        let result = plugin.process(
            &[&left_in],  // Only 1 channel
            &mut [&mut left_out],  // Only 1 channel
            512
        );

        // Should fail if plugin needs different channel count
        if input_ch != 1 || output_ch != 1 {
            assert!(result.is_err(), "process() should fail with wrong channel count");
            println!("✓ Correctly rejected wrong channel count (1 vs {}/{})", input_ch, output_ch);
        } else {
            println!("✓ Plugin is mono, 1 channel succeeded");
        }
    }

    #[test]
    fn test_process_with_correct_channel_count() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let input_ch = plugin.input_channels();
        let output_ch = plugin.output_channels();

        // Create buffers with CORRECT channel count
        let mut inputs: Vec<Vec<f32>> = (0..input_ch).map(|_| vec![0.0f32; 512]).collect();
        let mut outputs: Vec<Vec<f32>> = (0..output_ch).map(|_| vec![0.0f32; 512]).collect();

        // Fill input with test signal
        for ch in &mut inputs {
            for (i, sample) in ch.iter_mut().enumerate() {
                let t = i as f32 / 48000.0;
                *sample = (2.0 * std::f32::consts::PI * 440.0 * t).sin() * 0.5;
            }
        }

        // Convert to slices for process()
        let input_refs: Vec<&[f32]> = inputs.iter().map(|v| v.as_slice()).collect();
        let mut output_refs: Vec<&mut [f32]> = outputs.iter_mut().map(|v| v.as_mut_slice()).collect();

        let result = plugin.process(&input_refs, &mut output_refs, 512);

        assert!(result.is_ok(), "process() should succeed with correct channel count");
        println!("✓ Successfully processed with {}/{} channels", input_ch, output_ch);
    }

    #[test]
    fn test_process_with_too_many_channels() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let input_ch = plugin.input_channels();
        let output_ch = plugin.output_channels();

        // Create buffers with MORE channels than plugin expects
        let extra_channels = 2;
        let inputs: Vec<Vec<f32>> = (0..(input_ch + extra_channels)).map(|_| vec![0.0f32; 512]).collect();
        let mut outputs: Vec<Vec<f32>> = (0..(output_ch + extra_channels)).map(|_| vec![0.0f32; 512]).collect();

        let input_refs: Vec<&[f32]> = inputs.iter().map(|v| v.as_slice()).collect();
        let mut output_refs: Vec<&mut [f32]> = outputs.iter_mut().map(|v| v.as_mut_slice()).collect();

        let result = plugin.process(&input_refs, &mut output_refs, 512);

        // Should always fail with too many channels
        assert!(result.is_err(), "process() should fail when provided more channels than plugin expects");

        // Verify the error message is about channel count mismatch
        if let Err(e) = result {
            let err_msg = format!("{:?}", e);
            assert!(err_msg.contains("channel count mismatch") || err_msg.contains("mismatch"),
                    "Error should mention channel mismatch, got: {}", err_msg);
        }

        println!("✓ Correctly rejected too many channels ({}/{} provided vs {}/{} expected)",
                 input_ch + extra_channels, output_ch + extra_channels, input_ch, output_ch);
    }

    #[test]
    fn test_process_with_mismatched_buffer_lengths() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let input_ch = plugin.input_channels();
        let output_ch = plugin.output_channels();

        if input_ch < 2 {
            println!("✓ Plugin has < 2 input channels, skipping mismatched length test");
            return;
        }

        // Create buffers where channels have DIFFERENT lengths
        let mut inputs: Vec<Vec<f32>> = Vec::new();
        inputs.push(vec![0.0f32; 512]);      // First channel: 512 samples
        inputs.push(vec![0.0f32; 256]);      // Second channel: only 256 samples (WRONG!)
        for _ in 2..input_ch {
            inputs.push(vec![0.0f32; 512]);  // Rest: 512 samples
        }

        let mut outputs: Vec<Vec<f32>> = (0..output_ch).map(|_| vec![0.0f32; 512]).collect();

        let input_refs: Vec<&[f32]> = inputs.iter().map(|v| v.as_slice()).collect();
        let mut output_refs: Vec<&mut [f32]> = outputs.iter_mut().map(|v| v.as_mut_slice()).collect();

        // Try to process 512 frames, but second input channel only has 256
        let result = plugin.process(&input_refs, &mut output_refs, 512);

        // Should fail because channel 1 has only 256 samples but we're asking for 512
        assert!(result.is_err(), "process() should fail when channel buffers have insufficient length");

        if let Err(e) = result {
            let err_msg = format!("{:?}", e);
            assert!(err_msg.contains("channel") && err_msg.contains("samples"),
                    "Error should mention channel and samples, got: {}", err_msg);
        }

        println!("✓ Correctly rejected mismatched buffer lengths (channel 1: 256 < 512 frames)");
    }

    // Reset Tests

    #[test]
    fn test_reset_before_init() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");

        // Try to reset before initialization
        let result = plugin.reset();
        assert!(result.is_err(), "reset() should fail before initialization");

        println!("✓ Reset correctly fails before initialization");
    }

    #[test]
    fn test_reset_after_init() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        // Reset should succeed after initialization
        let result = plugin.reset();
        assert!(result.is_ok(), "reset() should succeed after initialization");

        println!("✓ Reset succeeds after initialization");
    }

    #[test]
    fn test_reset_clears_state() {
        let Some(info) = get_test_plugin() else {
            println!("No test plugins available, skipping test");
            return;
        };

        let mut plugin = AudioUnitPlugin::new(&info).expect("Failed to create plugin");
        plugin
            .initialize(48000.0, 512)
            .expect("Failed to initialize plugin");

        let input_ch = plugin.input_channels();
        let output_ch = plugin.output_channels();

        // Process some audio to build up state (delay lines, reverb, etc.)
        let inputs: Vec<Vec<f32>> = (0..input_ch).map(|_| vec![1.0f32; 512]).collect();
        let mut outputs: Vec<Vec<f32>> = (0..output_ch).map(|_| vec![0.0f32; 512]).collect();

        let input_refs: Vec<&[f32]> = inputs.iter().map(|v| v.as_slice()).collect();
        let mut output_refs: Vec<&mut [f32]> = outputs.iter_mut().map(|v| v.as_mut_slice()).collect();

        // Process several buffers to build up internal state
        for _ in 0..10 {
            plugin.process(&input_refs, &mut output_refs, 512)
                .expect("Failed to process audio");
        }

        // Reset should clear all internal state
        plugin.reset().expect("Failed to reset plugin");

        // Process silence after reset
        let silent_inputs: Vec<Vec<f32>> = (0..input_ch).map(|_| vec![0.0f32; 512]).collect();
        let mut silent_outputs: Vec<Vec<f32>> = (0..output_ch).map(|_| vec![0.0f32; 512]).collect();

        let silent_input_refs: Vec<&[f32]> = silent_inputs.iter().map(|v| v.as_slice()).collect();
        let mut silent_output_refs: Vec<&mut [f32]> = silent_outputs.iter_mut().map(|v| v.as_mut_slice()).collect();

        plugin.process(&silent_input_refs, &mut silent_output_refs, 512)
            .expect("Failed to process audio after reset");

        // Verify that reset actually cleared state by checking output
        // After reset + processing silence, output should be very quiet (no residual state)
        // We allow some small output (plugin might add noise or have DC offset)
        let max_abs_value = silent_outputs.iter()
            .flat_map(|ch| ch.iter())
            .map(|&v| v.abs())
            .max_by(|a, b| a.partial_cmp(b).unwrap())
            .unwrap_or(0.0);

        // After processing silence through a reset plugin, output should be negligible
        // Allow up to 0.1 for plugins that might have some DC offset or noise floor
        assert!(max_abs_value < 0.1,
                "After reset, processing silence should produce minimal output, got max: {}",
                max_abs_value);

        println!("✓ Reset successfully clears plugin state (verified: max output {:.6} after silence)",
                 max_abs_value);
    }
}