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);
131 for (
int j = 0; j< buffer.
available();j++){
132 char c = (char)buffer.
data()[j];
138 LOGI(
"Header: %s", msg);
143 struct WAVAudioInfo headerInfo;
144 SingleBuffer<uint8_t> buffer{MAX_WAV_HEADER_LEN};
147 bool setPos(
const char*
id){
148 int id_len = strlen(
id);
149 int pos = indexOf(
id);
150 if (pos < 0)
return false;
151 data_pos = pos + id_len;
155 int indexOf(
const char* str){
156 return StrView((
char*)buffer.
data(),MAX_WAV_HEADER_LEN, buffer.
available()).indexOf(str);
159 uint32_t read_tag() {
161 tag = (tag << 8) | getChar();
162 tag = (tag << 8) | getChar();
163 tag = (tag << 8) | getChar();
164 tag = (tag << 8) | getChar();
168 uint32_t getChar32() {
return getChar(); }
170 uint32_t read_int32() {
172 value |= getChar32() << 0;
173 value |= getChar32() << 8;
174 value |= getChar32() << 16;
175 value |= getChar32() << 24;
179 uint16_t read_int16() {
181 value |= getChar() << 0;
182 value |= getChar() << 8;
188 for (i = 0; i < n; i++) getChar();
192 if (data_pos < buffer.size())
193 return buffer.
data()[data_pos++];
198 void seek(
long int offset,
int origin) {
199 if (origin == SEEK_SET) {
201 }
else if (origin == SEEK_CUR) {
206 size_t tell() {
return data_pos; }
208 bool eof() {
return data_pos >= buffer.size() - 1; }
211 LOGI(
"WAVHeader sound_pos: %d",
getDataPos());
212 LOGI(
"WAVHeader channels: %d ", headerInfo.
channels);
214 LOGI(
"WAVHeader sample_rate: %d ", (
int) headerInfo.
sample_rate);
215 LOGI(
"WAVHeader format: %d", (
int)headerInfo.format);
218 void writeRiffHeader(BaseBuffer<uint8_t> &buffer) {
219 buffer.writeArray((uint8_t *)
"RIFF", 4);
220 write32(buffer, headerInfo.file_size - 8);
221 buffer.writeArray((uint8_t *)
"WAVE", 4);
224 void writeFMT(BaseBuffer<uint8_t> &buffer) {
225 uint16_t fmt_len = 16;
226 buffer.writeArray((uint8_t *)
"fmt ", 4);
227 write32(buffer, fmt_len);
228 write16(buffer, (uint16_t)headerInfo.format);
229 write16(buffer, headerInfo.
channels);
231 write32(buffer, headerInfo.byte_rate);
232 write16(buffer, headerInfo.block_align);
236 void write32(BaseBuffer<uint8_t> &buffer, uint64_t value) {
237 buffer.writeArray((uint8_t *)&value, 4);
240 void write16(BaseBuffer<uint8_t> &buffer, uint16_t value) {
241 buffer.writeArray((uint8_t *)&value, 2);
244 void writeDataHeader(BaseBuffer<uint8_t> &buffer) {
245 buffer.writeArray((uint8_t *)
"data", 4);
246 write32(buffer, headerInfo.file_size);
247 int offset = headerInfo.offset;
249 uint8_t empty[offset];
250 memset(empty, 0, offset);
251 buffer.writeArray(empty, offset);
287 decoder_format = fmt;
294 bool begin()
override {
304 void end()
override {
310 const char *mime() {
return wav_mime; }
312 WAVAudioInfo &audioInfoEx() {
return header.
audioInfo(); }
316 virtual size_t write(
const uint8_t *data,
size_t len)
override {
323 if (data_start == 0)
return len;
325 result = data_start + write_out((uint8_t *)data+data_start, len-data_start);
327 }
else if (isValid) {
328 result = write_out((uint8_t *)data, len);
334 virtual operator bool()
override {
return active; }
342 AudioDecoderExt *p_decoder =
nullptr;
343 EncodedAudioOutput dec_out;
344 SingleBuffer<uint8_t> buffer24;
347 return p_decoder==
nullptr ? *p_print : dec_out;
350 virtual size_t write_out(
const uint8_t *in_ptr,
size_t in_size) {
353 if (header.audioInfo().format == AudioFormat::PCM
354 && header.audioInfo().bits_per_sample == 24 &&
sizeof(int24_t)==4){
355 write_out_24(in_ptr, in_size);
358 result = out().write(in_ptr, in_size);
364 size_t write_out_24(
const uint8_t *in_ptr,
size_t in_size) {
366 AudioInfo& info = header.audioInfo();
368 buffer24.resize(info.channels*3);
370 int32_t frame[info.channels];
371 uint8_t val24[3]={0};
374 for (
int j=0;j<in_size;j++){
375 buffer24.write(in_ptr[j]);
377 if (buffer24.availableForWrite()==0){
378 for (
int ch=0;ch<info.channels;ch++){
379 buffer24.readArray((uint8_t*)&val24[0], 3);
380 frame[ch] = interpret24bitAsInt32(val24);
383 assert(buffer24.available()==0);
385 size_t written = out().write((uint8_t*)frame,
sizeof(frame));
386 assert(written==
sizeof(frame));
393 int32_t interpret24bitAsInt32(uint8_t* byteArray) {
395 (
static_cast<int32_t
>(byteArray[2]) << 24)
396 | (
static_cast<int32_t
>(byteArray[1]) << 16)
397 | (
static_cast<int32_t
>(byteArray[0]) << 8)
403 int result = in_size;
405 int written = header.write(in_ptr, in_size);
406 if (!header.isDataComplete()) {
407 LOGW(
"WAV header misses 'data' section in len: %d", (
int) header.available());
412 if (!header.parse()){
413 LOGE(
"WAV header parsing failed");
418 isValid = header.audioInfo().is_valid;
420 LOGI(
"WAV sample_rate: %d", (
int) header.audioInfo().sample_rate);
421 LOGI(
"WAV data_length: %u", (
unsigned)header.audioInfo().data_length);
422 LOGI(
"WAV is_streamed: %d", header.audioInfo().is_streamed);
423 LOGI(
"WAV is_valid: %s",
424 header.audioInfo().is_valid ?
"true" :
"false");
428 isValid = format == decoder_format;
431 if(p_decoder!=
nullptr){
432 int block_size = header.audioInfo().block_align;
433 p_decoder->setBlockSize(block_size);
439 bi.
channels = header.audioInfo().channels;
441 notifyAudioChange(bi);
443 LOGE(
"WAV format not supported: %d", (
int)format);
445 return header.getDataPos();
448 void setupEncodedAudio() {
449 if (p_decoder!=
nullptr){
450 assert(p_print!=
nullptr);
451 dec_out.setOutput(p_print);
452 dec_out.setDecoder(p_decoder);
478 setEncoder(enc, fmt);
483 audioInfo.format = fmt;
494 const char *
mime()
override {
return wav_mime; }
499 info.format = AudioFormat::PCM;
503 info.is_streamed =
true;
504 info.is_valid =
true;
505 info.data_length = 0x7fff0000;
506 info.file_size = info.data_length + 36;
524 LOGI(
"sample_rate: %d", (
int)audioInfo.
sample_rate);
525 LOGI(
"channels: %d", audioInfo.
channels);
528 if (audioInfo.format == AudioFormat::PCM){
531 if (audioInfo.is_streamed || audioInfo.data_length == 0 ||
532 audioInfo.data_length >= 0x7fff0000) {
533 LOGI(
"is_streamed! because length is %u",
534 (
unsigned)audioInfo.data_length);
535 audioInfo.is_streamed =
true;
536 audioInfo.data_length = ~0;
538 size_limit = audioInfo.data_length;
539 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 uint8_t *data,
size_t len)
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 int len = header.writeHeader(p_print);
578 audioInfo.file_size -= len;
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 *)data, len);
586 }
else if (size_limit > 0) {
587 size_t write_size = min((
size_t)len, (
size_t)size_limit);
588 result = p_out->write((uint8_t *)data, 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);
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