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();
584 isFirst = true;
585 active = true;
586 return true;
587 }
588
590 void end() override {
591 TRACED();
594 buffer24.reset();
596 active = false;
597 }
598
600 const char *mime() { return wav_mime; }
601
604
606 AudioInfo audioInfo() override {
608 if (convert8to16 && info.format == AudioFormat::PCM &&
609 info.bits_per_sample == 8) {
611 }
612 // 32 bits gives better result
613 if (convert24 && info.format == AudioFormat::PCM &&
614 info.bits_per_sample == 24) {
616 }
617 // ALAW/MULAW are expanded from 8-bit logarithmic to 16-bit linear PCM
618 if (convertALawMuLaw && (info.format == AudioFormat::ALAW ||
619 info.format == AudioFormat::MULAW)) {
621 }
622 // IEEE_FLOAT can optionally be converted to 16-bit linear PCM
624 info.bits_per_sample == 32) {
626 }
627 // any other non-PCM/non-IEEE_FLOAT format (e.g. ADPCM) is decoded by
628 // p_decoder to 16-bit PCM; IEEE_FLOAT is passed through as-is
629 if (info.format != AudioFormat::PCM &&
630 info.format != AudioFormat::IEEE_FLOAT &&
631 info.format != AudioFormat::ALAW &&
632 info.format != AudioFormat::MULAW) {
634 }
635 return info;
636 }
637
639 virtual size_t write(const uint8_t *data, size_t len) override {
640 TRACED();
641 size_t result = 0;
642 if (active) {
643 if (isFirst) {
644 int data_start = decodeHeader((uint8_t *)data, len);
645 // we do not have the complete header yet: need more data
646 if (data_start == 0) return len;
647 // process the outstanding data - only if the format is supported;
648 // otherwise report 0 bytes written, consistent with subsequent
649 // write() calls once the format has been found to be invalid
650 if (isValid) {
651 result = data_start +
652 write_out((uint8_t *)data + data_start, len - data_start);
653 }
654
655 } else if (isValid) {
656 result = write_out((uint8_t *)data, len);
657 }
658 }
659 return result;
660 }
661
663 virtual operator bool() override { return active; }
664
666 void setConvert8Bit(bool enable) {
667 convert8to16 = enable;
668 }
669
671 void setConvert24Bit(bool enable) {
672 convert24 = enable;
673 }
674
678 void setConvertALawMuLaw(bool enable) {
679 convertALawMuLaw = enable;
680 }
681
686 void setConvertFloatToInt16(bool enable) {
687 convertFloatToInt16 = enable;
688 }
689
692
693 protected:
702
704 bool isFirst = true;
705 bool isValid = true;
706 bool active = false;
716 bool convert8to16 = true; // Optional conversion flag
717 bool convert24 = true; // Optional conversion flag
718 bool convertALawMuLaw = true; // Optional conversion flag
719 bool convertFloatToInt16 = true; // Optional conversion flag
720 const size_t batch_size = 256;
721
722 Print &out() { return p_decoder == nullptr ? *p_print : dec_out; }
723
724 virtual size_t write_out(const uint8_t *in_ptr, size_t in_size) {
725 // check if we need to convert int24 data from 3 bytes to 4 bytes
726 size_t result = 0;
728 if (convert24 && format == AudioFormat::PCM &&
729 header.audioInfo().bits_per_sample == 24 && sizeof(int24_t) == 4) {
730 write_out_24(in_ptr, in_size);
731 result = in_size;
732 } else if (convert8to16 && format == AudioFormat::PCM &&
734 result = write_out_8to16(in_ptr, in_size);
735 } else if (convertALawMuLaw && format == AudioFormat::ALAW) {
736 result = write_out_alaw(in_ptr, in_size);
737 } else if (convertALawMuLaw && format == AudioFormat::MULAW) {
738 result = write_out_mulaw(in_ptr, in_size);
739 } else if (convertFloatToInt16 && format == AudioFormat::IEEE_FLOAT &&
741 result = write_out_float_to_int16(in_ptr, in_size);
742 } else {
743 result = out().write(in_ptr, in_size);
744 }
745 return result;
746 }
747
749 size_t write_out_8to16(const uint8_t *in_ptr, size_t in_size) {
750 size_t samples_remaining = in_size;
751 size_t offset = 0;
752 int16_t out_buf[batch_size];
753 while (samples_remaining > 0) {
754 size_t current_batch =
755 samples_remaining > batch_size ? batch_size : samples_remaining;
756 for (size_t i = 0; i < current_batch; ++i) {
757 out_buf[i] = ((int16_t)in_ptr[offset + i] - 128) << 8;
758 }
759 writeDataT<int16_t>(&out(), out_buf, current_batch);
760 offset += current_batch;
761 samples_remaining -= current_batch;
762 }
763 return in_size;
764 }
765
767 size_t write_out_24(const uint8_t *in_ptr, size_t in_size) {
768 // store 1 sample
771
772 for (size_t i = 0; i < in_size; i++) {
773 // Add byte to buffer
774 byte_buffer.write(in_ptr[i]);
775
776 // Process complete sample when buffer is full
777 if (byte_buffer.isFull()) {
778 int24_3bytes_t sample24{byte_buffer.data()};
779 int32_t converted_sample = sample24.scale32();
780 buffer24.write(converted_sample);
781 if (buffer24.isFull()) {
782 writeDataT<int32_t>(&out(), buffer24.data(), buffer24.available());
783 buffer24.reset();
784 }
786 }
787 }
788
789 return in_size;
790 }
791
794 size_t write_out_float_to_int16(const uint8_t *in_ptr, size_t in_size) {
797
798 for (size_t i = 0; i < in_size; i++) {
799 byte_buffer.write(in_ptr[i]);
800
801 if (byte_buffer.isFull()) {
802 float sample_f;
803 memcpy(&sample_f, byte_buffer.data(), sizeof(sample_f));
804 if (sample_f > 1.0f) sample_f = 1.0f;
805 if (sample_f < -1.0f) sample_f = -1.0f;
806 buffer_float16.write((int16_t)(sample_f * 32767.0f));
807 if (buffer_float16.isFull()) {
808 writeDataT<int16_t>(&out(), buffer_float16.data(),
811 }
813 }
814 }
815
816 return in_size;
817 }
818
820 size_t write_out_alaw(const uint8_t *in_ptr, size_t in_size) {
821 size_t remaining = in_size;
822 size_t offset = 0;
823 int16_t out_buf[batch_size];
824 while (remaining > 0) {
825 size_t current_batch =
826 remaining > batch_size ? batch_size : remaining;
827 for (size_t i = 0; i < current_batch; ++i) {
828 out_buf[i] = alaw2linear(in_ptr[offset + i]);
829 }
830 writeDataT<int16_t>(&out(), out_buf, current_batch);
831 offset += current_batch;
832 remaining -= current_batch;
833 }
834 return in_size;
835 }
836
838 size_t write_out_mulaw(const uint8_t *in_ptr, size_t in_size) {
839 size_t remaining = in_size;
840 size_t offset = 0;
841 int16_t out_buf[batch_size];
842 while (remaining > 0) {
843 size_t current_batch =
844 remaining > batch_size ? batch_size : remaining;
845 for (size_t i = 0; i < current_batch; ++i) {
846 out_buf[i] = ulaw2linear(in_ptr[offset + i]);
847 }
848 writeDataT<int16_t>(&out(), out_buf, current_batch);
849 offset += current_batch;
850 remaining -= current_batch;
851 }
852 return in_size;
853 }
854
857 a_val ^= 0x55;
858 int t = (a_val & 0x0f) << 4;
859 int seg = ((unsigned)a_val & 0x70) >> 4;
860 switch (seg) {
861 case 0:
862 t += 8;
863 break;
864 case 1:
865 t += 0x108;
866 break;
867 default:
868 t += 0x108;
869 t <<= (seg - 1);
870 break;
871 }
872 return (int16_t)((a_val & 0x80) ? t : -t);
873 }
874
877 u_val = ~u_val;
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));
881 }
882
886 int decodeHeader(uint8_t *in_ptr, size_t in_size) {
887 // bytes of earlier writes that are already in the header buffer: the
888 // data position found below is relative to the start of that buffer
889 int buffered_before = (int)header.available();
890 // we expect at least the full header
891 header.write(in_ptr, in_size);
892 if (!header.isDataComplete()) {
893 if (header.isOverflow()) {
894 // the header (up to and including the 'data' chunk tag) exceeds
895 // MAX_WAV_HEADER_LEN_LIMIT without ever completing - give up
896 // instead of waiting forever for data that will never arrive
897 LOGE(
898 "WAV header exceeds the maximum supported size of %d bytes "
899 "without a 'data' chunk - aborting",
901 isFirst = false;
902 isValid = false;
903 return 0;
904 }
905 LOGW("WAV header misses 'data' section in len: %d",
906 (int)header.available());
908 return 0;
909 }
910 // determine the data start before parse(), which consumes the buffer
911 int data_start = header.getDataPos();
912 // parse header
913 if (!header.parse()) {
914 LOGE("WAV header parsing failed");
915 return 0;
916 }
917
918 isFirst = false;
919
920 LOGI("WAV sample_rate: %d", (int)header.audioInfo().sample_rate);
921 LOGI("WAV data_length: %u", (unsigned)header.audioInfo().data_length);
922 LOGI("WAV is_streamed: %d", header.audioInfo().is_streamed);
923
924 // select the decoder matching the format found in the header (if any
925 // was registered via setDecoder()/addDecoder()); otherwise fall back to
926 // the natively supported formats (PCM, IEEE_FLOAT, ALAW, MULAW)
928 p_decoder = findDecoder(format);
929 isValid = p_decoder != nullptr ? true : isNativelySupported(format);
930 if (isValid) {
931 if (p_decoder != nullptr) {
932 // update blocksize before begin() is called in setupEncodedAudio()
933 int block_size = header.audioInfo().block_align;
934 p_decoder->setBlockSize(block_size);
936 header.extraData().size());
938 }
939
940 // update sampling rate if the target supports it
941 AudioInfo bi = audioInfo();
943 } else {
944 LOGE("WAV format not supported: 0x%04X", (unsigned)format);
946 }
947 // relative to this write (the 'data' tag is never in earlier writes,
948 // otherwise the header would have been complete already)
949 int result = data_start - buffered_before;
950 if (result < 0) result = 0;
951 if (result > (int)in_size) result = (int)in_size;
952 return result;
953 }
954
959 LOGE(
960 "Natively supported: PCM (0x%04X), IEEE_FLOAT (0x%04X), ALAW "
961 "(0x%04X), MULAW (0x%04X)",
962 (unsigned)AudioFormat::PCM, (unsigned)AudioFormat::IEEE_FLOAT,
963 (unsigned)AudioFormat::ALAW, (unsigned)AudioFormat::MULAW);
964 if (decoders.empty()) {
965 LOGE(
966 "No additional decoders registered - use "
967 "setDecoder()/addDecoder() to support e.g. ADPCM formats");
968 } else {
969 for (int i = 0; i < decoders.size(); i++) {
970 LOGE("Registered decoder for format: 0x%04X",
971 (unsigned)decoders[i].format);
972 }
973 }
974 }
975
978 for (int i = 0; i < decoders.size(); i++) {
979 if (decoders[i].format == format) return decoders[i].decoder;
980 }
981 return nullptr;
982 }
983
985 if (p_decoder != nullptr) {
986 assert(p_print != nullptr);
990 }
991 }
992
997 if (p_decoder != nullptr) {
998 p_decoder->end();
999 p_decoder = nullptr;
1000 }
1001 }
1002
1004 static bool isNativelySupported(AudioFormat format) {
1005 return format == AudioFormat::PCM || format == AudioFormat::IEEE_FLOAT ||
1006 format == AudioFormat::ALAW || format == AudioFormat::MULAW;
1007 }
1008};
1009
1020class CountingPrint : public Print {
1021 public:
1022 void setOutput(Print *out) { p_out = out; }
1023 size_t write(uint8_t c) override { return write(&c, 1); }
1024 size_t write(const uint8_t *data, size_t len) override {
1025 size_t written = p_out != nullptr ? p_out->write(data, len) : 0;
1026 count += written;
1027 return written;
1028 }
1029 size_t count = 0;
1030
1031 protected:
1032 Print *p_out = nullptr;
1033};
1034
1045class WAVEncoder : public AudioEncoder {
1046 public:
1050 WAVEncoder() = default;
1051
1056
1062 TRACED();
1063 wav_info.format = fmt;
1064 p_encoder = &enc;
1065 }
1066
1068 void setOutput(Print &out) override {
1069 TRACED();
1070 p_print = &out;
1071 }
1072
1074 const char *mime() override { return wav_mime; }
1075
1079 info.format = AudioFormat::PCM;
1083 info.is_streamed = true;
1084 info.is_valid = true;
1085 info.data_length = 0x7fff0000;
1086 info.file_size = info.data_length + 36;
1087 return info;
1088 }
1089
1091 virtual void setAudioInfo(AudioInfo from) override {
1093 wav_info.channels = from.channels;
1095 // recalculate byte rate, block align...
1097 }
1098
1104 virtual void setAudioInfo(WAVAudioInfo ai) {
1107 wav_info = ai;
1108 LOGI("sample_rate: %d", (int)wav_info.sample_rate);
1109 LOGI("channels: %d", wav_info.channels);
1110 LOGI("bits_per_sample: %d", wav_info.bits_per_sample);
1111 // bytes per second
1114 // uncompressed formats with a fixed size per sample: block_align is
1115 // simply the frame size. Compressed formats (e.g. ADPCM) are handled
1116 // via p_encoder instead.
1123 }
1125 }
1126
1129 setAudioInfo(ai);
1130 return begin();
1131 }
1132
1134 virtual bool begin() override {
1135 TRACED();
1136 header.clear();
1137 if (!setupEncodedAudio()) {
1138 is_open = false;
1139 return false;
1140 }
1141
1142 // normalize streaming mode and payload limits at start time
1144 wav_info.data_length >= 0x7fff0000) {
1145 LOGI("is_streamed! because length is %u",
1146 (unsigned)wav_info.data_length);
1147 wav_info.is_streamed = true;
1148 wav_info.data_length = ~0;
1149 size_limit = 0;
1150 } else {
1151 wav_info.is_streamed = false;
1153 LOGI("size_limit is %d", (int)size_limit);
1154 }
1155
1156 header_written = false;
1157 is_open = true;
1158 return true;
1159 }
1160
1162 void end() override { is_open = false; }
1163
1165 virtual size_t write(const uint8_t *data, size_t len) override {
1166 if (!is_open) {
1167 LOGE("The WAVEncoder is not open - please call begin()");
1168 return 0;
1169 }
1170
1171 if (p_print == nullptr) {
1172 LOGE("No output stream was provided");
1173 return 0;
1174 }
1175
1176 if (!header_written) {
1177 LOGI("Writing Header");
1179 LOGE("Failed to write WAV header");
1180 is_open = false;
1181 return 0;
1182 }
1183 header_written = true;
1184 }
1185
1186 size_t result = 0;
1187 Print *p_out = p_encoder == nullptr ? p_print : &enc_out;
1188
1189 if (wav_info.is_streamed) {
1190 result = p_out->write((uint8_t *)data, len);
1191 } else if (size_limit > 0) {
1192 if (p_encoder == nullptr) {
1193 // uncompressed: input bytes map 1:1 to output bytes, so we can
1194 // truncate exactly at the declared data_length boundary
1195 size_t write_size = min((size_t)len, (size_t)size_limit);
1196 result = p_out->write((uint8_t *)data, write_size);
1197 size_limit -= result;
1198 } else {
1199 // compressed: the number of encoded bytes an external encoder
1200 // produces for a given amount of PCM input is not known in advance
1201 // (and may only be emitted once a full internal block is ready), so
1202 // AudioEncoder::write()'s return value (raw PCM input bytes
1203 // accepted) cannot be used to track progress towards data_length.
1204 // Track the *actual* encoded bytes written via counting_print, and
1205 // feed the encoder in frame-aligned chunks so we can stop as soon
1206 // as data_length is reached - result then correctly reflects only
1207 // the bytes of `data` that were actually consumed, so the caller
1208 // knows what still needs to be resent (e.g. to a new file).
1209 int frame_size = wav_info.bits_per_sample / 8 * wav_info.channels;
1210 if (frame_size <= 0) frame_size = 1;
1211 size_t chunk = (WAV_ENCODER_COMPRESSED_CHUNK_SIZE / frame_size) * frame_size;
1212 if (chunk == 0) chunk = frame_size;
1213
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);
1219 result += accepted;
1220 if (accepted < n) break; // encoder didn't accept the full chunk
1221 }
1222 }
1223
1224 if (size_limit <= 0) {
1225 LOGI("The defined size was written - so we close the WAVEncoder now");
1226 is_open = false;
1227 }
1228 }
1229 return result;
1230 }
1231
1233 operator bool() override { return is_open; }
1234
1236 bool isOpen() { return is_open; }
1237
1241 void setDataOffset(uint16_t offset) {
1242 has_pending_offset = true;
1243 pending_offset = offset;
1244 wav_info.offset = offset;
1245 }
1246
1251 void setExtADPCMHeader(bool enable) {
1253 pending_ext_adpcm_header = enable;
1254 wav_info.ext_adpcm_header = enable;
1255 }
1256
1261 void setDataLength(uint32_t data_length) {
1263 pending_data_length = data_length;
1266 }
1267
1270
1273
1274 protected:
1276 Print *p_print = nullptr; // final output CopyEncoder copy; // used for PCM
1282 int64_t size_limit = 0;
1283 bool header_written = false;
1284 volatile bool is_open = false;
1285
1286 // fields set via setDataLength()/setDataOffset()/setExtADPCMHeader() that
1287 // must survive a subsequent wholesale setAudioInfo(WAVAudioInfo)/
1288 // begin(WAVAudioInfo) call, regardless of call order - see
1289 // applyPendingOverrides()
1293 uint16_t pending_offset = 0;
1296
1303 // must run before pending_data_length below: its file_size calculation
1304 // depends on wav_info.ext_adpcm_header
1307 } else if (p_encoder != nullptr && isADPCMFormat(wav_info.format)) {
1308 // default to the extended header for ADPCM formats unless the user
1309 // explicitly overrode it via setExtADPCMHeader()
1311 }
1312 if (has_pending_offset) {
1314 }
1318 (pending_data_length == 0 || pending_data_length >= 0x7fff0000);
1319 if (!wav_info.is_streamed) {
1320 // full file size = RIFF chunk (36) + format specific extra header
1321 // bytes (e.g. ADPCM 'fmt ' extension + 'fact' chunk) + data chunk
1322 // payload
1325 }
1326 }
1327 }
1328
1333 if (p_encoder != nullptr) {
1334 if (p_print == nullptr) {
1335 LOGE(
1336 "setupEncodedAudio: no output stream was provided - call "
1337 "setOutput() before begin()");
1338 return false;
1339 }
1345 enc_out.begin();
1346 // block size only available after begin(): update block size
1348 }
1349 return true;
1350 }
1351};
1352
1353} // 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:1020
size_t write(uint8_t c) override
Definition CodecWAV.h:1023
void setOutput(Print *out)
Definition CodecWAV.h:1022
size_t write(const uint8_t *data, size_t len) override
Definition CodecWAV.h:1024
size_t count
Definition CodecWAV.h:1029
Print * p_out
Definition CodecWAV.h:1032
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:639
bool active
Definition CodecWAV.h:706
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecWAV.h:570
bool isFirst
Definition CodecWAV.h:704
bool convertFloatToInt16
Definition CodecWAV.h:719
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:767
SingleBuffer< int16_t > buffer_float16
Definition CodecWAV.h:715
void setConvertFloatToInt16(bool enable)
Definition CodecWAV.h:686
static bool isNativelySupported(AudioFormat format)
Formats that are handled internally without an external decoder.
Definition CodecWAV.h:1004
Print & out()
Definition CodecWAV.h:722
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:711
int decodeHeader(uint8_t *in_ptr, size_t in_size)
Definition CodecWAV.h:886
size_t write_out_float_to_int16(const uint8_t *in_ptr, size_t in_size)
Definition CodecWAV.h:794
AudioDecoderExt * p_decoder
Definition CodecWAV.h:709
void setConvert8Bit(bool enable)
Convert 8 bit to 16 bit PCM data (default: enabled)
Definition CodecWAV.h:666
void setupEncodedAudio()
Definition CodecWAV.h:984
void end() override
Finish decoding and release temporary buffers.
Definition CodecWAV.h:590
void endCurrentDecoder()
Definition CodecWAV.h:996
WAVHeader & getHeader()
Access to the internal header parser and info.
Definition CodecWAV.h:691
const char * mime()
Provides MIME type "audio/wav".
Definition CodecWAV.h:600
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:820
bool isValid
Definition CodecWAV.h:705
bool convertALawMuLaw
Definition CodecWAV.h:718
void logSupportedFormats()
Definition CodecWAV.h:958
AudioDecoderExt * findDecoder(AudioFormat format)
Finds the decoder registered for the given format tag, if any.
Definition CodecWAV.h:977
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:856
const size_t batch_size
Definition CodecWAV.h:720
SingleBuffer< uint8_t > byte_buffer
Definition CodecWAV.h:713
WAVAudioInfo & audioInfoEx()
Extended WAV specific info (original header values)
Definition CodecWAV.h:603
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:838
WAVHeader header
Definition CodecWAV.h:703
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:876
void setConvert24Bit(bool enable)
Convert 24 bit (3 byte) to 32 bit (4 byte) PCM data (default: enabled)
Definition CodecWAV.h:671
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:749
EncodedAudioOutput dec_out
Definition CodecWAV.h:712
bool convert24
Definition CodecWAV.h:717
virtual size_t write_out(const uint8_t *in_ptr, size_t in_size)
Definition CodecWAV.h:724
AudioInfo audioInfo() override
Exposed AudioInfo (may reflect conversion flags)
Definition CodecWAV.h:606
bool convert8to16
Definition CodecWAV.h:716
SingleBuffer< int32_t > buffer24
Definition CodecWAV.h:714
void setConvertALawMuLaw(bool enable)
Definition CodecWAV.h:678
A simple WAV file encoder. If no AudioEncoderExt is specified the WAV file contains PCM data,...
Definition CodecWAV.h:1045
virtual size_t write(const uint8_t *data, size_t len) override
Writes PCM data to be encoded as WAV.
Definition CodecWAV.h:1165
void setOutput(Print &out) override
Defines the otuput stream.
Definition CodecWAV.h:1068
CountingPrint counting_print
tracks the actual encoded bytes p_encoder emits to p_print (see write())
Definition CodecWAV.h:1280
volatile bool is_open
Definition CodecWAV.h:1284
uint32_t pending_data_length
Definition CodecWAV.h:1291
bool isOpen()
Check if encoder is open.
Definition CodecWAV.h:1236
bool has_pending_offset
Definition CodecWAV.h:1292
virtual bool begin() override
starts the processing using the actual WAVAudioInfo
Definition CodecWAV.h:1134
bool header_written
Definition CodecWAV.h:1283
AudioEncoderExt * p_encoder
Definition CodecWAV.h:1277
bool begin(WAVAudioInfo ai)
starts the processing
Definition CodecWAV.h:1128
WAVAudioInfo wav_info
Definition CodecWAV.h:1281
void end() override
stops the processing
Definition CodecWAV.h:1162
virtual void setAudioInfo(WAVAudioInfo ai)
Definition CodecWAV.h:1104
int64_t size_limit
Definition CodecWAV.h:1282
WAVEncoder()=default
Construct a new WAVEncoder object for PCM data.
void setDataOffset(uint16_t offset)
Definition CodecWAV.h:1241
WAVHeader & getHeader()
Access to the internal header parser and info.
Definition CodecWAV.h:1272
EncodedAudioOutput enc_out
Definition CodecWAV.h:1278
void setExtADPCMHeader(bool enable)
Definition CodecWAV.h:1251
bool setupEncodedAudio()
Definition CodecWAV.h:1332
bool has_pending_data_length
Definition CodecWAV.h:1290
WAVAudioInfo & audioInfoEx()
Extended WAV specific info.
Definition CodecWAV.h:1269
bool pending_ext_adpcm_header
Definition CodecWAV.h:1295
WAVEncoder(AudioEncoderExt &enc, AudioFormat fmt)
Construct a new WAVEncoder object for ADPCM data.
Definition CodecWAV.h:1055
WAVAudioInfo defaultConfig()
Provides the default configuration.
Definition CodecWAV.h:1077
WAVHeader header
Definition CodecWAV.h:1275
const char * mime() override
Provides "audio/wav".
Definition CodecWAV.h:1074
virtual void setAudioInfo(AudioInfo from) override
Update actual WAVAudioInfo.
Definition CodecWAV.h:1091
Print * p_print
Definition CodecWAV.h:1276
void applyPendingOverrides()
Definition CodecWAV.h:1302
void setEncoder(AudioEncoderExt &enc, AudioFormat fmt)
Definition CodecWAV.h:1061
uint16_t pending_offset
Definition CodecWAV.h:1293
bool has_pending_ext_adpcm_header
Definition CodecWAV.h:1294
void setDataLength(uint32_t data_length)
Definition CodecWAV.h:1261
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:695
AudioFormat format
Definition CodecWAV.h:696
DecoderEntry()=default
AudioDecoderExt * decoder
Definition CodecWAV.h:697
DecoderEntry(AudioFormat fmt, AudioDecoderExt *dec)
Definition CodecWAV.h:699