libflo-audio 0.1.3

The official encoder/decoder for the flo audio format
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
//! flo Metadata
//!
//! Supports most commonly used ID3v2.4 fields plus flo-unique extensions
//! Uses MessagePack serialization for efficiency and flexibility

extern crate alloc;

use alloc::collections::BTreeMap;
use alloc::string::{String, ToString};
use alloc::vec;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

// ============================================================================
// Picture Types (ID3v2.4 APIC)
// ============================================================================

/// Picture type (matches ID3v2.4 APIC picture types)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum PictureType {
    Other,
    FileIcon, // 32x32 PNG only
    OtherFileIcon,
    #[default]
    CoverFront,
    CoverBack,
    LeafletPage,
    Media, // e.g. label side of CD
    LeadArtist,
    Artist,
    Conductor,
    Band,
    Composer,
    Lyricist,
    RecordingLocation,
    DuringRecording,
    DuringPerformance,
    VideoScreenCapture,
    BrightColouredFish, // Yes, this is real in ID3v2.4 🐟
    Illustration,
    BandLogo,
    PublisherLogo,
}

/// Attached picture (album art, etc.)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Picture {
    /// MIME type (e.g., "image/jpeg", "image/png")
    pub mime_type: String,
    /// Picture type
    pub picture_type: PictureType,
    /// Description
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Binary picture data
    #[serde(with = "serde_bytes")]
    pub data: Vec<u8>,
}

// ============================================================================
// Text Structures
// ============================================================================

/// Comment with optional language and description (COMM)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Comment {
    /// ISO-639-2 language code (e.g., "eng", "jpn")
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    /// Short content description
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// The actual comment text
    pub text: String,
}

/// Unsynchronized lyrics (USLT)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Lyrics {
    /// ISO-639-2 language code
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    /// Content description
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Lyrics text
    pub text: String,
}

/// Synchronized lyrics content type (SYLT)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum SyncedLyricsContentType {
    Other,
    #[default]
    Lyrics,
    TextTranscription,
    PartName, // e.g., "Adagio"
    Events,   // e.g., "Don Quijote enters the stage"
    Chord,    // e.g., "Bb F Fsus"
    Trivia,   // Pop-up information
    WebpageUrl,
    ImageUrl,
}

/// A single line of synchronized lyrics with timestamp
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncedLyricsLine {
    /// Timestamp in milliseconds from start
    pub timestamp_ms: u64,
    /// Text/syllable at this timestamp
    pub text: String,
}

/// Synchronized lyrics/text (SYLT): flo first-party support!
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SyncedLyrics {
    /// ISO-639-2 language code
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,
    /// Content type
    #[serde(default)]
    pub content_type: SyncedLyricsContentType,
    /// Content description
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    /// Lines with timestamps
    pub lines: Vec<SyncedLyricsLine>,
}

/// User-defined text field (TXXX)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserText {
    /// Description/key
    pub description: String,
    /// Value
    pub value: String,
}

/// User-defined URL (WXXX)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UserUrl {
    /// Description
    pub description: String,
    /// URL
    pub url: String,
}

/// Popularimeter (POPM)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Popularimeter {
    /// Email/identifier of the user
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub email: Option<String>,
    /// Rating (1-255, 0 = unknown)
    pub rating: u8,
    /// Play counter
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub play_count: Option<u64>,
}

// ============================================================================
// flo-Unique Structures
// ============================================================================

/// Pre-computed waveform data for instant visualization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WaveformData {
    /// Number of peak values per second of audio
    pub peaks_per_second: u32,
    /// Peak values (0.0 to 1.0): for stereo, interleaved L/R or combined
    pub peaks: Vec<f32>,
    /// Number of channels in peaks (1 = mono/combined, 2 = stereo)
    #[serde(default = "default_one")]
    pub channels: u8,
}

fn default_one() -> u8 {
    1
}

