arduino-audio-tools
Loading...
Searching...
No Matches
CodecMP2.h
Go to the documentation of this file.
1#pragma once
2
3#include <string.h>
4
6#include "TinyMP2.h"
7
8namespace audio_tools {
9
23class MP2Decoder : public AudioDecoder {
24 public:
25 MP2Decoder() = default;
26
28 if (active) end();
29 }
30
31 const char *mime() override { return "audio/mpeg"; }
32
34 void setOutput(Print &out_stream) override { p_print = &out_stream; }
35
36 bool begin() override {
37 TRACED();
38 decoder.reset();
39 buffer.resize(0);
40 pcm.resize(TinyMP2Decoder::kSamplesPerFrame * TinyMP2Decoder::kMaxChannels);
41 active = true;
42 return true;
43 }
44
45 void end() override {
46 TRACED();
47 buffer.resize(0);
48 active = false;
49 }
50
52 size_t write(const uint8_t *data, size_t len) override {
53 if (!active || len == 0) return 0;
54 size_t old_size = buffer.size();
55 buffer.resize(old_size + len);
56 memcpy(buffer.data() + old_size, data, len);
57 decode();
58 return len;
59 }
60
61 operator bool() override { return active; }
62
63 protected:
64 Print *p_print = nullptr;
65 TinyMP2Decoder decoder;
68 bool active = false;
69
73 void decode() {
74 size_t pos = 0;
75 while (pos < buffer.size()) {
76 TinyMP2Decoder::FrameHeader header;
77 size_t consumed = decoder.decodeFrame(
78 buffer.data() + pos, buffer.size() - pos, pcm.data(), &header);
79 if (consumed == 0) {
80 TinyMP2Decoder::FrameHeader peek;
81 if (decoder.parseHeader(buffer.data() + pos, buffer.size() - pos,
82 peek) &&
83 buffer.size() - pos < peek.frame_bytes) {
84 // valid header, but the frame is not fully buffered yet
85 break;
86 }
87 // no valid sync at this position: resync by advancing a single byte
88 pos++;
89 continue;
90 }
91 provideResult(header);
92 pos += consumed;
93 }
94 // keep unprocessed trailing bytes for the next write()
95 size_t remaining = buffer.size() - pos;
96 if (pos > 0) {
97 memmove(buffer.data(), buffer.data() + pos, remaining);
98 }
99 buffer.resize(remaining);
100 }
101
103 void provideResult(TinyMP2Decoder::FrameHeader &header) {
104 AudioInfo tmp;
105 tmp.sample_rate = header.sample_rate;
106 tmp.channels = header.channels;
107 tmp.bits_per_sample = 16;
108 if (tmp != info) {
109 info = tmp;
111 }
112 if (p_print != nullptr) {
113 size_t bytes =
114 TinyMP2Decoder::kSamplesPerFrame * header.channels * sizeof(int16_t);
115 p_print->write((const uint8_t *)pcm.data(), bytes);
116 }
117 }
118};
119
120} // namespace audio_tools
#define TRACED()
Definition AudioLoggerIDF.h:31
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:19
AudioInfo info
Definition AudioCodecsBase.h:80
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:175
Decoder for MPEG-1 Layer II (MP2) audio using the TinyMP2 library. Compressed data provided via write...
Definition CodecMP2.h:23
bool active
Definition CodecMP2.h:68
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecMP2.h:34
Vector< int16_t > pcm
Definition CodecMP2.h:67
TinyMP2Decoder decoder
Definition CodecMP2.h:65
void provideResult(TinyMP2Decoder::FrameHeader &header)
Notifies about audio info changes and writes the decoded PCM data.
Definition CodecMP2.h:103
void end() override
Definition CodecMP2.h:45
size_t write(const uint8_t *data, size_t len) override
Provide compressed MP2 data: no frame alignment required.
Definition CodecMP2.h:52
~MP2Decoder()
Definition CodecMP2.h:27
const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecMP2.h:31
void decode()
Definition CodecMP2.h:73
bool begin() override
Definition CodecMP2.h:36
Print * p_print
Definition CodecMP2.h:64
Vector< uint8_t > buffer
Definition CodecMP2.h:66
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(size_t newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:316
int size()
Definition Vector.h:178
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition LMSEchoCancellationStream.h:6
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57