arduino-audio-tools
Loading...
Searching...
No Matches
Public Member Functions | Public Attributes | Protected Attributes | List of all members
FileSeekableSource< FileT, WriterT > Class Template Reference

Adapts a single concrete file/stream (Arduino's File, or anything else exposing position(), seek(size_t) and a readBytes(size_t) overload) to SeekableSource, and also drives feeding that same file's bytes into a DemuxerMP4 (or any other AudioWriter with a setSeekSource(SeekableSource&) method) via copy() - replacing the separate CodecCopy/StreamCopy a sketch would otherwise need, and letting this class use just one File for everything instead of requiring two independent handles (one for sequential forward reads, one for the out-of-band seeks SourceSeekSampleTableStore performs). More...

#include <SampleTableStore.h>

Inheritance diagram for FileSeekableSource< FileT, WriterT >:
SeekableSource

Public Member Functions

 FileSeekableSource (FileT &file, WriterT &writer, int bufferSize=1024, bool quickStart=true)
 
size_t copy ()
 
size_t readBytes (uint8_t *data, size_t len) override
 
bool seek (size_t pos) override
 

Public Attributes

bool debug_trace = false
 

Protected Attributes

int buffer_len
 
FileT * p_file
 
WriterT * p_writer
 
bool quick_start_done = false
 
bool quick_start_enabled
 
size_t saved_pos = 0
 

Detailed Description

template<typename FileT, typename WriterT>
class audio_tools::FileSeekableSource< FileT, WriterT >

Adapts a single concrete file/stream (Arduino's File, or anything else exposing position(), seek(size_t) and a readBytes(size_t) overload) to SeekableSource, and also drives feeding that same file's bytes into a DemuxerMP4 (or any other AudioWriter with a setSeekSource(SeekableSource&) method) via copy() - replacing the separate CodecCopy/StreamCopy a sketch would otherwise need, and letting this class use just one File for everything instead of requiring two independent handles (one for sequential forward reads, one for the out-of-band seeks SourceSeekSampleTableStore performs).

That's safe because seek()/readBytes() always come in a matched pair (SourceSeekSampleTableStore::get() never calls one without the other)

This is the one place the concrete file type appears, isolating SD.h/FS.h's include-order requirements to wherever the including sketch instantiates this adapter (after it has already included SD.h), rather than to this header or to DemuxerMP4 itself.

WriterT is templated (duck-typed on write()+setSeekSource(), rather than a fixed AudioWriter&/DemuxerMP4&) purely to avoid this header having to depend on ContainerMP4.h, which itself depends on this header - a hard circular include otherwise.

'quickStart' (constructor arg, default true): if WriterT has a quickStart() method (e.g. DemuxerMP4's), the first copy() call invokes it automatically. No-op for any WriterT without one. Set to false to skip that check or call writer.quickStart() manually yourself.

Constructor & Destructor Documentation

◆ FileSeekableSource()

template<typename FileT , typename WriterT >
FileSeekableSource ( FileT &  file,
WriterT &  writer,
int  bufferSize = 1024,
bool  quickStart = true 
)
inline

'file' must already be open, positioned at the start (byte 0) of the MP4. 'writer' is both what copy() feeds decoded bytes into, and gets wired up automatically here via writer.setSeekSource(*this) - no separate setSeekSource() call needed in the sketch. See the class comment above for 'quickStart'.

Member Function Documentation

◆ copy()

template<typename FileT , typename WriterT >
size_t copy ( )
inline

Reads the next chunk from the file and feeds it to the configured AudioWriter, retrying on a partial write - the same contract CodecCopy/StreamCopy follow, since DemuxerMP4::write() (like most Print::write() implementations) may accept fewer bytes than requested in one call. Returns 0 once the file is exhausted.

On the first call, if enabled (see 'quickStart'), runs writer.quickStart() first - not done in the constructor since it must run after writer.begin(), which typically hasn't happened yet at construction time.

◆ readBytes()

template<typename FileT , typename WriterT >
size_t readBytes ( uint8_t *  data,
size_t  len 
)
inlineoverridevirtual

Implements SeekableSource.

◆ seek()

template<typename FileT , typename WriterT >
bool seek ( size_t  pos)
inlineoverridevirtual

Implements SeekableSource.

Member Data Documentation

◆ buffer_len

template<typename FileT , typename WriterT >
int buffer_len
protected

◆ debug_trace

template<typename FileT , typename WriterT >
bool debug_trace = false

Set true to LOGI every seek()/readBytes()/copy() call with the raw file positions involved - off by default (adds overhead on what can be a hot path during playback); a debugging aid, not something a normal sketch needs to touch.

◆ p_file

template<typename FileT , typename WriterT >
FileT* p_file
protected

◆ p_writer

template<typename FileT , typename WriterT >
WriterT* p_writer
protected

◆ quick_start_done

template<typename FileT , typename WriterT >
bool quick_start_done = false
protected

◆ quick_start_enabled

template<typename FileT , typename WriterT >
bool quick_start_enabled
protected

◆ saved_pos

template<typename FileT , typename WriterT >
size_t saved_pos = 0
protected

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