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
33 virtual const char *mime() override { return "audio/g722"; }
34
37 this->options = options;
38 }
39
40 virtual bool begin() {
41 TRACEI();
44
45 g722_dctx = g722_decoder_new(info.sample_rate, options);
46 if (g722_dctx == nullptr) {
47 LOGE("g722_decoder_new");
48 return false;
49 }
50
52 is_active = true;
53 return true;
54 }
55
56 virtual void end() {
57 TRACEI();
58 g722_decoder_destroy(g722_dctx);
59 is_active = false;
60 }
61
62 virtual void setOutput(Print &out_stream) { p_print = &out_stream; }
63
64 operator bool() { return is_active; }
65
66 virtual size_t write(const uint8_t *data, size_t len) {
67 LOGD("write: %d", len);
68 if (!is_active) {
69 LOGE("inactive");
70 return 0;
71 }
72
73 uint8_t *p_byte = (uint8_t *)data;
74 for (int j = 0; j < len; j++) {
75 processByte(p_byte[j]);
76 }
77
78 return len;
79 }
80
81 protected:
82 Print *p_print = nullptr;
83 G722_DEC_CTX *g722_dctx=nullptr;
86 int options = G722_SAMPLE_RATE_8000;
87 int input_pos = 0;
88 bool is_active = false;
89
91 void processByte(uint8_t byte) {
92 // add byte to buffer
93 input_buffer[input_pos++] = byte;
94
95 // decode if buffer is full
96 if (input_pos >= input_buffer.size()) {
97 int result_samples = g722_decode(g722_dctx, input_buffer.data(), input_buffer.size(),
98 (int16_t *)result_buffer.data());
99
100 if (result_samples*2>result_buffer.size()){
101 LOGE("Decoder:Result buffer too small: %d -> %d",result_buffer.size(),result_samples*2);
102 }
103
104 p_print->write(result_buffer.data(), result_samples);
105 input_pos = 0;
106 }
107 }
108};
109
119class G722Encoder : public AudioEncoder {
120 public:
121 G722Encoder() = default;
122
125 this->options = options;
126 }
127
128 bool begin() {
129 TRACEI();
130 if (info.channels != 1) {
131 LOGW("1 channel expected, was: %d", info.channels);
132 }
133
134 g722_ectx = g722_encoder_new(info.sample_rate, options);
135 if (g722_ectx == NULL) {
136 LOGE("g722_encoder_new");
137 return false;
138 }
139
142 is_active = true;
143 return true;
144 }
145
146 virtual void end() {
147 TRACEI();
148 g722_encoder_destroy(g722_ectx);
149 is_active = false;
150 }
151
152 virtual const char *mime() { return "audio/g722"; }
153
154 virtual void setOutput(Print &out_stream) { p_print = &out_stream; }
155
156 operator bool() { return is_active; }
157
158 virtual size_t write(const uint8_t *data, size_t len) {
159 LOGD("write: %d", len);
160 if (!is_active) {
161 LOGE("inactive");
162 return 0;
163 }
164 // encode bytes
165 uint8_t *p_byte = (uint8_t *)data;
166 for (int j = 0; j < len; j++) {
167 processByte(p_byte[j]);
168 }
169 return len;
170 }
171
172 protected:
173 Print *p_print = nullptr;
174 G722_ENC_CTX *g722_ectx = nullptr;
177 int options = G722_SAMPLE_RATE_8000;
178 int buffer_pos = 0;
179 bool is_active = false;
180
181 // add byte to decoding buffer and decode if buffer is full
182 void processByte(uint8_t byte) {
183 input_buffer[buffer_pos++] = byte;
184 if (buffer_pos >= input_buffer.size()) {
185 // convert for little endian
186 int samples = input_buffer.size() / 2;
187 // encode
188 int result_len = g722_encode(g722_ectx,(const int16_t*) input_buffer.data(), samples,
190 if (result_len>result_buffer.size()){
191 LOGE("Encoder:Result buffer too small: %d -> %d",result_buffer.size(),result_len);
192 }
193 p_print->write(result_buffer.data(), result_len);
194 buffer_pos = 0;
195 }
196 }
197};
198
199} // 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
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
Encoding of PCM data.
Definition AudioCodecsBase.h:101
AudioInfo info
Definition AudioCodecsBase.h:120
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:175
Decoder for G.722. Depends on https://github.com/pschatzmann/arduino-libg722.
Definition CodecG722.h:29
int input_pos
Definition CodecG722.h:87
virtual void setOutput(Print &out_stream)
Defines where the decoded result is written to.
Definition CodecG722.h:62
bool is_active
Definition CodecG722.h:88
Vector< uint8_t > result_buffer
Definition CodecG722.h:85
virtual bool begin()
Definition CodecG722.h:40
virtual const char * mime() override
Provides the mime type of the data that is expected by this decoder.
Definition CodecG722.h:33
int options
Definition CodecG722.h:86
virtual size_t write(const uint8_t *data, size_t len)
Definition CodecG722.h:66
G722_DEC_CTX * g722_dctx
Definition CodecG722.h:83
void processByte(uint8_t byte)
Build decoding buffer and decode when frame is full.
Definition CodecG722.h:91
Print * p_print
Definition CodecG722.h:82
void setOptions(int options)
Defines the options for the G.722 Codec: G722_SAMPLE_RATE_8000,G722_PACKED.
Definition CodecG722.h:36
virtual void end()
Definition CodecG722.h:56
Vector< uint8_t > input_buffer
Definition CodecG722.h:84
Encoder for G.722 - Depends on https://github.com/pschatzmann/arduino-libg722. Inspired by g722enc....
Definition CodecG722.h:119
virtual void setOutput(Print &out_stream)
Default output assignment (encoders may override to store Print reference)
Definition CodecG722.h:154
bool is_active
Definition CodecG722.h:179
Vector< uint8_t > result_buffer
Definition CodecG722.h:176
bool begin()
Definition CodecG722.h:128
virtual const char * mime()
Provides the mime type of the encoded result.
Definition CodecG722.h:152
int options
Definition CodecG722.h:177
int buffer_pos
Definition CodecG722.h:178
virtual size_t write(const uint8_t *data, size_t len)
Definition CodecG722.h:158
G722_ENC_CTX * g722_ectx
Definition CodecG722.h:174
void processByte(uint8_t byte)
Definition CodecG722.h:182
Print * p_print
Definition CodecG722.h:173
void setOptions(int options)
Defines the options for the G.722 Codec: G722_SAMPLE_RATE_8000,G722_PACKED.
Definition CodecG722.h:124
virtual void end()
Definition CodecG722.h:146
Vector< uint8_t > input_buffer
Definition CodecG722.h:175
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
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