13 #include "AudioTools/AudioCodecs/AudioEncoded.h"
14 #include "AudioTools/CoreAudio/Buffers.h"
15 #include "AudioTools/CoreAudio/AudioEffects/SoundGenerator.h"
18 #define GGWAVE_DEFAULT_SAMPLE_RATE 48000
19 #define GGWAVE_DEFAULT_PAYLOAD_LEN 16
20 #define GGWAVE_DEFAULT_SAMPLES_PER_FRAME 256
21 #define GGWAVE_DEFAULT_BYTES_PER_FRAME GGWAVE_DEFAULT_SAMPLES_PER_FRAME*GGWAVE_DEFAULT_PAYLOAD_LEN
22 #define GGWAVE_DEFAULT_PROTOCOL GGWAVE_PROTOCOL_AUDIBLE_FAST
23 #define GGWAVE_DEFAULT_SAMPLE_BYTESIZE 2
54 for (
int j=0;j<N;j++){
55 protocols.push_back(protocolsArray[j]);
59 void setSamplesFormatInput(ggwave_SampleFormat fmt){
60 samples_format_input = fmt;
63 void setSamplesFromatOutput(ggwave_SampleFormat fmt) {
64 samples_format_output = fmt;
67 void setSamplesPerFrame(
int samples){
68 samples_per_frame = samples;
71 void setPayloadLen(
int len){
75 void setSampleByteSize(
int size){
76 sample_byte_size = size;
80 if (pt_print==
nullptr){
81 LOGE(
"final destination not defined");
85 receive_buffer.resize(samples_per_frame*sample_byte_size);
86 ggwave.setLogFile(
nullptr);
88 auto p = GGWave::getDefaultParameters();
89 p.payloadLength = playload_len;
93 p.samplesPerFrame = samples_per_frame;
94 p.sampleFormatInp = samples_format_input;
95 p.sampleFormatOut = samples_format_output;
96 p.operatingMode = GGWAVE_OPERATING_MODE_RX | GGWAVE_OPERATING_MODE_USE_DSS ;
100 GGWave::Protocols::tx().disableAll();
101 GGWave::Protocols::rx().disableAll();
104 GGWave::Protocols::rx().toggle(GGWAVE_DEFAULT_PROTOCOL,
true);
105 for (
auto protocol: protocols){
106 GGWave::Protocols::rx().toggle(protocol,
true);
111 if (!ggwave.prepare(p,
true)){
113 LOGE(
"prepare failed");
119 ggwave.rxStopReceiving();
123 size_t write(
const uint8_t *data,
size_t len) {
124 if (!active)
return 0;
125 uint8_t *p_byte = (uint8_t *)data;
126 for (
int j=0;j<len;j++){
127 receive_buffer.
write(p_byte[j]);
135 operator bool() {
return active; }
141 Print *pt_print=
nullptr;
144 GGWave::TxRxData data;
146 Vector<ggwave_ProtocolId> protocols;
147 ggwave_SampleFormat samples_format_input = GGWAVE_SAMPLE_FORMAT_I16;
148 ggwave_SampleFormat samples_format_output = GGWAVE_SAMPLE_FORMAT_U8;
149 int samples_per_frame = 0;
150 int playload_len = GGWAVE_DEFAULT_PAYLOAD_LEN;
151 int sample_byte_size = GGWAVE_DEFAULT_SAMPLE_BYTESIZE;
158 int nr = ggwave.rxTakeData(data);
160 pt_print->write((uint8_t*) &data[0], nr);
163 LOGW(
"decoding error");
165 receive_buffer.
reset();
185 pt_print=&out_stream;
188 void setOutput(
Print &out_stream) {pt_print=&out_stream;}
190 void setSamplesFormatInput(ggwave_SampleFormat fmt){
191 samples_format_input = fmt;
194 void setSamplesFromatOutput(ggwave_SampleFormat fmt) {
195 samples_format_output = fmt;
198 void setSamplesPerFrame(
int samples){
199 samples_per_frame = samples;
202 void setPayloadLen(
int len){
206 void setProtocol(ggwave_ProtocolId protocol){
207 protocolId = protocol;
210 void setSampleByteSize(
int size){
211 sample_byte_size = size;
216 ggwave.setLogFile(
nullptr);
217 if (pt_print==
nullptr){
218 LOGE(
"final destination not defined");
223 auto parameters = GGWave::getDefaultParameters();
224 parameters.payloadLength = playload_len;
228 parameters.samplesPerFrame = samples_per_frame;
229 parameters.sampleFormatInp = samples_format_input;
230 parameters.sampleFormatOut = samples_format_output;
231 parameters.operatingMode = GGWAVE_OPERATING_MODE_TX | GGWAVE_OPERATING_MODE_USE_DSS;
234 GGWave::Protocols::tx().only(protocolId);
243 ggwave.prepare(parameters);
252 size_t write(
const uint8_t *data,
size_t len) {
253 if (!active)
return 0;
255 if (!ggwave.init(len, (
const char*)data, protocolId)){
259 size_t bytes = ggwave.encode();
260 LOGI(
"write %d", bytes);
263 const auto &protocol = GGWave::Protocols::tx()[protocolId];
264 const auto tones = ggwave.txTones();
265 const auto duration_ms = protocol.txDuration_ms(ggwave.samplesPerFrame(), ggwave.sampleRateOut());
266 for (
auto & curTone : tones) {
267 const auto freq_hz = (protocol.freqStart + curTone)*ggwave.hzPerSample();
268 play(freq_hz, duration_ms);
284 Print *pt_print=
nullptr;
286 ggwave_ProtocolId protocolId = GGWAVE_DEFAULT_PROTOCOL;
287 int samples_per_frame = GGWAVE_DEFAULT_SAMPLES_PER_FRAME;
288 ggwave_SampleFormat samples_format_input = GGWAVE_SAMPLE_FORMAT_I16;
289 ggwave_SampleFormat samples_format_output = GGWAVE_SAMPLE_FORMAT_U8;
290 int playload_len = GGWAVE_DEFAULT_PAYLOAD_LEN;
291 int volume = GGWave::kDefaultVolume;
292 int sample_byte_size = GGWAVE_DEFAULT_SAMPLE_BYTESIZE;
297 virtual void play(
int freq,
int ms){
299 int amplitude = 10000;
300 sine_wave.setAmplitude(amplitude);
302 uint64_t end =
millis()+ms;
305 for (
int ch=0;ch<info.
channels;ch++){
306 pt_print->write((uint8_t*)&sample,2);
311 virtual void silence(
int samples){
314 for (
int ch=0;ch<info.
channels;ch++){
315 pt_print->write((uint8_t*)&sample,2);