Skip to content

Commit d573756

Browse files
committed
AnalogAudioArduino corrections
1 parent bc8d410 commit d573756

File tree

4 files changed

+117
-39
lines changed

4 files changed

+117
-39
lines changed

examples/build-examples-log.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@
295295
../examples/tests/fft/fft-cmsis -> rc=1
296296
../examples/tests/fft/fft-esp32 -> rc=0
297297
../examples/tests/fft/fft-espressif -> rc=0
298-
../examples/tests/fft/fft-kiss -> rc=1
298+
../examples/tests/fft/fft-kiss -> rc=0
299299
../examples/tests/fft/fft-real -> rc=0
300300
../examples/tests/fft/fft-topn -> rc=0
301301
../examples/tests/fft/fft-window -> rc=0

src/AudioAnalog/AnalogAudioArduino.h

Lines changed: 71 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class AnalogAudioArduino : public AudioStream {
4747

4848
bool begin(AnalogConfigStd cfg) {
4949
TRACED();
50+
5051
config = cfg;
5152
if (config.rx_tx_mode == RXTX_MODE) {
5253
LOGE("RXTX not supported");
@@ -56,6 +57,8 @@ class AnalogAudioArduino : public AudioStream {
5657
frame_size = config.channels * (config.bits_per_sample / 8);
5758
result_factor = 1;
5859

60+
if (!setupPins()) return false;
61+
5962
if (!setupTx()) return false;
6063

6164
if (!setupBuffer()) return false;
@@ -112,10 +115,53 @@ class AnalogAudioArduino : public AudioStream {
112115
}
113116
}
114117

118+
size_t result = 0;;
119+
switch(config.bits_per_sample){
120+
case 8: {
121+
result = buffer->writeArray(data, len);
122+
} break;
123+
case 16: {
124+
size_t samples = len / 2;
125+
int16_t *p16 = (int16_t*)data;
126+
for (int j=0;j<samples;j++){
127+
uint8_t sample = map(p16[j],-32768, 32767,0,255);
128+
if (buffer->write(sample)){
129+
result += 2;
130+
} else {
131+
break;
132+
}
133+
}
134+
} break;
135+
case 24: {
136+
size_t samples = len / 3;
137+
int24_t *p24 = (int24_t*)data;
138+
for (int j=0;j<samples;j++){
139+
uint8_t sample = map(p24[j],-8388608, 8388607,0,255);
140+
if (buffer->write(sample)){
141+
result += 3;
142+
} else {
143+
break;
144+
}
145+
}
146+
147+
} break;
148+
case 32: {
149+
size_t samples = len / 4;
150+
int32_t *p32 = (int32_t*)data;
151+
for (int j=0;j<samples;j++){
152+
uint8_t sample = map(p32[j],-2147483648, 2147483647,0,255);
153+
if (buffer->write(sample)){
154+
result += 4;
155+
} else {
156+
break;
157+
}
158+
}
159+
160+
} break;
161+
}
162+
115163
// write data
116-
size_t result = buffer->writeArray(data, len) * result_factor;
117-
LOGD("write: -> %d / factor: %d", (int)result, result_factor);
118-
return result;
164+
return result * result_factor;
119165
}
120166

121167
protected:
@@ -128,6 +174,7 @@ class AnalogAudioArduino : public AudioStream {
128174
int result_factor = 1;
129175
int decim = 1;
130176

177+
131178
bool setupTx() {
132179
if (config.rx_tx_mode == TX_MODE) {
133180
// check channels
@@ -156,14 +203,11 @@ class AnalogAudioArduino : public AudioStream {
156203
bool setupBuffer() {
157204
if (buffer == nullptr) {
158205
// allocate buffer_count
159-
buffer =
160-
new RingBuffer<uint8_t>(config.buffer_size * config.buffer_count);
206+
buffer = new RingBuffer<uint8_t>(config.buffer_size * config.buffer_count);
161207
if (buffer == nullptr) {
162208
LOGE("Not enough memory for buffer");
163209
return false;
164210
}
165-
// setup pins
166-
setupPins();
167211
}
168212
return true;
169213
}
@@ -188,7 +232,7 @@ class AnalogAudioArduino : public AudioStream {
188232
int channels = self->config.channels;
189233
for (int j = 0; j < channels; j++) {
190234
// provides value in range 0…4095
191-
value = analogRead(self->config.start_pin + j);
235+
value = analogRead(self->config.pins_data[j]);
192236
if (self->config.is_auto_center_read) {
193237
self->updateMinMax(value);
194238
}
@@ -200,42 +244,50 @@ class AnalogAudioArduino : public AudioStream {
200244
int channels = self->config.channels;
201245
for (int j = 0; j < channels; j++) {
202246
int16_t sample = self->buffer->read();
203-
sample = map(sample, -32768, 32767, 0, 255);
204-
int pin = self->config.start_pin + j;
247+
int pin = self->config.pins_data[j];
205248
analogWrite(pin, sample);
206-
// LOGI("analogWrite(%d, %d)", pin, sample);
249+
//LOGW("analogWrite(%d, %d)", pin, sample);
207250
}
208251
}
209252
}
210253

211254
/// pinmode input for defined analog pins
212-
void setupPins() {
255+
bool setupPins() {
213256
TRACED();
257+
258+
Pins& pins = config.pins();
259+
if (pins.size()<config.channels){
260+
LOGE("Only pins %d of %d defined", pins.size(), config.channels);
261+
return false;
262+
}
263+
264+
214265
if (config.rx_tx_mode == RX_MODE) {
215266
LOGI("rx start_pin: %d", config.start_pin);
216267
// setup pins for read
217268
for (int j = 0; j < config.channels; j++) {
218-
int pin = config.start_pin + j;
269+
int pin = config.pins_data [j];
219270
pinMode(pin, INPUT);
220271
LOGD("pinMode(%d, INPUT)", pin);
221272
}
222273

223274
if (config.is_auto_center_read) {
224275
// calculate the avarage value to center the signal
225276
for (int j = 0; j < 1024; j++) {
226-
updateMinMax(analogRead(config.start_pin));
277+
updateMinMax(analogRead(config.pins_data[0]));
227278
}
228279
LOGI("Avg Signal was %d", avg_value);
229280
}
230281
} else if (config.rx_tx_mode == TX_MODE) {
231-
LOGI("tx start_pin: %d", config.start_pin);
232282
// setup pins for read
233283
for (int j = 0; j < config.channels; j++) {
234-
int pin = config.start_pin + j;
284+
int pin = config.pins_data[j];
285+
LOGI("tx pin %d: %d", j, pin);
235286
pinMode(pin, OUTPUT);
236287
LOGD("pinMode(%d, OUTPUT)", pin);
237288
}
238289
}
290+
return true;
239291
}
240292

241293
void updateMinMax(int value) {
@@ -254,7 +306,7 @@ class AnalogAudioArduino : public AudioStream {
254306
/// The requested sampling rate is too hight: we only process half of the
255307
/// samples so we can half the sampling rate
256308
bool isDecimateActive() {
257-
return config.sample_rate >= ANALOG_MAX_SAMPLE_RATE;
309+
return config.sample_rate >= config.max_sample_rate;
258310
}
259311

260312
// combined stereo channel to mono
@@ -264,9 +316,9 @@ class AnalogAudioArduino : public AudioStream {
264316
int effectiveOutputSampleRate() { return config.sample_rate / decimation(); }
265317

266318
int decimation() {
267-
if (config.sample_rate <= ANALOG_MAX_SAMPLE_RATE) return 1;
319+
if (config.sample_rate <= config.max_sample_rate) return 1;
268320
for (int j = 2; j < 6; j += 2) {
269-
if (config.sample_rate / j <= ANALOG_MAX_SAMPLE_RATE) {
321+
if (config.sample_rate / j <= config.max_sample_rate) {
270322
return j;
271323
}
272324
}

src/AudioAnalog/AnalogConfigStd.h

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#pragma once
22
#include "AudioConfig.h"
3-
//#if defined(USE_ANALOG)
3+
#include "AudioTools/AudioTypes.h"
44

55
#ifndef PIN_ANALOG_START
6-
# define PIN_ANALOG_START 1
6+
# define PIN_ANALOG_START -1
77
#endif
88

99
#ifndef ANALOG_BUFFERS
@@ -17,6 +17,8 @@
1717

1818
namespace audio_tools {
1919

20+
class AnalogAudioArduino;
21+
2022
/**
2123
* @brief Generic ADC and DAC configuration
2224
*
@@ -25,29 +27,53 @@ namespace audio_tools {
2527
* @copyright GPLv3
2628
*/
2729
class AnalogConfigStd : public AudioInfo {
30+
friend class AnalogAudioArduino;
2831
public:
29-
int buffer_count = PWM_BUFFER_COUNT;
30-
int buffer_size = PWM_BUFFER_SIZE;
31-
RxTxMode rx_tx_mode;
32+
int buffer_count = ANALOG_BUFFERS;
33+
int buffer_size = ANALOG_BUFFER_SIZE;
34+
RxTxMode rx_tx_mode = RX_MODE;
3235
bool is_blocking_write = true;
3336
bool is_auto_center_read = true;
37+
int max_sample_rate = ANALOG_MAX_SAMPLE_RATE;
38+
int start_pin = PIN_ANALOG_START;
3439

35-
/// Copy constructor
36-
AnalogConfigStd(const AnalogConfigStd &cfg) = default;
37-
38-
AnalogConfigStd() {
39-
sample_rate = 44100;
40-
bits_per_sample = 16;
41-
channels = 2;
42-
buffer_size = ANALOG_BUFFER_SIZE;
43-
buffer_count = ANALOG_BUFFERS;
44-
rx_tx_mode = RX_MODE;
45-
}
46-
/// Default constructor
40+
AnalogConfigStd() = default;
4741
AnalogConfigStd(RxTxMode rxtxMode) : AudioInfo() {
4842
rx_tx_mode = rxtxMode;
4943
}
50-
int start_pin = PIN_ANALOG_START;
44+
45+
/// support assignament of int array
46+
template <typename T, int N>
47+
void setPins(T (&a)[N]) {
48+
pins_data.clear();
49+
pins_data.resize(N);
50+
for (int i = 0; i < N; ++i) {
51+
pins_data[i] = a[i]; // reset all elements
52+
}
53+
}
54+
55+
// /// Defines the pins and the corresponding number of channels (=number of
56+
// /// pins)
57+
// void setPins(Pins &pins) {
58+
// pins_data.clear();
59+
// for (int i = 0; i < pins.size(); i++) {
60+
// pins_data.push_back(pins[i]);
61+
// }
62+
// }
63+
64+
/// Determines the pins (for all channels)
65+
Pins &pins() {
66+
if (pins_data.size() == 0 && start_pin >= 0) {
67+
pins_data.resize(channels);
68+
for (int j = 0; j < channels; j++) {
69+
pins_data[j] = start_pin + j;
70+
}
71+
}
72+
return pins_data;
73+
}
74+
75+
protected:
76+
Pins pins_data{0};
5177

5278
};
5379

src/AudioConfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ typedef WiFiClient WiFiClientSecure;
757757

758758

759759
#ifndef ANALOG_MAX_SAMPLE_RATE
760-
# define ANALOG_MAX_SAMPLE_RATE 44000
760+
# define ANALOG_MAX_SAMPLE_RATE 44100
761761
#endif
762762

763763
#ifndef URL_CLIENT_TIMEOUT

0 commit comments

Comments
 (0)