8#define READ_BUFFER_SIZE 512
9#define MAX_WAV_HEADER_LEN 200
14#define MAX_WAV_HEADER_LEN_LIMIT 2048
20#define WAV_ENCODER_COMPRESSED_CHUNK_SIZE 1024
22#define MAX_WAV_EXTRA_DATA_LEN 64
116 if (!
setPos(
"RIFF"))
return false;
119 if (!
setPos(
"WAVE"))
return false;
120 if (!
setPos(
"fmt "))
return false;
130 if (!
setPos(
"data"))
return false;
163 return pos > 0 ? pos + 8 : 0;
195 if (written != len) {
196 LOGE(
"Failed to write WAV header to output: written %d of %d bytes", written, len);
198 return written == len;
241 LOGI(
"Header: %s", msg);
254 int max_size = fmt_length - 18;
256 if (size > max_size) size = max_size;
261 for (
int j = 0; j < 16; j++) guid[j] =
getChar();
271 static const uint8_t atrac3plus[16] = {0xBF, 0xAA, 0x23, 0xE9, 0x58, 0xCB,
272 0x71, 0x44, 0xA1, 0x19, 0xFF, 0xFA,
273 0x01, 0xE4, 0xCE, 0x62};
274 static const uint8_t atrac9[16] = {0xD2, 0x42, 0xE1, 0x47, 0xBA, 0x36,
275 0x8D, 0x4D, 0x88, 0xFC, 0x61, 0x65,
276 0x4F, 0x8C, 0x83, 0x6C};
283 int id_len = strlen(
id);
285 if (pos < 0)
return false;
325 for (i = 0; i < n; i++)
getChar();
335 void seek(
long int offset,
int origin) {
336 if (origin == SEEK_SET) {
338 }
else if (origin == SEEK_CUR) {
360 LOGI(
"writeRiffHeader: file_size=%u riff_size=%u", info.
file_size, chunk_size);
368 uint16_t fmt_len = 16;
369 if (is_ima_adpcm) fmt_len = 20;
370 else if (is_ms_adpcm) fmt_len = 50;
389 uint16_t bits_per_coded_sample =
404 }
else if (is_ms_adpcm) {
406 static const int16_t ms_adpcm_coef[7][2] = {
407 {256, 0}, {512, -256}, {0, 0}, {192, 64},
408 {240, 0}, {460, -208}, {392, -232}};
412 for (
auto &c : ms_adpcm_coef) {
440 data_length = info.
file_size - header_bytes;
442 LOGI(
"writeDataHeader: data_length=%u", data_length);
447 memset(empty, 0, offset);
556 "addDecoder: could not determine the WAV format tag for this "
557 "decoder - please provide it explicitly");
560 for (
int i = 0; i <
decoders.size(); i++) {
646 if (data_start == 0)
return len;
651 result = data_start +
743 result =
out().
write(in_ptr, in_size);
750 size_t samples_remaining = in_size;
753 while (samples_remaining > 0) {
754 size_t current_batch =
756 for (
size_t i = 0; i < current_batch; ++i) {
757 out_buf[i] = ((
int16_t)in_ptr[offset + i] - 128) << 8;
759 writeDataT<int16_t>(&
out(), out_buf, current_batch);
760 offset += current_batch;
761 samples_remaining -= current_batch;
772 for (
size_t i = 0; i < in_size; i++) {
779 int32_t converted_sample = sample24.scale32();
798 for (
size_t i = 0; i < in_size; i++) {
804 if (sample_f > 1.0f) sample_f = 1.0f;
805 if (sample_f < -1.0f) sample_f = -1.0f;
821 size_t remaining = in_size;
824 while (remaining > 0) {
825 size_t current_batch =
827 for (
size_t i = 0; i < current_batch; ++i) {
830 writeDataT<int16_t>(&
out(), out_buf, current_batch);
831 offset += current_batch;
832 remaining -= current_batch;
839 size_t remaining = in_size;
842 while (remaining > 0) {
843 size_t current_batch =
845 for (
size_t i = 0; i < current_batch; ++i) {
848 writeDataT<int16_t>(&
out(), out_buf, current_batch);
849 offset += current_batch;
850 remaining -= current_batch;
858 int t = (a_val & 0x0f) << 4;
859 int seg = ((unsigned)a_val & 0x70) >> 4;
872 return (
int16_t)((a_val & 0x80) ? t : -t);
878 int t = ((u_val & 0x0f) << 3) + 0x84;
879 t <<= ((unsigned)u_val & 0x70) >> 4;
880 return (
int16_t)((u_val & 0x80) ? (0x84 - t) : (t - 0x84));
898 "WAV header exceeds the maximum supported size of %d bytes "
899 "without a 'data' chunk - aborting",
905 LOGW(
"WAV header misses 'data' section in len: %d",
914 LOGE(
"WAV header parsing failed");
944 LOGE(
"WAV format not supported: 0x%04X", (
unsigned)format);
949 int result = data_start - buffered_before;
950 if (result < 0) result = 0;
951 if (result > (
int)in_size) result = (
int)in_size;
960 "Natively supported: PCM (0x%04X), IEEE_FLOAT (0x%04X), ALAW "
961 "(0x%04X), MULAW (0x%04X)",
966 "No additional decoders registered - use "
967 "setDecoder()/addDecoder() to support e.g. ADPCM formats");
969 for (
int i = 0; i <
decoders.size(); i++) {
970 LOGE(
"Registered decoder for format: 0x%04X",
978 for (
int i = 0; i <
decoders.size(); i++) {
1083 info.is_streamed =
true;
1084 info.is_valid =
true;
1085 info.data_length = 0x7fff0000;
1086 info.file_size =
info.data_length + 36;
1145 LOGI(
"is_streamed! because length is %u",
1167 LOGE(
"The WAVEncoder is not open - please call begin()");
1172 LOGE(
"No output stream was provided");
1177 LOGI(
"Writing Header");
1179 LOGE(
"Failed to write WAV header");
1195 size_t write_size = min((
size_t)len, (
size_t)
size_limit);
1210 if (frame_size <= 0) frame_size = 1;
1212 if (chunk == 0) chunk = frame_size;
1214 while (result < len && size_limit > 0) {
1215 size_t n = min(chunk, len - result);
1217 size_t accepted = p_out->
write((
uint8_t *)data + result, n);
1220 if (accepted < n)
break;
1225 LOGI(
"The defined size was written - so we close the WAVEncoder now");
1336 "setupEncodedAudio: no output stream was provided - call "
1337 "setOutput() before begin()");
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define MAX_WAV_HEADER_LEN
Definition CodecWAV.h:9
#define MAX_WAV_EXTRA_DATA_LEN
Maximum size of the codec specific data of the 'fmt ' chunk that we keep.
Definition CodecWAV.h:22
#define MAX_WAV_HEADER_LEN_LIMIT
Definition CodecWAV.h:14
#define WAV_ENCODER_COMPRESSED_CHUNK_SIZE
Definition CodecWAV.h:20
#define assert(T)
Definition avr.h:10
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
AudioFormat
Audio format codes used by Microsoft e.g. in avi or wav files.
Definition AudioFormat.h:21
@ EXTENSIBLE
WAVE_FORMAT_EXTENSIBLE: the real format is defined by a sub format GUID.
Associates a WAV format tag with the decoder responsible for it.
Definition CodecWAV.h:695
AudioFormat format
Definition CodecWAV.h:696
AudioDecoderExt * decoder
Definition CodecWAV.h:697
DecoderEntry(AudioFormat fmt, AudioDecoderExt *dec)
Definition CodecWAV.h:699