1 #include "AudioConfig.h"
4 #define DELAY(j) {for(volatile int i=0;i<j;i++);}
24 void setStream(
Stream &in) { p_in = ∈ }
32 void setDecimationFactor(
int factor) { dec_factor = factor; }
34 size_t readBytes(uint8_t *data,
size_t len) {
35 LOGD(
"readBytes:%d", len);
36 assert(data !=
nullptr);
38 int samples = len /
sizeof(T);
39 int result_bytes = samples *
sizeof(T);
42 int max_value =
sizeof(T) * 8 * dec_factor;
43 int scaled_max = pow(2,
sizeof(T) * 8);
44 int factor = scaled_max / max_value;
45 int unsigned_to_signed = scaled_max / 2;
48 T *data_typed = (T *)data;
50 for (
int idx = 0; idx < samples; idx++) {
52 unsigned decimated = 0;
53 for (
int dec = 0; dec < dec_factor; dec++) {
54 int sample_bytes =
sizeof(T);
55 LOGD(
"readBytes: %d", sample_bytes);
56 if (p_in->readBytes((uint8_t *)&tmp_in, sample_bytes) != sample_bytes) {
57 LOGE(
"readBytes failed");
59 decimated += countSetBits(tmp_in);
63 data_typed[idx] = decimated;
85 unsigned countSetBits(
unsigned n) {
114 template <
typename T>
120 pinMode(pin_clock, OUTPUT);
121 pinMode(pin_data, INPUT);
125 size_t readBytes(uint8_t *data,
size_t len) {
126 LOGD(
"readBytes:%d", len);
127 assert(data !=
nullptr);
129 int samples = len /
sizeof(T);
130 int result_bytes = samples *
sizeof(T);
134 int scaled_max = pow(2,
sizeof(T) * 8);
135 int factor = scaled_max / max_value;
136 int unsigned_to_signed = scaled_max / 2;
139 T *data_typed = (T *)data;
141 for (
int idx = 0; idx < samples; idx++) {
143 unsigned decimated = 0;
144 for (
int dec = 0; dec < DecimationStreamExt<T>::dec_factor *
sizeof(T)*8; dec++) {
145 int sample_bytes =
sizeof(T);
146 digitalWrite(pin_clock, HIGH);
148 digitalWrite(pin_clock, LOW);
156 data_typed[idx] = decimated;
175 template <
typename T>
179 decimation_stream.setStream(in);
195 int decimation() {
return decimation_factor; }
198 void setDecimationFactor(
int factor) {
199 decimation_factor = factor;
205 LOGE(
"channels must be 1");
208 decimation_stream.setDecimationFactor(decimation_factor);
213 decimation_stream.begin(info);
214 bool rc = in_filtered.begin(info);
219 size_t readBytes(uint8_t *data,
size_t len) {
220 LOGD(
"readBytes:%d", len);
221 assert(data !=
nullptr);
223 int samples = len /
sizeof(T);
224 int result_bytes = samples *
sizeof(T);
225 assert(result_bytes <= len);
227 if (in_filtered.readBytes(data, result_bytes) != result_bytes) {
228 LOGE(
"readBytes failed");
236 return in_filtered.available();
240 void setFilterValues(
const T (&b)[B]) {
245 int decimation_factor =
sizeof(T) * 2;
248 -0.000704420658475743, -0.000537879918926308, 0.004114637509913062,
249 -0.012685775806621488, 0.027889173789107543, -0.049285026985058301,
250 0.074005079283040689, -0.097330704866957815, 0.114052040962871595,
251 0.880965753382213723, 0.114052040962871595, -0.097330704866957843,
252 0.074005079283040717, -0.049285026985058301, 0.027889173789107550,
253 -0.012685775806621504, 0.004114637509913064, -0.000537879918926308,
254 -0.000704420658475743};
259 constexpr int8_t bitsPerSample() {
return sizeof(T) * 8; }
int digitalRead(int pin)
e.g. for AudioActions
Definition: NoArduino.h:206