Skip to content

Commit

Permalink
FLAC decoding improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 16, 2023
1 parent 0f5e944 commit 73a13db
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
31 changes: 14 additions & 17 deletions src/AudioCodecs/CodecFLAC.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ class FLACDecoder : public StreamingDecoder {
return result;
}

/// Output decoded result to final output stream
/// Output decoded result to final output stream
static FLAC__StreamDecoderWriteStatus write_callback(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,const FLAC__int32 *const buffer[], void *client_data) {
LOGI("write_callback: %d", (int) frame->header.blocksize);
LOGI("write_callback: %d", frame->header.blocksize);
FLACDecoder *self = (FLACDecoder *)client_data;

AudioInfo actual_info = self->audioInfo();
Expand All @@ -200,7 +200,7 @@ class FLACDecoder : public StreamingDecoder {
self->info.logInfo();
int bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
if (bps!=16){
LOGI("Converting from %d bits", (int) bps);
LOGI("Converting from %d bits", bps);
}
if (self->p_notify != nullptr) {
self->info = actual_info;
Expand All @@ -210,55 +210,52 @@ class FLACDecoder : public StreamingDecoder {

// write audio data
int bps = FLAC__stream_decoder_get_bits_per_sample(decoder);
int16_t sample;
int16_t result_frame[actual_info.channels];

switch(bps){
case 8:
for (int j = 0; j < frame->header.blocksize; j++) {
for (int i = 0; i < actual_info.channels; i++) {
//self->output_buffer[j*actual_info.channels + i] = buffer[i][j]<<8;
sample = buffer[i][j]<<8;;
self->p_print->write((uint8_t *)&sample,2);
result_frame[i] = buffer[i][j]<<8;
}
self->p_print->write((uint8_t *)&frame, sizeof(frame));
}
break;
case 16:
for (int j = 0; j < frame->header.blocksize; j++) {
for (int i = 0; i < actual_info.channels; i++) {
//self->output_buffer[j*actual_info.channels + i] = buffer[i][j];
sample = buffer[i][j];
self->p_print->write((uint8_t *)&sample,2);
result_frame[i] = buffer[i][j];
}
self->p_print->write((uint8_t *)&frame, sizeof(frame));
}
break;
case 24:
for (int j = 0; j < frame->header.blocksize; j++) {
for (int i = 0; i < actual_info.channels; i++) {
//self->output_buffer[j*actual_info.channels + i] = buffer[i][j]>>8;
sample = buffer[i][j] >>8;
self->p_print->write((uint8_t *)&sample,2);
result_frame[i] = buffer[i][j] >>8;
}
self->p_print->write((uint8_t *)&frame, sizeof(frame));
}
break;
case 32:
for (int j = 0; j < frame->header.blocksize; j++) {
for (int i = 0; i < actual_info.channels; i++) {
//self->output_buffer[j*actual_info.channels+ i] = buffer[i][j]>>16;
sample = buffer[i][j] >>16;
self->p_print->write((uint8_t *)&sample,2);
result_frame[i] = buffer[i][j] >>16;
}
self->p_print->write((uint8_t *)&frame, sizeof(frame));
}
break;
default:
LOGE("Unsupported bps: %d", (int) bps);
LOGE("Unsupported bps: %d", bps);
}

return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}

};



/**
* @brief FLACEncoder
* @ingroup codecs
Expand Down
12 changes: 7 additions & 5 deletions src/AudioCodecs/DecoderFromStreaming.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class DecoderFromStreaming : public AudioDecoder {
void begin() override {
TRACED();
active = true;
p_dec->begin();
queue.begin();
}

void end() override {
Expand All @@ -44,12 +46,13 @@ class DecoderFromStreaming : public AudioDecoder {
}

size_t write(const void *data, size_t byteCount) override {
if (p_out == nullptr)
return 0;
TRACED();
size_t result = queue.write((uint8_t *)data, byteCount);
// trigger processing
while(p_dec->copy()) delay(1);
// trigger processing - we leave byteCount in the buffer
while(queue.available()>byteCount){
p_dec->copy();
delay(1);
}
return result;
}

Expand All @@ -60,7 +63,6 @@ class DecoderFromStreaming : public AudioDecoder {
StreamingDecoder *p_dec = nullptr;
RingBuffer<uint8_t> rbuffer{0};
QueueStream<uint8_t> queue{rbuffer}; // convert Buffer to Stream
Print *p_out = nullptr;
};

} // namespace audio_tools

0 comments on commit 73a13db

Please sign in to comment.