typedef struct {
qoa_desc info;
FILE *file; unsigned char *file_data; unsigned int file_data_size; unsigned int file_data_offset;
unsigned int first_frame_pos; unsigned int sample_position;
unsigned char *buffer; unsigned int buffer_len;
short *sample_data; unsigned int sample_data_len; unsigned int sample_data_pos;
} qoaplay_desc;
#if defined(__cplusplus)
extern "C" { #endif
qoaplay_desc *qoaplay_open(const char *path);
qoaplay_desc *qoaplay_open_memory(const unsigned char *data, int data_size);
void qoaplay_close(qoaplay_desc *qoa_ctx);
void qoaplay_rewind(qoaplay_desc *qoa_ctx);
void qoaplay_seek_frame(qoaplay_desc *qoa_ctx, int frame);
unsigned int qoaplay_decode(qoaplay_desc *qoa_ctx, float *sample_data, int num_samples);
unsigned int qoaplay_decode_frame(qoaplay_desc *qoa_ctx);
double qoaplay_get_duration(qoaplay_desc *qoa_ctx);
double qoaplay_get_time(qoaplay_desc *qoa_ctx);
int qoaplay_get_frame(qoaplay_desc *qoa_ctx);
#if defined(__cplusplus)
} #endif
qoaplay_desc *qoaplay_open(const char *path)
{
FILE *file = fopen(path, "rb");
if (!file) return NULL;
unsigned char header[QOA_MIN_FILESIZE];
int read = fread(header, QOA_MIN_FILESIZE, 1, file);
if (!read) return NULL;
qoa_desc qoa;
unsigned int first_frame_pos = qoa_decode_header(header, QOA_MIN_FILESIZE, &qoa);
if (!first_frame_pos) return NULL;
fseek(file, first_frame_pos, SEEK_SET);
unsigned int buffer_size = qoa_max_frame_size(&qoa);
unsigned int sample_data_size = qoa.channels*QOA_FRAME_LEN*sizeof(short)*2;
qoaplay_desc *qoa_ctx = QOA_MALLOC(sizeof(qoaplay_desc) + buffer_size + sample_data_size);
memset(qoa_ctx, 0, sizeof(qoaplay_desc));
qoa_ctx->file = file;
qoa_ctx->file_data = NULL;
qoa_ctx->file_data_size = 0;
qoa_ctx->file_data_offset = 0;
qoa_ctx->first_frame_pos = first_frame_pos;
qoa_ctx->buffer = ((unsigned char *)qoa_ctx) + sizeof(qoaplay_desc);
qoa_ctx->sample_data = (short *)(((unsigned char *)qoa_ctx) + sizeof(qoaplay_desc) + buffer_size);
qoa_ctx->info.channels = qoa.channels;
qoa_ctx->info.samplerate = qoa.samplerate;
qoa_ctx->info.samples = qoa.samples;
return qoa_ctx;
}
qoaplay_desc *qoaplay_open_memory(const unsigned char *data, int data_size)
{
unsigned char header[QOA_MIN_FILESIZE];
memcpy(header, data, QOA_MIN_FILESIZE);
qoa_desc qoa;
unsigned int first_frame_pos = qoa_decode_header(header, QOA_MIN_FILESIZE, &qoa);
if (!first_frame_pos) return NULL;
unsigned int buffer_size = qoa_max_frame_size(&qoa);
unsigned int sample_data_size = qoa.channels*QOA_FRAME_LEN*sizeof(short)*2;
qoaplay_desc *qoa_ctx = QOA_MALLOC(sizeof(qoaplay_desc) + buffer_size + sample_data_size);
memset(qoa_ctx, 0, sizeof(qoaplay_desc));
qoa_ctx->file = NULL;
qoa_ctx->file_data = (unsigned char *)QOA_MALLOC(data_size);
memcpy(qoa_ctx->file_data, data, data_size);
qoa_ctx->file_data_size = data_size;
qoa_ctx->file_data_offset = 0;
qoa_ctx->first_frame_pos = first_frame_pos;
qoa_ctx->buffer = ((unsigned char *)qoa_ctx) + sizeof(qoaplay_desc);
qoa_ctx->sample_data = (short *)(((unsigned char *)qoa_ctx) + sizeof(qoaplay_desc) + buffer_size);
qoa_ctx->info.channels = qoa.channels;
qoa_ctx->info.samplerate = qoa.samplerate;
qoa_ctx->info.samples = qoa.samples;
return qoa_ctx;
}
void qoaplay_close(qoaplay_desc *qoa_ctx)
{
if (qoa_ctx->file) fclose(qoa_ctx->file);
if ((qoa_ctx->file_data) && (qoa_ctx->file_data_size > 0))
{
QOA_FREE(qoa_ctx->file_data);
qoa_ctx->file_data_size = 0;
}
QOA_FREE(qoa_ctx);
}
unsigned int qoaplay_decode_frame(qoaplay_desc *qoa_ctx)
{
if (qoa_ctx->file) qoa_ctx->buffer_len = fread(qoa_ctx->buffer, 1, qoa_max_frame_size(&qoa_ctx->info), qoa_ctx->file);
else
{
qoa_ctx->buffer_len = qoa_max_frame_size(&qoa_ctx->info);
memcpy(qoa_ctx->buffer, qoa_ctx->file_data + qoa_ctx->file_data_offset, qoa_ctx->buffer_len);
qoa_ctx->file_data_offset += qoa_ctx->buffer_len;
}
unsigned int frame_len;
qoa_decode_frame(qoa_ctx->buffer, qoa_ctx->buffer_len, &qoa_ctx->info, qoa_ctx->sample_data, &frame_len);
qoa_ctx->sample_data_pos = 0;
qoa_ctx->sample_data_len = frame_len;
return frame_len;
}
void qoaplay_rewind(qoaplay_desc *qoa_ctx)
{
if (qoa_ctx->file) fseek(qoa_ctx->file, qoa_ctx->first_frame_pos, SEEK_SET);
else qoa_ctx->file_data_offset = 0;
qoa_ctx->sample_position = 0;
qoa_ctx->sample_data_len = 0;
qoa_ctx->sample_data_pos = 0;
}
unsigned int qoaplay_decode(qoaplay_desc *qoa_ctx, float *sample_data, int num_samples)
{
int src_index = qoa_ctx->sample_data_pos*qoa_ctx->info.channels;
int dst_index = 0;
for (int i = 0; i < num_samples; i++)
{
if (qoa_ctx->sample_data_len - qoa_ctx->sample_data_pos == 0)
{
if (!qoaplay_decode_frame(qoa_ctx))
{
qoaplay_rewind(qoa_ctx);
qoaplay_decode_frame(qoa_ctx);
}
src_index = 0;
}
for (int c = 0; c < qoa_ctx->info.channels; c++)
{
sample_data[dst_index++] = qoa_ctx->sample_data[src_index++]/32768.0;
}
qoa_ctx->sample_data_pos++;
qoa_ctx->sample_position++;
}
return num_samples;
}
double qoaplay_get_duration(qoaplay_desc *qoa_ctx)
{
return (double)qoa_ctx->info.samples/(double)qoa_ctx->info.samplerate;
}
double qoaplay_get_time(qoaplay_desc *qoa_ctx)
{
return (double)qoa_ctx->sample_position/(double)qoa_ctx->info.samplerate;
}
int qoaplay_get_frame(qoaplay_desc *qoa_ctx)
{
return qoa_ctx->sample_position/QOA_FRAME_LEN;
}
void qoaplay_seek_frame(qoaplay_desc *qoa_ctx, int frame)
{
if (frame < 0) frame = 0;
if (frame > qoa_ctx->info.samples/QOA_FRAME_LEN) frame = qoa_ctx->info.samples/QOA_FRAME_LEN;
qoa_ctx->sample_position = frame*QOA_FRAME_LEN;
qoa_ctx->sample_data_len = 0;
qoa_ctx->sample_data_pos = 0;
unsigned int offset = qoa_ctx->first_frame_pos + frame*qoa_max_frame_size(&qoa_ctx->info);
if (qoa_ctx->file) fseek(qoa_ctx->file, offset, SEEK_SET);
else qoa_ctx->file_data_offset = offset;
}