3 #include "AudioCodecs/AudioEncoded.h"
7 #define TAG(a, b, c, d) \
8 ((static_cast<uint32_t>(a) << 24) | (static_cast<uint32_t>(b) << 16) | \
9 (static_cast<uint32_t>(c) << 8) | (d))
10 #define READ_BUFFER_SIZE 512
23 sample_rate = from.sample_rate;
24 channels = from.channels;
25 bits_per_sample = from.bits_per_sample;
31 bool is_streamed =
true;
32 bool is_valid =
false;
33 uint32_t data_length = 0;
34 uint32_t file_size = 0;
38 static const char *wav_mime =
"audio/wav";
52 int write(uint8_t *data,
size_t data_len) {
53 int write_len = min(data_len, 44 - len);
54 memmove(buffer, data + len, write_len);
56 LOGI(
"WAVHeader::write: %u -> %d -> %d", (
unsigned)data_len, write_len,
63 LOGI(
"WAVHeader::begin: %u", (
unsigned)len);
67 uint32_t tag, tag2, length;
70 length = read_int32();
71 if (!length || length >= 0x7fff0000) {
72 headerInfo.is_streamed =
true;
75 if (tag != TAG(
'R',
'I',
'F',
'F') || length < 4) {
76 seek(length, SEEK_CUR);
81 if (tag2 != TAG(
'W',
'A',
'V',
'E')) {
82 seek(length, SEEK_CUR);
87 uint32_t subtag, sublength;
90 sublength = read_int32();
92 if (length < sublength)
break;
93 if (subtag == TAG(
'f',
'm',
't',
' ')) {
99 headerInfo.channels = read_int16();
100 headerInfo.sample_rate = read_int32();
101 headerInfo.byte_rate = read_int32();
102 headerInfo.block_align = read_int16();
103 headerInfo.bits_per_sample = read_int16();
105 if (sublength < 28) {
111 skip(sublength - 28);
113 skip(sublength - 16);
115 headerInfo.is_valid =
true;
116 }
else if (subtag == TAG(
'd',
'a',
't',
'a')) {
118 headerInfo.data_length = sublength;
119 if (!headerInfo.data_length || headerInfo.is_streamed) {
120 headerInfo.is_streamed =
true;
124 seek(sublength, SEEK_CUR);
132 seek(length, SEEK_CUR);
153 writeRiffHeader(buffer);
155 writeDataHeader(buffer);
165 size_t sound_pos = 0;
167 uint32_t read_tag() {
169 tag = (tag << 8) | getChar();
170 tag = (tag << 8) | getChar();
171 tag = (tag << 8) | getChar();
172 tag = (tag << 8) | getChar();
176 uint32_t getChar32() {
return getChar(); }
178 uint32_t read_int32() {
180 value |= getChar32() << 0;
181 value |= getChar32() << 8;
182 value |= getChar32() << 16;
183 value |= getChar32() << 24;
187 uint16_t read_int16() {
189 value |= getChar() << 0;
190 value |= getChar() << 8;
196 for (i = 0; i < n; i++) getChar();
201 return buffer[data_pos++];
206 void seek(
long int offset,
int origin) {
207 if (origin == SEEK_SET) {
209 }
else if (origin == SEEK_CUR) {
214 size_t tell() {
return data_pos; }
216 bool eof() {
return data_pos >= len - 1; }
219 LOGI(
"WAVHeader sound_pos: %lu", (
unsigned long)sound_pos);
220 LOGI(
"WAVHeader channels: %d ", headerInfo.channels);
221 LOGI(
"WAVHeader bits_per_sample: %d", headerInfo.bits_per_sample);
222 LOGI(
"WAVHeader sample_rate: %d ", headerInfo.sample_rate);
223 LOGI(
"WAVHeader format: %d", (
int)headerInfo.format);
226 void writeRiffHeader(BaseBuffer<uint8_t> &buffer) {
227 buffer.writeArray((uint8_t *)
"RIFF", 4);
228 write32(buffer, headerInfo.file_size - 8);
229 buffer.writeArray((uint8_t *)
"WAVE", 4);
232 void writeFMT(BaseBuffer<uint8_t> &buffer) {
233 uint16_t fmt_len = 16;
234 buffer.writeArray((uint8_t *)
"fmt ", 4);
235 write32(buffer, fmt_len);
236 write16(buffer, (uint16_t)headerInfo.format);
237 write16(buffer, headerInfo.channels);
238 write32(buffer, headerInfo.sample_rate);
239 write32(buffer, headerInfo.byte_rate);
240 write16(buffer, headerInfo.block_align);
241 write16(buffer, headerInfo.bits_per_sample);
244 void write32(BaseBuffer<uint8_t> &buffer, uint64_t value) {
245 buffer.writeArray((uint8_t *)&value, 4);
248 void write16(BaseBuffer<uint8_t> &buffer, uint16_t value) {
249 buffer.writeArray((uint8_t *)&value, 2);
252 void writeDataHeader(BaseBuffer<uint8_t> &buffer) {
253 buffer.writeArray((uint8_t *)
"data", 4);
254 write32(buffer, headerInfo.file_size);
255 int offset = headerInfo.offset;
257 uint8_t empty[offset];
258 memset(empty, 0, offset);
259 buffer.writeArray(empty, offset);
293 decoder_format = fmt;
302 this->audioBaseInfoSupport = &bi;
317 const char *mime() {
return wav_mime; }
319 WAVAudioInfo &audioInfoEx() {
return header.
audioInfo(); }
321 AudioInfo audioInfo() {
return header.
audioInfo(); }
323 virtual size_t write(
const void *in_ptr,
size_t in_size) {
328 result = decodeHeader((uint8_t*) in_ptr, in_size);
329 }
else if (isValid) {
330 result = out().write((uint8_t *)in_ptr, in_size);
336 virtual operator bool() {
return active; }
340 AudioInfoSupport *audioBaseInfoSupport=
nullptr;
345 AudioDecoderExt *p_decoder =
nullptr;
346 EncodedAudioOutput dec_out;
349 return p_decoder==
nullptr ? *p_print : dec_out;
352 int decodeHeader(uint8_t *in_ptr,
size_t in_size) {
355 int written = header.write(in_ptr, in_size);
356 if (!header.isDataComplete()) {
362 size_t len = in_size - written;
363 uint8_t *sound_ptr = (uint8_t *)in_ptr + written;
365 isValid = header.audioInfo().is_valid;
367 LOGI(
"WAV sample_rate: %d", header.audioInfo().sample_rate);
368 LOGI(
"WAV data_length: %u", (
unsigned)header.audioInfo().data_length);
369 LOGI(
"WAV is_streamed: %d", header.audioInfo().is_streamed);
370 LOGI(
"WAV is_valid: %s",
371 header.audioInfo().is_valid ?
"true" :
"false");
375 isValid = format == decoder_format;
378 if(p_decoder!=
nullptr){
379 int block_size = header.audioInfo().block_align;
380 p_decoder->setBlockSize(block_size);
385 bi.sample_rate = header.audioInfo().sample_rate;
386 bi.channels = header.audioInfo().channels;
387 bi.bits_per_sample = header.audioInfo().bits_per_sample;
390 if (audioBaseInfoSupport !=
nullptr) {
391 isValid = audioBaseInfoSupport->validate(bi);
393 LOGI(
"isValid: %s", isValid ?
"true" :
"false");
394 audioBaseInfoSupport->setAudioInfo(bi);
396 LOGI(
"WAVDecoder writing first sound data");
397 result = out().write(sound_ptr, len);
399 LOGE(
"isValid: %s", isValid ?
"true" :
"false");
403 LOGE(
"WAV format not supported: %d", (
int)format);
408 void setupEncodedAudio() {
409 if (p_decoder!=
nullptr){
410 assert(p_print!=
nullptr);
411 dec_out.setOutput(p_print);
412 dec_out.setDecoder(p_decoder);
438 setEncoder(enc, fmt);
443 audioInfo.format = fmt;
454 const char *
mime()
override {
return wav_mime; }
459 info.format = AudioFormat::PCM;
460 info.sample_rate = DEFAULT_SAMPLE_RATE;
461 info.bits_per_sample = DEFAULT_BITS_PER_SAMPLE;
462 info.channels = DEFAULT_CHANNELS;
463 info.is_streamed =
true;
464 info.is_valid =
true;
465 info.data_length = 0x7fff0000;
466 info.file_size = info.data_length + 36;
472 audioInfo.sample_rate = from.sample_rate;
473 audioInfo.channels = from.channels;
474 audioInfo.bits_per_sample = from.bits_per_sample;
482 LOGI(
"sample_rate: %d", audioInfo.sample_rate);
483 LOGI(
"channels: %d", audioInfo.channels);
485 audioInfo.byte_rate = audioInfo.sample_rate * audioInfo.channels * audioInfo.bits_per_sample / 8;
486 if (audioInfo.format == AudioFormat::PCM){
487 audioInfo.block_align = audioInfo.bits_per_sample / 8 * audioInfo.channels;
489 if (audioInfo.is_streamed || audioInfo.data_length == 0 ||
490 audioInfo.data_length >= 0x7fff0000) {
491 LOGI(
"is_streamed! because length is %u",
492 (
unsigned)audioInfo.data_length);
493 audioInfo.is_streamed =
true;
494 audioInfo.data_length = ~0;
496 size_limit = audioInfo.data_length;
497 LOGI(
"size_limit is %d", (
int)size_limit);
511 header_written =
false;
516 void end()
override { is_open =
false; }
519 virtual size_t write(
const void *in_ptr,
size_t in_size)
override {
521 LOGE(
"The WAVEncoder is not open - please call begin()");
525 if (p_print ==
nullptr) {
526 LOGE(
"No output stream was provided");
530 if (!header_written) {
531 LOGI(
"Writing Header");
532 header.setAudioInfo(audioInfo);
533 header.writeHeader(p_print);
534 audioInfo.file_size -= 44;
535 header_written =
true;
539 Print *p_out = p_encoder==
nullptr ? p_print : &enc_out;;
540 if (audioInfo.is_streamed) {
541 result = p_out->write((uint8_t *)in_ptr, in_size);
542 }
else if (size_limit > 0) {
543 size_t write_size = min((
size_t)in_size, (
size_t)size_limit);
544 result = p_out->write((uint8_t *)in_ptr, write_size);
545 size_limit -= result;
547 if (size_limit <= 0) {
548 LOGI(
"The defined size was written - so we close the WAVEncoder now");
555 operator bool()
override {
return is_open; }
557 bool isOpen() {
return is_open; }
564 Print *p_print =
nullptr;
568 int64_t size_limit = 0;
569 bool header_written =
false;
570 volatile bool is_open =
false;
572 void setupEncodedAudio() {
573 if (p_encoder!=
nullptr){
574 assert(p_print!=
nullptr);
576 enc_out.setEncoder(p_encoder);
577 enc_out.setAudioInfo(audioInfo);
580 audioInfo.block_align = p_encoder->blockSize();
AudioFormat
Audio format codes used by Microsoft e.g. in avi or wav files.
Definition: AudioFormat.h:19