1 #include "AudioTools/CoreAudio/AudioStreams.h"
27 return AudioStream::begin();
30 int available()
override{
31 if (p_in)
return p_in->available();
35 int availableForWrite()
override{
36 if (p_out)
return p_out->availableForWrite();
37 return DEFAULT_BUFFER_SIZE;
40 size_t readBytes(uint8_t *data,
size_t len)
override {
41 size_t result = p_in->readBytes(data, len);
44 detect<int16_t>((int16_t*)data, len/
sizeof(int16_t));
50 detect<int32_t>((int32_t*)data, len/
sizeof(int32_t));
56 virtual size_t write(
const uint8_t *data,
size_t len)
override {
59 detect<int16_t>((int16_t*)data, len/
sizeof(int16_t));
65 detect<int32_t>((int32_t*)data, len/
sizeof(int32_t));
70 if (p_out!=
nullptr) result = p_out->write(data, len);
77 LOGE(
"Invalid channel: %d", channel);
87 float sum=0, sum_old=0;
90 void detect(T* samples,
size_t len) {
92 for (
int ch=0; ch<info.
channels; ch++){
93 freq[ch] = detectChannel(ch, samples, len);
98 float detectChannel(
int channel, T* samples,
size_t len) {
100 uint8_t pd_state = 0;
104 for(
int i = channel; i < len; i+=info.
channels) {
107 for(
int k = channel; k < len-i; k+=info.
channels) {
108 sum += (samples[k])*(samples[k+i]);
112 if (pd_state == 2 && (sum-sum_old) <=0) {
116 if (pd_state == 1 && (sum > thresh) && (sum-sum_old) > 0) {
125 return static_cast<float>(info.
sample_rate) / period;
151 return AudioStream::begin();
154 int available()
override{
155 if (p_in)
return p_in->available();
159 int availableForWrite()
override{
160 if (p_out)
return p_out->availableForWrite();
161 return DEFAULT_BUFFER_SIZE;
164 size_t readBytes(uint8_t *data,
size_t len)
override {
165 size_t result = p_in->readBytes(data, len);
168 detect<int16_t>((int16_t*)data, len/
sizeof(int16_t));
174 detect<int32_t>((int32_t*)data, len/
sizeof(int32_t));
180 virtual size_t write(
const uint8_t *data,
size_t len)
override {
183 detect<int16_t>((int16_t*)data, len/
sizeof(int16_t));
189 detect<int32_t>((int32_t*)data, len/
sizeof(int32_t));
194 if (p_out!=
nullptr) result = p_out->write(data, len);
201 LOGE(
"Invalid channel: %d", channel);
204 return freq[channel];
207 void setFrequencyCallback(
void (*callback)(
int channel,
float freq)){
213 Print *p_out=
nullptr;
214 Stream *p_in =
nullptr;
217 void (*notify)(
int channel,
float freq);
220 void detect(T* samples,
size_t len) {
222 for (
int ch=0; ch<info.
channels; ch++){
223 detectChannel(ch, samples, len);
228 void detectChannel(
int channel, T* samples,
size_t len) {
233 if (samples[i]<=0 && samples[i+info.
channels]>0) {
235 if (notify) notifyAudioChange(channel, freq[channel]);