arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
M4AFileSampleSizeBuffer.h
1#pragma once
2
3#include "AudioTools/CoreAudio/AudioPlayer.h"
4#include "AudioTools/CoreAudio/Buffers.h"
5#include "M4AAudioFileDemuxer.h"
6
7namespace audio_tools {
8
23class M4AFileSampleSizeBuffer : public BaseBuffer<stsz_sample_size_t> {
24 public:
31 const char* fileExt = ".m4a") {
32 this->p_player = &player;
33 this->p_container = &container;
34 player.setReference(this);
36 addFileExtension(fileExt);
37 }
38
44 bool read(stsz_sample_size_t& data) override {
45 if (p_file != nullptr && demuxer.getMdatOffset() == 0) {
46 uint32_t offset = p_container->getDemuxer().getStszFileOffset();
47 uint32_t s_count = p_container->getDemuxer().getSampleCount();
48 demuxer.beginSampleSizeAccess(p_file, s_count, offset);
49 }
50 size_t pos = p_file->position();
52 p_file->seek(pos); // reset position after reading
53 return demuxer;
54 }
55
62
67 void addFileExtension(const char* fileExt) {
68 fileExtensions.push_back(fileExt);
69 }
70
71 void reset() {}
72
78 bool write(stsz_sample_size_t data) { return true; }
79
85 bool peek(stsz_sample_size_t& result) { return false; }
86
92 int available() { return demuxer.sampleIndex(); };
93
98 int availableForWrite() override { return 0; }
99
104 size_t size() override { return demuxer.size(); }
105
110 stsz_sample_size_t* address() override { return nullptr; }
111
112 protected:
114 File* p_file = nullptr;
117 ContainerM4A* p_container = nullptr;
118
125 bool isRelevantFile(const char* name) {
126 for (const auto& ext : fileExtensions) {
127 if (StrView(name).endsWith(name)) return true;
128 }
129 return false;
130 }
131
138 static void onFileChange(Stream* streamPtr, void* reference) {
140 *static_cast<M4AFileSampleSizeBuffer*>(reference);
141 self.p_file = (File*)streamPtr;
142 LOGI("===> M4AFileSampleSizeBuffer onFileChange: %s",
143 self.p_file ? self.p_file->name() : "nullptr");
144 }
145};
146
147} // namespace audio_tools
Implements a simple audio player which supports the following commands:
Definition AudioPlayer.h:38
void setOnStreamChangeCallback(void(*callback)(Stream *stream_ptr, void *reference))
Defines a callback that is called when the stream is changed.
Definition AudioPlayer.h:498
void setReference(void *ref)
this is used to set the reference for the stream change callback
Definition AudioPlayer.h:477
Shared functionality of all buffers.
Definition Buffers.h:22
M4A Demuxer that extracts audio from M4A/MP4 containers. The audio is decoded into pcm with the help ...
Definition ContainerM4A.h:17
Demuxer for M4A/MP4 files to extract audio data using an Arduino File. This class locates the mdat an...
Definition M4AAudioFileDemuxer.h:25
void setSamplesBufferSize(int size)
Sets the size of the samples buffer (in bytes).
Definition M4AAudioFileDemuxer.h:76
uint32_t getNextSampleSize()
Provides the next sample size (= frame size) from the stsz box queue.
Definition M4AAudioFileDemuxer.h:153
void beginSampleSizeAccess(File *filePtr, uint32_t sampleCount, uint32_t stszOffset)
Initializes the demuxer for reading sample sizes from the stsz box.
Definition M4AAudioFileDemuxer.h:192
uint32_t getSampleCount() const
samples in stsz
Definition M4ACommonDemuxer.h:427
uint32_t getStszFileOffset() const
File offset of stsz box.
Definition M4ACommonDemuxer.h:422
A buffer that reads sample sizes from an M4A file using the M4AAudioFileDemuxer. No RAM is used to st...
Definition M4AFileSampleSizeBuffer.h:23
void addFileExtension(const char *fileExt)
Add a file extension to recognize as relevant for this buffer.
Definition M4AFileSampleSizeBuffer.h:67
size_t size() override
Returns the total number of samples in the file.
Definition M4AFileSampleSizeBuffer.h:104
M4AAudioFileDemuxer demuxer
Demuxer used to extract sample sizes.
Definition M4AFileSampleSizeBuffer.h:115
static void onFileChange(Stream *streamPtr, void *reference)
Static callback for file change events. Updates the file pointer and re-parses the file if relevant.
Definition M4AFileSampleSizeBuffer.h:138
void setReadBufferSize(size_t size)
Defines how many samples are buffered with each file read.
Definition M4AFileSampleSizeBuffer.h:61
M4AFileSampleSizeBuffer(AudioPlayer &player, ContainerM4A &container, const char *fileExt=".m4a")
Constructor.
Definition M4AFileSampleSizeBuffer.h:30
int available()
Returns the number of samples already read (i.e., the current sample index).
Definition M4AFileSampleSizeBuffer.h:92
Vector< const char * > fileExtensions
List of recognized file extensions.
Definition M4AFileSampleSizeBuffer.h:116
File * p_file
Pointer to the currently open file.
Definition M4AFileSampleSizeBuffer.h:114
bool write(stsz_sample_size_t data)
Write is ignored; sample sizes are read directly from the file.
Definition M4AFileSampleSizeBuffer.h:78
AudioPlayer * p_player
Pointer to the AudioPlayer instance.
Definition M4AFileSampleSizeBuffer.h:113
int availableForWrite() override
Returns the available space for writing.
Definition M4AFileSampleSizeBuffer.h:98
bool peek(stsz_sample_size_t &result)
Peek is not supported for this buffer.
Definition M4AFileSampleSizeBuffer.h:85
stsz_sample_size_t * address() override
Returns a pointer to the buffer's physical address.
Definition M4AFileSampleSizeBuffer.h:110
bool isRelevantFile(const char *name)
Checks if the given file name matches any of the registered extensions.
Definition M4AFileSampleSizeBuffer.h:125
void reset()
clears the buffer
Definition M4AFileSampleSizeBuffer.h:71
bool read(stsz_sample_size_t &data) override
Get the next sample size from the demuxer.
Definition M4AFileSampleSizeBuffer.h:44
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:28
virtual bool endsWith(const char *str)
checks if the string ends with the indicated substring
Definition StrView.h:178
Definition NoArduino.h:142
Arduino File support using std::fstream.
Definition VFSFile.h:33
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
uint16_t stsz_sample_size_t
Definition M4ACommonDemuxer.h:17