12 uint16_t control_rate = CONTROL_RATE;
13 int input_range_from = 0;
14 int input_range_to = 1023;
15 int16_t output_volume = 10;
36 void setInput(Stream& in) { p_input = ∈ }
56 control_counter_max = info.
sample_rate / cfg.control_rate;
57 if (control_counter_max <= 0) {
58 control_counter_max = 1;
60 control_counter = control_counter_max;
65 void end() { active =
false; }
69 cfg.output_volume = vol;
75 if (!active)
return 0;
76 int samples = byteCount /
sizeof(int16_t);
78 int16_t* data16 = (int16_t*)data;
80 for (
int j = 0; j < frames; j++) {
81 int16_t sample = nextSample();
82 for (
int ch = 0; ch < cfg.
channels; ch++) {
83 data16[idx++] = sample;
86 return idx *
sizeof(int16_t);
90 size_t write(
const uint8_t* data,
size_t byteCount) {
91 if (!active)
return 0;
92 if (buffer.
size() == 0) {
93 buffer.resize(byteCount * 2);
103 if (p_input !=
nullptr) {
104 p_input->readBytes((uint8_t*)&result,
sizeof(int16_t) * cfg.
channels);
109 int sample = avg(result);
111 if (sample < input_min) input_min = sample;
112 if (sample > input_max) input_max = sample;
113 int sample1 =
map(sample, input_min, input_max, cfg.input_range_from,
120 int control_counter_max;
123 Stream* p_input =
nullptr;
125 int input_min = 32767;
126 int input_max = -32768;
128 int16_t avg(int16_t* values) {
130 for (
int ch = 0; ch < cfg.
channels; ch++) {
136 int16_t nextSample() {
138 if (--control_counter < 0) {
139 control_counter = control_counter_max;
140 LOGD(
"updateControl");
145 int result = updateAudio() * cfg.output_volume;
147 if (result > 32767) result = 32767;
148 if (result < -32768) result = -32768;