arduino-audio-tools
Loading...
Searching...
No Matches
CodecG722.h
Go to the documentation of this file.
1
9#pragma once
10
12#include "g722_codec.h"
13
14// size in bytes
15#define G722_PCM_SIZE 80
16#define G722_ENC_SIZE 40
17
18
19namespace audio_tools {
20
29class G722Decoder : public AudioDecoder {
30 public:
31 G722Decoder() = default;
32
35 this->options = options;
36 }
37
38 virtual bool begin() {
39 TRACEI();
42
44 if (g722_dctx == nullptr) {
45 LOGE("g722_decoder_new");
46 return false;
47 }
48
50 is_active = true;
51 return true;
52 }
53
54 virtual void end() {
55 TRACEI();
57 is_active = false;
58 }
59
61
62 operator bool() { return is_active; }
63
64 virtual size_t write(const uint8_t *data, size_t len) {
65 LOGD("write: %d", len);
66 if (!is_active) {
67 LOGE("inactive");
68 return 0;
69 }
70
71 uint8_t *p_byte = (uint8_t *)data;
72 for (int j = 0; j < len; j++) {
74 }
75
76 return len;
77 }
78
79 protected:
80 Print *p_print = nullptr;
85 int input_pos = 0;
86 bool is_active = false;
87
89 void processByte(uint8_t byte) {
90 // add byte to buffer
92
93 // decode if buffer is full
94 if (input_pos >= input_buffer.size()) {
97
99 LOGE("Decoder:Result buffer too small: %d -> %d",result_buffer.size(),result_samples*2);
100 }
101
103 input_pos = 0;
104 }
105 }
106};
107
117class G722Encoder : public AudioEncoder {
118 public:
119 G722Encoder() = default;
120
123 this->options = options;
124 }
125
126 bool begin() {
127 TRACEI();
128 if (info.channels != 1) {
129 LOGW("1 channel expected, was: %d", info.channels);
130 }
131
133 if (g722_ectx == NULL) {
134 LOGE("g722_encoder_new");
135 return false;
136 }
137
140 is_active = true;
141 return true;
142 }
143
144 virtual void end() {
145 TRACEI();
147 is_active = false;
148 }
149
150 virtual const char *mime() { return "audio/g722"; }
151
153
154 operator bool() { return is_active; }
155
156 virtual size_t write(const uint8_t *data, size_t len) {
157 LOGD("write: %d", len);
158 if (!is_active) {
159 LOGE("inactive");
160 return 0;
161 }
162 // encode bytes
163 uint8_t *p_byte = (uint8_t *)data;
164 for (int j = 0; j < len; j++) {
166 }
167 return len;
168 }
169
170 protected:
171 Print *p_print = nullptr;
176 int buffer_pos = 0;
177 bool is_active = false;
178
179 // add byte to decoding buffer and decode if buffer is full
180 void processByte(uint8_t byte) {
182 if (buffer_pos >= input_buffer.size()) {
183 // convert for little endian
184 int samples = input_buffer.size() / 2;
185 // encode
186 int result_len = g722_encode(g722_ectx,(const int16_t*) input_buffer.data(), samples,
189 LOGE("Encoder:Result buffer too small: %d -> %d",result_buffer.size(),result_len);
190 }
192 buffer_pos = 0;
193 }
194 }
195};
196
197} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACEI()
Definition AudioLoggerIDF.h:32
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define G722_ENC_SIZE
Definition CodecG722.h:16
#define G722_PCM_SIZE
Definition CodecG722.h:15
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:18
AudioInfo info
Definition AudioCodecsBase.h:76
Encoding of PCM data.
Definition AudioCodecsBase.h:97
AudioInfo info
Definition AudioCodecsBase.h:116
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:178
Decoder for G.722. Depends on https://github.com/pschatzmann/arduino-libg722.
Definition CodecG722.h:29
int input_pos
Definition CodecG722.h:85
virtual void setOutput(Print &out_stream)
Defines where the decoded result is written to.
Definition CodecG722.h:60
bool is_active
Definition CodecG722.h:86
Vector< uint8_t > result_buffer
Definition CodecG722.h:83
virtual bool begin()
Definition CodecG722.h:38
int options
Definition CodecG722.h:84
virtual size_t write(const uint8_t *data, size_t len)
Definition CodecG722.h:64
G722_DEC_CTX * g722_dctx
Definition CodecG722.h:81
void processByte(uint8_t byte)
Build decoding buffer and decode when frame is full.
Definition CodecG722.h:89
Print * p_print
Definition CodecG722.h:80
void setOptions(int options)
Defines the options for the G.722 Codec: G722_SAMPLE_RATE_8000,G722_PACKED.
Definition CodecG722.h:34
virtual void end()
Definition CodecG722.h:54
Vector< uint8_t > input_buffer
Definition CodecG722.h:82
Encoder for G.722 - Depends on https://github.com/pschatzmann/arduino-libg722. Inspired by g722enc....
Definition CodecG722.h:117
virtual void setOutput(Print &out_stream)
Default output assignment (encoders may override to store Print reference)
Definition CodecG722.h:152
bool is_active
Definition CodecG722.h:177
Vector< uint8_t > result_buffer
Definition CodecG722.h:174
bool begin()
Definition CodecG722.h:126
virtual const char * mime()
Provides the mime type of the encoded result.
Definition CodecG722.h:150
int options
Definition CodecG722.h:175
int buffer_pos
Definition CodecG722.h:176
virtual size_t write(const uint8_t *data, size_t len)
Definition CodecG722.h:156
G722_ENC_CTX * g722_ectx
Definition CodecG722.h:172
void processByte(uint8_t byte)
Definition CodecG722.h:180
Print * p_print
Definition CodecG722.h:171
void setOptions(int options)
Defines the options for the G.722 Codec: G722_SAMPLE_RATE_8000,G722_PACKED.
Definition CodecG722.h:122
virtual void end()
Definition CodecG722.h:144
Vector< uint8_t > input_buffer
Definition CodecG722.h:173
Definition NoArduino.h:62
virtual size_t write(const uint8_t *data, size_t len)
Definition NoArduino.h:126
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
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