11 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
33 virtual bool begin() {
36 result_buffer.resize(160 *
sizeof(int16_t));
38 input_buffer.resize(33);
41 notifyAudioChange(info);
54 operator bool() {
return is_active; }
56 virtual size_t write(
const uint8_t *data,
size_t len) {
57 LOGD(
"write: %d", len);
63 for (
int j = 0; j < len; j++) {
71 Print *p_print =
nullptr;
73 bool is_active =
false;
74 Vector<uint8_t> input_buffer;
75 Vector<uint8_t> result_buffer;
81 input_buffer[input_pos++] = byte;
84 if (input_pos >= input_buffer.size()) {
85 if (gsm_decode(v_gsm, input_buffer.data(), (gsm_signal*)result_buffer.data())!=0){
93 p_print->write(result_buffer.data(), result_buffer.size());
99 int16_t *pt16 = (int16_t *)vector.data();
100 for (
int j = 0; j < vector.size() / 2; j++) {
101 if (abs(pt16[j])<=4095){
102 pt16[j] = pt16[j] * 8;
103 }
else if(pt16[j]<0){
105 }
else if(pt16[j]>0){
111 void fromBigEndian(Vector<uint8_t> &vector){
112 int size = vector.size() / 2;
113 int16_t *data16 = (int16_t*) vector.data();
114 for (
int i=0; i<size; i++){
115 data16[i] = ntohs(data16[i]);
136 scaling_active = scaling;
143 LOGW(
"Sample rate is supposed to be 8000 - it was %d", info.
sample_rate);
146 LOGW(
"channels is supposed to be 1 - it was %d", info.
channels);
149 v_gsm = gsm_create();
151 input_buffer.resize(160 *
sizeof(int16_t));
153 result_buffer.resize(33);
164 virtual const char *
mime() {
return "audio/gsm"; }
166 virtual void setOutput(
Print &out_stream) { p_print = &out_stream; }
168 operator bool() {
return is_active; }
170 virtual size_t write(
const uint8_t *data,
size_t len) {
171 LOGD(
"write: %d", len);
177 for (
int j = 0; j < len; j++) {
178 processByte(data[j]);
184 Print *p_print =
nullptr;
186 bool is_active =
false;
189 Vector<uint8_t> input_buffer;
190 Vector<uint8_t> result_buffer;
193 void processByte(uint8_t
byte) {
194 input_buffer[buffer_pos++] = byte;
195 if (buffer_pos >= input_buffer.size()) {
196 scaleValues(input_buffer);
199 gsm_encode(v_gsm, (gsm_signal*)input_buffer.data(), result_buffer.data());
200 size_t written = p_print->write(result_buffer.data(), result_buffer.size());
201 assert(written == result_buffer.size());
206 void toBigEndian(Vector<uint8_t> &vector){
207 int size = vector.size() / 2;
208 int16_t *data16 = (int16_t*) vector.data();
209 for (
int i=0; i<size; i++){
210 data16[i] = htons(data16[i]);
214 void scaleValues(Vector<uint8_t> &vector) {
215 int16_t *pt16 = (int16_t *)vector.data();
216 int size = vector.size() / 2;
219 for (
int j = 0; j < size; j++) {
220 pt16[j] = pt16[j] / 8;
224 for (
int j = 0; j < size; j++) {