arduino-audio-tools
Loading...
Searching...
No Matches
Classes | Public Member Functions | Protected Types | Protected Member Functions | Protected Attributes | List of all members
HeaderParserMP3 Class Reference

MP3 header parser that processes MP3 data incrementally and extracts complete MP3 frames. Can validate MP3 data and extract audio information. When used with a Print output, it splits incoming data into complete MP3 frames and writes them to the output stream. More...

#include <HeaderParserMP3.h>

Public Member Functions

 HeaderParserMP3 ()=default
 Default constructor.
 
 HeaderParserMP3 (Print &output, int bufferSize=2048)
 Constructor for write support.
 
int findSyncWord (const uint8_t *buf, size_t nBytes, uint8_t synch=0xFF, uint8_t syncl=0xF0)
 Finds the mp3/aac sync word.
 
void flush ()
 
int getBitRate () const
 Bit rate from mp3 header.
 
int getChannels () const
 Number of channels from mp3 header.
 
FrameHeader getFrameHeader ()
 
int getFrameLength ()
 Frame length from mp3 header.
 
size_t getFrameRateHz ()
 frame rate in Hz (frames per second)
 
const chargetLayerStr () const
 Provides a string representation of the MPEG layer.
 
size_t getPlayingTime (size_t fileSizeBytes)
 
uint16_t getSampleRate () const
 Sample rate from mp3 header.
 
int getSamplesPerFrame ()
 number of samples per mp3 frame
 
size_t getTimePerFrameMs ()
 playing time per frame in ms
 
const chargetVersionStr () const
 Provides a string representation of the MPEG version.
 
bool hasValidFrame () const
 Returns true if we have parsed at least one valid frame.
 
bool isValid ()
 Returns true if a valid frame has been detected.
 
bool isValid (const uint8_t *data, int len)
 parses the header string and returns true if this is a valid mp3 file
 
void reset ()
 Clears internal buffer and resets state.
 
void resize (int size)
 
void setOutput (Print &output)
 
size_t write (const uint8_t *data, size_t len)
 split up the data into mp3 segements and write to output
 

Protected Types

enum class  FrameReason {
  VALID , INVALID_BITRATE_FOR_VERSION , INVALID_SAMPLERATE_FOR_VERSION , INVALID_MPEG_VERSION ,
  INVALID_LAYER , INVALID_LAYER_II_BITRATE_AND_MODE , INVALID_EMPHASIS , INVALID_CRC
}
 

Protected Member Functions

bool contains (const uint8_t *data, const char *toFind, size_t len)
 
bool processBuffer ()
 Processes the internal buffer to extract complete mp3 frames.
 
void readFrameHeader (const uint8_t *data)
 
int seekFrameSync (const uint8_t *str, size_t len)
 
bool validate (const uint8_t *data, size_t len)
 
FrameReason validateFrameHeader (const FrameHeader &header)
 

Protected Attributes

SingleBuffer< uint8_tbuffer {0}
 
size_t buffer_size = 0
 
bool frame_header_valid = false
 
FrameHeader header
 
size_t last_frame_size = 0
 
Printp_output = nullptr
 

Detailed Description

MP3 header parser that processes MP3 data incrementally and extracts complete MP3 frames. Can validate MP3 data and extract audio information. When used with a Print output, it splits incoming data into complete MP3 frames and writes them to the output stream.

Features:

Author
Phil Schatzmann

Member Enumeration Documentation

◆ FrameReason

enum class FrameReason
strongprotected
Enumerator
VALID 
INVALID_BITRATE_FOR_VERSION 
INVALID_SAMPLERATE_FOR_VERSION 
INVALID_MPEG_VERSION 
INVALID_LAYER 
INVALID_LAYER_II_BITRATE_AND_MODE 
INVALID_EMPHASIS 
INVALID_CRC 

Constructor & Destructor Documentation

◆ HeaderParserMP3() [1/2]

HeaderParserMP3 ( )
default

Default constructor.

◆ HeaderParserMP3() [2/2]

HeaderParserMP3 ( Print output,
int  bufferSize = 2048 
)
inline

Constructor for write support.

Member Function Documentation

◆ contains()

bool contains ( const uint8_t data,
const char toFind,
size_t  len 
)
inlineprotected

◆ findSyncWord()

int findSyncWord ( const uint8_t buf,
size_t  nBytes,
uint8_t  synch = 0xFF,
uint8_t  syncl = 0xF0 
)
inline

Finds the mp3/aac sync word.

◆ flush()

void flush ( )
inline

◆ getBitRate()

int getBitRate ( ) const
inline

Bit rate from mp3 header.

◆ getChannels()

int getChannels ( ) const
inline

Number of channels from mp3 header.

◆ getFrameHeader()

FrameHeader getFrameHeader ( )
inline

◆ getFrameLength()

int getFrameLength ( )
inline

Frame length from mp3 header.

◆ getFrameRateHz()

size_t getFrameRateHz ( )
inline

frame rate in Hz (frames per second)

◆ getLayerStr()

const char * getLayerStr ( ) const
inline

Provides a string representation of the MPEG layer.

◆ getPlayingTime()

size_t getPlayingTime ( size_t  fileSizeBytes)
inline

Provides the estimated playing time in seconds based on the bitrate of the first segment

◆ getSampleRate()

uint16_t getSampleRate ( ) const
inline

Sample rate from mp3 header.

◆ getSamplesPerFrame()

int getSamplesPerFrame ( )
inline

number of samples per mp3 frame

◆ getTimePerFrameMs()

size_t getTimePerFrameMs ( )
inline

playing time per frame in ms

◆ getVersionStr()

const char * getVersionStr ( ) const
inline

Provides a string representation of the MPEG version.

◆ hasValidFrame()

bool hasValidFrame ( ) const
inline

Returns true if we have parsed at least one valid frame.

◆ isValid() [1/2]

bool isValid ( )
inline

Returns true if a valid frame has been detected.

◆ isValid() [2/2]

bool isValid ( const uint8_t data,
int  len 
)
inline

parses the header string and returns true if this is a valid mp3 file

◆ processBuffer()

bool processBuffer ( )
inlineprotected

Processes the internal buffer to extract complete mp3 frames.

◆ readFrameHeader()

void readFrameHeader ( const uint8_t data)
inlineprotected

◆ reset()

void reset ( )
inline

Clears internal buffer and resets state.

◆ resize()

void resize ( int  size)
inline

◆ seekFrameSync()

int seekFrameSync ( const uint8_t str,
size_t  len 
)
inlineprotected

◆ setOutput()

void setOutput ( Print output)
inline

◆ validate()

bool validate ( const uint8_t data,
size_t  len 
)
inlineprotected

◆ validateFrameHeader()

FrameReason validateFrameHeader ( const FrameHeader &  header)
inlineprotected

◆ write()

size_t write ( const uint8_t data,
size_t  len 
)
inline

split up the data into mp3 segements and write to output

Member Data Documentation

◆ buffer

SingleBuffer<uint8_t> buffer {0}
protected

◆ buffer_size

size_t buffer_size = 0
protected

◆ frame_header_valid

bool frame_header_valid = false
protected

◆ header

FrameHeader header
protected

◆ last_frame_size

size_t last_frame_size = 0
protected

◆ p_output

Print* p_output = nullptr
protected

The documentation for this class was generated from the following file: