arduino-audio-tools
Loading...
Searching...
No Matches
CodecAACVO.h
Go to the documentation of this file.
1#pragma once
2
4#include "VOAACEncoder.h"
5
6#ifndef VOAAC_DEFAULT_BITRATE
7#define VOAAC_DEFAULT_BITRATE 128000
8#endif
9
10#ifndef VOAAC_DEFAULT_OUTPUT_BUFFER_SIZE
11#define VOAAC_DEFAULT_OUTPUT_BUFFER_SIZE 2048
12#endif
13
14namespace audio_tools {
15
27class AACEncoderVO : public AudioEncoder {
28public:
29 AACEncoderVO() = default;
30
31 AACEncoderVO(Print &out_stream) { setOutput(out_stream); }
32
33 ~AACEncoderVO() override { end(); }
34
36 void setOutput(Print &out_stream) override { p_print = &out_stream; }
37
39 void setBitrate(uint32_t bps) { bitrate = bps; }
40
42 void setAdts(bool enabled) { use_adts = enabled; }
43
45 void setOutputBufferSize(size_t len) { output_buffer_size = len; }
46
47 bool begin(AudioInfo info) override {
49 return begin();
50 }
51
52 bool begin() override {
53 if (p_print == nullptr) {
54 LOGE("Output undefined");
55 return false;
56 }
57 if (info.channels < 1 || info.channels > 2) {
58 LOGE("Invalid channels: %d", info.channels);
59 return false;
60 }
61 if (info.bits_per_sample != 16) {
62 LOGE("bits_per_sample must be 16, got %d", info.bits_per_sample);
63 return false;
64 }
65
67 VOAACEncoder::kFrameSamplesPerChannel * info.channels * sizeof(int16_t);
70 pcm_pos = 0;
71
73 if (!active) {
74 LOGE("VO AAC encoder init failed");
75 return false;
76 }
77 return true;
78 }
79
80 size_t write(const uint8_t *data, size_t len) override {
81 if (!active || data == nullptr || len == 0)
82 return 0;
83
84 size_t processed = 0;
85 while (processed < len) {
86 size_t open = frame_bytes - pcm_pos;
87 size_t to_copy = (len - processed) < open ? (len - processed) : open;
88 memcpy(pcm_buffer.data() + pcm_pos, data + processed, to_copy);
89 pcm_pos += to_copy;
90 processed += to_copy;
91
92 if (pcm_pos >= frame_bytes) {
93 if (!encodeFrame()) {
94 break;
95 }
96 pcm_pos = 0;
97 }
98 }
99 return processed;
100 }
101
102 void end() override {
103 if (!active) {
104 return;
105 }
106
107 if (pcm_pos > 0) {
108 // Flush remaining partial PCM frame padded with silence.
109 memset(pcm_buffer.data() + pcm_pos, 0, frame_bytes - pcm_pos);
110 encodeFrame();
111 pcm_pos = 0;
112 }
113
114 enc.end();
115 active = false;
116 }
117
118 const char *mime() override { return "audio/aac"; }
119
120 operator bool() override { return active && enc.isReady(); }
121
122 VOAACEncoder *driver() { return &enc; }
123
124protected:
125 VOAACEncoder enc;
126 Print *p_print = nullptr;
129 size_t frame_bytes = 0;
130 size_t pcm_pos = 0;
133 bool use_adts = true;
134 bool active = false;
135
136 bool encodeFrame() {
137 size_t out_bytes = 0;
138 bool ok = enc.encodeFrame((const int16_t *)pcm_buffer.data(),
139 VOAACEncoder::kFrameSamplesPerChannel,
140 out_buffer.data(), out_buffer.size(), out_bytes);
141 if (!ok) {
142 LOGE("VO AAC encodeFrame failed");
143 return false;
144 }
145 if (out_bytes > 0) {
146 size_t written = p_print->write(out_buffer.data(), out_bytes);
147 if (written != out_bytes) {
148 LOGW("Output truncated: %d of %d", (int)written, (int)out_bytes);
149 }
150 }
151 return true;
152 }
153};
154
155} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define VOAAC_DEFAULT_BITRATE
Definition CodecAACVO.h:7
#define VOAAC_DEFAULT_OUTPUT_BUFFER_SIZE
Definition CodecAACVO.h:11
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
AAC Encoder based on the VisualOn vo-aacenc implementation (https://github.com/pschatzmann/codec-vo-a...
Definition CodecAACVO.h:27
bool active
Definition CodecAACVO.h:134
void setOutput(Print &out_stream) override
Defines the output stream.
Definition CodecAACVO.h:36
bool encodeFrame()
Definition CodecAACVO.h:136
uint32_t bitrate
Definition CodecAACVO.h:132
size_t output_buffer_size
Definition CodecAACVO.h:131
size_t frame_bytes
Definition CodecAACVO.h:129
void end() override
Definition CodecAACVO.h:102
void setOutputBufferSize(size_t len)
Defines max encoded output frame size.
Definition CodecAACVO.h:45
size_t pcm_pos
Definition CodecAACVO.h:130
size_t write(const uint8_t *data, size_t len) override
Definition CodecAACVO.h:80
AACEncoderVO(Print &out_stream)
Definition CodecAACVO.h:31
VOAACEncoder * driver()
Definition CodecAACVO.h:122
Vector< uint8_t > pcm_buffer
Definition CodecAACVO.h:127
const char * mime() override
Provides the mime type of the encoded result.
Definition CodecAACVO.h:118
~AACEncoderVO() override
Definition CodecAACVO.h:33
Vector< uint8_t > out_buffer
Definition CodecAACVO.h:128
bool begin(AudioInfo info) override
Definition CodecAACVO.h:47
bool begin() override
Definition CodecAACVO.h:52
Print * p_print
Definition CodecAACVO.h:126
bool use_adts
Definition CodecAACVO.h:133
void setAdts(bool enabled)
Defines if ADTS headers are generated.
Definition CodecAACVO.h:42
void setBitrate(uint32_t bps)
Defines target bitrate in bps.
Definition CodecAACVO.h:39
VOAACEncoder enc
Definition CodecAACVO.h:125
Encoding of PCM data.
Definition AudioCodecsBase.h:97
AudioInfo info
Definition AudioCodecsBase.h:116
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:106
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 AudioCodecsBase.h:10
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