bravoh-daw 0.1.0

Parse Ableton Live, FL Studio, Logic Pro & REAPER project files into one unified schema
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
//! Ableton Live .als project file parser.
//!
//! .als files are gzip-compressed XML. We decompress with flate2,
//! then parse the XML with quick-xml's event-based reader.
//! Matches the Python backend parser's extraction logic.

use super::{MarkerInfo, MixerInfo, ParsedIntelligence, PluginInfo, SampleInfo, TrackInfo};
use flate2::read::GzDecoder;
use quick_xml::events::Event;
use quick_xml::Reader;
use std::collections::HashMap;
use std::io::{BufReader, Read};
use std::path::Path;

/// Max uncompressed XML size (5 MB).
const MAX_UNCOMPRESSED: usize = 5 * 1024 * 1024;

/// VST/AU wrapper element names — anything else inside <Devices> is a native Ableton device.
const VST_WRAPPER_TAGS: &[&str] = &[
    "PluginDevice",
    "AuPluginDevice",
    "Vst3PluginDevice",
    "MxPatchRef",
];

/// Track element tag → track type mapping.
fn track_type_for_tag(tag: &str) -> Option<&'static str> {
    match tag {
        "AudioTrack" => Some("audio"),
        "MidiTrack" => Some("midi"),
        "ReturnTrack" => Some("return"),
        "GroupTrack" => Some("group"),
        _ => None,
    }
}

/// Read and decompress a .als file, then parse the XML content.
pub fn parse(path: &Path) -> Result<ParsedIntelligence, String> {
    let file = std::fs::File::open(path).map_err(|e| format!("Open: {}", e))?;
    let decoder = GzDecoder::new(file);
    let mut reader = BufReader::new(decoder);

    let mut xml_bytes = Vec::new();
    reader
        .by_ref()
        .take(MAX_UNCOMPRESSED as u64)
        .read_to_end(&mut xml_bytes)
        .map_err(|e| format!("Decompress: {}", e))?;

    if xml_bytes.len() >= MAX_UNCOMPRESSED {
        return Err(format!(
            "Uncompressed XML exceeds {} byte limit",
            MAX_UNCOMPRESSED
        ));
    }

    parse_xml_content(&xml_bytes)
}

