Skip to content

Commit 301a570

Browse files
committed
2 parents ce1f14a + aba3b13 commit 301a570

File tree

1 file changed

+17
-91
lines changed

1 file changed

+17
-91
lines changed
Lines changed: 17 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,36 @@
11
#pragma once
22

33
#include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4-
#include "AudioTools/AudioCodecs/CodecWAV.h"
5-
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
64
#include "AudioTools/AudioCodecs/CodecAACHelix.h"
5+
#include "AudioTools/AudioCodecs/CodecMP3Helix.h"
6+
#include "AudioTools/AudioCodecs/CodecWAV.h"
77

88
namespace audio_tools {
99

1010
/**
1111
* @brief MP3 and AAC Decoder using libhelix:
12-
* https://github.com/pschatzmann/arduino-libhelix. We dynamically create a MP3 or AAC decoder dependent on the provided audio format.
12+
* https://github.com/pschatzmann/arduino-libhelix. We dynamically create a MP3
13+
* or AAC decoder dependent on the provided audio format. In addition WAV files
14+
* are also supported
1315
* @ingroup codecs
1416
* @ingroup decoder
1517
* @author Phil Schatzmann
1618
* @copyright GPLv3
1719
*/
1820

19-
class DecoderHelix : public AudioDecoder {
20-
public:
21-
DecoderHelix() { TRACED(); }
22-
23-
DecoderHelix(Print &out_stream) {
24-
TRACED();
25-
p_out_stream = &out_stream;
26-
}
27-
28-
DecoderHelix(Print &out_stream, AudioInfoSupport &bi) {
29-
TRACED();
30-
p_out_stream = &out_stream;
31-
p_bi = &bi;
32-
}
33-
34-
~DecoderHelix() { resetDecoder(); }
35-
36-
/// Defines the output Stream
37-
virtual void setOutput(Print &outStream) { p_out_stream = &outStream; }
38-
39-
/// Starts the processing
40-
bool begin() {
41-
TRACED();
42-
// reset actual decoder so that we start a new determination
43-
resetDecoder();
44-
return true;
21+
class DecoderHelix : public MultiDecoder {
22+
public:
23+
DecoderHelix() {
24+
// register supported codecs with their mime type
25+
multi.addDecoder(mp3, "audio/mpeg");
26+
multi.addDecoder(aac, "audio/aac");
27+
multi.addDecoder(wav, "audio/vnd.wave");
4528
}
4629

47-
/// Releases the reserved memory
48-
void end() {
49-
TRACED();
50-
if (p_decoder!=nullptr){
51-
p_decoder->end();
52-
}
53-
resetDecoder();
54-
}
55-
56-
AudioInfo audioInfo() override {
57-
return p_decoder != nullptr ? p_decoder->audioInfo() : noInfo;
58-
}
59-
60-
/// Write mp3 data to decoder
61-
size_t write(const uint8_t *data, size_t len) {
62-
LOGD("%s: %zu", LOG_METHOD, len);
63-
if (p_decoder == nullptr) {
64-
setupDecoder((const byte *)data);
65-
p_decoder->begin();
66-
}
67-
return p_decoder->write(data, len);
68-
}
69-
70-
/// checks if the class is active
71-
operator bool() { return p_decoder == nullptr ? false : *p_decoder; }
72-
73-
protected:
74-
AudioDecoder *p_decoder = nullptr;
75-
Print *p_out_stream = nullptr;
76-
AudioInfoSupport *p_bi = nullptr;
77-
AudioInfo noInfo;
78-
79-
/// Defines the decoder based on the audio format
80-
void setupDecoder(const byte *start) {
81-
if (start[0] == 0xFF && start[1] == 0xF1) {
82-
p_decoder = new AACDecoderHelix();
83-
LOGI("using AACDecoderHelix");
84-
} else if (start[0] == 0xFF || start[0] == 0xFE || strncmp("ID3", (const char*)start, 3)==0) {
85-
p_decoder = new MP3DecoderHelix();
86-
LOGI("using MP3DecoderHelix");
87-
} else if (strncmp("RIFF", (const char*)start, 4)==0) {
88-
p_decoder = new WAVDecoder();
89-
LOGI("using WAVDecoder");
90-
}
91-
// if we do not have a decoder yet we use a dummy to prevent NPE
92-
if (p_decoder == nullptr) {
93-
LOGW("Unknown Data Format: Content will be ignored...")
94-
p_decoder = CodecNOP::instance();
95-
}
96-
p_decoder->setOutput(*p_out_stream);
97-
p_decoder->addNotifyAudioChange(*p_bi);
98-
}
99-
100-
/// Deletes the decoder
101-
void resetDecoder() {
102-
if (p_decoder != nullptr) {
103-
delete p_decoder;
104-
}
105-
p_decoder = nullptr;
106-
}
30+
protected:
31+
MP3DecoderHelix mp3;
32+
AACDecoderHelix aac;
33+
WAVDecoder wav;
10734
};
10835

109-
} // namespace audio_tools
110-
36+
} // namespace audio_tools

0 commit comments

Comments
 (0)