/// Section type for track structure
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum SectionType {
    Intro,
    Verse,
    PreChorus,
    Chorus,
    PostChorus,
    Bridge,
    Breakdown,
    Drop,
    Buildup,
    Solo,
    Instrumental,
    Outro,
    Silence,
    Other,
}

/// Section marker with timestamp and label
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SectionMarker {
    /// Timestamp in milliseconds
    pub timestamp_ms: u64,
    /// Section type
    pub section_type: SectionType,
    /// Optional custom label (e.g., "Verse 2", "Guitar Solo")
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
}

/// BPM change point for tempo mapping
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BpmChange {
    /// Timestamp in milliseconds
    pub timestamp_ms: u64,
    /// BPM at this point (supports fractional BPM)
    pub bpm: f32,
}

/// Key change point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct KeyChange {
    /// Timestamp in milliseconds
    pub timestamp_ms: u64,
    /// Musical key (e.g., "Am", "F#m", "Bb")
    pub key: String,
}

/// Loudness measurement point for dynamic visualization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoudnessPoint {
    /// Timestamp in milliseconds
    pub timestamp_ms: u64,
    /// Loudness in LUFS
    pub lufs: f32,
}

/// Creator/producer note with optional timestamp
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreatorNote {
    /// Optional timestamp (None = applies to whole track)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timestamp_ms: Option<u64>,
    /// Note text
    pub text: String,
}

/// Collaboration credit with role and optional timestamp
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CollaborationCredit {
    /// Role (e.g., "Lead Vocals", "Bass Guitar", "Mixing")
    pub role: String,
    /// Person's name
    pub name: String,
    /// Optional timestamp for when they appear (e.g., guitar solo at 2:00)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timestamp_ms: Option<u64>,
}

/// Entry in remix/sample chain for tracking lineage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RemixChainEntry {
    /// Original track title
    pub title: String,
    /// Original artist
    pub artist: String,
    /// Year of original (if known)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub year: Option<u32>,
    /// ISRC of original (if known)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub isrc: Option<String>,
    /// Relationship type: "original", "remix", "sample", "cover", "mashup"
    pub relationship: String,
}

/// Animated cover art (GIF, animated WebP, or short video)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnimatedCover {
    /// MIME type (image/gif, image/webp, video/mp4)
    pub mime_type: String,
    /// Binary data
    #[serde(with = "serde_bytes")]
    pub data: Vec<u8>,
    /// Duration in milliseconds (if applicable)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub duration_ms: Option<u32>,
    /// Loop count (0 = infinite, None = play once)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub loop_count: Option<u32>,
}

/// Cover variant type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum CoverVariantType {
    Standard,
    Explicit,
    Clean,
    Remix,
    Deluxe,
    Limited,
    Vinyl,
    Cassette,
    Digital,
    Other,
}

/// Alternative cover art variant
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoverVariant {
    /// Variant type
    pub variant_type: CoverVariantType,
    /// MIME type
    pub mime_type: String,
    /// Binary data
    #[serde(with = "serde_bytes")]
    pub data: Vec<u8>,
    /// Description
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
}

