114 lines
3.5 KiB
C
114 lines
3.5 KiB
C
#include <malloc.h>
|
|
|
|
#include "fsl_debug_console.h"
|
|
|
|
/* FS */
|
|
/* clang-format off */
|
|
#include "ff.h"
|
|
#include "diskio.h"
|
|
#include "fsl_debug_console.h"
|
|
#include "fsl_sd.h"
|
|
#include "fsl_sd_disk.h"
|
|
#include "sdmmc_config.h"
|
|
/* clang-format on */
|
|
|
|
#include "app_flac_callbacks.h"
|
|
|
|
/**
|
|
* Initialize client data, while opening the file.
|
|
* @param client Pointer to the client struct
|
|
* @param filename File name to be opened
|
|
* @return 0 for success, negative for error.
|
|
*/
|
|
int app_flac_init(app_flac_client_t *client, const char *filename) {
|
|
if (f_open(&client->fd, filename, FA_READ) != FR_OK) {
|
|
return -1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
FLAC__StreamDecoderReadStatus app_flac_read(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes,
|
|
void *client_data) {
|
|
app_flac_client_t *client = client_data;
|
|
|
|
if (*bytes > 0) {
|
|
if (f_read(&client->fd, buffer, *bytes, bytes) != FR_OK) {
|
|
/* Unexpected error */
|
|
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
|
|
}
|
|
|
|
if (*bytes == 0) {
|
|
/* EOF reached */
|
|
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
|
|
}
|
|
|
|
/* More data available */
|
|
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
|
|
}
|
|
|
|
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
|
|
}
|
|
|
|
FLAC__StreamDecoderSeekStatus app_flac_seek(const FLAC__StreamDecoder *decoder, FLAC__uint64 absolute_byte_offset,
|
|
void *client_data) {
|
|
app_flac_client_t *client = client_data;
|
|
|
|
if (f_lseek(&client->fd, (FSIZE_t)absolute_byte_offset) != FR_OK) {
|
|
return FLAC__STREAM_DECODER_SEEK_STATUS_ERROR;
|
|
}
|
|
|
|
return FLAC__STREAM_DECODER_SEEK_STATUS_OK;
|
|
}
|
|
|
|
FLAC__StreamDecoderTellStatus app_flac_tell(const FLAC__StreamDecoder *decoder, FLAC__uint64 *absolute_byte_offset,
|
|
void *client_data) {
|
|
app_flac_client_t *client = client_data;
|
|
|
|
*absolute_byte_offset = (FLAC__uint64)f_tell(&client->fd);
|
|
|
|
return FLAC__STREAM_DECODER_TELL_STATUS_OK;
|
|
}
|
|
|
|
FLAC__StreamDecoderLengthStatus app_flac_length(const FLAC__StreamDecoder *decoder, FLAC__uint64 *stream_length,
|
|
void *client_data) {
|
|
app_flac_client_t *client = client_data;
|
|
|
|
*stream_length = (FLAC__uint64)f_size(&client->fd);
|
|
|
|
return FLAC__STREAM_DECODER_LENGTH_STATUS_OK;
|
|
}
|
|
|
|
FLAC__bool app_flac_eof(const FLAC__StreamDecoder *decoder, void *client_data) {
|
|
app_flac_client_t *client = client_data;
|
|
|
|
if (f_eof(&client->fd)) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
FLAC__StreamDecoderWriteStatus app_flac_write(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame,
|
|
const FLAC__int32 *const buffer[], void *client_data) {
|
|
app_flac_client_t *client = client_data;
|
|
|
|
struct mallinfo mi = mallinfo();
|
|
|
|
PRINTF("FP: %p, Decoded FLAC block size: %d, heap: %d\r\n", &client->fd, frame->header.blocksize, mi.uordblks);
|
|
|
|
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
|
|
}
|
|
|
|
void app_flac_meta(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data) {
|
|
app_flac_client_t *client = client_data;
|
|
|
|
PRINTF("FP: %p, meta type: %d\r\n", &client->fd, metadata->type);
|
|
}
|
|
|
|
void app_flac_error(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data) {
|
|
app_flac_client_t *client = client_data;
|
|
|
|
PRINTF("FP: %p, error: %d.\r\n", client, status);
|
|
}
|