| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
typedef struct { |
| 38 |
qoa_desc info; |
| 39 |
|
| 40 |
FILE *file; |
| 41 |
unsigned char *file_data; |
| 42 |
unsigned int file_data_size; |
| 43 |
unsigned int file_data_offset; |
| 44 |
|
| 45 |
unsigned int first_frame_pos; |
| 46 |
unsigned int sample_position; |
| 47 |
|
| 48 |
unsigned char *buffer; |
| 49 |
|
| 50 |
short *sample_data; |
| 51 |
unsigned int sample_data_len; |
| 52 |
unsigned int sample_data_pos; |
| 53 |
|
| 54 |
} qoaplay_desc; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#if defined(__cplusplus) |
| 61 |
extern "C" { |
| 62 |
#endif |
| 63 |
|
| 64 |
qoaplay_desc *qoaplay_open(const char *path); |
| 65 |
qoaplay_desc *qoaplay_open_memory(const unsigned char *data, int data_size); |
| 66 |
void qoaplay_close(qoaplay_desc *qoa_ctx); |
| 67 |
|
| 68 |
void qoaplay_rewind(qoaplay_desc *qoa_ctx); |
| 69 |
void qoaplay_seek_frame(qoaplay_desc *qoa_ctx, int frame); |
| 70 |
unsigned int qoaplay_decode(qoaplay_desc *qoa_ctx, float *sample_data, int num_samples); |
| 71 |
unsigned int qoaplay_decode_frame(qoaplay_desc *qoa_ctx); |
| 72 |
double qoaplay_get_duration(qoaplay_desc *qoa_ctx); |
| 73 |
double qoaplay_get_time(qoaplay_desc *qoa_ctx); |
| 74 |
int qoaplay_get_frame(qoaplay_desc *qoa_ctx); |
| 75 |
|
| 76 |
#if defined(__cplusplus) |
| 77 |
} |
| 78 |
#endif |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
qoaplay_desc *qoaplay_open(const char *path) |
| 86 |
{ |
| 87 |
FILE *file = fopen(path, "rb"); |
| 88 |
if (!file) return NULL; |
| 89 |
|
| 90 |
|
| 91 |
unsigned char header[QOA_MIN_FILESIZE]; |
| 92 |
int read = fread(header, QOA_MIN_FILESIZE, 1, file); |
| 93 |
if (!read) return NULL; |
| 94 |
|
| 95 |
qoa_desc qoa; |
| 96 |
unsigned int first_frame_pos = qoa_decode_header(header, QOA_MIN_FILESIZE, &qoa); |
| 97 |
if (!first_frame_pos) return NULL; |
| 98 |
|
| 99 |
|
| 100 |
fseek(file, first_frame_pos, SEEK_SET); |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
unsigned int buffer_size = qoa_max_frame_size(&qoa); |
| 106 |
unsigned int sample_data_size = qoa.channels*QOA_FRAME_LEN*sizeof(short)*2; |
| 107 |
qoaplay_desc *qoa_ctx = (qoaplay_desc *)QOA_MALLOC(sizeof(qoaplay_desc) + buffer_size + sample_data_size); |
| 108 |
memset(qoa_ctx, 0, sizeof(qoaplay_desc)); |
| 109 |
|
| 110 |
qoa_ctx->file = file; |
| 111 |
qoa_ctx->file_data = NULL; |
| 112 |
qoa_ctx->file_data_size = 0; |
| 113 |
qoa_ctx->file_data_offset = first_frame_pos; |
| 114 |
qoa_ctx->first_frame_pos = first_frame_pos; |
| 115 |
|
| 116 |
|
| 117 |
qoa_ctx->buffer = ((unsigned char *)qoa_ctx) + sizeof(qoaplay_desc); |
| 118 |
qoa_ctx->sample_data = (short *)(((unsigned char *)qoa_ctx) + sizeof(qoaplay_desc) + buffer_size); |
| 119 |
|
| 120 |
qoa_ctx->info.channels = qoa.channels; |
| 121 |
qoa_ctx->info.samplerate = qoa.samplerate; |
| 122 |
qoa_ctx->info.samples = qoa.samples; |
| 123 |
|
| 124 |
return qoa_ctx; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
qoaplay_desc *qoaplay_open_memory(const unsigned char *data, int data_size) |
| 129 |
{ |
| 130 |
qoa_desc qoa; |
| 131 |
if (data_size < QOA_MIN_FILESIZE) return NULL; |
| 132 |
unsigned int first_frame_pos = qoa_decode_header(data, QOA_MIN_FILESIZE, &qoa); |
| 133 |
if (!first_frame_pos) return NULL; |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
unsigned int sample_data_size = qoa.channels*QOA_FRAME_LEN*sizeof(short)*2; |
| 139 |
qoaplay_desc *qoa_ctx = (qoaplay_desc *)QOA_MALLOC(sizeof(qoaplay_desc) + sample_data_size + data_size); |
| 140 |
memset(qoa_ctx, 0, sizeof(qoaplay_desc)); |
| 141 |
|
| 142 |
qoa_ctx->file = NULL; |
| 143 |
|
| 144 |
|
| 145 |
qoa_ctx->file_data = (((unsigned char *)qoa_ctx) + sizeof(qoaplay_desc) + sample_data_size); |
| 146 |
memcpy(qoa_ctx->file_data, data, data_size); |
| 147 |
qoa_ctx->file_data_size = data_size; |
| 148 |
qoa_ctx->file_data_offset = first_frame_pos; |
| 149 |
qoa_ctx->first_frame_pos = first_frame_pos; |
| 150 |
|
| 151 |
|
| 152 |
qoa_ctx->buffer = NULL; |
| 153 |
qoa_ctx->sample_data = (short *)(((unsigned char *)qoa_ctx) + sizeof(qoaplay_desc)); |
| 154 |
|
| 155 |
qoa_ctx->info.channels = qoa.channels; |
| 156 |
qoa_ctx->info.samplerate = qoa.samplerate; |
| 157 |
qoa_ctx->info.samples = qoa.samples; |
| 158 |
|
| 159 |
return qoa_ctx; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
void qoaplay_close(qoaplay_desc *qoa_ctx) |
| 164 |
{ |
| 165 |
if (qoa_ctx->file) fclose(qoa_ctx->file); |
| 166 |
|
| 167 |
qoa_ctx->file_data_size = 0; |
| 168 |
|
| 169 |
QOA_FREE(qoa_ctx); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
unsigned int qoaplay_decode_frame(qoaplay_desc *qoa_ctx) |
| 174 |
{ |
| 175 |
unsigned char *buffer; |
| 176 |
unsigned int buffer_len; |
| 177 |
|
| 178 |
if (qoa_ctx->file) |
| 179 |
{ |
| 180 |
buffer = qoa_ctx->buffer; |
| 181 |
buffer_len = fread(buffer, 1, qoa_max_frame_size(&qoa_ctx->info), qoa_ctx->file); |
| 182 |
} |
| 183 |
else |
| 184 |
{ |
| 185 |
buffer = qoa_ctx->file_data + qoa_ctx->file_data_offset; |
| 186 |
buffer_len = qoa_max_frame_size(&qoa_ctx->info); |
| 187 |
qoa_ctx->file_data_offset += buffer_len; |
| 188 |
} |
| 189 |
|
| 190 |
unsigned int frame_len; |
| 191 |
qoa_decode_frame(buffer, buffer_len, &qoa_ctx->info, qoa_ctx->sample_data, &frame_len); |
| 192 |
qoa_ctx->sample_data_pos = 0; |
| 193 |
qoa_ctx->sample_data_len = frame_len; |
| 194 |
|
| 195 |
return frame_len; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
void qoaplay_rewind(qoaplay_desc *qoa_ctx) |
| 200 |
{ |
| 201 |
if (qoa_ctx->file) fseek(qoa_ctx->file, qoa_ctx->first_frame_pos, SEEK_SET); |
| 202 |
else qoa_ctx->file_data_offset = qoa_ctx->first_frame_pos; |
| 203 |
|
| 204 |
qoa_ctx->sample_position = 0; |
| 205 |
qoa_ctx->sample_data_len = 0; |
| 206 |
qoa_ctx->sample_data_pos = 0; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
unsigned int qoaplay_decode(qoaplay_desc *qoa_ctx, float *sample_data, int num_samples) |
| 211 |
{ |
| 212 |
int src_index = qoa_ctx->sample_data_pos*qoa_ctx->info.channels; |
| 213 |
int dst_index = 0; |
| 214 |
|
| 215 |
for (int i = 0; i < num_samples; i++) |
| 216 |
{ |
| 217 |
|
| 218 |
if (qoa_ctx->sample_data_len - qoa_ctx->sample_data_pos == 0) |
| 219 |
{ |
| 220 |
if (!qoaplay_decode_frame(qoa_ctx)) |
| 221 |
{ |
| 222 |
|
| 223 |
qoaplay_rewind(qoa_ctx); |
| 224 |
qoaplay_decode_frame(qoa_ctx); |
| 225 |
} |
| 226 |
|
| 227 |
src_index = 0; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
for (int c = 0; c < qoa_ctx->info.channels; c++) |
| 232 |
{ |
| 233 |
sample_data[dst_index++] = qoa_ctx->sample_data[src_index++]/32768.0; |
| 234 |
} |
| 235 |
|
| 236 |
qoa_ctx->sample_data_pos++; |
| 237 |
qoa_ctx->sample_position++; |
| 238 |
} |
| 239 |
|
| 240 |
return num_samples; |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
double qoaplay_get_duration(qoaplay_desc *qoa_ctx) |
| 245 |
{ |
| 246 |
return (double)qoa_ctx->info.samples/(double)qoa_ctx->info.samplerate; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
double qoaplay_get_time(qoaplay_desc *qoa_ctx) |
| 251 |
{ |
| 252 |
return (double)qoa_ctx->sample_position/(double)qoa_ctx->info.samplerate; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
int qoaplay_get_frame(qoaplay_desc *qoa_ctx) |
| 257 |
{ |
| 258 |
return qoa_ctx->sample_position/QOA_FRAME_LEN; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
void qoaplay_seek_frame(qoaplay_desc *qoa_ctx, int frame) |
| 263 |
{ |
| 264 |
if (frame < 0) frame = 0; |
| 265 |
|
| 266 |
if (frame > qoa_ctx->info.samples/QOA_FRAME_LEN) frame = qoa_ctx->info.samples/QOA_FRAME_LEN; |
| 267 |
|
| 268 |
qoa_ctx->sample_position = frame*QOA_FRAME_LEN; |
| 269 |
qoa_ctx->sample_data_len = 0; |
| 270 |
qoa_ctx->sample_data_pos = 0; |
| 271 |
|
| 272 |
unsigned int offset = qoa_ctx->first_frame_pos + frame*qoa_max_frame_size(&qoa_ctx->info); |
| 273 |
|
| 274 |
if (qoa_ctx->file) fseek(qoa_ctx->file, offset, SEEK_SET); |
| 275 |
else qoa_ctx->file_data_offset = offset; |
| 276 |
} |