/* mp3_decode.h */

#ifndef __MP3_DECODE_H__
#define __MP3_DECODE_H__

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

/* the len of pcm buffer must be an integral multiple of MP3_DECODE_FRAME_SAMPLES. */
#define MP3_DECODE_FRAME_SAMPLES    (1152 * 2) /* 1152 samples per channels on one frame, usually the channels is 2, all samples is 1152 * 2. */
/* the mp3_buf size must be more than MP3_DECODE_BUF_MIN_SIZE. */
#define MP3_DECODE_BUF_MIN_SIZE     7760

/* mp3 decode interface. */
typedef struct
{
    /* buffer. */
    uint8_t * mp3_buf;
    uint32_t  mp3_buf_size;

    /* file system interface. */
    bool     (*file_open) (char * file_name);
    uint32_t (*file_read) (uint8_t *buf, uint32_t buf_size);
    void     (*file_close)(void);
} mp3_decode_if_t;

/* mp3 decode object. */
typedef struct
{
    /* interface. */
    mp3_decode_if_t * interface;

    /* decoder handler. */
    void            * dec;

    /* buf handler. */
    uint32_t          buf_offset;
    uint32_t          buf_remain;

    /* MP3 handler. */
    uint32_t          sample_rate;
} mp3_decode_obj_t;

/* init mp3 decode. */
void     mp3_decode_init (mp3_decode_obj_t * obj, mp3_decode_if_t * interface);
/* open the mp3 file. */
bool     mp3_decode_open (mp3_decode_obj_t * obj, char * file_name);
/* decode the mp3 file, the pcm data will be filled in pcm_buf. */
uint32_t mp3_decode      (mp3_decode_obj_t * obj, int16_t * pcm_buf, uint32_t pcm_samples);
/* close file. */
void     mp3_decode_close(mp3_decode_obj_t * obj);
/* get the mp3 sample rate, this function can only be used after using mp3_decode() once. */
uint32_t mp3_decode_get_sample_rate(mp3_decode_obj_t * obj);

#endif /* __MP3_DECODE_H__. */
