|
1 | 1 | #pragma once |
2 | 2 |
|
3 | 3 | #include "AudioTools/AudioCodecs/AudioCodecsBase.h" |
4 | | -#include "AudioTools/AudioCodecs/CodecWAV.h" |
5 | | -#include "AudioTools/AudioCodecs/CodecMP3Helix.h" |
6 | 4 | #include "AudioTools/AudioCodecs/CodecAACHelix.h" |
| 5 | +#include "AudioTools/AudioCodecs/CodecMP3Helix.h" |
| 6 | +#include "AudioTools/AudioCodecs/CodecWAV.h" |
7 | 7 |
|
8 | 8 | namespace audio_tools { |
9 | 9 |
|
10 | 10 | /** |
11 | 11 | * @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 |
13 | 15 | * @ingroup codecs |
14 | 16 | * @ingroup decoder |
15 | 17 | * @author Phil Schatzmann |
16 | 18 | * @copyright GPLv3 |
17 | 19 | */ |
18 | 20 |
|
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"); |
45 | 28 | } |
46 | 29 |
|
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; |
107 | 34 | }; |
108 | 35 |
|
109 | | -} // namespace audio_tools |
110 | | - |
| 36 | +} // namespace audio_tools |
0 commit comments