#include "audio_viewer.h"
#include <SDL2/SDL.h>

#define NUM_SAMPLES 256
#define MAX_VOLUME  256
namespace audio_viewer
{
    SDL_Window *win = nullptr;
    SDL_Renderer *ren = nullptr;
    //SDL_Texture *tex = nullptr;
    uint8_t buffer[NUM_SAMPLES];
    uint8_t pos = 0;
    uint8_t screen[NUM_SAMPLES*MAX_VOLUME];

    void init()
    {
        win = SDL_CreateWindow("Audio viewer", 1, 1, 256, 256, SDL_WINDOW_SHOWN);
        ren = SDL_CreateRenderer(win, -1, 0);
        //tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 256, 256);
        for (int i=0; i<NUM_SAMPLES; ++i) buffer[i] = 0;
    }

    void addsample(uint8_t sample)
    {
        buffer[pos] = sample;
        pos = (pos+1)%NUM_SAMPLES;
    }

    void refresh()
    {
        SDL_SetRenderDrawColor(ren, 0, 0, 0, 0);
        SDL_RenderClear(ren);
        SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
        for (int i=0; i<NUM_SAMPLES;++i) {
            SDL_RenderDrawLine(ren, i, buffer[(pos+i)%NUM_SAMPLES], i+1, buffer[(pos+i+1)%NUM_SAMPLES]);
        }
        SDL_RenderPresent(ren);
    }
}
