/*
// Created by bruno on 16.2.2025.
*/

#ifndef RISCB_AUDIO_H
#define RISCB_AUDIO_H

#include <SDL2/SDL.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define SAMPLE_RATE 44100
#define NUM_SYNTH_VOICES 3
#define PCM_BUFFER_SIZE 4096

typedef enum {
    WAVE_SINE,
    WAVE_SQUARE,
    WAVE_SAWTOOTH,
    WAVE_TRIANGLE,
    WAVE_NOISE
} Waveform;

typedef struct {
    uint8_t volume;
    uint16_t frequency;
    uint8_t phase;
    Waveform waveform;
} SynthVoice;

typedef struct {
    uint8_t volume;
    uint8_t upsample_factor;
    uint8_t upsample_count;
    uint16_t buffer[PCM_BUFFER_SIZE];
    size_t head, tail;
} PcmVoice;

typedef struct {
    SynthVoice synthVoices[NUM_SYNTH_VOICES];
    PcmVoice pcmVoice;
} AudioData;

int pcm_buffer_empty(PcmVoice *pcm);

int pcm_buffer_full(PcmVoice *pcm);

void pcm_buffer_push(PcmVoice *pcm, uint16_t sample);

uint16_t pcm_buffer_pop(PcmVoice *pcm);

void audio_callback(void *userdata, Uint8 *stream, int len);

#endif //RISCB_AUDIO_H