// ============================================================================
// Main Metadata Structure
// ============================================================================

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct FloMetadata {
    // ==================== IDENTIFICATION ====================
    /// Title/songname (TIT2)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    /// Subtitle/description refinement (TIT3)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subtitle: Option<String>,

    /// Content group description (TIT1)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub content_group: Option<String>,

    /// Album/movie/show title (TALB)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub album: Option<String>,

    /// Original album (TOAL)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub original_album: Option<String>,

    /// Set subtitle (TSST)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub set_subtitle: Option<String>,

    /// Track number (TRCK)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub track_number: Option<u32>,

    /// Total tracks
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub track_total: Option<u32>,

    /// Disc number (TPOS)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub disc_number: Option<u32>,

    /// Total discs
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub disc_total: Option<u32>,

    /// ISRC code (TSRC)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub isrc: Option<String>,

    // ==================== INVOLVED PERSONS ====================
    /// Lead artist/performer (TPE1)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub artist: Option<String>,

    /// Album artist/band (TPE2)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub album_artist: Option<String>,

    /// Conductor (TPE3)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub conductor: Option<String>,

    /// Remixer/modifier (TPE4)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub remixer: Option<String>,

    /// Original artist (TOPE)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub original_artist: Option<String>,

    /// Composer (TCOM)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub composer: Option<String>,

    /// Lyricist/text writer (TEXT)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub lyricist: Option<String>,

    /// Original lyricist (TOLY)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub original_lyricist: Option<String>,

    /// Encoded by (TENC)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub encoded_by: Option<String>,

    /// Involved people list (TIPL)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub involved_people: Option<Vec<(String, String)>>,

    /// Musician credits (TMCL)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub musician_credits: Option<Vec<(String, String)>>,

    // ==================== PROPERTIES ====================
    /// Genre (TCON)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub genre: Option<String>,

    /// Mood (TMOO)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mood: Option<String>,

    /// BPM (TBPM)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bpm: Option<u32>,

    /// Initial musical key (TKEY)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key: Option<String>,

    /// Language (TLAN)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub language: Option<String>,

    /// Length in milliseconds (TLEN)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub length_ms: Option<u64>,

    // ==================== DATES/TIMES ====================
    /// Year
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub year: Option<u32>,

    /// Recording time (TDRC)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub recording_time: Option<String>,

    /// Release time (TDRL)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub release_time: Option<String>,

    /// Original release time (TDOR)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub original_release_time: Option<String>,

    /// Encoding time (TDEN)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub encoding_time: Option<String>,

    /// Tagging time (TDTG)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tagging_time: Option<String>,

    // ==================== RIGHTS/LICENSE ====================
    /// Copyright message (TCOP)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub copyright: Option<String>,

    /// Production copyright (TPRO)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub produced_notice: Option<String>,

    /// Publisher (TPUB)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub publisher: Option<String>,

    /// File owner/licensee (TOWN)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub file_owner: Option<String>,

    /// Internet radio station name (TRSN)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub radio_station: Option<String>,

    /// Internet radio station owner (TRSO)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub radio_station_owner: Option<String>,

    // ==================== SORT ORDER ====================
    /// Album sort order (TSOA)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub album_sort: Option<String>,

    /// Performer sort order (TSOP)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub artist_sort: Option<String>,

    /// Title sort order (TSOT)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub title_sort: Option<String>,

    // ==================== OTHER TEXT ====================
    /// Original filename (TOFN)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub original_filename: Option<String>,

    /// Playlist delay in ms (TDLY)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub playlist_delay: Option<u32>,

    /// Encoder software/settings (TSSE)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub encoder_settings: Option<String>,

    // ==================== URLS ====================
    /// Commercial info URL (WCOM)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url_commercial: Option<String>,

    /// Copyright/legal URL (WCOP)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url_copyright: Option<String>,

    /// Official audio file URL (WOAF)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url_audio_file: Option<String>,

    /// Official artist URL (WOAR)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url_artist: Option<String>,

    /// Official audio source URL (WOAS)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url_audio_source: Option<String>,

    /// Official radio station URL (WORS)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url_radio_station: Option<String>,

    /// Payment URL (WPAY)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url_payment: Option<String>,

    /// Publisher URL (WPUB)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub url_publisher: Option<String>,

    /// User-defined URLs (WXXX)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub user_urls: Vec<UserUrl>,

    // ==================== COMPLEX FRAMES ====================
    /// Comments (COMM)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub comments: Vec<Comment>,

    /// Unsynchronized lyrics (USLT)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub lyrics: Vec<Lyrics>,

    /// Synchronized lyrics (SYLT)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub synced_lyrics: Vec<SyncedLyrics>,

    /// Attached pictures (APIC)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub pictures: Vec<Picture>,

    /// User-defined text (TXXX)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub user_text: Vec<UserText>,

    /// Play counter (PCNT)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub play_count: Option<u64>,

    /// Popularimeter/rating (POPM)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub popularimeter: Option<Popularimeter>,

    // ==================== VISUALIZATION (flo-unique) ====================
    /// Pre-computed waveform peaks for instant visualization
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub waveform_data: Option<WaveformData>,

    /// Spectral analysis data / audio fingerprint for visual EQ
    #[serde(default, skip_serializing_if = "Option::is_none")]
    #[serde(with = "serde_bytes_option")]
    pub spectrum_fingerprint: Option<Vec<u8>>,

    // ==================== TIMING & ANALYSIS (flo-unique) ====================
    /// BPM changes throughout the track
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub bpm_map: Vec<BpmChange>,

    /// Musical key changes with timestamps
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub key_changes: Vec<KeyChange>,

    /// Frame-by-frame loudness profile (ReplayGain dynamic)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub loudness_profile: Vec<LoudnessPoint>,

    /// Integrated loudness (LUFS): EBU R128
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub integrated_loudness_lufs: Option<f32>,

    /// Loudness range (LU)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub loudness_range_lu: Option<f32>,

    /// True peak (dBTP)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub true_peak_dbtp: Option<f32>,

    /// Section markers (intro/verse/chorus/etc.)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub section_markers: Vec<SectionMarker>,

    // ==================== CREATOR INFO (flo-unique) ====================
    /// Producer commentary with timestamps
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub creator_notes: Vec<CreatorNote>,

    /// Detailed collaboration credits
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub collaboration_credits: Vec<CollaborationCredit>,

    /// Remix/sample lineage chain
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub remix_chain: Vec<RemixChainEntry>,

    // ==================== COVERS (flo-unique) ====================
    /// Animated cover art (GIF/WebP/short video)
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub animated_cover: Option<AnimatedCover>,

    /// Alternative cover variants (explicit, remix, etc.)
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub cover_variants: Vec<CoverVariant>,

    /// Artist signature image
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub artist_signature: Option<Picture>,

    // ==================== flo-SPECIFIC ====================
    /// flo encoder version used
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub flo_encoder_version: Option<String>,

    /// Source format (e.g., "MP3", "FLAC", "WAV")
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source_format: Option<String>,

    /// Custom key-value pairs for extensions
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub custom: BTreeMap<String, String>,
}

