Skip to content

Commit

Permalink
DecoderFromStreaming
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 30, 2023
1 parent 4dc149e commit 17f901b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/AudioCodecs/AudioCodecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
#include "AudioCodecs/CodecL8.h"
#include "AudioCodecs/CodecFloat.h"
#include "AudioCodecs/CodecBase64.h"
#include "AudioCodecs/DecoderFromStreaming.h"

70 changes: 70 additions & 0 deletions src/AudioCodecs/DecoderFromStreaming.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#pragma once

#include "AudioCodecs/AudioEncoded.h"
#include "AudioTools/Buffers.h"
#include "AudioTools/AudioStreams.h"

namespace audio_tools {

/**
* @brief Adapter class which allows the AudioDecoder API on a StreamingDecoder
* @ingroup codecs
* @ingroup decoder
* @author Phil Schatzmann
* @copyright GPLv3
*/
class DecoderFromStreaming : public AudioDecoder {
public:
/**
* @brief Constructor for a new DecoderBase64 object
*
* @param out_buffeream Output Stream to which we write the decoded result
*/
DecoderFromStreaming(StreamingDecoder &dec, int bufferSize) {
TRACED();
p_dec = &dec;
resize(bufferSize);
}

/// Defines the output Stream
void setOutput(Print &out) override {
p_dec->setInputStream(queue);
p_dec->setOutput(out);
}

void begin() override {
TRACED();
active = true;
}

void end() override {
TRACED();
active = false;
}

/// resize the buffer
void resize(int size){
rbuffer.resize(size);
}

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);
return result;
}

operator bool() override { return active; }

protected:
bool active = false;
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 17f901b

Please sign in to comment.