arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
Public Types | Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
M4ACommonDemuxer::SampleExtractor Class Reference

Extracts audio data based on the sample sizes defined in the stsz box. It collects the data from the mdat box and calls the callback with the extracted frames. More...

#include <M4ACommonDemuxer.h>

Public Types

using Codec = M4ACommonDemuxer::Codec
 
using Frame = M4ACommonDemuxer::Frame
 
using FrameCallback = std::function< void(const Frame &, void *)>
 
using M4AAudioConfig = M4ACommonDemuxer::M4AAudioConfig
 

Public Member Functions

 SampleExtractor (M4AAudioConfig &cfg)
 Constructor. Initializes the extractor.
 
void begin ()
 Resets the extractor state.
 
BaseBuffer< uint32_t > & getChunkOffsetsBuffer ()
 Returns the buffer of chunk offsets.
 
Frame getFrame (size_t size, SingleBuffer< uint8_t > &buffer)
 Constructs a Frame object for the current codec.
 
BaseBuffer< stsz_sample_size_t > & getSampleSizesBuffer ()
 Returns the buffer of sample sizes.
 
void setCallback (FrameCallback cb)
 Sets the callback to be called for each extracted frame.
 
void setChunkOffsetsBuffer (BaseBuffer< uint32_t > &buffer)
 Sets the buffer to use for chunk offsets.
 
void setFixedSampleCount (uint32_t sampleSize, uint32_t sampleCount)
 Sets a fixed sample size/count instead of using the sampleSizes table.
 
void setMaxSize (size_t size)
 Sets the maximum box size (e.g., for mdat). This is called before the mdat data is posted. In order to be able to play a file multiple times we just reset the sampleIndex!
 
void setReference (void *r)
 Sets a reference pointer passed to the callback.
 
void setSampleSizesBuffer (BaseBuffer< stsz_sample_size_t > &buffer)
 Sets the buffer to use for sample sizes.
 
size_t write (const uint8_t *data, size_t len, bool is_final)
 Writes data to the extractor, extracting frames as sample sizes are met. Provides the data via the callback.
 

Protected Member Functions

size_t currentSampleSize ()
 Returns the current sample size.
 
void executeCallback (size_t size)
 Executes the callback for a completed frame.
 
void resize (size_t newSize)
 Resizes the internal buffer if needed.
 

Static Protected Member Functions

static void writeAdtsHeader (uint8_t *adts, int aacProfile, int sampleRateIdx, int channelCfg, int frameLen)
 Writes an ADTS header for an AAC frame.
 

Protected Attributes

M4AAudioConfigaudio_config
 
size_t box_pos = 0
 Current position in the box.
 
size_t box_size = 0
 Maximum size of the current sample.
 
SingleBuffer< uint8_t > buffer
 Buffer for accumulating sample data.
 
FrameCallback callback = nullptr
 Frame callback.
 
size_t current_size = 0
 Current sample size.
 
SingleBuffer< uint32_t > defaultChunkOffsets
 Table of chunk offsets.
 
SingleBuffer< stsz_sample_size_tdefaultSampleSizes
 Table of sample sizes.
 
uint32_t fixed_sample_count = 0
 Fixed sample count (if used).
 
uint32_t fixed_sample_size = 0
 Fixed sample size (if used).
 
BaseBuffer< uint32_t > * p_chunk_offsets = &defaultChunkOffsets
 
BaseBuffer< stsz_sample_size_t > * p_sample_sizes = &defaultSampleSizes
 
void * ref = nullptr
 Reference pointer for callback.
 
size_t sampleIndex = 0
 Current sample index.
 
Vector< uint8_t > tmp
 

Detailed Description

Extracts audio data based on the sample sizes defined in the stsz box. It collects the data from the mdat box and calls the callback with the extracted frames.

Member Function Documentation

◆ currentSampleSize()

size_t currentSampleSize ( )
inlineprotected

Returns the current sample size.

Returns
Size of the current sample.

◆ executeCallback()

void executeCallback ( size_t  size)
inlineprotected

Executes the callback for a completed frame.

Parameters
sizeSize of the frame.

◆ getChunkOffsetsBuffer()

BaseBuffer< uint32_t > & getChunkOffsetsBuffer ( )
inline

Returns the buffer of chunk offsets.

Returns
Reference to the buffer of chunk offsets.

◆ getFrame()

Frame getFrame ( size_t  size,
SingleBuffer< uint8_t > &  buffer 
)
inline

Constructs a Frame object for the current codec.

Parameters
sizeSize of the frame.
bufferSingleBuffer with data.
Returns
Frame object.

◆ getSampleSizesBuffer()

BaseBuffer< stsz_sample_size_t > & getSampleSizesBuffer ( )
inline

Returns the buffer of sample sizes.

Returns
Reference to the buffer of sample sizes.

◆ resize()

void resize ( size_t  newSize)
inlineprotected

Resizes the internal buffer if needed.

Parameters
newSizeNew buffer size.

◆ setCallback()

void setCallback ( FrameCallback  cb)
inline

Sets the callback to be called for each extracted frame.

Parameters
cbCallback function.

◆ setChunkOffsetsBuffer()

void setChunkOffsetsBuffer ( BaseBuffer< uint32_t > &  buffer)
inline

Sets the buffer to use for chunk offsets.

Parameters
bufferReference to the buffer to use.

◆ setFixedSampleCount()

void setFixedSampleCount ( uint32_t  sampleSize,
uint32_t  sampleCount 
)
inline

Sets a fixed sample size/count instead of using the sampleSizes table.

Parameters
sampleSizeSize of each sample.
sampleCountNumber of samples.

◆ setMaxSize()

void setMaxSize ( size_t  size)
inline

Sets the maximum box size (e.g., for mdat). This is called before the mdat data is posted. In order to be able to play a file multiple times we just reset the sampleIndex!

Parameters
sizeMaximum size in bytes.

◆ setReference()

void setReference ( void *  r)
inline

Sets a reference pointer passed to the callback.

Parameters
rReference pointer.

◆ setSampleSizesBuffer()

void setSampleSizesBuffer ( BaseBuffer< stsz_sample_size_t > &  buffer)
inline

Sets the buffer to use for sample sizes.

Parameters
bufferReference to the buffer to use.

◆ write()

size_t write ( const uint8_t *  data,
size_t  len,
bool  is_final 
)
inline

Writes data to the extractor, extracting frames as sample sizes are met. Provides the data via the callback.

Parameters
dataPointer to input data.
lenLength of input data.
is_finalTrue if this is the last chunk of the box.
Returns
Number of bytes processed.

fill buffer up to the current sample size

◆ writeAdtsHeader()

static void writeAdtsHeader ( uint8_t *  adts,
int  aacProfile,
int  sampleRateIdx,
int  channelCfg,
int  frameLen 
)
inlinestaticprotected

Writes an ADTS header for an AAC frame.

Parameters
adtsOutput buffer for the header.
aacProfileAAC profile.
sampleRateIdxSample rate index.
channelCfgChannel configuration.
frameLenFrame length.

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