// Helper for Option<Vec<u8>> serialization
mod serde_bytes_option {
    use alloc::vec::Vec;
    use serde::{Deserialize, Deserializer, Serialize, Serializer};

    pub fn serialize<S>(data: &Option<Vec<u8>>, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match data {
            Some(bytes) => serde_bytes::serialize(bytes, serializer),
            None => Option::<&[u8]>::None.serialize(serializer),
        }
    }

    pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<Vec<u8>>, D::Error>
    where
        D: Deserializer<'de>,
    {
        let opt: Option<serde_bytes::ByteBuf> = Option::deserialize(deserializer)?;
        Ok(opt.map(|b| b.into_vec()))
    }
}

impl FloMetadata {
    /// Create empty metadata
    pub fn new() -> Self {
        Self::default()
    }

    /// Create metadata with basic fields
    pub fn with_basic(
        title: Option<String>,
        artist: Option<String>,
        album: Option<String>,
    ) -> Self {
        Self {
            title,
            artist,
            album,
            ..Default::default()
        }
    }

    /// Serialize to MessagePack bytes
    pub fn to_msgpack(
        &self,
    ) -> Result<Vec<u8>, messagepack_serde::ser::Error<core::convert::Infallible>> {
        messagepack_serde::to_vec(self)
    }

    /// Deserialize from MessagePack bytes
    pub fn from_msgpack(
        data: &[u8],
    ) -> Result<Self, messagepack_serde::de::Error<messagepack_serde::messagepack_core::io::RError>>
    {
        messagepack_serde::from_slice(data)
    }

