arduino-audio-tools
Loading...
Searching...
No Matches
CodecGSM.h
Go to the documentation of this file.
1
9#pragma once
10
12#include "gsm.h"
13
14
15namespace audio_tools {
16
26class GSMDecoder : public AudioDecoder {
27 public:
29 info.sample_rate = 8000;
30 info.channels = 1;
31 }
32
33 virtual bool begin() {
34 TRACEI();
35 // 160 13-bit samples
36 result_buffer.resize(160 * sizeof(int16_t));
37 // gsm_frame of 33 bytes
39
40 v_gsm = gsm_create();
42 is_active = true;
43 return true;
44 }
45
46 virtual void end() {
47 TRACEI();
49 is_active = false;
50 }
51
53
54 operator bool() { return is_active; }
55
56 virtual size_t write(const uint8_t *data, size_t len) {
57 LOGD("write: %d", len);
58 if (!is_active) {
59 LOGE("inactive");
60 return 0;
61 }
62
63 for (int j = 0; j < len; j++) {
64 processByte(data[j]);
65 }
66
67 return len;
68 }
69
70 protected:
71 Print *p_print = nullptr;
73 bool is_active = false;
76 int input_pos = 0;
77
79 void processByte(uint8_t byte) {
80 // add byte to buffer
82
83 // decode if buffer is full
84 if (input_pos >= input_buffer.size()) {
86 LOGE("gsm_decode");
87 }
88
89 //fromBigEndian(result_buffer);
90 // scale to 13 to 16-bit samples
92
94 input_pos = 0;
95 }
96 }
97
98 void scale(Vector<uint8_t> &vector){
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){
104 pt16[j] = -32767;
105 } else if(pt16[j]>0){
106 pt16[j] = 32767;
107 }
108 }
109 }
110
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]);
116 }
117 }
118
119
120};
121
131class GSMEncoder : public AudioEncoder {
132 public:
133 GSMEncoder(bool scaling=true) {
134 info.sample_rate = 8000;
135 info.channels = 1;
137 }
138
139 bool begin() {
140 TRACEI();
141
142 if (info.sample_rate != 8000) {
143 LOGW("Sample rate is supposed to be 8000 - it was %d", info.sample_rate);
144 }
145 if (info.channels != 1) {
146 LOGW("channels is supposed to be 1 - it was %d", info.channels);
147 }
148
149 v_gsm = gsm_create();
150 // 160 13-bit samples
151 input_buffer.resize(160 * sizeof(int16_t));
152 // gsm_frame of 33 bytes
154 is_active = true;
155 return true;
156 }
157
158 virtual void end() {
159 TRACEI();
161 is_active = false;
162 }
163
164 virtual const char *mime() { return "audio/gsm"; }
165
167
168 operator bool() { return is_active; }
169
170 virtual size_t write(const uint8_t *data, size_t len) {
171 LOGD("write: %d", len);
172 if (!is_active) {
173 LOGE("inactive");
174 return 0;
175 }
176 // encode bytes
177 for (int j = 0; j < len; j++) {
178 processByte(data[j]);
179 }
180 return len;
181 }
182
183 protected:
184 Print *p_print = nullptr;
186 bool is_active = false;
187 int buffer_pos = 0;
191
192 // add byte to decoding buffer and decode if buffer is full
193 void processByte(uint8_t byte) {
195 if (buffer_pos >= input_buffer.size()) {
197 // toBigEndian(input_buffer);
198 // encode
200 size_t written = p_print->write(result_buffer.data(), result_buffer.size());
201 assert(written == result_buffer.size());
202 buffer_pos = 0;
203 }
204 }
205
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]);
211 }
212 }
213
215 int16_t *pt16 = (int16_t *)vector.data();
216 int size = vector.size() / 2;
217 if (scaling_active){
218 // scale to 16 to 13-bit samples
219 for (int j = 0; j < size; j++) {
220 pt16[j] = pt16[j] / 8;
221 }
222 } else {
223 // clip value to 13-bits
224 for (int j = 0; j < size; j++) {
225 if ( pt16[j]>4095){
226 pt16[j] = 4095;
227 }
228 if ( pt16[j]<-4095){
229 pt16[j] = -4095;
230 }
231 }
232 }
233 }
234};
235
236} // 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 htons(x)
Definition Net.h:7
#define ntohs(x)
Definition Net.h:9
#define assert(T)
Definition avr.h:10
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 GSM. Depends on https://github.com/pschatzmann/arduino-libgsm. Inspired by gsmdec....
Definition CodecGSM.h:26
int input_pos
Definition CodecGSM.h:76
void fromBigEndian(Vector< uint8_t > &vector)
Definition CodecGSM.h:111
virtual void setOutput(Print &out_stream)
Defines where the decoded result is written to.
Definition CodecGSM.h:52
bool is_active
Definition CodecGSM.h:73
Vector< uint8_t > result_buffer
Definition CodecGSM.h:75
virtual bool begin()
Definition CodecGSM.h:33
GSMDecoder()
Definition CodecGSM.h:28
virtual size_t write(const uint8_t *data, size_t len)
Definition CodecGSM.h:56
void processByte(uint8_t byte)
Build decoding buffer and decode when frame is full.
Definition CodecGSM.h:79
void scale(Vector< uint8_t > &vector)
Definition CodecGSM.h:98
Print * p_print
Definition CodecGSM.h:71
virtual void end()
Definition CodecGSM.h:46
Vector< uint8_t > input_buffer
Definition CodecGSM.h:74
gsm v_gsm
Definition CodecGSM.h:72
Encoder for GSM - Depends on https://github.com/pschatzmann/arduino-libgsm. Inspired by gsmenc....
Definition CodecGSM.h:131
GSMEncoder(bool scaling=true)
Definition CodecGSM.h:133
virtual void setOutput(Print &out_stream)
Default output assignment (encoders may override to store Print reference)
Definition CodecGSM.h:166
bool is_active
Definition CodecGSM.h:186
Vector< uint8_t > result_buffer
Definition CodecGSM.h:190
bool begin()
Definition CodecGSM.h:139
virtual const char * mime()
Provides the mime type of the encoded result.
Definition CodecGSM.h:164
void scaleValues(Vector< uint8_t > &vector)
Definition CodecGSM.h:214
int buffer_pos
Definition CodecGSM.h:187
virtual size_t write(const uint8_t *data, size_t len)
Definition CodecGSM.h:170
void processByte(uint8_t byte)
Definition CodecGSM.h:193
bool scaling_active
Definition CodecGSM.h:188
Print * p_print
Definition CodecGSM.h:184
virtual void end()
Definition CodecGSM.h:158
Vector< uint8_t > input_buffer
Definition CodecGSM.h:189
void toBigEndian(Vector< uint8_t > &vector)
Definition CodecGSM.h:206
gsm v_gsm
Definition CodecGSM.h:185
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