2 #include "AudioTools/CoreAudio/AudioBasic/Collections.h"
3 #include "AudioTools/CoreAudio/AudioStreams.h"
4 #include "SoundGenerator.h"
5 #include "AudioEffect.h"
6 #if defined(USE_VARIANTS) && __cplusplus >= 201703L
29 effects.push_back(&effect);
35 effects.push_back(effect);
36 LOGI(
"addEffect -> Number of effects: %d", (
int)
size());
46 return effects.size();
53 for (
int j=0;j<
size();j++){
55 if (effects[j]->
id()==id){
58 LOGI(
"--> findEffect -> %d", effects[j]->
id());
86 template <
class GeneratorT>
96 p_generator = copy.p_generator;
97 for (
int j=0;j<copy.
size();j++){
100 LOGI(
"Number of effects %d -> %d", copy.
size(), this->size());
111 setInput(* (
new GeneratorT(input, channels, volume)));
112 owns_generator =
true;
118 if (owns_generator && p_generator!=
nullptr){
121 for (
int j=0;j<effects.
size();j++){
147 LOGI(
"addEffect -> Number of effects: %d",
size());
153 if (p_generator!=
nullptr){
154 sample = p_generator->readSample();
156 for (
int j=0; j<
size; j++){
157 sample = effects[j]->process(sample);
171 return effects.
size();
191 GeneratorT *p_generator=
nullptr;
192 bool owns_generator =
false;
244 void end()
override {
263 if (!active || p_io==
nullptr)
return 0;
264 size_t result_size = 0;
267 size_t result = p_io->readBytes((uint8_t*)data, len);
268 int frames = result /
sizeof(T) / info.
channels;
269 T* samples = (T*) data;
271 for (
int count=0;count<frames;count++){
274 for (
int ch=0;ch<info.
channels;ch++){
275 T sample = *samples++;
277 result_sample += sample / info.
channels;
281 for (
int j=0; j<
size(); j++){
282 result_sample = effects[j]->process(result_sample);
286 T* p_buffer = ((T*)data)+(count*info.
channels);
287 for (
int ch=0;ch<info.
channels;ch++){
288 p_buffer[ch] = result_sample;
289 result_size +=
sizeof(T);
299 size_t write(
const uint8_t *data,
size_t len)
override {
300 if (!active)
return 0;
302 assert(len % (
sizeof(T)*info.
channels)==0);
303 int frames = len /
sizeof(T) / info.
channels;
304 size_t result_size = 0;
307 for (
int j=0;j<frames;j++){
309 T* p_buffer = ((T*)data) + (j*info.
channels);
311 for (
int ch=0;ch<info.
channels;ch++){
312 sample += p_buffer[ch] / info.
channels;
313 result_size +=
sizeof(T);
317 for (
int j=0; j<
size(); j++){
318 sample = effects[j]->process(sample);
322 for (
int ch=0;ch<info.
channels;ch++){
324 p_io->write((uint8_t*)&sample,
sizeof(T));
326 }
else if (p_print!=
nullptr){
327 p_print->write((uint8_t*)&sample,
sizeof(T));
334 int available()
override {
335 if (p_io==
nullptr)
return 0;
336 return p_io->available();
339 int availableForWrite()
override {
340 if (p_print!=
nullptr)
return p_print->availableForWrite();
341 if (p_io!=
nullptr)
return p_io->availableForWrite();
355 LOGI(
"addEffect -> Number of effects: %d", (
int)
size());
366 return effects.
size();
383 Print *p_print=
nullptr;
386 #if defined(USE_VARIANTS) && __cplusplus >= 201703L || defined(DOXYGEN)
426 variant.emplace<0>();
429 variant.emplace<1>();
432 variant.emplace<2>();
438 std::visit( [
this](
auto&& e) {
return e.setOutput(*p_print);}, variant );
439 std::visit( [
this](
auto&& e) {
return e.setInput(*p_io);}, variant );
440 return std::visit( [cfg](
auto&& e) {
return e.begin(info);}, variant );
443 void end()
override {
444 std::visit( [](
auto&& e) {e.end();}, variant );
447 void setInput(
Stream &io){
451 void setStream(
Stream &io){
455 void setOutput(
Stream &io){
459 void setOutput(
Print &print){
467 size_t readBytes(uint8_t *data,
size_t len)
override {
468 return std::visit( [data, len](
auto&& e) {
return e.readBytes(data, len);}, variant );
475 size_t write(
const uint8_t *data,
size_t len)
override {
476 return std::visit( [data, len](
auto&& e) {
return e.write(data, len);}, variant );
479 int available()
override {
480 return std::visit( [](
auto&& e) {
return e.available();}, variant );
483 int availableForWrite()
override {
484 return std::visit( [](
auto&& e) {
return e.availableForWrite();}, variant );
494 std::visit( [effect](
auto&& e) {e.addEffect(effect);}, variant );
499 std::visit( [](
auto&& e) {e.clear();}, variant );
504 return std::visit( [](
auto&& e) {
return e.size();}, variant );
509 return std::visit( [idx](
auto&& e) {
return e[idx];}, variant );
514 return std::visit( [
id](
auto&& e) {
return e.findEffect(
id);}, variant );
520 Print *p_print=
nullptr;