arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
CodecWAV.h
1#pragma once
2
3#include "AudioTools/AudioCodecs/AudioCodecsBase.h"
5#include "AudioTools/AudioCodecs/AudioEncoded.h"
6#include "AudioTools/CoreAudio/AudioBasic/StrView.h"
7
8#define READ_BUFFER_SIZE 512
9#define MAX_WAV_HEADER_LEN 200
10
11namespace audio_tools {
12
20 WAVAudioInfo() = default;
21 WAVAudioInfo(const AudioInfo &from) {
23 channels = from.channels;
25 }
26
27 AudioFormat format = AudioFormat::PCM;
28 int byte_rate = 0;
29 int block_align = 0;
30 bool is_streamed = true;
31 bool is_valid = false;
32 uint32_t data_length = 0;
33 uint32_t file_size = 0;
34 int offset = 0;
35};
36
37static const char *wav_mime = "audio/wav";
38
46class WAVHeader {
47 public:
48 WAVHeader() = default;
49
51 int write(uint8_t *data, size_t data_len) {
52 return buffer.writeArray(data, data_len);
53 }
54
56 bool parse() {
57 LOGI("WAVHeader::begin: %u", (unsigned)buffer.available());
58 this->data_pos = 0l;
59 memset((void *)&headerInfo, 0, sizeof(WAVAudioInfo));
60
61 if (!setPos("RIFF")) return false;
62 headerInfo.file_size = read_int32();
63 if (!setPos("WAVE")) return false;
64 if (!setPos("fmt ")) return false;
65 int fmt_length = read_int32();
66 headerInfo.format = (AudioFormat)read_int16();
67 headerInfo.channels = read_int16();
68 headerInfo.sample_rate = read_int32();
69 headerInfo.byte_rate = read_int32();
70 headerInfo.block_align = read_int16();
71 headerInfo.bits_per_sample = read_int16();
72 if (!setPos("data")) return false;
73 headerInfo.data_length = read_int32();
74 if (!headerInfo.data_length==0 || headerInfo.data_length >= 0x7fff0000) {
75 headerInfo.is_streamed = true;
76 headerInfo.data_length = ~0;
77 }
78
79 logInfo();
80 buffer.clear();
81 return true;
82 }
83
86 int pos = getDataPos();
87 return pos > 0 && buffer.available() >= pos;
88 }
89
91 size_t available(){
92 return buffer.available();
93 }
94
96 int getDataPos() {
97 int pos = StrView((char*)buffer.data(),MAX_WAV_HEADER_LEN, buffer.available()).indexOf("data");
98 return pos > 0 ? pos + 8 : 0;
99 }
100
102 WAVAudioInfo &audioInfo() { return headerInfo; }
103
106 headerInfo = info;
107 }
108
110 int writeHeader(Print *out) {
111 writeRiffHeader(buffer);
112 writeFMT(buffer);
113 writeDataHeader(buffer);
114 int len = buffer.available();
115 out->write(buffer.data(), buffer.available());
116 return len;
117 }
118
119 void clear() {
120 data_pos = 0;
121 WAVAudioInfo empty;
122 empty.sample_rate = 0;
123 empty.channels = 0;
124 empty.bits_per_sample = 0;
125 headerInfo = empty;
126 buffer.setClearWithZero(true);
127 buffer.reset();
128 }
129
130 void dumpHeader() {
131 char msg[buffer.available()+1] = {0};
132 for (int j = 0; j< buffer.available();j++){
133 char c = (char)buffer.data()[j];
134 if (!isalpha(c)){
135 c = '.';
136 }
137 msg[j] = c;
138 }
139 LOGI("Header: %s", msg);
140 }
141
142
143 protected:
144 struct WAVAudioInfo headerInfo;
145 SingleBuffer<uint8_t> buffer{MAX_WAV_HEADER_LEN};
146 size_t data_pos = 0;
147
148 bool setPos(const char*id){
149 int id_len = strlen(id);
150 int pos = indexOf(id);
151 if (pos < 0) return false;
152 data_pos = pos + id_len;
153 return true;
154 }
155
156 int indexOf(const char* str){
157 return StrView((char*)buffer.data(),MAX_WAV_HEADER_LEN, buffer.available()).indexOf(str);
158 }
159
160 uint32_t read_tag() {
161 uint32_t tag = 0;
162 tag = (tag << 8) | getChar();
163 tag = (tag << 8) | getChar();
164 tag = (tag << 8) | getChar();
165 tag = (tag << 8) | getChar();
166 return tag;
167 }
168
169 uint32_t getChar32() { return getChar(); }
170
171 uint32_t read_int32() {
172 uint32_t value = 0;
173 value |= getChar32() << 0;
174 value |= getChar32() << 8;
175 value |= getChar32() << 16;
176 value |= getChar32() << 24;
177 return value;
178 }
179
180 uint16_t read_int16() {
181 uint16_t value = 0;
182 value |= getChar() << 0;
183 value |= getChar() << 8;
184 return value;
185 }
186
187 void skip(int n) {
188 int i;
189 for (i = 0; i < n; i++) getChar();
190 }
191
192 int getChar() {
193 if (data_pos < buffer.size())
194 return buffer.data()[data_pos++];
195 else
196 return -1;
197 }
198
199 void seek(long int offset, int origin) {
200 if (origin == SEEK_SET) {
201 data_pos = offset;
202 } else if (origin == SEEK_CUR) {
203 data_pos += offset;
204 }
205 }
206
207 size_t tell() { return data_pos; }
208
209 bool eof() { return data_pos >= buffer.size() - 1; }
210
211 void logInfo() {
212 LOGI("WAVHeader sound_pos: %d", getDataPos());
213 LOGI("WAVHeader channels: %d ", headerInfo.channels);
214 LOGI("WAVHeader bits_per_sample: %d", headerInfo.bits_per_sample);
215 LOGI("WAVHeader sample_rate: %d ", (int) headerInfo.sample_rate);
216 LOGI("WAVHeader format: %d", (int)headerInfo.format);
217 }
218
219 void writeRiffHeader(BaseBuffer<uint8_t> &buffer) {
220 buffer.writeArray((uint8_t *)"RIFF", 4);
221 write32(buffer, headerInfo.file_size - 8);
222 buffer.writeArray((uint8_t *)"WAVE", 4);
223 }
224
225 void writeFMT(BaseBuffer<uint8_t> &buffer) {
226 uint16_t fmt_len = 16;
227 buffer.writeArray((uint8_t *)"fmt ", 4);
228 write32(buffer, fmt_len);
229 write16(buffer, (uint16_t)headerInfo.format); // PCM
230 write16(buffer, headerInfo.channels);
231 write32(buffer, headerInfo.sample_rate);
232 write32(buffer, headerInfo.byte_rate);
233 write16(buffer, headerInfo.block_align); // frame size
234 write16(buffer, headerInfo.bits_per_sample);
235 }
236
237 void write32(BaseBuffer<uint8_t> &buffer, uint64_t value) {
238 buffer.writeArray((uint8_t *)&value, 4);
239 }
240
241 void write16(BaseBuffer<uint8_t> &buffer, uint16_t value) {
242 buffer.writeArray((uint8_t *)&value, 2);
243 }
244
245 void writeDataHeader(BaseBuffer<uint8_t> &buffer) {
246 buffer.writeArray((uint8_t *)"data", 4);
247 write32(buffer, headerInfo.file_size);
248 int offset = headerInfo.offset;
249 if (offset > 0) {
250 uint8_t empty[offset];
251 memset(empty, 0, offset);
252 buffer.writeArray(empty, offset); // resolve issue with wrong aligment
253 }
254 }
255
256};
257
270class WAVDecoder : public AudioDecoder {
271 public:
275 WAVDecoder() = default;
276
282 setDecoder(dec, fmt);
283 }
284
287 TRACED();
288 decoder_format = fmt;
289 p_decoder = &dec;
290 }
291
293 void setOutput(Print &out_stream) override { this->p_print = &out_stream; }
294
295 bool begin() override {
296 TRACED();
297 header.clear();
298 setupEncodedAudio();
299 buffer24.reset();
300 isFirst = true;
301 active = true;
302 return true;
303 }
304
305 void end() override {
306 TRACED();
307 buffer24.reset();
308 active = false;
309 }
310
311 const char *mime() { return wav_mime; }
312
313 WAVAudioInfo &audioInfoEx() { return header.audioInfo(); }
314
315 AudioInfo audioInfo() override { return header.audioInfo(); }
316
317 virtual size_t write(const uint8_t *data, size_t len) override {
318 TRACED();
319 size_t result = 0;
320 if (active) {
321 if (isFirst) {
322 int data_start = decodeHeader((uint8_t*) data, len);
323 // we do not have the complete header yet: need more data
324 if (data_start == 0) return len;
325 // process the outstanding data
326 result = data_start + write_out((uint8_t *)data+data_start, len-data_start);
327
328 } else if (isValid) {
329 result = write_out((uint8_t *)data, len);
330 }
331 }
332 return result;
333 }
334
335 virtual operator bool() override { return active; }
336
337 protected:
338 WAVHeader header;
339 bool isFirst = true;
340 bool isValid = true;
341 bool active = false;
342 AudioFormat decoder_format = AudioFormat::PCM;
343 AudioDecoderExt *p_decoder = nullptr;
344 EncodedAudioOutput dec_out;
345 SingleBuffer<uint8_t> buffer24;
346
347 Print& out() {
348 return p_decoder==nullptr ? *p_print : dec_out;
349 }
350
351 virtual size_t write_out(const uint8_t *in_ptr, size_t in_size) {
352 // check if we need to convert int24 data from 3 bytes to 4 bytes
353 size_t result = 0;
354 if (header.audioInfo().format == AudioFormat::PCM
355 && header.audioInfo().bits_per_sample == 24 && sizeof(int24_t)==4){
356 write_out_24(in_ptr, in_size);
357 result = in_size;
358 } else {
359 result = out().write(in_ptr, in_size);
360 }
361 return result;
362 }
363
364 // convert int24 to int32
365 size_t write_out_24(const uint8_t *in_ptr, size_t in_size) {
366 // make sure we can store a frame of 24bit (3bytes)
367 AudioInfo& info = header.audioInfo();
368 // in_size might be not a multiple of 3, so we use a buffer for a single frame
369 buffer24.resize(info.channels*3);
370 int result = 0;
371 int32_t frame[info.channels];
372 uint8_t val24[3]={0};
373
374 // add all bytes to buffer
375 for (int j=0;j<in_size;j++){
376 buffer24.write(in_ptr[j]);
377 // if buffer is full convert and output
378 if (buffer24.availableForWrite()==0){
379 for (int ch=0;ch<info.channels;ch++){
380 buffer24.readArray((uint8_t*)&val24[0], 3);
381 frame[ch] = interpret24bitAsInt32(val24);
382 //LOGW("%d", frame[ch]);
383 }
384 assert(buffer24.available()==0);
385 buffer24.reset();
386 size_t written = out().write((uint8_t*)frame,sizeof(frame));
387 assert(written==sizeof(frame));
388 result += written;
389 }
390 }
391 return result;
392 }
393
394 int32_t interpret24bitAsInt32(uint8_t* byteArray) {
395 return (
396 (static_cast<int32_t>(byteArray[2]) << 24)
397 | (static_cast<int32_t>(byteArray[1]) << 16)
398 | (static_cast<int32_t>(byteArray[0]) << 8)
399 );
400 }
401
403 int decodeHeader(uint8_t *in_ptr, size_t in_size) {
404 int result = in_size;
405 // we expect at least the full header
406 int written = header.write(in_ptr, in_size);
407 if (!header.isDataComplete()) {
408 LOGW("WAV header misses 'data' section in len: %d", (int) header.available());
409 header.dumpHeader();
410 return 0;
411 }
412 // parse header
413 if (!header.parse()){
414 LOGE("WAV header parsing failed");
415 return 0;
416 }
417
418 isFirst = false;
419 isValid = header.audioInfo().is_valid;
420
421 LOGI("WAV sample_rate: %d", (int) header.audioInfo().sample_rate);
422 LOGI("WAV data_length: %u", (unsigned)header.audioInfo().data_length);
423 LOGI("WAV is_streamed: %d", header.audioInfo().is_streamed);
424 LOGI("WAV is_valid: %s",
425 header.audioInfo().is_valid ? "true" : "false");
426
427 // check format
428 AudioFormat format = header.audioInfo().format;
429 isValid = format == decoder_format;
430 if (isValid) {
431 // update blocksize
432 if(p_decoder!=nullptr){
433 int block_size = header.audioInfo().block_align;
434 p_decoder->setBlockSize(block_size);
435 }
436
437 // update sampling rate if the target supports it
438 AudioInfo bi;
439 bi.sample_rate = header.audioInfo().sample_rate;
440 bi.channels = header.audioInfo().channels;
441 bi.bits_per_sample = header.audioInfo().bits_per_sample;
442 notifyAudioChange(bi);
443 } else {
444 LOGE("WAV format not supported: %d", (int)format);
445 }
446 return header.getDataPos();
447 }
448
449 void setupEncodedAudio() {
450 if (p_decoder!=nullptr){
451 assert(p_print!=nullptr);
452 dec_out.setOutput(p_print);
453 dec_out.setDecoder(p_decoder);
454 dec_out.begin(info);
455 }
456 }
457};
458
468class WAVEncoder : public AudioEncoder {
469 public:
473 WAVEncoder() = default;
474
479 setEncoder(enc, fmt);
480 };
481
482 void setEncoder(AudioEncoderExt &enc, AudioFormat fmt) {
483 TRACED();
484 audioInfo.format = fmt;
485 p_encoder = &enc;
486 }
487
489 void setOutput(Print &out) override {
490 TRACED();
491 p_print = &out;
492 }
493
495 const char *mime() override { return wav_mime; }
496
497 // Provides the default configuration
498 WAVAudioInfo defaultConfig() {
499 WAVAudioInfo info;
500 info.format = AudioFormat::PCM;
501 info.sample_rate = DEFAULT_SAMPLE_RATE;
502 info.bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
503 info.channels = DEFAULT_CHANNELS;
504 info.is_streamed = true;
505 info.is_valid = true;
506 info.data_length = 0x7fff0000;
507 info.file_size = info.data_length + 36;
508 return info;
509 }
510
512 virtual void setAudioInfo(AudioInfo from) override {
513 audioInfo.sample_rate = from.sample_rate;
514 audioInfo.channels = from.channels;
515 audioInfo.bits_per_sample = from.bits_per_sample;
516 // recalculate byte rate, block align...
517 setAudioInfo(audioInfo);
518 }
519
521 virtual void setAudioInfo(WAVAudioInfo ai) {
523 if (p_encoder) p_encoder->setAudioInfo(ai);
524 audioInfo = ai;
525 LOGI("sample_rate: %d", (int)audioInfo.sample_rate);
526 LOGI("channels: %d", audioInfo.channels);
527 // bytes per second
528 audioInfo.byte_rate = audioInfo.sample_rate * audioInfo.channels * audioInfo.bits_per_sample / 8;
529 if (audioInfo.format == AudioFormat::PCM){
530 audioInfo.block_align = audioInfo.bits_per_sample / 8 * audioInfo.channels;
531 }
532 if (audioInfo.is_streamed || audioInfo.data_length == 0 ||
533 audioInfo.data_length >= 0x7fff0000) {
534 LOGI("is_streamed! because length is %u",
535 (unsigned)audioInfo.data_length);
536 audioInfo.is_streamed = true;
537 audioInfo.data_length = ~0;
538 } else {
539 size_limit = audioInfo.data_length;
540 LOGI("size_limit is %d", (int)size_limit);
541 }
542 }
543
546 header.clear();
547 setAudioInfo(ai);
548 return begin();
549 }
550
552 virtual bool begin() override {
553 TRACED();
554 setupEncodedAudio();
555 header_written = false;
556 is_open = true;
557 return true;
558 }
559
561 void end() override { is_open = false; }
562
564 virtual size_t write(const uint8_t *data, size_t len) override {
565 if (!is_open) {
566 LOGE("The WAVEncoder is not open - please call begin()");
567 return 0;
568 }
569
570 if (p_print == nullptr) {
571 LOGE("No output stream was provided");
572 return 0;
573 }
574
575 if (!header_written) {
576 LOGI("Writing Header");
577 header.setAudioInfo(audioInfo);
578 int len = header.writeHeader(p_print);
579 audioInfo.file_size -= len;
580 header_written = true;
581 }
582
583 int32_t result = 0;
584 Print *p_out = p_encoder==nullptr ? p_print : &enc_out;;
585 if (audioInfo.is_streamed) {
586 result = p_out->write((uint8_t *)data, len);
587 } else if (size_limit > 0) {
588 size_t write_size = min((size_t)len, (size_t)size_limit);
589 result = p_out->write((uint8_t *)data, write_size);
590 size_limit -= result;
591
592 if (size_limit <= 0) {
593 LOGI("The defined size was written - so we close the WAVEncoder now");
594 is_open = false;
595 }
596 }
597 return result;
598 }
599
600 operator bool() override { return is_open; }
601
602 bool isOpen() { return is_open; }
603
605 void setDataOffset(uint16_t offset) { audioInfo.offset = offset; }
606
607 protected:
608 WAVHeader header;
609 Print *p_print = nullptr; // final output CopyEncoder copy; // used for PCM
610 AudioEncoderExt *p_encoder = nullptr;
611 EncodedAudioOutput enc_out;
612 WAVAudioInfo audioInfo = defaultConfig();
613 int64_t size_limit = 0;
614 bool header_written = false;
615 volatile bool is_open = false;
616
617 void setupEncodedAudio() {
618 if (p_encoder!=nullptr){
619 assert(p_print!=nullptr);
620 enc_out.setOutput(p_print);
621 enc_out.setEncoder(p_encoder);
622 enc_out.setAudioInfo(audioInfo);
623 enc_out.begin();
624 // block size only available after begin(): update block size
625 audioInfo.block_align = p_encoder->blockSize();
626 }
627 }
628};
629
630} // namespace audio_tools
WAV Audio Formats used by Microsoft e.g. in AVI video files.
Definition AudioCodecsBase.h:106
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:18
Definition AudioCodecsBase.h:111
Encoding of PCM data.
Definition AudioCodecsBase.h:90
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:99
virtual int writeArray(const T data[], int len)
Fills the buffer data.
Definition Buffers.h:59
void clear()
same as reset
Definition Buffers.h:99
A more natural Print class to process encoded data (aac, wav, mp3...). Just define the output and the...
Definition AudioEncoded.h:21
bool begin() override
Starts the processing - sets the status to active.
Definition AudioEncoded.h:137
virtual void setAudioInfo(AudioInfo newInfo) override
Defines the input AudioInfo.
Definition AudioEncoded.h:87
void setOutput(Print &outputStream)
Defines/Changes the output target.
Definition AudioEncoded.h:97
Definition NoArduino.h:62
void setClearWithZero(bool flag)
Sets the buffer to 0 on clear.
Definition Buffers.h:290
int available() override
provides the number of entries that are available to read
Definition Buffers.h:227
T * data()
Provides address of actual data.
Definition Buffers.h:261
void reset() override
clears the buffer
Definition Buffers.h:263
A simple wrapper to provide string functions on existing allocated char*. If the underlying char* is ...
Definition StrView.h:28
virtual int indexOf(const char c, int start=0)
Definition StrView.h:260
A simple WAVDecoder: We parse the header data on the first record to determine the format....
Definition CodecWAV.h:270
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecWAV.h:293
void setDecoder(AudioDecoderExt &dec, AudioFormat fmt)
Defines an optional decoder if the format is not PCM.
Definition CodecWAV.h:286
int decodeHeader(uint8_t *in_ptr, size_t in_size)
Decodes the header data: Returns the start pos of the data.
Definition CodecWAV.h:403
WAVDecoder()=default
Construct a new WAVDecoder object for PCM data.
WAVDecoder(AudioDecoderExt &dec, AudioFormat fmt)
Construct a new WAVDecoder object for ADPCM data.
Definition CodecWAV.h:281
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition CodecWAV.h:315
A simple WAV file encoder. If no AudioEncoderExt is specified the WAV file contains PCM data,...
Definition CodecWAV.h:468
virtual size_t write(const uint8_t *data, size_t len) override
Writes PCM data to be encoded as WAV.
Definition CodecWAV.h:564
void setOutput(Print &out) override
Defines the otuput stream.
Definition CodecWAV.h:489
virtual bool begin() override
starts the processing using the actual WAVAudioInfo
Definition CodecWAV.h:552
bool begin(WAVAudioInfo ai)
starts the processing
Definition CodecWAV.h:545
void end() override
stops the processing
Definition CodecWAV.h:561
virtual void setAudioInfo(WAVAudioInfo ai)
Defines the WAVAudioInfo.
Definition CodecWAV.h:521
WAVEncoder()=default
Construct a new WAVEncoder object for PCM data.
void setDataOffset(uint16_t offset)
Adds n empty bytes at the beginning of the data.
Definition CodecWAV.h:605
WAVEncoder(AudioEncoderExt &enc, AudioFormat fmt)
Construct a new WAVEncoder object for ADPCM data.
Definition CodecWAV.h:478
const char * mime() override
Provides "audio/wav".
Definition CodecWAV.h:495
virtual void setAudioInfo(AudioInfo from) override
Update actual WAVAudioInfo.
Definition CodecWAV.h:512
Parser for Wav header data for details see https://de.wikipedia.org/wiki/RIFF_WAVE.
Definition CodecWAV.h:46
bool parse()
Call begin when header data is complete to parse the data.
Definition CodecWAV.h:56
bool isDataComplete()
Returns true if the header is complete (containd data tag)
Definition CodecWAV.h:85
void setAudioInfo(WAVAudioInfo info)
Sets the info in the header.
Definition CodecWAV.h:105
size_t available()
number of bytes available in the header buffer
Definition CodecWAV.h:91
int getDataPos()
Determines the data start position using the data tag.
Definition CodecWAV.h:96
WAVAudioInfo & audioInfo()
provides the info from the header
Definition CodecWAV.h:102
int write(uint8_t *data, size_t data_len)
Adds data to the 44 byte wav header data buffer and make it available for parsing.
Definition CodecWAV.h:51
int writeHeader(Print *out)
Just write a wav header to the indicated outputbu.
Definition CodecWAV.h:110
AudioFormat
Audio format codes used by Microsoft e.g. in avi or wav files.
Definition AudioFormat.h:19
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:53
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:59
Sound information which is available in the WAV header.
Definition CodecWAV.h:19