/// Parse raw Ableton XML bytes into structured intelligence.
/// Public for unit tests (pass raw XML strings without gzip).
pub fn parse_xml_content(xml: &[u8]) -> Result<ParsedIntelligence, String> {
    let mut intel = ParsedIntelligence {
        daw: "Ableton Live".to_string(),
        ..Default::default()
    };

    let mut reader = Reader::from_reader(xml);
    reader.config_mut().trim_text(true);

    let mut buf = Vec::new();

    // Element stack for context tracking
    let mut stack: Vec<String> = Vec::new();

    // Track state
    let mut in_track = false;
    let mut current_track_type: Option<&'static str> = None;
    let mut current_track_name = String::new();
    let mut current_track_plugins: Vec<String> = Vec::new();

    // Plugin state
    let mut in_devices = false;
    let mut in_vst_wrapper = false;
    let mut plugin_counts: HashMap<String, u32> = HashMap::new();

    // BPM state: track whether we're inside MasterTrack/MainTrack
    let mut in_master_track = false;

    // Marker state
    let mut in_locator = false;
    let mut locator_name = String::new();
    let mut locator_time: f64 = 0.0;

    // Sample state
    let mut in_sample_ref = false;
    let mut in_file_ref = false;

    // MIDI state (v0.29.0)
    let mut in_midi_clip = false;
    let mut in_key_track = false;
    let mut midi_note_count: u32 = 0;
    let midi_track_name = String::new();

    // Sidechain state (v0.29.0)
    let mut in_compressor = false;
    let mut in_sidechain_section = false;
    let mut sidechain_on_pending = false;

    // Automation state (v0.29.0)
    let mut in_automation_envelope = false;

    // Track color (v0.29.0)
    let mut current_track_color: Option<String> = None;

    // Version
    let mut found_version = false;

    // Track counts for MixerInfo
    let mut audio_count: u32 = 0;
    let mut midi_count: u32 = 0;
    let mut return_count: u32 = 0;
    let mut group_count: u32 = 0;

    // All tracks collected
    let mut tracks: Vec<TrackInfo> = Vec::new();

    loop {
        match reader.read_event_into(&mut buf) {
            Ok(Event::Start(ref e)) => {
                let tag_name = String::from_utf8_lossy(e.name().as_ref()).to_string();
                stack.push(tag_name.clone());

                // Ableton version from root <Ableton> element
                if !found_version && tag_name == "Ableton" {
                    for attr in e.attributes().flatten() {
                        if attr.key.as_ref() == b"Creator" {
                            intel.daw_version =
                                Some(String::from_utf8_lossy(&attr.value).to_string());
                            found_version = true;
                        }
                    }
                }

                // MasterTrack (Live 10/11) or MainTrack (Live 12)
                if tag_name == "MasterTrack" || tag_name == "MainTrack" {
                    in_master_track = true;
                }

                // Track elements
                if let Some(ttype) = track_type_for_tag(&tag_name) {
                    in_track = true;
                    current_track_type = Some(ttype);
                    current_track_name.clear();
                    current_track_plugins.clear();
                }

                // Devices container
                if tag_name == "Devices" && in_track {
                    in_devices = true;
                }

                // VST/AU wrapper inside Devices
                if in_devices && VST_WRAPPER_TAGS.contains(&tag_name.as_str()) {
                    in_vst_wrapper = true;
                }

                // Native device: inside <Devices> but NOT a VST wrapper and not a child of one
                if in_devices && !in_vst_wrapper && tag_name != "Devices" {
                    // Check if direct child of Devices by seeing if the parent is "Devices"
                    if stack.len() >= 2 && stack[stack.len() - 2] == "Devices" {
                        let pname = tag_name.clone();
                        current_track_plugins.push(pname.clone());
                        let key = format!("native:{}", pname);
                        *plugin_counts.entry(key).or_insert(0) += 1;
                    }
                }

                // MidiClip for MIDI note counting (v0.29.0)
                if tag_name == "MidiClip" {
                    in_midi_clip = true;
                    midi_note_count = 0;
                }
                if tag_name == "KeyTrack" && in_midi_clip {
                    in_key_track = true;
                }
                if tag_name == "MidiNoteEvent" && in_key_track {
                    midi_note_count += 1;
                }

                // Sidechain detection (v0.29.0)
                if tag_name == "Compressor" || tag_name == "GlueCompressor" || tag_name == "Gate" {
                    in_compressor = true;
                }
                if in_compressor && (tag_name == "Sidechain" || tag_name == "SideChain") {
                    in_sidechain_section = true;
                }

                // Automation envelope detection (v0.29.0)
                if tag_name == "AutomationEnvelope" {
                    in_automation_envelope = true;
                }

                // Locator for markers
                if tag_name == "Locator" {
                    in_locator = true;
                    locator_name.clear();
                    locator_time = 0.0;
                }

                // SampleRef / FileRef for samples
                if tag_name == "SampleRef" {
                    in_sample_ref = true;
                }
                if tag_name == "FileRef" && in_sample_ref {
                    in_file_ref = true;
                }
            }

            Ok(Event::Empty(ref e)) => {
                let tag_name = String::from_utf8_lossy(e.name().as_ref()).to_string();

                // MIDI note event as self-closing tag: <MidiNoteEvent/>
                if tag_name == "MidiNoteEvent" && in_key_track {
                    midi_note_count += 1;
                }

                // Native device as self-closing tag: <Eq8/> inside <Devices>
                if in_devices
                    && !in_vst_wrapper
                    && tag_name != "Devices"
                    && stack.last().map(|s| s.as_str()) == Some("Devices")
                {
                    current_track_plugins.push(tag_name.clone());
                    let key = format!("native:{}", tag_name);
                    *plugin_counts.entry(key).or_insert(0) += 1;
                }

                // BPM: <Manual Value="128.0"/> inside <Tempo>
                if tag_name == "Manual" {
                    let parent = stack.last().map(|s| s.as_str());
                    if parent == Some("Tempo") {
                        for attr in e.attributes().flatten() {
                            if attr.key.as_ref() == b"Value" {
                                if let Ok(val) = String::from_utf8_lossy(&attr.value).parse::<f64>()
                                {
                                    if (30.0..=300.0).contains(&val) {
                                        // Only set BPM from master/main track level or top-level
                                        // (before any track context, or inside master track)
                                        if in_master_track || !in_track {
                                            intel.bpm = Some(val);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

                // Track name: <EffectiveName Value="..."/> inside <Name>
                // or <UserName Value="..."/>
                if in_track && current_track_name.is_empty() {
                    if tag_name == "EffectiveName" {
                        let parent = stack.last().map(|s| s.as_str());
                        if parent == Some("Name") {
                            for attr in e.attributes().flatten() {
                                if attr.key.as_ref() == b"Value" {
                                    let val = String::from_utf8_lossy(&attr.value).to_string();
                                    if !val.is_empty() {
                                        current_track_name = val;
                                    }
                                }
                            }
                        }
                    }
                    if current_track_name.is_empty() && tag_name == "UserName" {
                        for attr in e.attributes().flatten() {
                            if attr.key.as_ref() == b"Value" {
                                let val = String::from_utf8_lossy(&attr.value).to_string();
                                if !val.is_empty() {
                                    current_track_name = val;
                                }
                            }
                        }
                    }
                }

                // Plugin name from VST wrapper: <PlugName Value="Serum"/>
                if in_vst_wrapper && (tag_name == "PlugName" || tag_name == "UserName") {
                    for attr in e.attributes().flatten() {
                        if attr.key.as_ref() == b"Value" {
                            let pname = String::from_utf8_lossy(&attr.value).to_string();
                            if !pname.is_empty() {
                                current_track_plugins.push(pname.clone());
                                let key = format!("vst:{}", pname);
                                *plugin_counts.entry(key).or_insert(0) += 1;
                            }
                        }
                    }
                }

                // Locator fields
                if in_locator {
                    if tag_name == "Name" {
                        for attr in e.attributes().flatten() {
                            if attr.key.as_ref() == b"Value" {
                                locator_name = String::from_utf8_lossy(&attr.value).to_string();
                            }
                        }
                    }
                    if tag_name == "Time" {
                        for attr in e.attributes().flatten() {
                            if attr.key.as_ref() == b"Value" {
                                locator_time =
                                    String::from_utf8_lossy(&attr.value).parse().unwrap_or(0.0);
                            }
                        }
                    }
                }

                // Sidechain On: <Manual Value="true"/> inside <On> inside <Sidechain> (v0.29.0)
                if in_sidechain_section && tag_name == "Manual" {
                    let parent = stack.last().map(|s| s.as_str());
                    if parent == Some("On") {
                        for attr in e.attributes().flatten() {
                            if attr.key.as_ref() == b"Value" {
                                let val = String::from_utf8_lossy(&attr.value);
                                if val == "true" {
                                    sidechain_on_pending = true;
                                }
                            }
                        }
                    }
                }

                // Track color: <Color Value="N"/> (v0.29.0)
                if in_track && tag_name == "Color" && current_track_color.is_none() {
                    for attr in e.attributes().flatten() {
                        if attr.key.as_ref() == b"Value" {
                            let val = String::from_utf8_lossy(&attr.value).to_string();
                            if !val.is_empty() {
                                current_track_color = Some(format!("ableton_color_{}", val));
                            }
                        }
                    }
                }

                // Automation: <PointeeId Value="N"/> (v0.29.0)
                if in_automation_envelope && tag_name == "PointeeId" {
                    for attr in e.attributes().flatten() {
                        if attr.key.as_ref() == b"Value" {
                            let val = String::from_utf8_lossy(&attr.value).to_string();
                            if !val.is_empty() && !intel.automated_params.contains(&val) {
                                intel.automated_params.push(val);
                            }
                        }
                    }
                }

                // Sample name: <Name Value="kick.wav"/> inside <FileRef> inside <SampleRef>
                if in_file_ref && tag_name == "Name" {
                    for attr in e.attributes().flatten() {
                        if attr.key.as_ref() == b"Value" {
                            let sname = String::from_utf8_lossy(&attr.value).to_string();
                            if !sname.is_empty() {
                                intel.samples.push(SampleInfo {
                                    name: sname,
                                    path: None,
                                });
                            }
                        }
                    }
                }
            }

            Ok(Event::End(ref e)) => {
                let tag_name = String::from_utf8_lossy(e.name().as_ref()).to_string();

                // Close MasterTrack / MainTrack
                if tag_name == "MasterTrack" || tag_name == "MainTrack" {
                    in_master_track = false;
                }

                // Close MidiClip — emit MIDI track info (v0.29.0)
                if tag_name == "MidiClip" {
                    if in_midi_clip && midi_note_count > 0 {
                        let track_name = if midi_track_name.is_empty() {
                            current_track_name.clone()
                        } else {
                            midi_track_name.clone()
                        };
                        intel.midi_tracks.push(super::MidiTrackInfo {
                            track_name,
                            note_count: midi_note_count,
                            pitch_low: None,
                            pitch_high: None,
                        });
                    }
                    in_midi_clip = false;
                    in_key_track = false;
                    midi_note_count = 0;
                }
                if tag_name == "KeyTrack" {
                    in_key_track = false;
                }

                // Close Compressor/Sidechain (v0.29.0)
                if tag_name == "Compressor" || tag_name == "GlueCompressor" || tag_name == "Gate" {
                    if sidechain_on_pending {
                        intel.mixer.has_sidechain = true;
                        sidechain_on_pending = false;
                    }
                    in_compressor = false;
                    in_sidechain_section = false;
                }
                if tag_name == "Sidechain" || tag_name == "SideChain" {
                    in_sidechain_section = false;
                }

                // Close AutomationEnvelope (v0.29.0)
                if tag_name == "AutomationEnvelope" {
                    in_automation_envelope = false;
                }

                // Close track
                if let Some(ttype) = track_type_for_tag(&tag_name) {
                    if in_track && current_track_type == Some(ttype) {
                        // Count track type
                        match ttype {
                            "audio" => audio_count += 1,
                            "midi" => midi_count += 1,
                            "return" => return_count += 1,
                            "group" => group_count += 1,
                            _ => {}
                        }

                        tracks.push(TrackInfo {
                            name: current_track_name.clone(),
                            track_type: ttype.to_string(),
                            plugins: current_track_plugins.clone(),
                            color: current_track_color.take(),
                        });

                        in_track = false;
                        current_track_type = None;
                        in_devices = false;
                        in_vst_wrapper = false;
                    }
                }

                // Close Devices
                if tag_name == "Devices" {
                    in_devices = false;
                    in_vst_wrapper = false;
                }

                // Close VST wrapper
                if VST_WRAPPER_TAGS.contains(&tag_name.as_str()) {
                    in_vst_wrapper = false;
                }

                // Close Locator — emit marker
                if tag_name == "Locator" {
                    if in_locator && !locator_name.is_empty() {
                        intel.markers.push(MarkerInfo {
                            name: locator_name.clone(),
                            position_seconds: locator_time,
                        });
                    }
                    in_locator = false;
                }

                // Close SampleRef / FileRef
                if tag_name == "FileRef" {
                    in_file_ref = false;
                }
                if tag_name == "SampleRef" {
                    in_sample_ref = false;
                }

                stack.pop();
            }

            Ok(Event::Eof) => break,
            Err(e) => return Err(format!("XML parse error: {}", e)),
            _ => {}
        }

        buf.clear();
    }

    // Build plugin list from counts
    for (key, count) in &plugin_counts {
        let parts: Vec<&str> = key.splitn(2, ':').collect();
        if parts.len() == 2 {
            intel.plugins.push(PluginInfo {
                name: parts[1].to_string(),
                plugin_type: parts[0].to_string(),
                count: *count,
            });
        }
    }

    // Deduplicate samples by name
    let mut seen_samples: HashMap<String, bool> = HashMap::new();
    intel.samples.retain(|s| {
        if seen_samples.contains_key(&s.name) {
            false
        } else {
            seen_samples.insert(s.name.clone(), true);
            true
        }
    });

    // Build MixerInfo
    let total = tracks.len() as u32;
    intel.mixer = MixerInfo {
        total_tracks: total,
        audio_tracks: audio_count,
        midi_tracks: midi_count,
        return_tracks: return_count,
        group_tracks: group_count,
        has_sidechain: intel.mixer.has_sidechain, // Detected from Compressor/Gate Sidechain On
    };

    intel.tracks = tracks;

    Ok(intel)
}

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

    #[test]
    fn parse_bpm() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <MasterTrack>
            <DeviceChain>
                <Mixer>
                    <Tempo>
                        <Manual Value="128"/>
                    </Tempo>
                </Mixer>
            </DeviceChain>
        </MasterTrack>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.bpm, Some(128.0));
        assert_eq!(intel.daw, "Ableton Live");
        assert_eq!(intel.daw_version.as_deref(), Some("Ableton Live 11.3.2"));
    }

    #[test]
    fn parse_bpm_live12_main_track() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 12.0">
    <LiveSet>
        <MainTrack>
            <DeviceChain>
                <Mixer>
                    <Tempo>
                        <Manual Value="140.5"/>
                    </Tempo>
                </Mixer>
            </DeviceChain>
        </MainTrack>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.bpm, Some(140.5));
    }

    #[test]
    fn parse_bpm_rejects_out_of_range() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <MasterTrack>
            <Tempo>
                <Manual Value="999"/>
            </Tempo>
        </MasterTrack>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.bpm, None);
    }

    #[test]
    fn parse_tracks() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <AudioTrack>
                <Name>
                    <EffectiveName Value="Drums"/>
                </Name>
            </AudioTrack>
            <MidiTrack>
                <Name>
                    <EffectiveName Value="Synth Lead"/>
                </Name>
            </MidiTrack>
            <ReturnTrack>
                <Name>
                    <EffectiveName Value="Reverb Bus"/>
                </Name>
            </ReturnTrack>
            <GroupTrack>
                <Name>
                    <EffectiveName Value="Drums Group"/>
                </Name>
            </GroupTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.tracks.len(), 4);

        assert_eq!(intel.tracks[0].name, "Drums");
        assert_eq!(intel.tracks[0].track_type, "audio");

        assert_eq!(intel.tracks[1].name, "Synth Lead");
        assert_eq!(intel.tracks[1].track_type, "midi");

        assert_eq!(intel.tracks[2].name, "Reverb Bus");
        assert_eq!(intel.tracks[2].track_type, "return");

        assert_eq!(intel.tracks[3].name, "Drums Group");
        assert_eq!(intel.tracks[3].track_type, "group");

        assert_eq!(intel.mixer.total_tracks, 4);
        assert_eq!(intel.mixer.audio_tracks, 1);
        assert_eq!(intel.mixer.midi_tracks, 1);
        assert_eq!(intel.mixer.return_tracks, 1);
        assert_eq!(intel.mixer.group_tracks, 1);
    }

    #[test]
    fn parse_plugins_native() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <AudioTrack>
                <Name>
                    <EffectiveName Value="Bass"/>
                </Name>
                <DeviceChain>
                    <Devices>
                        <Eq8/>
                        <Compressor/>
                    </Devices>
                </DeviceChain>
            </AudioTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();

        // Track should have the native plugins listed
        assert_eq!(intel.tracks.len(), 1);
        assert!(intel.tracks[0].plugins.contains(&"Eq8".to_string()));
        assert!(intel.tracks[0].plugins.contains(&"Compressor".to_string()));

        // Global plugin list
        assert_eq!(intel.plugins.len(), 2);
        let names: Vec<&str> = intel.plugins.iter().map(|p| p.name.as_str()).collect();
        assert!(names.contains(&"Eq8"));
        assert!(names.contains(&"Compressor"));
        for p in &intel.plugins {
            assert_eq!(p.plugin_type, "native");
        }
    }

    #[test]
    fn parse_plugins_vst() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <MidiTrack>
                <Name>
                    <EffectiveName Value="Lead"/>
                </Name>
                <DeviceChain>
                    <Devices>
                        <PluginDevice>
                            <PluginDesc>
                                <VstPluginInfo>
                                    <PlugName Value="Serum"/>
                                </VstPluginInfo>
                            </PluginDesc>
                        </PluginDevice>
                        <AuPluginDevice>
                            <PluginDesc>
                                <AuPluginInfo>
                                    <UserName Value="FabFilter Pro-Q 3"/>
                                </AuPluginInfo>
                            </PluginDesc>
                        </AuPluginDevice>
                    </Devices>
                </DeviceChain>
            </MidiTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();

        assert_eq!(intel.tracks.len(), 1);
        assert!(intel.tracks[0].plugins.contains(&"Serum".to_string()));
        assert!(intel.tracks[0]
            .plugins
            .contains(&"FabFilter Pro-Q 3".to_string()));

        let names: Vec<&str> = intel.plugins.iter().map(|p| p.name.as_str()).collect();
        assert!(names.contains(&"Serum"));
        assert!(names.contains(&"FabFilter Pro-Q 3"));
        for p in &intel.plugins {
            assert_eq!(p.plugin_type, "vst");
        }
    }

    #[test]
    fn parse_markers() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Locators>
            <Locators>
                <Locator>
                    <Name Value="Intro"/>
                    <Time Value="0.0"/>
                </Locator>
                <Locator>
                    <Name Value="Chorus"/>
                    <Time Value="45.0"/>
                </Locator>
            </Locators>
        </Locators>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.markers.len(), 2);
        assert_eq!(intel.markers[0].name, "Intro");
        assert!((intel.markers[0].position_seconds - 0.0).abs() < 0.01);
        assert_eq!(intel.markers[1].name, "Chorus");
        assert!((intel.markers[1].position_seconds - 45.0).abs() < 0.01);
    }

    #[test]
    fn parse_samples() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <AudioTrack>
                <Name>
                    <EffectiveName Value="Drums"/>
                </Name>
                <DeviceChain>
                    <MainSequencer>
                        <ClipSlotList>
                            <ClipSlot>
                                <ClipSlot>
                                    <Value>
                                        <AudioClip>
                                            <SampleRef>
                                                <FileRef>
                                                    <Name Value="kick.wav"/>
                                                </FileRef>
                                            </SampleRef>
                                        </AudioClip>
                                    </Value>
                                </ClipSlot>
                            </ClipSlot>
                        </ClipSlotList>
                    </MainSequencer>
                </DeviceChain>
            </AudioTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.samples.len(), 1);
        assert_eq!(intel.samples[0].name, "kick.wav");
    }

    #[test]
    fn parse_empty_project() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.daw, "Ableton Live");
        assert!(intel.tracks.is_empty());
        assert!(intel.plugins.is_empty());
        assert!(intel.samples.is_empty());
        assert!(intel.markers.is_empty());
        assert_eq!(intel.mixer.total_tracks, 0);
    }

    #[test]
    fn parse_track_name_fallback_to_username() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <AudioTrack>
                <Name>
                    <EffectiveName Value=""/>
                    <UserName Value="My Custom Name"/>
                </Name>
            </AudioTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.tracks.len(), 1);
        assert_eq!(intel.tracks[0].name, "My Custom Name");
    }

    #[test]
    fn parse_midi_note_count() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <MidiTrack>
                <Name><EffectiveName Value="Piano"/></Name>
                <DeviceChain>
                    <MainSequencer>
                        <ClipSlotList>
                            <ClipSlot><ClipSlot><Value>
                                <MidiClip>
                                    <Notes>
                                        <KeyTracks>
                                            <KeyTrack>
                                                <MidiKey Value="60"/>
                                                <Notes>
                                                    <MidiNoteEvent/>
                                                    <MidiNoteEvent/>
                                                    <MidiNoteEvent/>
                                                </Notes>
                                            </KeyTrack>
                                        </KeyTracks>
                                    </Notes>
                                </MidiClip>
                            </Value></ClipSlot></ClipSlot>
                        </ClipSlotList>
                    </MainSequencer>
                </DeviceChain>
            </MidiTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert!(!intel.midi_tracks.is_empty(), "Should have MIDI track info");
        assert_eq!(intel.midi_tracks[0].note_count, 3);
    }

    #[test]
    fn parse_sidechain_detection() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <AudioTrack>
                <Name><EffectiveName Value="Bass"/></Name>
                <DeviceChain>
                    <Devices>
                        <Compressor>
                            <Sidechain>
                                <On>
                                    <Manual Value="true"/>
                                </On>
                            </Sidechain>
                        </Compressor>
                    </Devices>
                </DeviceChain>
            </AudioTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert!(intel.mixer.has_sidechain, "Should detect sidechain");
    }

    #[test]
    fn parse_track_color() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <AudioTrack>
                <Color Value="13"/>
                <Name><EffectiveName Value="Drums"/></Name>
            </AudioTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.tracks[0].color.as_deref(), Some("ableton_color_13"));
    }

    #[test]
    fn parse_automation_pointee_id() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <AudioTrack>
                <Name><EffectiveName Value="Lead"/></Name>
                <AutomationEnvelopes>
                    <Envelopes>
                        <AutomationEnvelope>
                            <PointeeId Value="42"/>
                        </AutomationEnvelope>
                        <AutomationEnvelope>
                            <PointeeId Value="77"/>
                        </AutomationEnvelope>
                    </Envelopes>
                </AutomationEnvelopes>
            </AudioTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.automated_params.len(), 2);
        assert!(intel.automated_params.contains(&"42".to_string()));
        assert!(intel.automated_params.contains(&"77".to_string()));
    }

    #[test]
    fn parse_no_sidechain_when_off() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <AudioTrack>
                <Name><EffectiveName Value="Bass"/></Name>
                <DeviceChain>
                    <Devices>
                        <Compressor>
                            <Sidechain>
                                <On>
                                    <Manual Value="false"/>
                                </On>
                            </Sidechain>
                        </Compressor>
                    </Devices>
                </DeviceChain>
            </AudioTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert!(
            !intel.mixer.has_sidechain,
            "Sidechain Off should not be detected"
        );
    }

    #[test]
    fn parse_deduplicates_samples() {
        let xml = br#"<?xml version="1.0" encoding="UTF-8"?>
<Ableton Creator="Ableton Live 11.3.2">
    <LiveSet>
        <Tracks>
            <AudioTrack>
                <Name><EffectiveName Value="Drums"/></Name>
                <DeviceChain>
                    <MainSequencer>
                        <SampleRef><FileRef><Name Value="kick.wav"/></FileRef></SampleRef>
                        <SampleRef><FileRef><Name Value="kick.wav"/></FileRef></SampleRef>
                        <SampleRef><FileRef><Name Value="snare.wav"/></FileRef></SampleRef>
                    </MainSequencer>
                </DeviceChain>
            </AudioTrack>
        </Tracks>
    </LiveSet>
</Ableton>"#;
        let intel = parse_xml_content(xml).unwrap();
        assert_eq!(intel.samples.len(), 2);
        let names: Vec<&str> = intel.samples.iter().map(|s| s.name.as_str()).collect();
        assert!(names.contains(&"kick.wav"));
        assert!(names.contains(&"snare.wav"));
    }

    // ── v0.29.7: Edge-case hardening ─────────────────────────────────

    #[test]
    fn parse_empty_xml() {
        let result = parse_xml_content(b"");
        assert!(result.is_ok());
        assert!(result.unwrap().tracks.is_empty());
    }

    #[test]
    fn parse_non_ableton_xml() {
        let xml = br#"<?xml version="1.0"?><Root><Child/></Root>"#;
        let result = parse_xml_content(xml);
        assert!(result.is_ok());
    }

    #[test]
    fn parse_truncated_xml_no_panic() {
        let result = parse_xml_content(b"<Ableton Creator=\"Test\"><LiveSet><Tracks><AudioTrack>");
        // Truncated — may error or produce partial result, but must not panic
        let _ = result;
    }

    #[test]
    fn parse_deeply_nested_xml() {
        let mut xml = String::from("<?xml version=\"1.0\"?><Ableton Creator=\"Test\">");
        for _ in 0..100 {
            xml.push_str("<Nested>");
        }
        for _ in 0..100 {
            xml.push_str("</Nested>");
        }
        xml.push_str("</Ableton>");
        assert!(parse_xml_content(xml.as_bytes()).is_ok());
    }
}