Skip to content

Commit

Permalink
MetaDataFilterDecoder
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 20, 2024
1 parent d9f33b8 commit fc3864e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
9 changes: 3 additions & 6 deletions src/AudioTools/AudioCodecs/AudioCodecsBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include "AudioConfig.h"
#include "AudioLogger.h"
#include "AudioTools/CoreAudio/AudioTypes.h"
#include "AudioTools/CoreAudio/AudioBasic/Collections/Vector.h"
#include "AudioTools/CoreAudio/AudioTypes.h"

namespace audio_tools {

Expand Down Expand Up @@ -48,9 +48,7 @@ class AudioDecoder : public AudioWriter, public AudioInfoSource {
virtual void setOutput(Print &out_stream) override { p_print = &out_stream; }

/// Returns true to indicate that the decoding result is PCM data
virtual bool isResultPCM() {
return true;
}
virtual bool isResultPCM() { return true; }
virtual bool begin(AudioInfo info) override {
setAudioInfo(info);
return begin();
Expand Down Expand Up @@ -249,5 +247,4 @@ class StreamingDecoderAdapter : public StreamingDecoder {
}
};


}
} // namespace audio_tools
57 changes: 54 additions & 3 deletions src/AudioTools/CoreAudio/AudioMetaData/MetaDataFilter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "AudioLogger.h"
#include "AudioTools/AudioCodecs/AudioCodecsBase.h"
#include "AudioTools/CoreAudio/AudioOutput.h"
#include "AudioTools/CoreAudio/Buffers.h"

Expand All @@ -19,9 +20,13 @@ class MetaDataFilter : public AudioOutput {

/// Constructor which assigns the decoder
MetaDataFilter(Print &out) { setOutput(out); }
/// Constructor which assigns the decoder
MetaDataFilter(AudioWriter &out) { setOutput(out); }

/// Defines the decoder to which we write the data
void setOutput(Print &out) { p_out = &out; }
/// Defines the decoder to which we write the data
void setOutput(AudioWriter &out) { p_writer = &out; }

/// (Re)starts the processing
bool begin() override {
Expand All @@ -35,7 +40,9 @@ class MetaDataFilter : public AudioOutput {
TRACEI();
size_t result = len;
// prevent npe
if ((p_out == nullptr) || (data == nullptr) || (len == 0)) return 0;
if ((p_out == nullptr && p_writer == nullptr) || (data == nullptr) ||
(len == 0))
return 0;

// find tag
int meta_len = 0;
Expand All @@ -48,7 +55,8 @@ class MetaDataFilter : public AudioOutput {

// nothing to ignore
if (!metadata_range.isDefined()) {
return p_out->write(data, len);
if (p_out) return p_out->write(data, len);
if (p_writer) return p_writer->write(data, len);
}

// ignore data in metadata range
Expand All @@ -61,7 +69,10 @@ class MetaDataFilter : public AudioOutput {
}

// write partial data
if (tmp.available() > 0) p_out->write(tmp.data(), tmp.available());
if (tmp.available() > 0) {
if (p_out) p_out->write(tmp.data(), tmp.available());
if (p_writer) p_writer->write(tmp.data(), tmp.available());
}

// reset for next run
if (current_pos > metadata_range.to) {
Expand All @@ -74,6 +85,7 @@ class MetaDataFilter : public AudioOutput {

protected:
Print *p_out = nullptr;
AudioWriter *p_writer = nullptr;
int current_pos = 0;
enum MetaType { TAG, TAG_PLUS, ID3 };
int start = 0;
Expand Down Expand Up @@ -151,4 +163,43 @@ class MetaDataFilter : public AudioOutput {
}
};

/***
* MetaDataFiler applied to the indicated decoder: Class which filters out ID3v1
* and ID3v2 Metadata and provides only the audio data to the decoder
* @ingroup metadata
* @ingroup codecs
* @author Phil Schatzmann
* @copyright GPLv3
*/
class MetaDataFilterDecoder : public AudioDecoder {
public:
MetaDataFilterDecoder(AudioDecoder &decoder) {
p_decoder = &decoder;
filter.setOutput(decoder);
}

bool begin() override {
is_active = true;
filter.begin();
return AudioDecoder::begin();
}

void end() override {
is_active = false;
filter.end();
AudioDecoder::begin();
}

size_t write(const uint8_t *data, size_t len) override {
return filter.write(data, len);
}

operator bool() override { return p_print != nullptr && is_active; }

protected:
AudioDecoder *p_decoder = nullptr;
MetaDataFilter filter;
bool is_active = false;
};

} // namespace audio_tools

0 comments on commit fc3864e

Please sign in to comment.