    /// Check if metadata is empty (no significant fields set)
    pub fn is_empty(&self) -> bool {
        self.title.is_none()
            && self.artist.is_none()
            && self.album.is_none()
            && self.pictures.is_empty()
            && self.comments.is_empty()
            && self.lyrics.is_empty()
            && self.synced_lyrics.is_empty()
    }

    // ==================== PICTURE HELPERS ====================

    /// Add a picture
    pub fn add_picture(&mut self, mime_type: &str, picture_type: PictureType, data: Vec<u8>) {
        self.pictures.push(Picture {
            mime_type: mime_type.to_string(),
            picture_type,
            description: None,
            data,
        });
    }

    /// Get the front cover picture if present
    pub fn front_cover(&self) -> Option<&Picture> {
        self.pictures
            .iter()
            .find(|p| p.picture_type == PictureType::CoverFront)
    }

    /// Get the first picture of any type
    pub fn any_picture(&self) -> Option<&Picture> {
        self.pictures.first()
    }

    // ==================== TEXT HELPERS ====================

    /// Add a comment
    pub fn add_comment(&mut self, text: &str, language: Option<&str>) {
        self.comments.push(Comment {
            language: language.map(|s| s.to_string()),
            description: None,
            text: text.to_string(),
        });
    }

    /// Add unsynchronized lyrics
    pub fn add_lyrics(&mut self, text: &str, language: Option<&str>) {
        self.lyrics.push(Lyrics {
            language: language.map(|s| s.to_string()),
            description: None,
            text: text.to_string(),
        });
    }

    /// Add synchronized lyrics line
    pub fn add_synced_lyrics_line(
        &mut self,
        timestamp_ms: u64,
        text: &str,
        language: Option<&str>,
    ) {
        let lang = language.map(|s| s.to_string());
        if let Some(synced) = self.synced_lyrics.iter_mut().find(|s| s.language == lang) {
            synced.lines.push(SyncedLyricsLine {
                timestamp_ms,
                text: text.to_string(),
            });
        } else {
            self.synced_lyrics.push(SyncedLyrics {
                language: lang,
                content_type: SyncedLyricsContentType::Lyrics,
                description: None,
                lines: vec![SyncedLyricsLine {
                    timestamp_ms,
                    text: text.to_string(),
                }],
            });
        }
    }

    // ==================== CUSTOM FIELD HELPERS ====================

    /// Set a custom field
    pub fn set_custom(&mut self, key: &str, value: &str) {
        self.custom.insert(key.to_string(), value.to_string());
    }

    /// Get a custom field
    pub fn get_custom(&self, key: &str) -> Option<&str> {
        self.custom.get(key).map(|s| s.as_str())
    }

    // ==================== HELPERS (flo-unique) ====================

    /// Add a section marker
    pub fn add_section(
        &mut self,
        timestamp_ms: u64,
        section_type: SectionType,
        label: Option<&str>,
    ) {
        self.section_markers.push(SectionMarker {
            timestamp_ms,
            section_type,
            label: label.map(|s| s.to_string()),
        });
    }

    /// Add a BPM change point
    pub fn add_bpm_change(&mut self, timestamp_ms: u64, bpm: f32) {
        self.bpm_map.push(BpmChange { timestamp_ms, bpm });
    }

    /// Add a key change point
    pub fn add_key_change(&mut self, timestamp_ms: u64, key: &str) {
        self.key_changes.push(KeyChange {
            timestamp_ms,
            key: key.to_string(),
        });
    }

    /// Add a creator note
    pub fn add_creator_note(&mut self, text: &str, timestamp_ms: Option<u64>) {
        self.creator_notes.push(CreatorNote {
            timestamp_ms,
            text: text.to_string(),
        });
    }

    /// Add collaboration credit
    pub fn add_collaboration(&mut self, role: &str, name: &str, timestamp_ms: Option<u64>) {
        self.collaboration_credits.push(CollaborationCredit {
            role: role.to_string(),
            name: name.to_string(),
            timestamp_ms,
        });
    }
}