#ifndef TWN_AUDIO_C_H
#define TWN_AUDIO_C_H

#include <SDL2/SDL_audio.h>

#define STB_VORBIS_HEADER_ONLY
#include <stb_vorbis.c>
#include <xm.h>

#include <stdbool.h>
#include <stdint.h>


#define AUDIO_FREQUENCY 48000

/* TODO: specify which PCM formats are usable with WAV */
/* TODO: specify limitations of libxm */
/* TODO: specify limitations of stb_vorbis */
typedef enum AudioFileType {
    AUDIO_FILE_TYPE_OGG,
    AUDIO_FILE_TYPE_WAV,
    AUDIO_FILE_TYPE_XM,
    AUDIO_FILE_TYPE_COUNT,
    AUDIO_FILE_TYPE_UNKNOWN,
} AudioFileType;


union AudioContext {
    struct {
        stb_vorbis *handle;
        unsigned char *data;
        int frequency;
        uint8_t channel_count;
    } vorbis;

    struct {
        void *samples;
        SDL_AudioSpec spec;
        size_t position;
    } wav;

    struct {
        xm_context_t *handle;
    } xm;
};


typedef struct AudioChannel {
    AudioFileType file_type;
    union AudioContext context; /* interpreted by `file_type` value */
    char *path;
    bool repeat;
    float volume;
    float panning;
    bool finished;
} AudioChannel;


typedef struct AudioChannelItem {
    char *key;
    struct AudioChannel value;
} AudioChannelItem;


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

#endif
