FastLED 3.10.8
Loading...
Searching...
No Matches

◆ parseMp3Info()

Mp3Info fl::Mp3::parseMp3Info ( fl::span< const fl::u8 > data,
fl::string * error_message = nullptr )
static

Definition at line 638 of file mp3.cpp.hpp.

638 {
639 Mp3Info info;
640
641 // Validate input data
642 if (data.empty()) {
643 if (error_message) {
644 *error_message = "Empty MP3 data";
645 }
646 return info; // returns invalid info
647 }
648
649 // Minimum size check - need at least an MP3 frame header (4 bytes) + some data
650 if (data.size() < 128) {
651 if (error_message) {
652 *error_message = "MP3 data too small";
653 }
654 return info;
655 }
656
657 // Look for MP3 sync word (11 bits set: 0xFFE or 0xFFF)
658 bool foundSync = false;
659 fl::size syncOffset = 0;
660
661 for (fl::size i = 0; i <= data.size() - 4; i++) {
662 if (data[i] == 0xFF && (data[i + 1] & 0xE0) == 0xE0) {
663 foundSync = true;
664 syncOffset = i;
665 break;
666 }
667 }
668
669 if (!foundSync) {
670 if (error_message) {
671 *error_message = "Invalid MP3 stream - no sync word found";
672 }
673 return info;
674 }
675
676 // Use the decoder to parse the first frame
678 if (!decoder.init()) {
679 if (error_message) {
680 *error_message = "Failed to initialize MP3 decoder";
681 }
682 return info;
683 }
684
685 const fl::u8* inptr = data.data() + syncOffset;
686 fl::size bytes_left = data.size() - syncOffset;
687
688 // Try to decode first frame to get metadata
689 int offset = decoder.findSyncWord(inptr, bytes_left);
690 if (offset >= 0) {
691 inptr += offset;
692 bytes_left -= offset;
693
694 int result = decoder.decodeFrame(&inptr, &bytes_left);
695 if (result == 0) {
696 // Successfully decoded - extract metadata
697 info.sampleRate = decoder.mFrameInfo.samprate;
698 info.channels = static_cast<fl::u8>(decoder.mFrameInfo.nChans);
699 info.bitrate = decoder.mFrameInfo.bitrate;
700 info.version = static_cast<fl::u8>(decoder.mFrameInfo.version);
701 info.layer = static_cast<fl::u8>(decoder.mFrameInfo.layer);
702 info.isValid = true;
703 } else if (error_message) {
704 *error_message = "Failed to decode MP3 frame header";
705 }
706 } else if (error_message) {
707 *error_message = "Failed to find MP3 sync word";
708 }
709
710 return info;
711}
constexpr bool empty() const FL_NO_EXCEPT
Definition span.h:535
const T * data() const FL_NO_EXCEPT
Definition span.h:486
constexpr fl::size size() const FL_NO_EXCEPT
Definition span.h:483
fl::UISlider offset("Offset", 0.0f, 0.0f, 1.0f, 0.01f)
Mp3Minimp3Decoder Mp3SelectedDecoder
Definition mp3.cpp.hpp:264
unsigned char u8
Definition stdint.h:131
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31

References fl::Mp3Info::bitrate, fl::third_party::Mp3FrameInfo::bitrate, fl::Mp3Info::channels, fl::third_party::Mp3Minimp3Decoder::decodeFrame(), fl::third_party::Mp3Minimp3Decoder::findSyncWord(), fl::FL_NO_EXCEPT, fl::third_party::Mp3Minimp3Decoder::init(), fl::Mp3Info::isValid, fl::Mp3Info::layer, fl::third_party::Mp3FrameInfo::layer, fl::third_party::Mp3Minimp3Decoder::mFrameInfo, fl::third_party::Mp3FrameInfo::nChans, offset(), fl::Mp3Info::sampleRate, fl::third_party::Mp3FrameInfo::samprate, fl::Mp3Info::version, and fl::third_party::Mp3FrameInfo::version.

+ Here is the call graph for this function: