#include "visualize.h"
#include "core/core.h"

MU_Context *Mu;
int Audio_Start;
int Audio_I;
float T;
void Audio_Callback(MU_Context *mu, uint16_t *buffer, uint32_t samples_to_fill) {
    for (uint32_t i = 0; i < samples_to_fill; i += 2) {
        float s = sinf(T * 0.3157f);
        float ss = sinf(T * (0.3157f + 0.02f));
        float comb = s + ss;
        float value = comb * 500.0f;
        buffer[i] = (int16_t)value;
        buffer[i + 1] = (int16_t)value;
        T += 0.1f;

        Audio_Start += 1;
        if (Audio_I < 48000) {
            Audio_I += 1;
            VIS_PlotKind(VIS_PlotKind_Scatter);
            VIS_Series(0);
            VIS_Point((float)Audio_I, s);
            VIS_Series(1);
            VIS_Point((float)Audio_I, ss);
            VIS_Series(2);
            VIS_Point((float)Audio_I, comb);
        }
        else {
            buffer[i] = 0;
            buffer[i + 1] = 0;
            MU_Quit(Mu);
        }
    }
}

int main() {
    VIS_Clear();
    MU_Params params = {};
    params.sound_callback = Audio_Callback;
    Mu = MU_Start(params);

    while (MU_Update(Mu)) {
        if (Mu->window->key[MU_KEY_ESCAPE].down) {
            MU_Quit(Mu);
        }
    }
}