arduino-audio-tools
Loading...
Searching...
No Matches
CodecWAV.h
Go to the documentation of this file.
1#pragma once
2
7
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
23
24namespace audio_tools {
25
51
52static const char *wav_mime = "audio/wav";
53
60static bool isADPCMFormat(AudioFormat format) {
61 return format == AudioFormat::ADPCM || format == AudioFormat::DVI_ADPCM ||
63}
64
91class WAVHeader {
92 public:
93 WAVHeader() = default;
94
99 int write(uint8_t *data, size_t data_len) {
100 size_t needed = (size_t)buffer.available() + data_len;
101 if (needed > buffer.size() && buffer.size() < MAX_WAV_HEADER_LEN_LIMIT) {
102 size_t new_size = needed < (size_t)MAX_WAV_HEADER_LEN_LIMIT
103 ? needed
105 buffer.resize(new_size);
106 }
107 return buffer.writeArray(data, data_len);
108 }
109
111 bool parse() {
112 LOGI("WAVHeader::begin: %u", (unsigned)buffer.available());
113 this->data_pos = 0l;
114 memset((void *)&headerInfo, 0, sizeof(WAVAudioInfo));
115
116 if (!setPos("RIFF")) return false;
117 // RIFF stores chunk_size (= file_size - 8): normalize to full file size
119 if (!setPos("WAVE")) return false;
120 if (!setPos("fmt ")) return false;
121 int fmt_length = read_int32();
129 if (fmt_length >= 18) parseFormatExtension(fmt_length);
130 if (!setPos("data")) return false;
132 if (headerInfo.data_length == 0 || headerInfo.data_length >= 0x7fff0000) {
133 headerInfo.is_streamed = true;
135 }
136
137 logInfo();
138 buffer.clear();
139 return true;
140 }
141
144 int pos = getDataPos();
145 return pos > 0 && buffer.available() >= pos;
146 }
147
151 bool isOverflow() {
153 }
154
156 size_t available() { return buffer.available(); }
157
160 int pos =
162 .indexOf("data");
163 return pos > 0 ? pos + 8 : 0;
164 }
165
168
174
176 void setAudioInfo(WAVAudioInfo info) { headerInfo = info; }
177
179 bool writeHeader(Print *out) {
180 return writeHeader(out, headerInfo);
181 }
182
184 bool writeHeader(Print *out, const WAVAudioInfo &info) {
185 // reset first: buffer otherwise keeps accumulating bytes from earlier calls
186 buffer.reset();
187 writeRiffHeader(buffer, info);
188 writeFMT(buffer, info);
189 if (isADPCMFormat(info.format) && info.ext_adpcm_header) {
190 writeFactChunk(buffer, info);
191 }
192 writeDataHeader(buffer, info);
193 int len = buffer.available();
194 int written = out->write(buffer.data(), len);
195 if (written != len) {
196 LOGE("Failed to write WAV header to output: written %d of %d bytes", written, len);
197 }
198 return written == len;
199 }
200
205 static int extraHeaderBytes(const WAVAudioInfo &info) {
206 if (!info.ext_adpcm_header) return 0;
207 switch (info.format) {
208 case AudioFormat::ADPCM: // MS ADPCM: 34 byte fmt extension + 12 byte fact chunk
209 return 34 + 12;
210 case AudioFormat::DVI_ADPCM: // IMA/DVI ADPCM: 4 byte fmt extension + 12 byte fact chunk
211 return 4 + 12;
212 default:
213 return 0;
214 }
215 }
216
218 void clear() {
219 data_pos = 0;
220 WAVAudioInfo empty;
221 empty.sample_rate = 0;
222 empty.channels = 0;
223 empty.bits_per_sample = 0;
224 headerInfo = empty;
227 buffer.reset();
228 }
229
231 void dumpHeader() {
232 char msg[buffer.available() + 1];
233 memset(msg, 0, buffer.available() + 1);
234 for (int j = 0; j < buffer.available(); j++) {
235 char c = (char)buffer.data()[j];
236 if (!isalpha(c)) {
237 c = '.';
238 }
239 msg[j] = c;
240 }
241 LOGI("Header: %s", msg);
242 }
243
244 protected:
248 size_t data_pos = 0;
249
253 void parseFormatExtension(int fmt_length) {
254 int max_size = fmt_length - 18;
255 int size = read_int16();
256 if (size > max_size) size = max_size;
258 if (headerInfo.format == AudioFormat::EXTENSIBLE && size >= 22) {
259 skip(6); // valid bits per sample, channel mask
260 uint8_t guid[16];
261 for (int j = 0; j < 16; j++) guid[j] = getChar();
263 size -= 22;
264 }
265 extradata.resize(size);
266 for (int j = 0; j < size; j++) extradata[j] = getChar();
267 }
268
270 static AudioFormat formatFromGuid(const uint8_t *guid) {
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};
277 if (memcmp(guid, atrac3plus, 16) == 0) return AudioFormat::ATRAC3_PLUS;
278 if (memcmp(guid, atrac9, 16) == 0) return AudioFormat::ATRAC9;
280 }
281
282 bool setPos(const char *id) {
283 int id_len = strlen(id);
284 int pos = indexOf(id);
285 if (pos < 0) return false;
286 data_pos = pos + id_len;
287 return true;
288 }
289
290 int indexOf(const char *str) {
291 return StrView((char *)buffer.data(), MAX_WAV_HEADER_LEN,
293 .indexOf(str);
294 }
295
297 uint32_t tag = 0;
298 tag = (tag << 8) | getChar();
299 tag = (tag << 8) | getChar();
300 tag = (tag << 8) | getChar();
301 tag = (tag << 8) | getChar();
302 return tag;
303 }
304
305 uint32_t getChar32() { return getChar(); }
306
308 uint32_t value = 0;
309 value |= getChar32() << 0;
310 value |= getChar32() << 8;
311 value |= getChar32() << 16;
312 value |= getChar32() << 24;
313 return value;
314 }
315
316 uint16_t read_int16() {
317 uint16_t value = 0;
318 value |= getChar() << 0;
319 value |= getChar() << 8;
320 return value;
321 }
322
323 void skip(int n) {
324 int i;
325 for (i = 0; i < n; i++) getChar();
326 }
327
328 int getChar() {
329 if (data_pos < buffer.size())
330 return buffer.data()[data_pos++];
331 else
332 return -1;
333 }
334
335 void seek(long int offset, int origin) {
336 if (origin == SEEK_SET) {
337 data_pos = offset;
338 } else if (origin == SEEK_CUR) {
339 data_pos += offset;
340 }
341 }
342
343 size_t tell() { return data_pos; }
344
345 bool eof() { return data_pos >= buffer.size() - 1; }
346
347 void logInfo() {
348 LOGI("WAVHeader sound_pos: %d", getDataPos());
349 LOGI("WAVHeader channels: %d ", headerInfo.channels);
350 LOGI("WAVHeader bits_per_sample: %d", headerInfo.bits_per_sample);
351 LOGI("WAVHeader sample_rate: %d ", (int)headerInfo.sample_rate);
352 LOGI("WAVHeader format: %d", (int)headerInfo.format);
353 }
354
356 const WAVAudioInfo &info) {
357 buffer.writeArray((uint8_t *)"RIFF", 4);
358 // chunk_size = file_size - 8 (RIFF header size)
359 uint32_t chunk_size = info.file_size > 8 ? info.file_size - 8 : 0;
360 LOGI("writeRiffHeader: file_size=%u riff_size=%u", info.file_size, chunk_size);
361 write32(buffer, chunk_size);
362 buffer.writeArray((uint8_t *)"WAVE", 4);
363 }
364
366 bool is_ms_adpcm = info.ext_adpcm_header && info.format == AudioFormat::ADPCM;
367 bool is_ima_adpcm = info.ext_adpcm_header && info.format == AudioFormat::DVI_ADPCM;
368 uint16_t fmt_len = 16;
369 if (is_ima_adpcm) fmt_len = 20;
370 else if (is_ms_adpcm) fmt_len = 50;
371
372 uint16_t spb = samplesPerBlock(info);
373 uint32_t byte_rate = info.byte_rate;
374 // use the real average byte rate for ADPCM formats whenever the block
375 // layout is known, regardless of whether the extended 'fmt '/'fact'
376 // chunk is written: byte_rate is informational (e.g. used by some
377 // players for scrubbing/duration estimates) and the linear-PCM-based
378 // default in info.byte_rate is misleading for compressed formats
379 if (isADPCMFormat(info.format) && spb > 0 && info.block_align > 0) {
380 // average bytes/sec = (sample_rate * block_align) / samples_per_block
381 byte_rate = ((uint64_t)info.sample_rate * info.block_align) / spb;
382 }
383
384 // wBitsPerSample for ADPCM formats is the number of bits per *coded*
385 // sample (always 4 for MS/IMA WAV ADPCM), not the decoded PCM depth
386 // tracked in info.bits_per_sample (typically 16) - writing 16 here
387 // makes strict decoders (e.g. ffmpeg) reject the stream since it is
388 // outside the codec's valid range
389 uint16_t bits_per_coded_sample =
390 isADPCMFormat(info.format) ? 4 : info.bits_per_sample;
391
392 buffer.writeArray((uint8_t *)"fmt ", 4);
393 write32(buffer, fmt_len);
394 write16(buffer, (uint16_t)info.format);
395 write16(buffer, info.channels);
397 write32(buffer, byte_rate);
398 write16(buffer, info.block_align); // frame size
399 write16(buffer, bits_per_coded_sample);
400
401 if (is_ima_adpcm) {
402 write16(buffer, 2); // cbSize: size of extra format bytes
403 write16(buffer, spb); // wSamplesPerBlock
404 } else if (is_ms_adpcm) {
405 // standard MS ADPCM coefficient table (7 predictor pairs)
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}};
409 write16(buffer, 32); // cbSize: size of extra format bytes
410 write16(buffer, spb); // wSamplesPerBlock
411 write16(buffer, 7); // wNumCoef
412 for (auto &c : ms_adpcm_coef) {
413 write16(buffer, (uint16_t)c[0]);
414 write16(buffer, (uint16_t)c[1]);
415 }
416 }
417 }
418
422 buffer.writeArray((uint8_t *)"fact", 4);
423 write32(buffer, 4); // chunk size
424 uint32_t sample_length = 0;
425 uint16_t spb = samplesPerBlock(info);
426 if (!info.is_streamed && spb > 0 && info.block_align > 0) {
427 sample_length = (info.data_length / info.block_align) * spb;
428 }
429 write32(buffer, sample_length);
430 }
431
433 buffer.writeArray((uint8_t *)"data", 4);
434 uint32_t data_length = info.data_length;
435 uint32_t header_bytes = 36 + extraHeaderBytes(info);
436 if (headerInfo.is_streamed && data_length == 0) {
437 data_length = ~0; // use max value for streamed data if not set
438 }
439 if (!headerInfo.is_streamed && info.file_size >= header_bytes && (data_length == 0 || data_length == ~0)) {
440 data_length = info.file_size - header_bytes; // data length = file size - header size
441 }
442 LOGI("writeDataHeader: data_length=%u", data_length);
443 write32(buffer, data_length);
444 int offset = info.offset;
445 if (offset > 0) {
446 uint8_t empty[offset];
447 memset(empty, 0, offset);
448 buffer.writeArray(empty, offset); // resolve issue with wrong aligment
449 }
450 }
451
454 static uint16_t samplesPerBlock(const WAVAudioInfo &info) {
455 if (info.channels <= 0 || info.block_align <= 0) return 0;
456 switch (info.format) {
457 case AudioFormat::ADPCM: // MS ADPCM
458 return ((info.block_align / info.channels) - 7) * 2 + 2;
459 case AudioFormat::DVI_ADPCM: // IMA/DVI ADPCM
460 return ((info.block_align / info.channels) - 4) * 2 + 1;
461 default:
462 return 0;
463 }
464 }
465
467 buffer.writeArray((uint8_t *)&value, 4);
468 }
469
470 void write16(BaseBuffer<uint8_t> &buffer, uint16_t value) {
471 buffer.writeArray((uint8_t *)&value, 2);
472 }
473
474};
475
510class WAVDecoder : public AudioDecoder {
511
512 public:
516 WAVDecoder() = default;
517
525
538 TRACED();
539 decoders.clear();
540 addDecoder(dec, fmt);
541 }
542
552 TRACED();
553 if (fmt == AudioFormat::UNKNOWN) fmt = dec.wavFormat();
554 if (fmt == AudioFormat::UNKNOWN) {
555 LOGE(
556 "addDecoder: could not determine the WAV format tag for this "
557 "decoder - please provide it explicitly");
558 return;
559 }
560 for (int i = 0; i < decoders.size(); i++) {
561 if (decoders[i].format == fmt) {
562 decoders[i].decoder = &dec;
563 return;
564 }
565 }
566 decoders.push_back({fmt, &dec});
567 }
568
570 void setOutput(Print &out_stream) override { this->p_print = &out_stream; }
571
573 bool begin() override {
574 TRACED();
575 header.clear();
576 // close the decoder used for a previous stream (if any) before
577 // selecting a new one for this stream: the match is only known once
578 // the header has been parsed, so setupEncodedAudio() is triggered from
579 // decodeHeader() instead of here
582 buffer24.reset();
585 isFirst = true;
586 active = true;
587 return true;
588 }
589
591 void end() override {
592 TRACED();
595 buffer24.reset();
597 active = false;
598 }
599
601 const char *mime() { return wav_mime; }
602
605
607 AudioInfo audioInfo() override {
609 if (convert8to16 && info.format == AudioFormat::PCM &&
610 info.bits_per_sample == 8) {
612 }
613 // 32 bits gives better result
614 if (convert24 && info.format == AudioFormat::PCM &&
615 info.bits_per_sample == 24) {
617 }
618 // ALAW/MULAW are expanded from 8-bit logarithmic to 16-bit linear PCM
619 if (convertALawMuLaw && (info.format == AudioFormat::ALAW ||
620 info.format == AudioFormat::MULAW)) {
622 }
623 // IEEE_FLOAT can optionally be converted to 16-bit linear PCM
625 info.bits_per_sample == 32) {
627 }
628 // any other non-PCM/non-IEEE_FLOAT format (e.g. ADPCM) is decoded by
629 // p_decoder to 16-bit PCM; IEEE_FLOAT is passed through as-is
630 if (info.format != AudioFormat::PCM &&
631 info.format != AudioFormat::IEEE_FLOAT &&
632 info.format != AudioFormat::ALAW &&
633 info.format != AudioFormat::MULAW) {
635 }
636 return info;
637 }
638
640 virtual size_t write(const uint8_t *data, size_t len) override {
641 TRACED();
642 size_t result = 0;
643 if (active) {
644 size_t data_start = 0;
645 if (isFirst) {
646 int header_size = decodeHeader((uint8_t *)data, len);
647 // we do not have the complete header yet: need more data
648 if (header_size == 0) return len;
649 data_start = header_size;
650 }
651 // process the outstanding data - only if the format is supported;
652 // otherwise report 0 bytes written, consistent with subsequent
653 // write() calls once the format has been found to be invalid
654 if (isValid) {
655 size_t audio_size = len - data_start;
656 // never play the bytes after the declared 'data' chunk (e.g. trailing
657 // LIST/id3 chunks); streamed files have no declared length
659 audio_size = min(audio_size, (size_t)data_bytes_remaining);
660 }
661 size_t written = write_out((uint8_t *)data + data_start, audio_size);
663 data_bytes_remaining -= min((size_t)data_bytes_remaining, written);
664 }
665 result = data_start + written;
666 if (written == audio_size) {
667 // everything we are interested in was written: report the complete
668 // input as consumed (bytes after the data chunk are ignored)
669 result = len;
670 }
671 }
672 }
673 return result;
674 }
675
677 virtual operator bool() override { return active; }
678
680 void setConvert8Bit(bool enable) {
681 convert8to16 = enable;
682 }
683
685 void setConvert24Bit(bool enable) {
686 convert24 = enable;
687 }
688
692 void setConvertALawMuLaw(bool enable) {
693 convertALawMuLaw = enable;
694 }
695
700 void setConvertFloatToInt16(bool enable) {
701 convertFloatToInt16 = enable;
702 }
703
706
707 protected:
716
718 bool isFirst = true;
719 bool isValid = true;
720 bool active = false;
732 bool convert8to16 = true; // Optional conversion flag
733 bool convert24 = true; // Optional conversion flag
734 bool convertALawMuLaw = true; // Optional conversion flag
735 bool convertFloatToInt16 = true; // Optional conversion flag
736 const size_t batch_size = 256;
737
738 Print &out() { return p_decoder == nullptr ? *p_print : dec_out; }
739
740 virtual size_t write_out(const uint8_t *in_ptr, size_t in_size) {
741 // check if we need to convert int24 data from 3 bytes to 4 bytes
742 size_t result = 0;
744 if (convert24 && format == AudioFormat::PCM &&
745 header.audioInfo().bits_per_sample == 24 && sizeof(int24_t) == 4) {
746 write_out_24(in_ptr, in_size);
747 result = in_size;
748 } else if (convert8to16 && format == AudioFormat::PCM &&
750 result = write_out_8to16(in_ptr, in_size);
751 } else if (convertALawMuLaw && format == AudioFormat::ALAW) {
752 result = write_out_alaw(in_ptr, in_size);
753 } else if (convertALawMuLaw && format == AudioFormat::MULAW) {
754 result = write_out_mulaw(in_ptr, in_size);
755 } else if (convertFloatToInt16 && format == AudioFormat::IEEE_FLOAT &&
757 result = write_out_float_to_int16(in_ptr, in_size);
758 } else {
759 result = out().write(in_ptr, in_size);
760 }
761 return result;
762 }
763
765 size_t write_out_8to16(const uint8_t *in_ptr, size_t in_size) {
766 size_t samples_remaining = in_size;
767 size_t offset = 0;
768 int16_t out_buf[batch_size];
769 while (samples_remaining > 0) {
770 size_t current_batch =
771 samples_remaining > batch_size ? batch_size : samples_remaining;
772 for (size_t i = 0; i < current_batch; ++i) {
773 out_buf[i] = ((int16_t)in_ptr[offset + i] - 128) << 8;
774 }
775 writeDataT<int16_t>(&out(), out_buf, current_batch);
776 offset += current_batch;
777 samples_remaining -= current_batch;
778 }
779 return in_size;
780 }
781
783 size_t write_out_24(const uint8_t *in_ptr, size_t in_size) {
784 // store 1 sample
787
788 for (size_t i = 0; i < in_size; i++) {
789 // Add byte to buffer
790 byte_buffer.write(in_ptr[i]);
791
792 // Process complete sample when buffer is full
793 if (byte_buffer.isFull()) {
794 int24_3bytes_t sample24{byte_buffer.data()};
795 int32_t converted_sample = sample24.scale32();
796 buffer24.write(converted_sample);
797 if (buffer24.isFull()) {
798 writeDataT<int32_t>(&out(), buffer24.data(), buffer24.available());
799 buffer24.reset();
800 }
802 }
803 }
804
805 return in_size;
806 }
807
810 size_t write_out_float_to_int16(const uint8_t *in_ptr, size_t in_size) {
813
814 for (size_t i = 0; i < in_size; i++) {
815 byte_buffer.write(in_ptr[i]);
816
817 if (byte_buffer.isFull()) {
818 float sample_f;
819 memcpy(&sample_f, byte_buffer.data(), sizeof(sample_f));
820 buffer_float16.write(NumberConverter::fromFloatT<int16_t>(sample_f));
821 if (buffer_float16.isFull()) {
822 writeDataT<int16_t>(&out(), buffer_float16.data(),
825 }
827 }
828 }
829
830 return in_size;
831 }
832
834 size_t write_out_alaw(const uint8_t *in_ptr, size_t in_size) {
835 size_t remaining = in_size;
836 size_t offset = 0;
837 int16_t out_buf[batch_size];
838 while (remaining > 0) {
839 size_t current_batch =
840 remaining > batch_size ? batch_size : remaining;
841 for (size_t i = 0; i < current_batch; ++i) {
842 out_buf[i] = alaw2linear(in_ptr[offset + i]);
843 }
844 writeDataT<int16_t>(&out(), out_buf, current_batch);
845 offset += current_batch;
846 remaining -= current_batch;
847 }
848 return in_size;
849 }
850
852 size_t write_out_mulaw(const uint8_t *in_ptr, size_t in_size) {
853 size_t remaining = in_size;
854 size_t offset = 0;
855 int16_t out_buf[batch_size];
856 while (remaining > 0) {
857 size_t current_batch =
858 remaining > batch_size ? batch_size : remaining;
859 for (size_t i = 0; i < current_batch; ++i) {
860 out_buf[i] = ulaw2linear(in_ptr[offset + i]);
861 }
862 writeDataT<int16_t>(&out(), out_buf, current_batch);
863 offset += current_batch;
864 remaining -= current_batch;
865 }
866 return in_size;
867 }
868
871 a_val ^= 0x55;
872 int t = (a_val & 0x0f) << 4;
873 int seg = ((unsigned)a_val & 0x70) >> 4;
874 switch (seg) {
875 case 0:
876 t += 8;
877 break;
878 case 1:
879 t += 0x108;
880 break;
881 default:
882 t += 0x108;
883 t <<= (seg - 1);
884 break;
885 }
886 return (int16_t)((a_val & 0x80) ? t : -t);
887 }
888
891 u_val = ~u_val;
892 int t = ((u_val & 0x0f) << 3) + 0x84;
893 t <<= ((unsigned)u_val & 0x70) >> 4;
894 return (int16_t)((u_val & 0x80) ? (0x84 - t) : (t - 0x84));
895 }
896
900 int decodeHeader(uint8_t *in_ptr, size_t in_size) {
901 // bytes of earlier writes that are already in the header buffer: the
902 // data position found below is relative to the start of that buffer
903 int buffered_before = (int)header.available();
904 // we expect at least the full header
905 header.write(in_ptr, in_size);
906 if (!header.isDataComplete()) {
907 if (header.isOverflow()) {
908 // the header (up to and including the 'data' chunk tag) exceeds
909 // MAX_WAV_HEADER_LEN_LIMIT without ever completing - give up
910 // instead of waiting forever for data that will never arrive
911 LOGE(
912 "WAV header exceeds the maximum supported size of %d bytes "
913 "without a 'data' chunk - aborting",
915 isFirst = false;
916 isValid = false;
917 return 0;
918 }
919 LOGW("WAV header misses 'data' section in len: %d",
920 (int)header.available());
922 return 0;
923 }
924 // determine the data start before parse(), which consumes the buffer
925 int data_start = header.getDataPos();
926 // parse header
927 if (!header.parse()) {
928 LOGE("WAV header parsing failed");
929 return 0;
930 }
931
932 isFirst = false;
933
934 LOGI("WAV sample_rate: %d", (int)header.audioInfo().sample_rate);
935 LOGI("WAV data_length: %u", (unsigned)header.audioInfo().data_length);
936 LOGI("WAV is_streamed: %d", header.audioInfo().is_streamed);
938
939 // select the decoder matching the format found in the header (if any
940 // was registered via setDecoder()/addDecoder()); otherwise fall back to
941 // the natively supported formats (PCM, IEEE_FLOAT, ALAW, MULAW)
943 p_decoder = findDecoder(format);
944 isValid = p_decoder != nullptr ? true : isNativelySupported(format);
945 if (isValid) {
946 if (p_decoder != nullptr) {
947 // update blocksize before begin() is called in setupEncodedAudio()
948 int block_size = header.audioInfo().block_align;
949 p_decoder->setBlockSize(block_size);
951 header.extraData().size());
953 }
954
955 // update sampling rate if the target supports it
956 AudioInfo bi = audioInfo();
958 } else {
959 LOGE("WAV format not supported: 0x%04X", (unsigned)format);
961 }
962 // relative to this write (the 'data' tag is never in earlier writes,
963 // otherwise the header would have been complete already)
964 int result = data_start - buffered_before;
965 if (result < 0) result = 0;
966 if (result > (int)in_size) result = (int)in_size;
967 return result;
968 }
969
974 LOGE(
975 "Natively supported: PCM (0x%04X), IEEE_FLOAT (0x%04X), ALAW "
976 "(0x%04X), MULAW (0x%04X)",
977 (unsigned)AudioFormat::PCM, (unsigned)AudioFormat::IEEE_FLOAT,
978 (unsigned)AudioFormat::ALAW, (unsigned)AudioFormat::MULAW);
979 if (decoders.empty()) {
980 LOGE(
981 "No additional decoders registered - use "
982 "setDecoder()/addDecoder() to support e.g. ADPCM formats");
983 } else {
984 for (int i = 0; i < decoders.size(); i++) {
985 LOGE("Registered decoder for format: 0x%04X",
986 (unsigned)decoders[i].format);
987 }
988 }
989 }
990
993 for (int i = 0; i < decoders.size(); i++) {
994 if (decoders[i].format == format) return decoders[i].decoder;
995 }
996 return nullptr;
997 }
998
1000 if (p_decoder != nullptr) {
1001 assert(p_print != nullptr);
1005 }
1006 }
1007
1012 if (p_decoder != nullptr) {
1013 p_decoder->end();
1014 p_decoder = nullptr;
1015 }
1016 }
1017
1019 static bool isNativelySupported(AudioFormat format) {
1020 return format == AudioFormat::PCM || format == AudioFormat::IEEE_FLOAT ||
1021 format == AudioFormat::ALAW || format == AudioFormat::MULAW;
1022 }
1023};
1024
1035class CountingPrint : public Print {
1036 public:
1037 void setOutput(Print *out) { p_out = out; }
1038 size_t write(uint8_t c) override { return write(&c, 1); }
1039 size_t write(const uint8_t *data, size_t len) override {
1040 size_t written = p_out != nullptr ? p_out->write(data, len) : 0;
1041 count += written;
1042 return written;
1043 }
1044 size_t count = 0;
1045
1046 protected:
1047 Print *p_out = nullptr;
1048};
1049
1060class WAVEncoder : public AudioEncoder {
1061 public:
1065 WAVEncoder() = default;
1066
1071
1077 TRACED();
1078 wav_info.format = fmt;
1079 p_encoder = &enc;
1080 }
1081
1083 void setOutput(Print &out) override {
1084 TRACED();
1085 p_print = &out;
1086 }
1087
1089 const char *mime() override { return wav_mime; }
1090
1094 info.format = AudioFormat::PCM;
1098 info.is_streamed = true;
1099 info.is_valid = true;
1100 info.data_length = 0x7fff0000;
1101 info.file_size = info.data_length + 36;
1102 return info;
1103 }
1104
1106 virtual void setAudioInfo(AudioInfo from) override {
1108 wav_info.channels = from.channels;
1110 // recalculate byte rate, block align...
1112 }
1113
1119 virtual void setAudioInfo(WAVAudioInfo ai) {
1122 wav_info = ai;
1123 LOGI("sample_rate: %d", (int)wav_info.sample_rate);
1124 LOGI("channels: %d", wav_info.channels);
1125 LOGI("bits_per_sample: %d", wav_info.bits_per_sample);
1126 // bytes per second
1129 // uncompressed formats with a fixed size per sample: block_align is
1130 // simply the frame size. Compressed formats (e.g. ADPCM) are handled
1131 // via p_encoder instead.
1138 }
1140 }
1141
1144 setAudioInfo(ai);
1145 return begin();
1146 }
1147
1149 virtual bool begin() override {
1150 TRACED();
1151 header.clear();
1152 if (!setupEncodedAudio()) {
1153 is_open = false;
1154 return false;
1155 }
1156
1157 // normalize streaming mode and payload limits at start time
1159 wav_info.data_length >= 0x7fff0000) {
1160 LOGI("is_streamed! because length is %u",
1161 (unsigned)wav_info.data_length);
1162 wav_info.is_streamed = true;
1163 wav_info.data_length = ~0;
1164 size_limit = 0;
1165 } else {
1166 wav_info.is_streamed = false;
1168 LOGI("size_limit is %d", (int)size_limit);
1169 }
1170
1171 header_written = false;
1172 is_open = true;
1173 return true;
1174 }
1175
1177 void end() override { is_open = false; }
1178
1180 virtual size_t write(const uint8_t *data, size_t len) override {
1181 if (!is_open) {
1182 LOGE("The WAVEncoder is not open - please call begin()");
1183 return 0;
1184 }
1185
1186 if (p_print == nullptr) {
1187 LOGE("No output stream was provided");
1188 return 0;
1189 }
1190
1191 if (!header_written) {
1192 LOGI("Writing Header");
1194 LOGE("Failed to write WAV header");
1195 is_open = false;
1196 return 0;
1197 }
1198 header_written = true;
1199 }
1200
1201 size_t result = 0;
1202 Print *p_out = p_encoder == nullptr ? p_print : &enc_out;
1203
1204 if (wav_info.is_streamed) {
1205 result = p_out->write((uint8_t *)data, len);
1206 } else if (size_limit > 0) {
1207 if (p_encoder == nullptr) {
1208 // uncompressed: input bytes map 1:1 to output bytes, so we can
1209 // truncate exactly at the declared data_length boundary
1210 size_t write_size = min((size_t)len, (size_t)size_limit);
1211 result = p_out->write((uint8_t *)data, write_size);
1212 size_limit -= result;
1213 } else {
1214 // compressed: the number of encoded bytes an external encoder
1215 // produces for a given amount of PCM input is not known in advance
1216 // (and may only be emitted once a full internal block is ready), so
1217 // AudioEncoder::write()'s return value (raw PCM input bytes
1218 // accepted) cannot be used to track progress towards data_length.
1219 // Track the *actual* encoded bytes written via counting_print, and
1220 // feed the encoder in frame-aligned chunks so we can stop as soon
1221 // as data_length is reached - result then correctly reflects only
1222 // the bytes of `data` that were actually consumed, so the caller
1223 // knows what still needs to be resent (e.g. to a new file).
1224 int frame_size = wav_info.bits_per_sample / 8 * wav_info.channels;
1225 if (frame_size <= 0) frame_size = 1;
1226 size_t chunk = (WAV_ENCODER_COMPRESSED_CHUNK_SIZE / frame_size) * frame_size;
1227 if (chunk == 0) chunk = frame_size;
1228
1229 while (result < len && size_limit > 0) {
1230 size_t n = min(chunk, len - result);
1232 size_t accepted = p_out->write((uint8_t *)data + result, n);
1234 result += accepted;
1235 if (accepted < n) break; // encoder didn't accept the full chunk
1236 }
1237 }
1238
1239 if (size_limit <= 0) {
1240 LOGI("The defined size was written - so we close the WAVEncoder now");
1241 is_open = false;
1242 }
1243 }
1244 return result;
1245 }
1246
1248 operator bool() override { return is_open; }
1249
1251 bool isOpen() { return is_open; }
1252
1256 void setDataOffset(uint16_t offset) {
1257 has_pending_offset = true;
1258 pending_offset = offset;
1259 wav_info.offset = offset;
1260 }
1261
1266 void setExtADPCMHeader(bool enable) {
1268 pending_ext_adpcm_header = enable;
1269 wav_info.ext_adpcm_header = enable;
1270 }
1271
1276 void setDataLength(uint32_t data_length) {
1278 pending_data_length = data_length;
1281 }
1282
1285
1288
1289 protected:
1291 Print *p_print = nullptr; // final output CopyEncoder copy; // used for PCM
1297 int64_t size_limit = 0;
1298 bool header_written = false;
1299 volatile bool is_open = false;
1300
1301 // fields set via setDataLength()/setDataOffset()/setExtADPCMHeader() that
1302 // must survive a subsequent wholesale setAudioInfo(WAVAudioInfo)/
1303 // begin(WAVAudioInfo) call, regardless of call order - see
1304 // applyPendingOverrides()
1308 uint16_t pending_offset = 0;
1311
1318 // must run before pending_data_length below: its file_size calculation
1319 // depends on wav_info.ext_adpcm_header
1322 } else if (p_encoder != nullptr && isADPCMFormat(wav_info.format)) {
1323 // default to the extended header for ADPCM formats unless the user
1324 // explicitly overrode it via setExtADPCMHeader()
1326 }
1327 if (has_pending_offset) {
1329 }
1333 (pending_data_length == 0 || pending_data_length >= 0x7fff0000);
1334 if (!wav_info.is_streamed) {
1335 // full file size = RIFF chunk (36) + format specific extra header
1336 // bytes (e.g. ADPCM 'fmt ' extension + 'fact' chunk) + data chunk
1337 // payload
1340 }
1341 }
1342 }
1343
1348 if (p_encoder != nullptr) {
1349 if (p_print == nullptr) {
1350 LOGE(
1351 "setupEncodedAudio: no output stream was provided - call "
1352 "setOutput() before begin()");
1353 return false;
1354 }
1360 enc_out.begin();
1361 // block size only available after begin(): update block size
1363 }
1364 return true;
1365 }
1366};
1367
1368} // namespace audio_tools
WAV Audio Formats used by Microsoft e.g. in AVI video files.
#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 DEFAULT_BITS_PER_SAMPLE
Definition AudioToolsConfig.h:99
#define DEFAULT_CHANNELS
Definition AudioToolsConfig.h:95
#define DEFAULT_SAMPLE_RATE
Definition AudioToolsConfig.h:91
#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
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
Extended AudioDecoder interface to support block size configuration.
Definition AudioCodecsBase.h:133
virtual void setExtraData(const uint8_t *data, size_t len)
Definition AudioCodecsBase.h:146
virtual void setBlockSize(int blockSize)=0
virtual AudioFormat wavFormat()
Definition AudioCodecsBase.h:142
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:19
AudioInfo info
Definition AudioCodecsBase.h:80
void end() override
Definition AudioCodecsBase.h:63
Print * p_print
Definition AudioCodecsBase.h:79
Extended AudioEncoder interface to support block size configuration.
Definition AudioCodecsBase.h:150
Encoding of PCM data.
Definition AudioCodecsBase.h:101
AudioInfo info
Definition AudioCodecsBase.h:129
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:110
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:180
Shared functionality of all buffers.
Definition Buffers.h:23
void clear()
same as reset
Definition Buffers.h:96
Minimal Print wrapper that counts the bytes actually written to the wrapped output....
Definition CodecWAV.h:1035
size_t write(uint8_t c) override
Definition CodecWAV.h:1038
void setOutput(Print *out)
Definition CodecWAV.h:1037
size_t write(const uint8_t *data, size_t len) override
Definition CodecWAV.h:1039
size_t count
Definition CodecWAV.h:1044
Print * p_out
Definition CodecWAV.h:1047
A more natural Print class to process encoded data (aac, wav, mp3...). Just define the output and the...
Definition AudioEncoded.h:21
void setEncoder(AudioEncoder *encoder)
Definition AudioEncoded.h:131
bool begin() override
Starts the processing - sets the status to active.
Definition AudioEncoded.h:161
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition AudioEncoded.h:87
void setDecoder(AudioDecoder *decoder)
Definition AudioEncoded.h:144
void setOutput(Print *outputStream)
Defines the output.
Definition AudioEncoded.h:107
A simple Buffer implementation which just uses a (dynamically sized) array.
Definition Buffers.h:194
size_t size() override
Definition Buffers.h:325
bool write(T sample) override
write add an entry to the buffer
Definition Buffers.h:228
void setClearWithZero(bool flag)
Sets the buffer to 0 on clear.
Definition Buffers.h:336
int available() override
provides the number of entries that are available to read
Definition Buffers.h:255
bool isFull() override
checks if the buffer is full
Definition Buffers.h:262
int writeArray(const T data[], int len) override
Fills the buffer data.
Definition Buffers.h:223
T * data()
Provides address of actual data.
Definition Buffers.h:306
bool resize(size_t size)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:327
void reset() override
clears the buffer
Definition Buffers.h:308
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:29
virtual int indexOf(const char c, int start=0)
Definition StrView.h:336
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
void clear()
Definition Vector.h:176
bool resize(size_t newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:321
int size()
Definition Vector.h:178
A simple WAVDecoder: We parse the header data on the first record to determine the format....
Definition CodecWAV.h:510
virtual size_t write(const uint8_t *data, size_t len) override
Write incoming WAV data (header + PCM) into output.
Definition CodecWAV.h:640
bool active
Definition CodecWAV.h:720
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecWAV.h:570
bool isFirst
Definition CodecWAV.h:718
bool convertFloatToInt16
Definition CodecWAV.h:735
size_t write_out_24(const uint8_t *in_ptr, size_t in_size)
convert 3 byte int24 to 4 byte int32
Definition CodecWAV.h:783
SingleBuffer< int16_t > buffer_float16
Definition CodecWAV.h:731
void setConvertFloatToInt16(bool enable)
Definition CodecWAV.h:700
static bool isNativelySupported(AudioFormat format)
Formats that are handled internally without an external decoder.
Definition CodecWAV.h:1019
Print & out()
Definition CodecWAV.h:738
void setDecoder(AudioDecoderExt &dec, AudioFormat fmt=AudioFormat::UNKNOWN)
Definition CodecWAV.h:537
Vector< DecoderEntry > decoders
decoders registered via setDecoder()/addDecoder(), keyed by format tag
Definition CodecWAV.h:727
int decodeHeader(uint8_t *in_ptr, size_t in_size)
Definition CodecWAV.h:900
size_t write_out_float_to_int16(const uint8_t *in_ptr, size_t in_size)
Definition CodecWAV.h:810
AudioDecoderExt * p_decoder
Definition CodecWAV.h:725
void setConvert8Bit(bool enable)
Convert 8 bit to 16 bit PCM data (default: enabled)
Definition CodecWAV.h:680
void setupEncodedAudio()
Definition CodecWAV.h:999
void end() override
Finish decoding and release temporary buffers.
Definition CodecWAV.h:591
void endCurrentDecoder()
Definition CodecWAV.h:1011
WAVHeader & getHeader()
Access to the internal header parser and info.
Definition CodecWAV.h:705
const char * mime()
Provides MIME type "audio/wav".
Definition CodecWAV.h:601
size_t write_out_alaw(const uint8_t *in_ptr, size_t in_size)
Expand G.711 A-law 8-bit samples to 16-bit linear PCM and write out.
Definition CodecWAV.h:834
bool isValid
Definition CodecWAV.h:719
bool convertALawMuLaw
Definition CodecWAV.h:734
void logSupportedFormats()
Definition CodecWAV.h:973
AudioDecoderExt * findDecoder(AudioFormat format)
Finds the decoder registered for the given format tag, if any.
Definition CodecWAV.h:992
WAVDecoder()=default
Construct a new WAVDecoder object for PCM data.
WAVDecoder(AudioDecoderExt &dec, AudioFormat fmt=AudioFormat::UNKNOWN)
Construct a new WAVDecoder object for ADPCM data. If fmt is not provided, it is derived from dec....
Definition CodecWAV.h:522
static int16_t alaw2linear(uint8_t a_val)
Converts a G.711 A-law encoded byte to a 16-bit linear PCM sample.
Definition CodecWAV.h:870
const size_t batch_size
Definition CodecWAV.h:736
SingleBuffer< uint8_t > byte_buffer
Definition CodecWAV.h:729
WAVAudioInfo & audioInfoEx()
Extended WAV specific info (original header values)
Definition CodecWAV.h:604
size_t write_out_mulaw(const uint8_t *in_ptr, size_t in_size)
Expand G.711 mu-law 8-bit samples to 16-bit linear PCM and write out.
Definition CodecWAV.h:852
WAVHeader header
Definition CodecWAV.h:717
void addDecoder(AudioDecoderExt &dec, AudioFormat fmt=AudioFormat::UNKNOWN)
Definition CodecWAV.h:551
static int16_t ulaw2linear(uint8_t u_val)
Converts a G.711 mu-law encoded byte to a 16-bit linear PCM sample.
Definition CodecWAV.h:890
void setConvert24Bit(bool enable)
Convert 24 bit (3 byte) to 32 bit (4 byte) PCM data (default: enabled)
Definition CodecWAV.h:685
bool begin() override
Prepare decoder for a new WAV stream.
Definition CodecWAV.h:573
size_t write_out_8to16(const uint8_t *in_ptr, size_t in_size)
Convert 8-bit PCM to 16-bit PCM and write out.
Definition CodecWAV.h:765
EncodedAudioOutput dec_out
Definition CodecWAV.h:728
bool convert24
Definition CodecWAV.h:733
uint32_t data_bytes_remaining
bytes of the declared 'data' chunk that are still to be decoded
Definition CodecWAV.h:722
virtual size_t write_out(const uint8_t *in_ptr, size_t in_size)
Definition CodecWAV.h:740
AudioInfo audioInfo() override
Exposed AudioInfo (may reflect conversion flags)
Definition CodecWAV.h:607
bool convert8to16
Definition CodecWAV.h:732
SingleBuffer< int32_t > buffer24
Definition CodecWAV.h:730
void setConvertALawMuLaw(bool enable)
Definition CodecWAV.h:692
A simple WAV file encoder. If no AudioEncoderExt is specified the WAV file contains PCM data,...
Definition CodecWAV.h:1060
virtual size_t write(const uint8_t *data, size_t len) override
Writes PCM data to be encoded as WAV.
Definition CodecWAV.h:1180
void setOutput(Print &out) override
Defines the otuput stream.
Definition CodecWAV.h:1083
CountingPrint counting_print
tracks the actual encoded bytes p_encoder emits to p_print (see write())
Definition CodecWAV.h:1295
volatile bool is_open
Definition CodecWAV.h:1299
uint32_t pending_data_length
Definition CodecWAV.h:1306
bool isOpen()
Check if encoder is open.
Definition CodecWAV.h:1251
bool has_pending_offset
Definition CodecWAV.h:1307
virtual bool begin() override
starts the processing using the actual WAVAudioInfo
Definition CodecWAV.h:1149
bool header_written
Definition CodecWAV.h:1298
AudioEncoderExt * p_encoder
Definition CodecWAV.h:1292
bool begin(WAVAudioInfo ai)
starts the processing
Definition CodecWAV.h:1143
WAVAudioInfo wav_info
Definition CodecWAV.h:1296
void end() override
stops the processing
Definition CodecWAV.h:1177
virtual void setAudioInfo(WAVAudioInfo ai)
Definition CodecWAV.h:1119
int64_t size_limit
Definition CodecWAV.h:1297
WAVEncoder()=default
Construct a new WAVEncoder object for PCM data.
void setDataOffset(uint16_t offset)
Definition CodecWAV.h:1256
WAVHeader & getHeader()
Access to the internal header parser and info.
Definition CodecWAV.h:1287
EncodedAudioOutput enc_out
Definition CodecWAV.h:1293
void setExtADPCMHeader(bool enable)
Definition CodecWAV.h:1266
bool setupEncodedAudio()
Definition CodecWAV.h:1347
bool has_pending_data_length
Definition CodecWAV.h:1305
WAVAudioInfo & audioInfoEx()
Extended WAV specific info.
Definition CodecWAV.h:1284
bool pending_ext_adpcm_header
Definition CodecWAV.h:1310
WAVEncoder(AudioEncoderExt &enc, AudioFormat fmt)
Construct a new WAVEncoder object for ADPCM data.
Definition CodecWAV.h:1070
WAVAudioInfo defaultConfig()
Provides the default configuration.
Definition CodecWAV.h:1092
WAVHeader header
Definition CodecWAV.h:1290
const char * mime() override
Provides "audio/wav".
Definition CodecWAV.h:1089
virtual void setAudioInfo(AudioInfo from) override
Update actual WAVAudioInfo.
Definition CodecWAV.h:1106
Print * p_print
Definition CodecWAV.h:1291
void applyPendingOverrides()
Definition CodecWAV.h:1317
void setEncoder(AudioEncoderExt &enc, AudioFormat fmt)
Definition CodecWAV.h:1076
uint16_t pending_offset
Definition CodecWAV.h:1308
bool has_pending_ext_adpcm_header
Definition CodecWAV.h:1309
void setDataLength(uint32_t data_length)
Definition CodecWAV.h:1276
Parser for Wav header data for details see https://de.wikipedia.org/wiki/RIFF_WAVE.
Definition CodecWAV.h:91
void writeDataHeader(BaseBuffer< uint8_t > &buffer, const WAVAudioInfo &info)
Definition CodecWAV.h:432
void logInfo()
Definition CodecWAV.h:347
void write16(BaseBuffer< uint8_t > &buffer, uint16_t value)
Definition CodecWAV.h:470
bool parse()
Call when header data write is complete to parse the data.
Definition CodecWAV.h:111
bool isDataComplete()
Returns true if the header is complete (containd data tag)
Definition CodecWAV.h:143
void skip(int n)
Definition CodecWAV.h:323
void seek(long int offset, int origin)
Definition CodecWAV.h:335
void parseFormatExtension(int fmt_length)
Definition CodecWAV.h:253
Vector< uint8_t > & extraData()
Definition CodecWAV.h:173
void setAudioInfo(WAVAudioInfo info)
Sets the info in the header.
Definition CodecWAV.h:176
bool eof()
Definition CodecWAV.h:345
void writeFMT(BaseBuffer< uint8_t > &buffer, const WAVAudioInfo &info)
Definition CodecWAV.h:365
bool writeHeader(Print *out, const WAVAudioInfo &info)
Just write a wav header with explicit info to the indicated output.
Definition CodecWAV.h:184
size_t available()
number of bytes available in the header buffer
Definition CodecWAV.h:156
bool writeHeader(Print *out)
Just write a wav header to the indicated outputbu.
Definition CodecWAV.h:179
Vector< uint8_t > extradata
Definition CodecWAV.h:246
int getChar()
Definition CodecWAV.h:328
int getDataPos()
Determines the data start position using the data tag.
Definition CodecWAV.h:159
SingleBuffer< uint8_t > buffer
Definition CodecWAV.h:247
WAVAudioInfo & audioInfo()
provides the info from the header
Definition CodecWAV.h:167
static AudioFormat formatFromGuid(const uint8_t *guid)
Maps the sub format GUID of WAVE_FORMAT_EXTENSIBLE to a format.
Definition CodecWAV.h:270
static uint16_t samplesPerBlock(const WAVAudioInfo &info)
Definition CodecWAV.h:454
int write(uint8_t *data, size_t data_len)
Definition CodecWAV.h:99
void clear()
Reset internal stored header information and buffer.
Definition CodecWAV.h:218
uint32_t read_tag()
Definition CodecWAV.h:296
void writeRiffHeader(BaseBuffer< uint8_t > &buffer, const WAVAudioInfo &info)
Definition CodecWAV.h:355
WAVAudioInfo headerInfo
Definition CodecWAV.h:245
uint16_t read_int16()
Definition CodecWAV.h:316
bool isOverflow()
Definition CodecWAV.h:151
bool setPos(const char *id)
Definition CodecWAV.h:282
uint32_t read_int32()
Definition CodecWAV.h:307
int indexOf(const char *str)
Definition CodecWAV.h:290
void writeFactChunk(BaseBuffer< uint8_t > &buffer, const WAVAudioInfo &info)
Definition CodecWAV.h:421
size_t data_pos
Definition CodecWAV.h:248
static int extraHeaderBytes(const WAVAudioInfo &info)
Definition CodecWAV.h:205
void write32(BaseBuffer< uint8_t > &buffer, uint64_t value)
Definition CodecWAV.h:466
void dumpHeader()
Debug helper: dumps header bytes as printable characters.
Definition CodecWAV.h:231
size_t tell()
Definition CodecWAV.h:343
uint32_t getChar32()
Definition CodecWAV.h:305
24bit integer which is used for I2S sound processing. The values are really using 3 bytes....
Definition int24_3bytes_t.h:21
24bit integer which is used for I2S sound processing. The values are represented as int32_t,...
Definition int24_4bytes_t.h:22
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.
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
static const char * wav_mime
Definition CodecWAV.h:52
static bool isADPCMFormat(AudioFormat format)
Definition CodecWAV.h:60
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:56
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:58
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:60
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:62
Sound information which is available in the WAV header.
Definition CodecWAV.h:32
AudioFormat format
Definition CodecWAV.h:40
bool is_streamed
Definition CodecWAV.h:43
bool is_valid
Definition CodecWAV.h:44
int block_align
Definition CodecWAV.h:42
WAVAudioInfo(const AudioInfo &from)
Definition CodecWAV.h:34
uint32_t file_size
Definition CodecWAV.h:46
int offset
Definition CodecWAV.h:47
int byte_rate
Definition CodecWAV.h:41
uint32_t data_length
Definition CodecWAV.h:45
bool ext_adpcm_header
write the extended 'fmt ' chunk + 'fact' chunk for ADPCM formats
Definition CodecWAV.h:49
Associates a WAV format tag with the decoder responsible for it.
Definition CodecWAV.h:709
AudioFormat format
Definition CodecWAV.h:710
DecoderEntry()=default
AudioDecoderExt * decoder
Definition CodecWAV.h:711
DecoderEntry(AudioFormat fmt, AudioDecoderExt *dec)
Definition CodecWAV.h:713