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// Reference bitrate for 16 bit stereo @ 44100 Hz; scaled by sample rate and
7// channel count when the bitrate is not set explicitly via setBitrate().
8#ifndef VOAAC_DEFAULT_BITRATE
9#define VOAAC_DEFAULT_BITRATE 128000
10#endif
11
12#ifndef VOAAC_DEFAULT_OUTPUT_BUFFER_SIZE
13#define VOAAC_DEFAULT_OUTPUT_BUFFER_SIZE 2048
14#endif
15
16namespace audio_tools {
17
29class AACEncoderVO : public AudioEncoder {
30public:
31 AACEncoderVO() = default;
32
33 AACEncoderVO(Print &out_stream) { setOutput(out_stream); }
34
35 ~AACEncoderVO() override { end(); }
36
38 void setOutput(Print &out_stream) override { p_print = &out_stream; }
39
42 void setBitrate(uint32_t bps) {
43 bitrate = bps;
44 bitrate_set = true;
45 }
46
48 void setAdts(bool enabled) { use_adts = enabled; }
49
51 void setOutputBufferSize(size_t len) { output_buffer_size = len; }
52
53 bool begin(AudioInfo info) override {
55 return begin();
56 }
57
58 bool begin() override {
59 if (p_print == nullptr) {
60 LOGE("Output undefined");
61 return false;
62 }
63 if (info.channels < 1 || info.channels > 2) {
64 LOGE("Invalid channels: %d", info.channels);
65 return false;
66 }
67 if (info.bits_per_sample != 16) {
68 LOGE("bits_per_sample must be 16, got %d", info.bits_per_sample);
69 return false;
70 }
71
73 VOAACEncoder::kFrameSamplesPerChannel * info.channels * sizeof(int16_t);
76 pcm_pos = 0;
77
78 uint32_t effective_bitrate =
80 active = enc.begin(info.sample_rate, effective_bitrate, info.channels, use_adts);
81 if (!active) {
82 LOGE("VO AAC encoder init failed");
83 return false;
84 }
85 return true;
86 }
87
88 size_t write(const uint8_t *data, size_t len) override {
89 if (!active || data == nullptr || len == 0)
90 return 0;
91
92 size_t processed = 0;
93 while (processed < len) {
94 size_t open = frame_bytes - pcm_pos;
95 size_t to_copy = (len - processed) < open ? (len - processed) : open;
96 memcpy(pcm_buffer.data() + pcm_pos, data + processed, to_copy);
97 pcm_pos += to_copy;
98 processed += to_copy;
99
100 if (pcm_pos >= frame_bytes) {
101 if (!encodeFrame()) {
102 break;
103 }
104 pcm_pos = 0;
105 }
106 }
107 return processed;
108 }
109
110 void end() override {
111 if (!active) {
112 return;
113 }
114
115 if (pcm_pos > 0) {
116 // Flush remaining partial PCM frame padded with silence.
117 memset(pcm_buffer.data() + pcm_pos, 0, frame_bytes - pcm_pos);
118 encodeFrame();
119 pcm_pos = 0;
120 }
121
122 enc.end();
123 active = false;
124 }
125
126 const char *mime() override { return "audio/aac"; }
127
128 operator bool() override { return active && enc.isReady(); }
129
130 VOAACEncoder *driver() { return &enc; }
131
132protected:
133 VOAACEncoder enc;
134 Print *p_print = nullptr;
137 size_t frame_bytes = 0;
138 size_t pcm_pos = 0;
141 bool bitrate_set = false;
142 bool use_adts = true;
143 bool active = false;
144
147 static uint32_t defaultBitrate(uint32_t sample_rate, int channels) {
148 double ref_bps = VOAAC_DEFAULT_BITRATE;
149 if (channels == 1) {
150 ref_bps *= 96.0 / 128.0;
151 }
152 return (uint32_t)(ref_bps * sample_rate / 44100.0);
153 }
154
155 bool encodeFrame() {
156 size_t out_bytes = 0;
157 bool ok = enc.encodeFrame((const int16_t *)pcm_buffer.data(),
158 VOAACEncoder::kFrameSamplesPerChannel,
159 out_buffer.data(), out_buffer.size(), out_bytes);
160 if (!ok) {
161 LOGE("VO AAC encodeFrame failed");
162 return false;
163 }
164 if (out_bytes > 0) {
165 size_t written = p_print->write(out_buffer.data(), out_bytes);
166 if (written != out_bytes) {
167 LOGW("Output truncated: %d of %d", (int)written, (int)out_bytes);
168 }
169 }
170 return true;
171 }
172};
173
174} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define VOAAC_DEFAULT_BITRATE
Definition CodecAACVO.h:9
#define VOAAC_DEFAULT_OUTPUT_BUFFER_SIZE
Definition CodecAACVO.h:13
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:29
bool active
Definition CodecAACVO.h:143
void setOutput(Print &out_stream) override
Defines the output stream.
Definition CodecAACVO.h:38
bool encodeFrame()
Definition CodecAACVO.h:155
uint32_t bitrate
Definition CodecAACVO.h:140
bool bitrate_set
Definition CodecAACVO.h:141
size_t output_buffer_size
Definition CodecAACVO.h:139
size_t frame_bytes
Definition CodecAACVO.h:137
void end() override
Definition CodecAACVO.h:110
void setOutputBufferSize(size_t len)
Defines max encoded output frame size.
Definition CodecAACVO.h:51
size_t pcm_pos
Definition CodecAACVO.h:138
size_t write(const uint8_t *data, size_t len) override
Definition CodecAACVO.h:88
AACEncoderVO(Print &out_stream)
Definition CodecAACVO.h:33
VOAACEncoder * driver()
Definition CodecAACVO.h:130
Vector< uint8_t > pcm_buffer
Definition CodecAACVO.h:135
const char * mime() override
Provides the mime type of the encoded result.
Definition CodecAACVO.h:126
~AACEncoderVO() override
Definition CodecAACVO.h:35
static uint32_t defaultBitrate(uint32_t sample_rate, int channels)
Definition CodecAACVO.h:147
Vector< uint8_t > out_buffer
Definition CodecAACVO.h:136
bool begin(AudioInfo info) override
Definition CodecAACVO.h:53
bool begin() override
Definition CodecAACVO.h:58
Print * p_print
Definition CodecAACVO.h:134
bool use_adts
Definition CodecAACVO.h:142
void setAdts(bool enabled)
Defines if ADTS headers are generated.
Definition CodecAACVO.h:48
void setBitrate(uint32_t bps)
Definition CodecAACVO.h:42
VOAACEncoder enc
Definition CodecAACVO.h:133
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 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