3 #include "AudioConfig.h"
4 #if defined(USE_ANALOG_ARDUINO) || defined(DOXYGEN)
5 #include "AudioAnalog/AnalogAudioBase.h"
6 #include "AudioTimer/AudioTimer.h"
7 #include "AudioTools/AudioStreams.h"
8 #include "AudioTools/AudioTypes.h"
9 #include "AudioTools/Buffers.h"
29 if (config.rx_tx_mode == RXTX_MODE) {
30 LOGE(
"RXTX not supported");
37 if (!setupTx())
return false;
39 if (!setupBuffer())
return false;
45 void end()
override { timer.end(); }
47 int available()
override {
48 if (config.rx_tx_mode == TX_MODE)
return 0;
49 return buffer ==
nullptr ? 0 : buffer->
available() * 2;
53 size_t readBytes(uint8_t *values,
size_t len)
override {
54 if (config.rx_tx_mode == TX_MODE)
return 0;
55 if (buffer ==
nullptr)
return 0;
56 int bytes = len / frame_size * frame_size;
60 int availableForWrite()
override {
61 if (config.rx_tx_mode == RX_MODE)
return 0;
62 if (buffer ==
nullptr)
return 0;
63 return config.is_blocking_write ? config.buffer_size : buffer->
availableForWrite();
66 size_t write(
const uint8_t *data,
size_t len)
override {
67 LOGD(
"write: %d", (
int)len);
68 if (config.rx_tx_mode == RX_MODE)
return 0;
70 len = len / frame_size * frame_size;
72 if (isCombinedChannel()){
74 len = cr.convert((uint8_t*)data, len);
75 LOGD(
"ChannelReducer len: %d", (
int) len);
80 len = dec.convert((uint8_t*)data, len);
81 LOGD(
"Decimate len: %d for factor %d", (
int) len, decim);
85 if (config.is_blocking_write) {
86 LOGD(
"Waiting for buffer to be available");
93 size_t result = buffer->
writeArray(data, len) * result_factor;
94 LOGD(
"write: -> %d / factor: %d", (
int)result, result_factor);
100 TimerAlarmRepeating timer;
101 BaseBuffer<uint8_t> *buffer =
nullptr;
102 int avg_value, min, max, count;
103 bool is_combined_channels =
false;
104 uint16_t frame_size = 0;
105 int result_factor = 1;
109 if (config.rx_tx_mode == TX_MODE) {
111 if (config.channels>ANALOG_MAX_OUT_CHANNELS){
112 if (config.channels == 2){
113 is_combined_channels =
true;
116 LOGE(
"Unsupported channels");
122 decim = decimation();
123 result_factor = result_factor * decim;
125 if (isCombinedChannel()){
126 LOGI(
"Combining channels");
127 result_factor = result_factor * 2;
134 if (buffer ==
nullptr) {
136 buffer =
new RingBuffer<uint8_t>(config.buffer_size * config.buffer_count);
137 if (buffer ==
nullptr) {
138 LOGE(
"Not enough memory for buffer");
149 LOGI(
"sample_rate: %d", sample_rate);
150 timer.setCallbackParameter(
this);
151 return timer.begin(
callback, sample_rate, TimeUnit::HZ);
159 if (self->buffer ==
nullptr)
return;
162 if (self->config.rx_tx_mode == RX_MODE) {
163 int channels =
self->config.
channels;
164 for (
int j = 0; j < channels; j++) {
166 value = analogRead(self->config.start_pin + j);
167 if (self->config.is_auto_center_read){
168 self->updateMinMax(value);
170 value = (value -
self->avg_value) * 16;
171 self->buffer->write(value);
174 }
else if (self->config.rx_tx_mode == TX_MODE) {
175 int channels =
self->config.channels;
176 for (
int j = 0; j < channels; j++) {
177 int16_t sample =
self->buffer->read();
178 sample =
map(sample, -32768, 32767, 0, 255);
179 int pin =
self->config.start_pin + j;
180 analogWrite(pin, sample);
189 if (config.rx_tx_mode == RX_MODE) {
190 LOGI(
"rx start_pin: %d", config.start_pin);
192 for (
int j = 0; j < config.channels; j++) {
193 int pin = config.start_pin + j;
195 LOGD(
"pinMode(%d, INPUT)", pin);
198 if (config.is_auto_center_read){
200 for (
int j = 0; j < 1024; j++) {
201 updateMinMax(analogRead(config.start_pin));
203 LOGI(
"Avg Signal was %d", avg_value);
205 }
else if (config.rx_tx_mode == TX_MODE) {
206 LOGI(
"tx start_pin: %d", config.start_pin);
208 for (
int j = 0; j < config.channels; j++) {
209 int pin = config.start_pin + j;
210 pinMode(pin, OUTPUT);
211 LOGD(
"pinMode(%d, OUTPUT)", pin);
216 void updateMinMax(
int value){
217 if (value<min) min = value;
218 if (value>max) max = value;
219 if (count++==1024) updateAvg();
223 avg_value = (max + min) / 2;
231 return config.sample_rate >= ANALOG_MAX_SAMPLE_RATE;
235 bool isCombinedChannel(){
236 return is_combined_channels;
241 return config.sample_rate / decimation();
245 if (config.sample_rate <= ANALOG_MAX_SAMPLE_RATE)
return 1;
246 for(
int j=2;j<6;j+=2){
247 if (config.sample_rate/j <= ANALOG_MAX_SAMPLE_RATE) {