Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add operators for combining oscillators together #90

Open
sofian opened this issue Jan 28, 2024 · 2 comments
Open

Add operators for combining oscillators together #90

sofian opened this issue Jan 28, 2024 · 2 comments

Comments

@sofian
Copy link
Collaborator

sofian commented Jan 28, 2024

Since by default the oscillators are set in the [0, 1] range the usual operations of addition will not work as planned. So we should provide a way to easily add oscillators.

Notice though that an average of oscillators would do the trick; furthermore it will ensure result stays within [0, 1] so perhaps that's all we would need. Something to think about...

More broadly we can think about ways to combine multiple sources together (composition?)

  • add
  • subtract
  • min
  • max
  • average
  • not
  • and
  • or
  • xor
@sofian
Copy link
Collaborator Author

sofian commented Jan 14, 2025

In an effort to try to address this issue I tried implementing a solution using variadic functions. It is far from ideal and also, well, it does not work. The problem is that in order to call the superclass we need to use some kind of casting and I don't think it is possible without using pointers. So we have to come up with another solution.

void _addWavesHelper(int count, float& sum, float& sumAmps, ...) {
    sum = 0;  // Assuming AbstractWave has a default constructor
    sumAmps = 0;

    if (count <= 0) return;  // Handle edge case where no waves are provided

    va_list args;
    va_start(args, sumAmps);

    for (int i = 0; i < count; ++i) {
        Serial.println(i);
        AbstractWave& wave = va_arg(args, AbstractWave);
        sum += wave.get();  // <------------------- THIS DOES NOT WORK AND RESULTS IN CORE DUMP
        sumAmps += wave.amplitude();
    }

    va_end(args);
}

float addWaves(int count, ...) {
  float sum;
  float sumAmps;
  va_list args;
  va_start(args, count);
  _addWavesHelper(count, sum, sumAmps, args);
  va_end(args);
  return sum / count;
}

float addWaves01(int count, ...) {
  float sum;
  float sumAmps;
  va_list args;
  va_start(args, count);
  _addWavesHelper(count, sum, sumAmps, args);
  va_end(args);
  return sum / sumAmps;
}

float addWavesAmplify(int count, float amplitude, ...) {
  va_list args;
  va_start(args, count);
  float result = mapFrom01(addWaves01(count, args), (1 - amplitude) / 2, (1 + amplitude)/2);
  va_end(args);
  return result;
}

@sofian
Copy link
Collaborator Author

sofian commented Jan 15, 2025

We could possibly write such operators for a bunch of floats but not for objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant