Debian Codegraph @

src/external/qoaplay.c

raw symbols SCIP
1 /*******************************************************************************************
2 *
3 * qoaplay - QOA stream playing helper functions
4 *
5 * qoaplay is a tiny abstraction to read and decode a QOA file "on the fly".
6 * It reads and decodes one frame at a time with minimal memory requirements.
7 * qoaplay also provides some functions to seek to a specific frame.
8 *
9 * LICENSE: MIT License
10 *
11 * Copyright (c) 2023 Dominic Szablewski (@phoboslab), reviewed by Ramon Santamaria (@raysan5)
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this software and associated documentation files (the "Software"), to deal
15 * in the Software without restriction, including without limitation the rights
16 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 * copies of the Software, and to permit persons to whom the Software is
18 * furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in all
21 * copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * SOFTWARE.
30 *
31 **********************************************************************************************/
32
33 //----------------------------------------------------------------------------------
34 // Types and Structures Definition
35 //----------------------------------------------------------------------------------
36 // QOA streaming data descriptor
37 typedef struct {
38 qoa_desc info; // QOA descriptor data
39
40 FILE *file; // QOA file to read, if NULL, using memory buffer -> file_data
41 unsigned char *file_data; // QOA file data on memory
42 unsigned int file_data_size; // QOA file data on memory size
43 unsigned int file_data_offset; // QOA file data on memory offset for next read
44
45 unsigned int first_frame_pos; // First frame position (after QOA header, required for offset)
46 unsigned int sample_position; // Current streaming sample position
47
48 unsigned char *buffer; // Buffer used to read samples from file/memory (used on decoding)
49
50 short *sample_data; // Sample data decoded
51 unsigned int sample_data_len; // Sample data decoded length
52 unsigned int sample_data_pos; // Sample data decoded position
53
54 } qoaplay_desc;
55
56 //----------------------------------------------------------------------------------
57 // Module Functions Declaration
58 //----------------------------------------------------------------------------------
59
60 #if defined(__cplusplus)
61 extern "C" { // Prevents name mangling of functions
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 } // Prevents name mangling of functions
78 #endif
79
80 //----------------------------------------------------------------------------------
81 // Module Functions Definition
82 //----------------------------------------------------------------------------------
83
84 // Open QOA file, keep FILE pointer to keep reading from file
85 qoaplay_desc *qoaplay_open(const char *path)
86 {
87 FILE *file = fopen(path, "rb");
88 if (!file) return NULL;
89
90 // Read and decode the file header
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 // Rewind the file back to beginning of the first frame
100 fseek(file, first_frame_pos, SEEK_SET);
101
102 // Allocate one chunk of memory for the qoaplay_desc struct
103 // + the sample data for one frame
104 // + a buffer to hold one frame of encoded data
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 // Setup data pointers to previously allocated data
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 // Open QOA file from memory, no FILE pointer required
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 // Allocate one chunk of memory for the qoaplay_desc struct
136 // + the sample data for one frame
137 // + a buffer to hold one frame of encoded data
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 // Keep a copy of file data provided to be managed internally
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 // Setup data pointers to previously allocated data
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 // Close QOA file (if open) and free internal memory
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 // Decode one frame from QOA data
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 // Rewind QOA file or memory pointer to beginning
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 // Decode required QOA frames
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 // Do we have to decode more samples?
218 if (qoa_ctx->sample_data_len - qoa_ctx->sample_data_pos == 0)
219 {
220 if (!qoaplay_decode_frame(qoa_ctx))
221 {
222 // Loop to the beginning
223 qoaplay_rewind(qoa_ctx);
224 qoaplay_decode_frame(qoa_ctx);
225 }
226
227 src_index = 0;
228 }
229
230 // Normalize to -1..1 floats and write to dest
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 // Get QOA total time duration in seconds
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 // Get QOA current time position in seconds
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 // Get QOA current audio frame
256 int qoaplay_get_frame(qoaplay_desc *qoa_ctx)
257 {
258 return qoa_ctx->sample_position/QOA_FRAME_LEN;
259 }
260
261 // Seek QOA audio frame
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 }