3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
5 #include "AudioTools/CoreAudio/AudioBasic/StrView.h"
7 #define READ_BUFFER_SIZE 512
8 #define MAX_WAV_HEADER_LEN 200
29 bool is_streamed =
true;
30 bool is_valid =
false;
31 uint32_t data_length = 0;
32 uint32_t file_size = 0;
36 static const char *wav_mime =
"audio/wav";
50 int write(uint8_t *data,
size_t data_len) {
56 LOGI(
"WAVHeader::begin: %u", (
unsigned)buffer.
available());
60 if (!setPos(
"RIFF"))
return false;
61 headerInfo.file_size = read_int32();
62 if (!setPos(
"WAVE"))
return false;
63 if (!setPos(
"fmt "))
return false;
64 int fmt_length = read_int32();
66 headerInfo.channels = read_int16();
67 headerInfo.sample_rate = read_int32();
68 headerInfo.byte_rate = read_int32();
69 headerInfo.block_align = read_int16();
70 headerInfo.bits_per_sample = read_int16();
71 if (!setPos(
"data"))
return false;
72 headerInfo.data_length = read_int32();
73 if (!headerInfo.data_length==0 || headerInfo.data_length >= 0x7fff0000) {
74 headerInfo.is_streamed =
true;
75 headerInfo.data_length = ~0;
86 return pos > 0 && buffer.
available() >= pos;
97 return pos > 0 ? pos + 8 : 0;
110 writeRiffHeader(buffer);
112 writeDataHeader(buffer);
127 for (
int j = 0; j< buffer.
available();j++){
128 char c = (char)buffer.
data()[j];
134 LOGI(
"Header: %s", msg);
139 struct WAVAudioInfo headerInfo;
140 SingleBuffer<uint8_t> buffer{MAX_WAV_HEADER_LEN};
143 bool setPos(
const char*
id){
144 int id_len = strlen(
id);
145 int pos = indexOf(
id);
146 if (pos < 0)
return false;
147 data_pos = pos + id_len;
151 int indexOf(
const char* str){
152 return StrView((
char*)buffer.
data(),MAX_WAV_HEADER_LEN, buffer.
available()).indexOf(str);
155 uint32_t read_tag() {
157 tag = (tag << 8) | getChar();
158 tag = (tag << 8) | getChar();
159 tag = (tag << 8) | getChar();
160 tag = (tag << 8) | getChar();
164 uint32_t getChar32() {
return getChar(); }
166 uint32_t read_int32() {
168 value |= getChar32() << 0;
169 value |= getChar32() << 8;
170 value |= getChar32() << 16;
171 value |= getChar32() << 24;
175 uint16_t read_int16() {
177 value |= getChar() << 0;
178 value |= getChar() << 8;
184 for (i = 0; i < n; i++) getChar();
188 if (data_pos < buffer.size())
189 return buffer.
data()[data_pos++];
194 void seek(
long int offset,
int origin) {
195 if (origin == SEEK_SET) {
197 }
else if (origin == SEEK_CUR) {
202 size_t tell() {
return data_pos; }
204 bool eof() {
return data_pos >= buffer.size() - 1; }
207 LOGI(
"WAVHeader sound_pos: %d",
getDataPos());
208 LOGI(
"WAVHeader channels: %d ", headerInfo.
channels);
210 LOGI(
"WAVHeader sample_rate: %d ", (
int) headerInfo.
sample_rate);
211 LOGI(
"WAVHeader format: %d", (
int)headerInfo.format);
214 void writeRiffHeader(BaseBuffer<uint8_t> &buffer) {
215 buffer.writeArray((uint8_t *)
"RIFF", 4);
216 write32(buffer, headerInfo.file_size - 8);
217 buffer.writeArray((uint8_t *)
"WAVE", 4);
220 void writeFMT(BaseBuffer<uint8_t> &buffer) {
221 uint16_t fmt_len = 16;
222 buffer.writeArray((uint8_t *)
"fmt ", 4);
223 write32(buffer, fmt_len);
224 write16(buffer, (uint16_t)headerInfo.format);
225 write16(buffer, headerInfo.
channels);
227 write32(buffer, headerInfo.byte_rate);
228 write16(buffer, headerInfo.block_align);
232 void write32(BaseBuffer<uint8_t> &buffer, uint64_t value) {
233 buffer.writeArray((uint8_t *)&value, 4);
236 void write16(BaseBuffer<uint8_t> &buffer, uint16_t value) {
237 buffer.writeArray((uint8_t *)&value, 2);
240 void writeDataHeader(BaseBuffer<uint8_t> &buffer) {
241 buffer.writeArray((uint8_t *)
"data", 4);
242 write32(buffer, headerInfo.file_size);
243 int offset = headerInfo.offset;
245 uint8_t empty[offset];
246 memset(empty, 0, offset);
247 buffer.writeArray(empty, offset);
283 decoder_format = fmt;
290 bool begin()
override {
300 void end()
override {
306 const char *mime() {
return wav_mime; }
308 WAVAudioInfo &audioInfoEx() {
return header.
audioInfo(); }
312 virtual size_t write(
const uint8_t *data,
size_t len)
override {
319 if (data_start == 0)
return len;
321 result = data_start + write_out((uint8_t *)data+data_start, len-data_start);
323 }
else if (isValid) {
324 result = write_out((uint8_t *)data, len);
330 virtual operator bool()
override {
return active; }
338 AudioDecoderExt *p_decoder =
nullptr;
339 EncodedAudioOutput dec_out;
340 SingleBuffer<uint8_t> buffer24;
343 return p_decoder==
nullptr ? *p_print : dec_out;
346 virtual size_t write_out(
const uint8_t *in_ptr,
size_t in_size) {
349 if (header.audioInfo().format == AudioFormat::PCM
350 && header.audioInfo().bits_per_sample == 24 &&
sizeof(int24_t)==4){
351 write_out_24(in_ptr, in_size);
354 result = out().write(in_ptr, in_size);
360 size_t write_out_24(
const uint8_t *in_ptr,
size_t in_size) {
362 AudioInfo& info = header.audioInfo();
364 buffer24.resize(info.channels*3);
366 int32_t frame[info.channels];
367 uint8_t val24[3]={0};
370 for (
int j=0;j<in_size;j++){
371 buffer24.write(in_ptr[j]);
373 if (buffer24.availableForWrite()==0){
374 for (
int ch=0;ch<info.channels;ch++){
375 buffer24.readArray((uint8_t*)&val24[0], 3);
376 frame[ch] = interpret24bitAsInt32(val24);
379 assert(buffer24.available()==0);
381 size_t written = out().write((uint8_t*)frame,
sizeof(frame));
382 assert(written==
sizeof(frame));
389 int32_t interpret24bitAsInt32(uint8_t* byteArray) {
391 (
static_cast<int32_t
>(byteArray[2]) << 24)
392 | (
static_cast<int32_t
>(byteArray[1]) << 16)
393 | (
static_cast<int32_t
>(byteArray[0]) << 8)
399 int result = in_size;
401 int written = header.write(in_ptr, in_size);
402 if (!header.isDataComplete()) {
403 LOGW(
"WAV header misses 'data' section in len: %d", (
int) header.available());
408 if (!header.parse()){
409 LOGE(
"WAV header parsing failed");
414 isValid = header.audioInfo().is_valid;
416 LOGI(
"WAV sample_rate: %d", (
int) header.audioInfo().sample_rate);
417 LOGI(
"WAV data_length: %u", (
unsigned)header.audioInfo().data_length);
418 LOGI(
"WAV is_streamed: %d", header.audioInfo().is_streamed);
419 LOGI(
"WAV is_valid: %s",
420 header.audioInfo().is_valid ?
"true" :
"false");
424 isValid = format == decoder_format;
427 if(p_decoder!=
nullptr){
428 int block_size = header.audioInfo().block_align;
429 p_decoder->setBlockSize(block_size);
435 bi.
channels = header.audioInfo().channels;
437 notifyAudioChange(bi);
439 LOGE(
"WAV format not supported: %d", (
int)format);
441 return header.getDataPos();
444 void setupEncodedAudio() {
445 if (p_decoder!=
nullptr){
446 assert(p_print!=
nullptr);
447 dec_out.setOutput(p_print);
448 dec_out.setDecoder(p_decoder);
474 setEncoder(enc, fmt);
479 audioInfo.format = fmt;
490 const char *
mime()
override {
return wav_mime; }
495 info.format = AudioFormat::PCM;
499 info.is_streamed =
true;
500 info.is_valid =
true;
501 info.data_length = 0x7fff0000;
502 info.file_size = info.data_length + 36;
520 LOGI(
"sample_rate: %d", (
int)audioInfo.
sample_rate);
521 LOGI(
"channels: %d", audioInfo.
channels);
524 if (audioInfo.format == AudioFormat::PCM){
527 if (audioInfo.is_streamed || audioInfo.data_length == 0 ||
528 audioInfo.data_length >= 0x7fff0000) {
529 LOGI(
"is_streamed! because length is %u",
530 (
unsigned)audioInfo.data_length);
531 audioInfo.is_streamed =
true;
532 audioInfo.data_length = ~0;
534 size_limit = audioInfo.data_length;
535 LOGI(
"size_limit is %d", (
int)size_limit);
550 header_written =
false;
556 void end()
override { is_open =
false; }
559 virtual size_t write(
const uint8_t *data,
size_t len)
override {
561 LOGE(
"The WAVEncoder is not open - please call begin()");
565 if (p_print ==
nullptr) {
566 LOGE(
"No output stream was provided");
570 if (!header_written) {
571 LOGI(
"Writing Header");
572 header.setAudioInfo(audioInfo);
573 int len = header.writeHeader(p_print);
574 audioInfo.file_size -= len;
575 header_written =
true;
579 Print *p_out = p_encoder==
nullptr ? p_print : &enc_out;;
580 if (audioInfo.is_streamed) {
581 result = p_out->write((uint8_t *)data, len);
582 }
else if (size_limit > 0) {
583 size_t write_size = min((
size_t)len, (
size_t)size_limit);
584 result = p_out->write((uint8_t *)data, write_size);
585 size_limit -= result;
587 if (size_limit <= 0) {
588 LOGI(
"The defined size was written - so we close the WAVEncoder now");
595 operator bool()
override {
return is_open; }
597 bool isOpen() {
return is_open; }
604 Print *p_print =
nullptr;
608 int64_t size_limit = 0;
609 bool header_written =
false;
610 volatile bool is_open =
false;
612 void setupEncodedAudio() {
613 if (p_encoder!=
nullptr){
614 assert(p_print!=
nullptr);
616 enc_out.setEncoder(p_encoder);
620 audioInfo.block_align = p_encoder->blockSize();
AudioFormat
Audio format codes used by Microsoft e.g. in avi or wav files.
Definition: AudioFormat.h:19