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
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);
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);
239 write32(buffer, headerInfo.byte_rate);
240 write16(buffer, headerInfo.block_align);
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;
315 const char *mime() {
return wav_mime; }
317 WAVAudioInfo &audioInfoEx() {
return header.
audioInfo(); }
319 AudioInfo audioInfo()
override {
return header.
audioInfo(); }
321 virtual size_t write(
const void *in_ptr,
size_t in_size) {
326 result = decodeHeader((uint8_t*) in_ptr, in_size);
328 result += write_out((uint8_t *)in_ptr+result, in_size-result);
330 }
else if (isValid) {
331 result = write_out((uint8_t *)in_ptr, in_size);
337 virtual operator bool() {
return active; }
345 AudioDecoderExt *p_decoder =
nullptr;
346 EncodedAudioOutput dec_out;
347 SingleBuffer<uint8_t> buffer24;
350 return p_decoder==
nullptr ? *p_print : dec_out;
353 virtual size_t write_out(
const uint8_t *in_ptr,
size_t in_size) {
356 if (header.audioInfo().bits_per_sample == 24 &&
sizeof(int24_t)==4){
357 write_out_24(in_ptr, in_size);
360 result = out().write(in_ptr, in_size);
366 size_t write_out_24(
const uint8_t *in_ptr,
size_t in_size) {
368 AudioInfo& info = header.audioInfo();
370 buffer24.resize(info.channels*3);
372 int32_t frame[info.channels];
373 uint8_t val24[3]={0};
376 for (
int j=0;j<in_size;j++){
377 buffer24.write(in_ptr[j]);
379 if (buffer24.availableForWrite()==0){
380 for (
int ch=0;ch<info.channels;ch++){
381 buffer24.readArray((uint8_t*)&val24[0], 3);
382 frame[ch] = interpret24bitAsInt32(val24);
385 assert(buffer24.available()==0);
387 size_t written = out().write((uint8_t*)frame,
sizeof(frame));
388 assert(written==
sizeof(frame));
395 int32_t interpret24bitAsInt32(uint8_t* byteArray) {
397 (
static_cast<int32_t
>(byteArray[2]) << 24)
398 | (
static_cast<int32_t
>(byteArray[1]) << 16)
399 | (
static_cast<int32_t
>(byteArray[0]) << 8)
404 int decodeHeader(uint8_t *in_ptr,
size_t in_size) {
407 int written = header.write(in_ptr, in_size);
408 if (!header.isDataComplete()) {
414 size_t len = in_size - written;
415 uint8_t *sound_ptr = (uint8_t *)in_ptr + written;
417 isValid = header.audioInfo().is_valid;
419 LOGI(
"WAV sample_rate: %d", header.audioInfo().sample_rate);
420 LOGI(
"WAV data_length: %u", (
unsigned)header.audioInfo().data_length);
421 LOGI(
"WAV is_streamed: %d", header.audioInfo().is_streamed);
422 LOGI(
"WAV is_valid: %s",
423 header.audioInfo().is_valid ?
"true" :
"false");
427 isValid = format == decoder_format;
430 if(p_decoder!=
nullptr){
431 int block_size = header.audioInfo().block_align;
432 p_decoder->setBlockSize(block_size);
437 bi.sample_rate = header.audioInfo().sample_rate;
438 bi.channels = header.audioInfo().channels;
439 bi.bits_per_sample = header.audioInfo().bits_per_sample;
440 notifyAudioChange(bi);
442 LOGI(
"WAVDecoder writing first sound data");
443 result = out().write(sound_ptr, len);
445 LOGE(
"WAV format not supported: %d", (
int)format);
450 void setupEncodedAudio() {
451 if (p_decoder!=
nullptr){
452 assert(p_print!=
nullptr);
453 dec_out.setOutput(p_print);
454 dec_out.setDecoder(p_decoder);
480 setEncoder(enc, fmt);
485 audioInfo.format = fmt;
496 const char *
mime()
override {
return wav_mime; }
501 info.format = AudioFormat::PCM;
505 info.is_streamed =
true;
506 info.is_valid =
true;
507 info.data_length = 0x7fff0000;
508 info.file_size = info.data_length + 36;
526 LOGI(
"channels: %d", audioInfo.
channels);
529 if (audioInfo.format == AudioFormat::PCM){
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;
539 size_limit = audioInfo.data_length;
540 LOGI(
"size_limit is %d", (
int)size_limit);
554 header_written =
false;
560 void end()
override { is_open =
false; }
563 virtual size_t write(
const void *in_ptr,
size_t in_size)
override {
565 LOGE(
"The WAVEncoder is not open - please call begin()");
569 if (p_print ==
nullptr) {
570 LOGE(
"No output stream was provided");
574 if (!header_written) {
575 LOGI(
"Writing Header");
576 header.setAudioInfo(audioInfo);
577 header.writeHeader(p_print);
578 audioInfo.file_size -= 44;
579 header_written =
true;
583 Print *p_out = p_encoder==
nullptr ? p_print : &enc_out;;
584 if (audioInfo.is_streamed) {
585 result = p_out->write((uint8_t *)in_ptr, in_size);
586 }
else if (size_limit > 0) {
587 size_t write_size = min((
size_t)in_size, (
size_t)size_limit);
588 result = p_out->write((uint8_t *)in_ptr, write_size);
589 size_limit -= result;
591 if (size_limit <= 0) {
592 LOGI(
"The defined size was written - so we close the WAVEncoder now");
599 operator bool()
override {
return is_open; }
601 bool isOpen() {
return is_open; }
608 Print *p_print =
nullptr;
612 int64_t size_limit = 0;
613 bool header_written =
false;
614 volatile bool is_open =
false;
616 void setupEncodedAudio() {
617 if (p_encoder!=
nullptr){
618 assert(p_print!=
nullptr);
620 enc_out.setEncoder(p_encoder);
621 enc_out.setAudioInfo(audioInfo);
624 audioInfo.block_align = p_encoder->blockSize();
AudioFormat
Audio format codes used by Microsoft e.g. in avi or wav files.
Definition: AudioFormat.h:19