arduino-audio-tools
Loading...
Searching...
No Matches
CodecFLACFoxen.h
Go to the documentation of this file.
1#pragma once
2
5#include "foxen-flac.h"
6
7namespace audio_tools {
8
9#define FOXEN_IN_BUFFER_SIZE 1024 * 2
10#define FOXEN_OUT_BUFFER_SIZE 1024 * 4
11
22 public:
23 FLACDecoderFoxen() = default;
24
33
36
37 bool begin() {
38 TRACEI();
39 is_active = false;
43
44 if (flac != nullptr) {
45 is_active = true;
48 } else {
49 LOGE("not enough memory");
51 }
52
53 return is_active;
54 }
55
56 void end() {
57 TRACEI();
58 flush();
59 if (flac != nullptr && is_release_memory_on_end) {
62 out.resize(0);
63 }
64 is_active = false;
65 }
66
67 size_t write(const uint8_t *data, size_t len) override {
68 LOGD("write: %d", len);
69 // no processing if not active
70 if (!is_active) return 0;
71
72 size_t result = write_buffer.writeArray(data, len);
73 LOGD("write_buffer availabe: %d", write_buffer.available());
74
75 while (write_buffer.available() > 0) {
76 if (!decode()) break;
77 }
78
79 // if the buffer is full we could not decode anything
81 LOGE("Decoder did not consume any data");
83 }
84
85 LOGD("write: %d -> %d", len, result);
86 return result;
87 }
88
89 void flush() { decode(); }
90
91 operator bool() override { return is_active; }
92
94 void setInBufferSize(int size) { in_buffer_size = size; }
95
98 void setOutBufferSize(int size) { out_buffer_size = size; }
99
101 void setMaxBlockSize(int size) { max_block_size = size; }
102
105
108
109 protected:
110 fx_flac_t *flac = nullptr;
114 bool is_active = false;
115 bool is_convert_to_16 = true;
116 bool is_stop_on_error = true;
118 int bits_eff = 0;
119 int max_block_size = 5 * 1024;
123
124 bool decode() {
125 TRACED();
126 if (!is_active) return false;
131 out.data(), &out_len);
132 // assert(out_len <= FOXEN_OUT_BUFFER_SIZE);
133
134 switch (rc) {
137 } break;
138
139 case FLAC_ERR: {
140 LOGE("FLAC decoder in error state!");
141 if (is_stop_on_error) stop();
142 } break;
143
144 default: {
145 if (out_len > 0) {
146 LOGD("Providing data: %d samples", out_len);
147 if (is_convert_to_16) {
149 } else {
151 }
152 }
153 } break;
154 }
155 LOGD("processed: %d bytes of %d -> %d samples", buf_len_result, buf_len,
156 out_len);
157 // removed processed bytes from buffer
159 return buf_len_result > 0 || out_len > 0;
160 }
161
163 TRACED();
164 // write the result to the output destination
166 }
167
169 TRACED();
170 // in place convert to 16 bits
171 int16_t *out16 = (int16_t *)out.data();
172 for (int j = 0; j < out_len; j++) {
173 out16[j] = out.data()[j] >> 16; // 65538;
174 }
175 // write the result to the output destination
176 LOGI("writeBlocking: %d", out_len * sizeof(int16_t));
178 }
179
183
184 LOGI("bits: %d", bits_eff);
185 LOGI("blocksize: %d", info_blocksize);
186 // assert(bits_eff == 32);
190 info.logInfo();
191 if (info.channels > max_channels) {
192 LOGE("max channels too low: %d -> %d", max_channels, info.channels);
193 if (is_stop_on_error) stop();
194 }
196 LOGE("max channels too low: %d -> %d", max_block_size, info_blocksize);
197 if (is_stop_on_error) stop();
198 }
200 }
201};
202
203} // namespace audio_tools
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGI(...)
Definition AudioLoggerIDF.h:28
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define FOXEN_OUT_BUFFER_SIZE
Definition CodecFLACFoxen.h:10
#define FOXEN_IN_BUFFER_SIZE
Definition CodecFLACFoxen.h:9
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:18
AudioInfo info
Definition AudioCodecsBase.h:76
Print * p_print
Definition AudioCodecsBase.h:75
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:178
void writeBlocking(Print *out, uint8_t *data, size_t len)
Definition AudioTypes.h:223
Foxen FLAC Decoder using https://github.com/astoeckel/libfoxenflac Unlike FLACDecoder which is a stre...
Definition CodecFLACFoxen.h:21
SingleBuffer< uint8_t > write_buffer
Definition CodecFLACFoxen.h:111
int max_block_size
Definition CodecFLACFoxen.h:119
bool is_stop_on_error
Definition CodecFLACFoxen.h:116
bool is_release_memory_on_end
Definition CodecFLACFoxen.h:117
bool is_active
Definition CodecFLACFoxen.h:114
FLACDecoderFoxen(int maxBlockSize, int maxChannels, bool convertTo16Bits=true, bool releaseOnEnd=false)
Default Constructor.
Definition CodecFLACFoxen.h:26
void set32Bit(bool flag)
Select between 16 and 32 bit output: the default is 16 bits.
Definition CodecFLACFoxen.h:107
void write16BitData(int out_len)
Definition CodecFLACFoxen.h:168
void setMaxChannels(int ch)
Defines the maximum number of channels: drives the buffer allocation.
Definition CodecFLACFoxen.h:104
Vector< uint8_t > foxen_data
Definition CodecFLACFoxen.h:113
Vector< int32_t > out
Definition CodecFLACFoxen.h:112
bool begin()
Definition CodecFLACFoxen.h:37
void setOutBufferSize(int size)
Definition CodecFLACFoxen.h:98
~FLACDecoderFoxen()
Destructor - calls end();.
Definition CodecFLACFoxen.h:35
void setMaxBlockSize(int size)
Defines the maximum FLAC blocksize: drives the buffer allocation.
Definition CodecFLACFoxen.h:101
bool decode()
Definition CodecFLACFoxen.h:124
size_t write(const uint8_t *data, size_t len) override
Definition CodecFLACFoxen.h:67
int in_buffer_size
Definition CodecFLACFoxen.h:121
void write32BitData(int out_len)
Definition CodecFLACFoxen.h:162
int out_buffer_size
Definition CodecFLACFoxen.h:122
void setInBufferSize(int size)
Defines the input buffer size (default is 2k)
Definition CodecFLACFoxen.h:94
void end()
Definition CodecFLACFoxen.h:56
bool is_convert_to_16
Definition CodecFLACFoxen.h:115
void processMetadata()
Definition CodecFLACFoxen.h:180
void flush()
Definition CodecFLACFoxen.h:89
int max_channels
Definition CodecFLACFoxen.h:120
int bits_eff
Definition CodecFLACFoxen.h:118
fx_flac_t * flac
Definition CodecFLACFoxen.h:110
A simple Buffer implementation which just uses a (dynamically sized) array.
Definition Buffers.h:172
size_t size() override
Definition Buffers.h:303
int available() override
provides the number of entries that are available to read
Definition Buffers.h:233
bool resize(int size)
Resizes the buffer if supported: returns false if not supported.
Definition Buffers.h:305
int writeArray(const T data[], int len) override
Fills the buffer data.
Definition Buffers.h:201
T * data()
Provides address of actual data.
Definition Buffers.h:284
int clearArray(int len) override
consumes len bytes and moves current data to the beginning
Definition Buffers.h:252
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(int newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:316
int size()
Definition Vector.h:178
void stop()
stops any further processing by spinning in an endless loop
Definition AudioRuntime.h:12
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:512
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:57
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:59
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:61
virtual void logInfo(const char *source="")
Definition AudioTypes.h:125