arduino-audio-tools
Loading...
Searching...
No Matches
WAVFileInfo.h
Go to the documentation of this file.
1#pragma once
2
4#include "FS.h"
5
6namespace audio_tools {
7
19template <typename FileT = File>
21 public:
22 WAVFileInfo() = default;
23
36 bool getInfo(FileT file, WAVAudioInfo& info) {
37 WAVHeader header;
39 size_t bytesRead = file.read(buffer, MAX_WAV_HEADER_LEN);
40 if (bytesRead > 0) {
41 header.write(buffer, bytesRead);
42 if (header.isDataComplete() && header.parse()) {
43 info = header.audioInfo();
44 return true;
45 }
46 }
47 return false;
48 }
49
60 bool updateSize(FileT file, WAVAudioInfo& info) {
61 size_t fileSize = file.size();
62 info.file_size = fileSize;
63 info.data_length = fileSize - 36; // Assuming standard 44-byte header
64 info.is_streamed = false; // File-based WAV is not streamed
65 WAVHeader header;
66 if (!header.writeHeader(&file, info)) {
67 LOGE("Failed to write updated header with new file size");
68 return false; // write failed
69 }
70 return true;
71 }
72
87 bool updateSize(FileT file) {
88 if (!file.seek(0)) {
89 LOGE("Failed to seek to beginning of file for header read");
90 return false;
91 }
92
93 WAVAudioInfo info;
94 if (!getInfo(file, info)) {
95 LOGE("Failed to read existing header for size update");
96 return false; // read failed
97 }
98 if (!file.seek(0)) {
99 LOGE("Failed to seek to beginning of file for header rewrite");
100 return false; // seek failed
101 }
102 return updateSize(file, info);
103 }
104};
105
106} // namespace audio_tools
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define MAX_WAV_HEADER_LEN
Definition CodecWAV.h:9
Utility for reading and rewriting WAV file metadata in place.
Definition WAVFileInfo.h:20
bool updateSize(FileT file)
Reads the current WAV header and rewrites it with the actual file size.
Definition WAVFileInfo.h:87
bool updateSize(FileT file, WAVAudioInfo &info)
Rewrites the WAV header with the current file size information.
Definition WAVFileInfo.h:60
bool getInfo(FileT file, WAVAudioInfo &info)
Reads and parses the WAV header from a file.
Definition WAVFileInfo.h:36
Parser for Wav header data for details see https://de.wikipedia.org/wiki/RIFF_WAVE.
Definition CodecWAV.h:65
bool parse()
Call when header data write is complete to parse the data.
Definition CodecWAV.h:76
bool isDataComplete()
Returns true if the header is complete (containd data tag)
Definition CodecWAV.h:106
bool writeHeader(Print *out)
Just write a wav header to the indicated outputbu.
Definition CodecWAV.h:129
WAVAudioInfo & audioInfo()
provides the info from the header
Definition CodecWAV.h:123
int write(uint8_t *data, size_t data_len)
Definition CodecWAV.h:71
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
Sound information which is available in the WAV header.
Definition CodecWAV.h:19
bool is_streamed
Definition CodecWAV.h:30
uint32_t file_size
Definition CodecWAV.h:33
uint32_t data_length
Definition CodecWAV.h:32