rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c
Go to the documentation of this file.
1 /*#define DR_FLAC_NO_CRC*/
2 /*#define DR_FLAC_NO_SIMD*/
3 /*#define DR_FLAC_BUFFER_SIZE 4096*/
4 #include "dr_flac_common.c"
5 
6 #define FILE_NAME_WIDTH 40
7 #define NUMBER_WIDTH 10
8 #define TABLE_MARGIN 2
9 
10 #define DEFAULT_SOURCE_DIR "testvectors/flac/tests"
11 
12 drflac_result decode_test__read_and_compare_pcm_frames_s32(libflac* pLibFlac, drflac* pFlac, drflac_uint64 pcmFrameCount, drflac_int32* pPCMFrames_libflac, drflac_int32* pPCMFrames_drflac)
13 {
14  drflac_uint64 pcmFrameCount_libflac;
15  drflac_uint64 pcmFrameCount_drflac;
16  drflac_uint64 iPCMFrame;
17 
18  /* To test decoding we just read a number of PCM frames from each decoder and compare. */
19  pcmFrameCount_libflac = libflac_read_pcm_frames_s32(pLibFlac, pcmFrameCount, pPCMFrames_libflac);
20  pcmFrameCount_drflac = drflac_read_pcm_frames_s32(pFlac, pcmFrameCount, pPCMFrames_drflac);
21 
22  /* The total number of frames we decoded need to match. */
23  if (pcmFrameCount_libflac != pcmFrameCount_drflac) {
24  printf(" Decoded frame counts differ: pcmFrameCount=%d, libFLAC=%d, dr_flac=%d", (int)pcmFrameCount, (int)pcmFrameCount_libflac, (int)pcmFrameCount_drflac);
25  return DRFLAC_ERROR;
26  }
27 
28  /* Each of the decoded PCM frames need to match. */
29  DRFLAC_ASSERT(pcmFrameCount_libflac == pcmFrameCount_drflac);
30 
31  for (iPCMFrame = 0; iPCMFrame < pcmFrameCount_libflac; iPCMFrame += 1) {
32  drflac_int32* pPCMFrame_libflac = pPCMFrames_libflac + (iPCMFrame * pLibFlac->channels);
33  drflac_int32* pPCMFrame_drflac = pPCMFrames_drflac + (iPCMFrame * pLibFlac->channels);
34  drflac_uint32 iChannel;
35  drflac_bool32 hasError = DRFLAC_FALSE;
36 
37  for (iChannel = 0; iChannel < pLibFlac->channels; iChannel += 1) {
38  if (pPCMFrame_libflac[iChannel] != pPCMFrame_drflac[iChannel]) {
39  printf(" PCM Frame @ %d[%d] does not match: pcmFrameCount=%d\n", (int)iPCMFrame, iChannel, (int)pcmFrameCount);
40  hasError = DRFLAC_TRUE;
41  break;
42  }
43  }
44 
45  if (hasError) {
46  return DRFLAC_ERROR; /* Decoded frames do not match. */
47  }
48  }
49 
50  /* Done. */
51  return DRFLAC_SUCCESS;
52 }
53 
55 {
57  drflac_uint64 iPCMFrame;
58  drflac_int32* pPCMFrames_libflac;
59  drflac_int32* pPCMFrames_drflac;
60 
61  /* Make sure the decoder's are seeked back to the start first. */
62  drflac_seek_to_pcm_frame(pFlac, 0);
63  libflac_seek_to_pcm_frame(pLibFlac, 0);
64 
65  pPCMFrames_libflac = (drflac_int32*)malloc((size_t)(pcmFrameChunkSize * pLibFlac->channels * sizeof(drflac_int32)));
66  if (pPCMFrames_libflac == NULL) {
67  printf(" [libFLAC] Out of memory");
68  return DRFLAC_ERROR;
69  }
70 
71  pPCMFrames_drflac = (drflac_int32*)malloc((size_t)(pcmFrameChunkSize * pLibFlac->channels * sizeof(drflac_int32)));
72  if (pPCMFrames_drflac == NULL) {
73  free(pPCMFrames_libflac);
74  printf(" [dr_flac] Out of memory");
75  return DRFLAC_ERROR;
76  }
77 
78  for (iPCMFrame = 0; iPCMFrame < pLibFlac->pcmFrameCount; iPCMFrame += pcmFrameChunkSize) {
79  result = decode_test__read_and_compare_pcm_frames_s32(pLibFlac, pFlac, pcmFrameChunkSize, pPCMFrames_libflac, pPCMFrames_drflac);
80  if (result != DRFLAC_SUCCESS) {
81  break;
82  }
83  }
84 
85  free(pPCMFrames_libflac);
86  free(pPCMFrames_drflac);
87 
88  return result;
89 }
90 
91 
92 drflac_result decode_test__read_and_compare_pcm_frames_f32(libflac* pLibFlac, drflac* pFlac, drflac_uint64 pcmFrameCount, float* pPCMFrames_libflac, float* pPCMFrames_drflac)
93 {
94  drflac_uint64 pcmFrameCount_libflac;
95  drflac_uint64 pcmFrameCount_drflac;
96  drflac_uint64 iPCMFrame;
97 
98  /* To test decoding we just read a number of PCM frames from each decoder and compare. */
99  pcmFrameCount_libflac = libflac_read_pcm_frames_f32(pLibFlac, pcmFrameCount, pPCMFrames_libflac);
100  pcmFrameCount_drflac = drflac_read_pcm_frames_f32(pFlac, pcmFrameCount, pPCMFrames_drflac);
101 
102  /* The total number of frames we decoded need to match. */
103  if (pcmFrameCount_libflac != pcmFrameCount_drflac) {
104  printf(" Decoded frame counts differ: pcmFrameCount=%d, libFLAC=%d, dr_flac=%d", (int)pcmFrameCount, (int)pLibFlac->currentPCMFrame, (int)pFlac->currentPCMFrame);
105  return DRFLAC_ERROR;
106  }
107 
108  /* Each of the decoded PCM frames need to match. */
109  DRFLAC_ASSERT(pcmFrameCount_libflac == pcmFrameCount_drflac);
110 
111  for (iPCMFrame = 0; iPCMFrame < pcmFrameCount_libflac; iPCMFrame += 1) {
112  float* pPCMFrame_libflac = pPCMFrames_libflac + (iPCMFrame * pLibFlac->channels);
113  float* pPCMFrame_drflac = pPCMFrames_drflac + (iPCMFrame * pLibFlac->channels);
114  drflac_uint32 iChannel;
115  drflac_bool32 hasError = DRFLAC_FALSE;
116 
117  for (iChannel = 0; iChannel < pLibFlac->channels; iChannel += 1) {
118  if (pPCMFrame_libflac[iChannel] != pPCMFrame_drflac[iChannel]) {
119  printf(" PCM Frame @ %d[%d] does not match: pcmFrameCount=%d", (int)iPCMFrame, iChannel, (int)pcmFrameCount);
120  hasError = DRFLAC_TRUE;
121  break;
122  }
123  }
124 
125  if (hasError) {
126  return DRFLAC_ERROR; /* Decoded frames do not match. */
127  }
128  }
129 
130  /* Done. */
131  return DRFLAC_SUCCESS;
132 }
133 
135 {
136  drflac_result result = DRFLAC_SUCCESS;
137  drflac_uint64 iPCMFrame;
138  float* pPCMFrames_libflac;
139  float* pPCMFrames_drflac;
140 
141  /* Make sure the decoder's are seeked back to the start first. */
142  drflac_seek_to_pcm_frame(pFlac, 0);
143  libflac_seek_to_pcm_frame(pLibFlac, 0);
144 
145  pPCMFrames_libflac = (float*)malloc((size_t)(pcmFrameChunkSize * pLibFlac->channels * sizeof(float)));
146  if (pPCMFrames_libflac == NULL) {
147  printf(" [libFLAC] Out of memory");
148  return DRFLAC_ERROR;
149  }
150 
151  pPCMFrames_drflac = (float*)malloc((size_t)(pcmFrameChunkSize * pLibFlac->channels * sizeof(float)));
152  if (pPCMFrames_drflac == NULL) {
153  free(pPCMFrames_libflac);
154  printf(" [dr_flac] Out of memory");
155  return DRFLAC_ERROR;
156  }
157 
158  for (iPCMFrame = 0; iPCMFrame < pLibFlac->pcmFrameCount; iPCMFrame += pcmFrameChunkSize) {
159  result = decode_test__read_and_compare_pcm_frames_f32(pLibFlac, pFlac, pcmFrameChunkSize, pPCMFrames_libflac, pPCMFrames_drflac);
160  if (result != DRFLAC_SUCCESS) {
161  break;
162  }
163  }
164 
165  free(pPCMFrames_libflac);
166  free(pPCMFrames_drflac);
167 
168  return result;
169 }
170 
171 
172 drflac_result decode_test__read_and_compare_pcm_frames_s16(libflac* pLibFlac, drflac* pFlac, drflac_uint64 pcmFrameCount, drflac_int16* pPCMFrames_libflac, drflac_int16* pPCMFrames_drflac)
173 {
174  drflac_uint64 pcmFrameCount_libflac;
175  drflac_uint64 pcmFrameCount_drflac;
176  drflac_uint64 iPCMFrame;
177 
178  /* To test decoding we just read a number of PCM frames from each decoder and compare. */
179  pcmFrameCount_libflac = libflac_read_pcm_frames_s16(pLibFlac, pcmFrameCount, pPCMFrames_libflac);
180  pcmFrameCount_drflac = drflac_read_pcm_frames_s16(pFlac, pcmFrameCount, pPCMFrames_drflac);
181 
182  /* The total number of frames we decoded need to match. */
183  if (pcmFrameCount_libflac != pcmFrameCount_drflac) {
184  printf(" Decoded frame counts differ: pcmFrameCount=%d, libFLAC=%d, dr_flac=%d", (int)pcmFrameCount, (int)pLibFlac->currentPCMFrame, (int)pFlac->currentPCMFrame);
185  return DRFLAC_ERROR;
186  }
187 
188  /* Each of the decoded PCM frames need to match. */
189  DRFLAC_ASSERT(pcmFrameCount_libflac == pcmFrameCount_drflac);
190 
191  for (iPCMFrame = 0; iPCMFrame < pcmFrameCount_libflac; iPCMFrame += 1) {
192  drflac_int16* pPCMFrame_libflac = pPCMFrames_libflac + (iPCMFrame * pLibFlac->channels);
193  drflac_int16* pPCMFrame_drflac = pPCMFrames_drflac + (iPCMFrame * pLibFlac->channels);
194  drflac_uint32 iChannel;
195  drflac_bool32 hasError = DRFLAC_FALSE;
196 
197  for (iChannel = 0; iChannel < pLibFlac->channels; iChannel += 1) {
198  if (pPCMFrame_libflac[iChannel] != pPCMFrame_drflac[iChannel]) {
199  printf(" PCM Frame @ %d[%d] does not match: pcmFrameCount=%d", (int)iPCMFrame, iChannel, (int)pcmFrameCount);
200  hasError = DRFLAC_TRUE;
201  break;
202  }
203  }
204 
205  if (hasError) {
206  return DRFLAC_ERROR; /* Decoded frames do not match. */
207  }
208  }
209 
210  /* Done. */
211  return DRFLAC_SUCCESS;
212 }
213 
215 {
216  drflac_result result = DRFLAC_SUCCESS;
217  drflac_uint64 iPCMFrame;
218  drflac_int16* pPCMFrames_libflac;
219  drflac_int16* pPCMFrames_drflac;
220 
221  /* Make sure the decoder's are seeked back to the start first. */
222  drflac_seek_to_pcm_frame(pFlac, 0);
223  libflac_seek_to_pcm_frame(pLibFlac, 0);
224 
225  pPCMFrames_libflac = (drflac_int16*)malloc((size_t)(pcmFrameChunkSize * pLibFlac->channels * sizeof(drflac_int16)));
226  if (pPCMFrames_libflac == NULL) {
227  printf(" [libFLAC] Out of memory");
228  return DRFLAC_ERROR;
229  }
230 
231  pPCMFrames_drflac = (drflac_int16*)malloc((size_t)(pcmFrameChunkSize * pLibFlac->channels * sizeof(drflac_int16)));
232  if (pPCMFrames_drflac == NULL) {
233  free(pPCMFrames_libflac);
234  printf(" [dr_flac] Out of memory");
235  return DRFLAC_ERROR;
236  }
237 
238  for (iPCMFrame = 0; iPCMFrame < pLibFlac->pcmFrameCount; iPCMFrame += pcmFrameChunkSize) {
239  result = decode_test__read_and_compare_pcm_frames_s16(pLibFlac, pFlac, pcmFrameChunkSize, pPCMFrames_libflac, pPCMFrames_drflac);
240  if (result != DRFLAC_SUCCESS) {
241  break;
242  }
243  }
244 
245  free(pPCMFrames_libflac);
246  free(pPCMFrames_drflac);
247 
248  return result;
249 }
250 
251 
252 
254 {
255  drflac_result result = DRFLAC_SUCCESS;
256 
257  /* Start with reading the entire file in one go. */
258  if (result == DRFLAC_SUCCESS) {
259  result = decode_test__read_and_compare_pcm_frame_chunks_s32(pLibFlac, pFlac, pLibFlac->pcmFrameCount);
260  }
261 
262  /* Now try with reading one PCM frame at a time.*/
263  if (result == DRFLAC_SUCCESS) {
264  result = decode_test__read_and_compare_pcm_frame_chunks_s32(pLibFlac, pFlac, 1);
265  }
266 
267  /* Now test FLAC frame boundaries. */
268  if (result == DRFLAC_SUCCESS) {
269  result = decode_test__read_and_compare_pcm_frame_chunks_s32(pLibFlac, pFlac, (pFlac->maxBlockSizeInPCMFrames > 0) ? pFlac->maxBlockSizeInPCMFrames : 4096);
270  }
271 
272  return result;
273 }
274 
276 {
277  drflac_result result = DRFLAC_SUCCESS;
278 
279  /* Start with reading the entire file in one go. */
280  if (result == DRFLAC_SUCCESS) {
281  result = decode_test__read_and_compare_pcm_frame_chunks_f32(pLibFlac, pFlac, pLibFlac->pcmFrameCount);
282  }
283 
284  /* Now try with reading one PCM frame at a time.*/
285  if (result == DRFLAC_SUCCESS) {
286  result = decode_test__read_and_compare_pcm_frame_chunks_f32(pLibFlac, pFlac, 1);
287  }
288 
289  /* Now test FLAC frame boundaries. */
290  if (result == DRFLAC_SUCCESS) {
291  result = decode_test__read_and_compare_pcm_frame_chunks_f32(pLibFlac, pFlac, (pFlac->maxBlockSizeInPCMFrames > 0) ? pFlac->maxBlockSizeInPCMFrames : 4096);
292  }
293 
294  return result;
295 }
296 
298 {
299  drflac_result result = DRFLAC_SUCCESS;
300 
301  /* Start with reading the entire file in one go. */
302  if (result == DRFLAC_SUCCESS) {
303  result = decode_test__read_and_compare_pcm_frame_chunks_s16(pLibFlac, pFlac, pLibFlac->pcmFrameCount);
304  }
305 
306  /* Now try with reading one PCM frame at a time.*/
307  if (result == DRFLAC_SUCCESS) {
308  result = decode_test__read_and_compare_pcm_frame_chunks_s16(pLibFlac, pFlac, 1);
309  }
310 
311  /* Now test FLAC frame boundaries. */
312  if (result == DRFLAC_SUCCESS) {
313  result = decode_test__read_and_compare_pcm_frame_chunks_s16(pLibFlac, pFlac, (pFlac->maxBlockSizeInPCMFrames > 0) ? pFlac->maxBlockSizeInPCMFrames : 4096);
314  }
315 
316  return result;
317 }
318 
319 drflac_result decode_test_file(const char* pFilePath)
320 {
321  /* To test seeking we just seek to our target PCM frame and then decode whatever is remaining and compare it against libFLAC. */
322  drflac_result result;
324  drflac* pFlac;
325 
327 
328  /* First load the decoder from libFLAC. */
329  result = libflac_init_file(pFilePath, &libflac);
330  if (result != DRFLAC_SUCCESS) {
331  printf(" Failed to open via libFLAC.");
332  return result;
333  }
334 
335  /* Now load from dr_flac. */
336  pFlac = drflac_open_file(pFilePath, NULL);
337  if (pFlac == NULL) {
338  printf(" Failed to open via dr_flac.");
340  return DRFLAC_ERROR; /* Failed to load dr_flac decoder. */
341  }
342 
343  /* At this point we should have both libFLAC and dr_flac decoders open. We can now perform identical operations on each of them and compare. */
344  result = decode_test_file_s32(&libflac, pFlac);
345  if (result != DRFLAC_SUCCESS) {
346  drflac_close(pFlac);
348  return result;
349  }
350 
351  result = decode_test_file_f32(&libflac, pFlac);
352  if (result != DRFLAC_SUCCESS) {
353  drflac_close(pFlac);
355  return result;
356  }
357 
358  result = decode_test_file_s16(&libflac, pFlac);
359  if (result != DRFLAC_SUCCESS) {
360  drflac_close(pFlac);
362  return result;
363  }
364 
365 
366  /* We're done with our decoders. */
367  drflac_close(pFlac);
369 
370  if (result == DRFLAC_SUCCESS) {
371  printf(" Passed");
372  }
373 
374  return result;
375 }
376 
377 drflac_result decode_test_directory(const char* pDirectoryPath)
378 {
379  dr_file_iterator iteratorState;
380  dr_file_iterator* pFile;
381 
382  dr_printf_fixed(FILE_NAME_WIDTH, "%s", pDirectoryPath);
384  printf("\n");
385 
386  pFile = dr_file_iterator_begin(pDirectoryPath, &iteratorState);
387  while (pFile != NULL) {
388  drflac_result result;
389 
390  /* Skip directories for now, but we may want to look at doing recursive file iteration. */
391  if (!pFile->isDirectory) {
392  result = decode_test_file(pFile->absolutePath);
393  (void)result;
394 
395  printf("\n");
396  }
397 
398  pFile = dr_file_iterator_next(pFile);
399  }
400 
401  return DRFLAC_SUCCESS;
402 }
403 
405 {
406  drflac_result result = DRFLAC_SUCCESS;
407 
408  /* Directories. */
409  {
411  (void)result;
412  }
413 
414  return result;
415 }
416 
417 
418 
419 drflac_result open_and_read_test_file_s32(libflac* pLibFlac, const char* pFilePath)
420 {
421  drflac_int32* pPCMFrames;
422  drflac_uint64 pcmFrameCount;
423  drflac_uint32 channels;
424  drflac_uint32 sampleRate;
425  drflac_uint64 iPCMFrame;
426 
427  pPCMFrames = drflac_open_file_and_read_pcm_frames_s32(pFilePath, &channels, &sampleRate, &pcmFrameCount, NULL);
428  if (pPCMFrames == NULL) {
429  printf(" drflac_open_and_read failed.");
430  return DRFLAC_ERROR; /* Error decoding */
431  }
432 
433  if (pcmFrameCount != pLibFlac->pcmFrameCount) {
434  printf(" Decoded frame counts differ: pcmFrameCount=%d, libFLAC=%d, dr_flac=%d", (int)pcmFrameCount, (int)pLibFlac->currentPCMFrame, (int)pcmFrameCount);
435  drflac_free(pPCMFrames, NULL);
436  return DRFLAC_ERROR;
437  }
438 
439  for (iPCMFrame = 0; iPCMFrame < pLibFlac->pcmFrameCount; iPCMFrame += 1) {
440  drflac_int32* pPCMFrame_libflac = pLibFlac->pPCMFrames + (iPCMFrame * pLibFlac->channels);
441  drflac_int32* pPCMFrame_drflac = pPCMFrames + (iPCMFrame * pLibFlac->channels);
442  drflac_uint32 iChannel;
443  drflac_bool32 hasError = DRFLAC_FALSE;
444 
445  for (iChannel = 0; iChannel < pLibFlac->channels; iChannel += 1) {
446  if (pPCMFrame_libflac[iChannel] != pPCMFrame_drflac[iChannel]) {
447  printf(" PCM Frame @ %d[%d] does not match: pcmFrameCount=%d", (int)iPCMFrame, iChannel, (int)pcmFrameCount);
448  hasError = DRFLAC_TRUE;
449  break;
450  }
451  }
452 
453  if (hasError) {
454  drflac_free(pPCMFrames, NULL);
455  return DRFLAC_ERROR; /* Decoded frames do not match. */
456  }
457  }
458 
459  drflac_free(pPCMFrames, NULL);
460  return DRFLAC_SUCCESS;
461 }
462 
463 drflac_result open_and_read_test_file_f32(libflac* pLibFlac, const char* pFilePath)
464 {
465  float* pPCMFrames;
466  drflac_uint64 pcmFrameCount;
467  drflac_uint32 channels;
468  drflac_uint32 sampleRate;
469  drflac_uint64 iPCMFrame;
470 
471  pPCMFrames = drflac_open_file_and_read_pcm_frames_f32(pFilePath, &channels, &sampleRate, &pcmFrameCount, NULL);
472  if (pPCMFrames == NULL) {
473  printf(" drflac_open_and_read failed.");
474  return DRFLAC_ERROR; /* Error decoding */
475  }
476 
477  if (pcmFrameCount != pLibFlac->pcmFrameCount) {
478  printf(" Decoded frame counts differ: pcmFrameCount=%d, libFLAC=%d, dr_flac=%d", (int)pcmFrameCount, (int)pLibFlac->currentPCMFrame, (int)pcmFrameCount);
479  drflac_free(pPCMFrames, NULL);
480  return DRFLAC_ERROR;
481  }
482 
483  for (iPCMFrame = 0; iPCMFrame < pLibFlac->pcmFrameCount; iPCMFrame += 1) {
484  drflac_int32* pPCMFrame_libflac = pLibFlac->pPCMFrames + (iPCMFrame * pLibFlac->channels);
485  float* pPCMFrame_drflac = pPCMFrames + (iPCMFrame * pLibFlac->channels);
486  drflac_uint32 iChannel;
487  drflac_bool32 hasError = DRFLAC_FALSE;
488 
489  for (iChannel = 0; iChannel < pLibFlac->channels; iChannel += 1) {
490  if ((pPCMFrame_libflac[iChannel] / 2147483648.0) != pPCMFrame_drflac[iChannel]) {
491  printf(" PCM Frame @ %d[%d] does not match: pcmFrameCount=%d", (int)iPCMFrame, iChannel, (int)pcmFrameCount);
492  hasError = DRFLAC_TRUE;
493  break;
494  }
495  }
496 
497  if (hasError) {
498  drflac_free(pPCMFrames, NULL);
499  return DRFLAC_ERROR; /* Decoded frames do not match. */
500  }
501  }
502 
503  drflac_free(pPCMFrames, NULL);
504  return DRFLAC_SUCCESS;
505 }
506 
507 drflac_result open_and_read_test_file_s16(libflac* pLibFlac, const char* pFilePath)
508 {
509  drflac_int16* pPCMFrames;
510  drflac_uint64 pcmFrameCount;
511  drflac_uint32 channels;
512  drflac_uint32 sampleRate;
513  drflac_uint64 iPCMFrame;
514 
515  pPCMFrames = drflac_open_file_and_read_pcm_frames_s16(pFilePath, &channels, &sampleRate, &pcmFrameCount, NULL);
516  if (pPCMFrames == NULL) {
517  printf(" drflac_open_and_read failed.");
518  return DRFLAC_ERROR; /* Error decoding */
519  }
520 
521  if (pcmFrameCount != pLibFlac->pcmFrameCount) {
522  printf(" Decoded frame counts differ: pcmFrameCount=%d, libFLAC=%d, dr_flac=%d", (int)pcmFrameCount, (int)pLibFlac->currentPCMFrame, (int)pcmFrameCount);
523  drflac_free(pPCMFrames, NULL);
524  return DRFLAC_ERROR;
525  }
526 
527  for (iPCMFrame = 0; iPCMFrame < pLibFlac->pcmFrameCount; iPCMFrame += 1) {
528  drflac_int32* pPCMFrame_libflac = pLibFlac->pPCMFrames + (iPCMFrame * pLibFlac->channels);
529  drflac_int16* pPCMFrame_drflac = pPCMFrames + (iPCMFrame * pLibFlac->channels);
530  drflac_uint32 iChannel;
531  drflac_bool32 hasError = DRFLAC_FALSE;
532 
533  for (iChannel = 0; iChannel < pLibFlac->channels; iChannel += 1) {
534  if ((pPCMFrame_libflac[iChannel] >> 16) != pPCMFrame_drflac[iChannel]) {
535  printf(" PCM Frame @ %d[%d] does not match: pcmFrameCount=%d", (int)iPCMFrame, iChannel, (int)pcmFrameCount);
536  hasError = DRFLAC_TRUE;
537  break;
538  }
539  }
540 
541  if (hasError) {
542  drflac_free(pPCMFrames, NULL);
543  return DRFLAC_ERROR; /* Decoded frames do not match. */
544  }
545  }
546 
547  drflac_free(pPCMFrames, NULL);
548  return DRFLAC_SUCCESS;
549 }
550 
552 {
553  drflac_result result;
555 
557 
558  /* First load the decoder from libFLAC. */
559  result = libflac_init_file(pFilePath, &libflac);
560  if (result != DRFLAC_SUCCESS) {
561  printf(" Failed to open via libFLAC.");
562  return result;
563  }
564 
565  result = open_and_read_test_file_s32(&libflac, pFilePath);
566  if (result != DRFLAC_SUCCESS) {
567  return result;
568  }
569 
571  if (result != DRFLAC_SUCCESS) {
572  return result;
573  }
574 
576  if (result != DRFLAC_SUCCESS) {
577  return result;
578  }
579 
581 
582  if (result == DRFLAC_SUCCESS) {
583  printf(" Passed");
584  }
585 
586  return result;
587 }
588 
589 drflac_result open_and_read_test_directory(const char* pDirectoryPath)
590 {
591  dr_file_iterator iteratorState;
592  dr_file_iterator* pFile;
593 
594  dr_printf_fixed(FILE_NAME_WIDTH, "%s", pDirectoryPath);
596  printf("\n");
597 
598  pFile = dr_file_iterator_begin(pDirectoryPath, &iteratorState);
599  while (pFile != NULL) {
600  drflac_result result;
601 
602  /* Skip directories for now, but we may want to look at doing recursive file iteration. */
603  if (!pFile->isDirectory) {
604  result = open_and_read_test_file(pFile->absolutePath);
605  (void)result;
606 
607  printf("\n");
608  }
609 
610  pFile = dr_file_iterator_next(pFile);
611  }
612 
613  return DRFLAC_SUCCESS;
614 }
615 
617 {
618  drflac_result result = DRFLAC_SUCCESS;
619 
620  /* Directories. */
621  {
623  (void)result;
624  }
625 
626  return result;
627 }
628 
629 
630 
631 drflac_result decode_profiling_file(const char* pFilePath)
632 {
633  drflac_result result;
635  drflac* pFlac;
636  drflac_int32* pTempBuffer;
637  double decodeTimeBeg;
638  double decodeTimeEnd;
639  double drflacDecodeTimeInSeconds;
640  void* pFileData;
641  size_t fileSizeInBytes;
642 
644 
645  /* libFLAC */
646  result = libflac_init_file(pFilePath, &libflac);
647  if (result != DRFLAC_SUCCESS) {
648  printf(" [libFLAC] Failed to load file");
649  return result;
650  }
651 
652  /* dr_flac */
653  pFileData = dr_open_and_read_file(pFilePath, &fileSizeInBytes);
654  if (pFileData == NULL) {
655  printf(" Failed to load file");
656  return DRFLAC_ERROR; /* Failed to open the file. */
657  }
658 
659  pFlac = drflac_open_memory(pFileData, fileSizeInBytes, NULL);
660  if (pFlac == NULL) {
661  free(pFileData);
662  printf(" [dr_flac] Failed to load file.");
663  return DRFLAC_ERROR;
664  }
665 
666  /* libFLAC decode time. */
668 
669  /* dr_flac decode time. */
670  pTempBuffer = (drflac_int32*)malloc((size_t)(libflac.pcmFrameCount * libflac.channels * sizeof(drflac_int32)));
671  if (pTempBuffer == NULL) {
673  drflac_close(pFlac);
674  free(pFileData);
675  printf(" Out of memory.");
676  return DRFLAC_ERROR; /* Out of memory. */
677  }
678 
679  DRFLAC_ZERO_MEMORY(pTempBuffer, (size_t)(libflac.pcmFrameCount * libflac.channels * sizeof(drflac_int32)));
680 
681  decodeTimeBeg = dr_timer_now();
682  drflac_read_pcm_frames_s32(pFlac, libflac.pcmFrameCount, pTempBuffer);
683  decodeTimeEnd = dr_timer_now();
684 
685  free(pTempBuffer);
686  free(pFileData);
687 
688  drflacDecodeTimeInSeconds = decodeTimeEnd - decodeTimeBeg;
689  dr_printf_fixed_with_margin(NUMBER_WIDTH, TABLE_MARGIN, "%.2fms", drflacDecodeTimeInSeconds*1000);
690 
691  /* Difference. */
692  dr_printf_fixed_with_margin(NUMBER_WIDTH, TABLE_MARGIN, "%d%%", (int)(drflacDecodeTimeInSeconds/libflac.decodeTimeInSeconds * 100));
693 
695  drflac_close(pFlac);
696 
697  return DRFLAC_SUCCESS;
698 }
699 
700 drflac_result decode_profiling_directory(const char* pDirectoryPath)
701 {
702  dr_file_iterator iteratorState;
703  dr_file_iterator* pFile;
704  drflac_bool32 foundError = DRFLAC_FALSE;
705 
706  dr_printf_fixed(FILE_NAME_WIDTH, "%s", pDirectoryPath);
709  printf("\n");
710 
711  pFile = dr_file_iterator_begin(pDirectoryPath, &iteratorState);
712  while (pFile != NULL) {
713  drflac_result result;
714 
715  /* Skip directories for now, but we may want to look at doing recursive file iteration. */
716  if (!pFile->isDirectory) {
717  result = decode_profiling_file(pFile->absolutePath);
718  if (result != DRFLAC_SUCCESS) {
719  foundError = DRFLAC_TRUE;
720  }
721 
722  printf("\n");
723  }
724 
725  pFile = dr_file_iterator_next(pFile);
726  }
727 
728  return (foundError) ? DRFLAC_ERROR : DRFLAC_SUCCESS;
729 }
730 
732 {
733  drflac_result result = DRFLAC_SUCCESS;
734 
735  /* Directories. */
736  {
738  }
739 
740  return result;
741 }
742 
743 
744 int main(int argc, char** argv)
745 {
746  drflac_result result = DRFLAC_SUCCESS;
747  drflac_bool32 doTesting = DRFLAC_TRUE;
748  drflac_bool32 doProfiling = DRFLAC_TRUE;
749 
750  /* This program has two main parts. The first is just a normal functionality test. The second is a profiling of the different seeking methods. */
751  if (dr_argv_is_set(argc, argv, "--onlyprofile")) {
752  doTesting = DRFLAC_FALSE;
753  }
754 
755  print_cpu_caps();
756 
757  /* Exhaustive seek test. */
758  if (doTesting) {
759  printf("=======================================================================\n");
760  printf("DECODE TESTING\n");
761  printf("=======================================================================\n");
762  result = decode_test();
763  if (result != DRFLAC_SUCCESS) {
764  return (int)result; /* Don't continue if an error occurs during testing. */
765  }
766  printf("\n");
767 
768  printf("=======================================================================\n");
769  printf("OPEN-AND-READ TESTING - drflac_open_*_and_read_pcm_frames_*()\n");
770  printf("=======================================================================\n");
771  result = open_and_read_test();
772  if (result != DRFLAC_SUCCESS) {
773  return (int)result; /* Don't continue if an error occurs during testing. */
774  }
775  printf("\n");
776  } else {
777  printf("=======================================================================\n");
778  printf("WARNING: Correctness Tests Disabled\n");
779  printf("=======================================================================\n");
780  }
781 
782  /* Profiling. */
783  if (doProfiling) {
784  printf("=======================================================================\n");
785  printf("DECODE PROFILING (LOWER IS BETTER)\n");
786  printf("=======================================================================\n");
787  result = decode_profiling();
788  printf("\n");
789  }
790 
791  /*getchar();*/
792  return (int)result;
793 }
DRFLAC_ZERO_MEMORY
#define DRFLAC_ZERO_MEMORY(p, sz)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:51350
dr_file_iterator_next
dr_file_iterator * dr_file_iterator_next(dr_file_iterator *pState)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:495
libflac::pcmFrameCount
drflac_uint64 pcmFrameCount
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:28
TABLE_MARGIN
#define TABLE_MARGIN
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:8
drflac_uint64
unsigned long long drflac_uint64
Definition: porcupine/demo/c/dr_libs/dr_flac.h:259
drflac_read_pcm_frames_f32
DRFLAC_API drflac_uint64 drflac_read_pcm_frames_f32(drflac *pFlac, drflac_uint64 framesToRead, float *pBufferOut)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:58850
decode_profiling_directory
drflac_result decode_profiling_directory(const char *pDirectoryPath)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:700
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
dr_flac_common.c
DRFLAC_ASSERT
#define DRFLAC_ASSERT(expression)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:51335
drflac
Definition: porcupine/demo/c/dr_libs/dr_flac.h:688
libflac_read_pcm_frames_s16
drflac_uint64 libflac_read_pcm_frames_s16(libflac *pDecoder, drflac_uint64 framesToRead, drflac_int16 *pBufferOut)
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:271
decode_test_file_s16
drflac_result decode_test_file_s16(libflac *pLibFlac, drflac *pFlac)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:297
libflac_init_file
drflac_result libflac_init_file(const char *pFilePath, libflac *pDecoder)
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:148
decode_test__read_and_compare_pcm_frames_s32
drflac_result decode_test__read_and_compare_pcm_frames_s32(libflac *pLibFlac, drflac *pFlac, drflac_uint64 pcmFrameCount, drflac_int32 *pPCMFrames_libflac, drflac_int32 *pPCMFrames_drflac)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:12
libflac_seek_to_pcm_frame
drflac_bool32 libflac_seek_to_pcm_frame(libflac *pDecoder, drflac_uint64 targetPCMFrameIndex)
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:295
open_and_read_test_file
drflac_result open_and_read_test_file(const char *pFilePath)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:551
libflac_read_pcm_frames_f32
drflac_uint64 libflac_read_pcm_frames_f32(libflac *pDecoder, drflac_uint64 framesToRead, float *pBufferOut)
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:247
dr_file_iterator::isDirectory
dr_bool32 isDirectory
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:383
DRFLAC_ERROR
#define DRFLAC_ERROR
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:51358
open_and_read_test_file_f32
drflac_result open_and_read_test_file_f32(libflac *pLibFlac, const char *pFilePath)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:463
dr_printf_fixed_with_margin
int dr_printf_fixed_with_margin(int width, int margin, const char *const format,...)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:755
decode_test__read_and_compare_pcm_frames_f32
drflac_result decode_test__read_and_compare_pcm_frames_f32(libflac *pLibFlac, drflac *pFlac, drflac_uint64 pcmFrameCount, float *pPCMFrames_libflac, float *pPCMFrames_drflac)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:92
open_and_read_test_directory
drflac_result open_and_read_test_directory(const char *pDirectoryPath)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:589
dr_file_iterator
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:378
dr_open_and_read_file
void * dr_open_and_read_file(const char *filePath, size_t *pFileSizeOut)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:675
main
int main(int argc, char **argv)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:744
dr_timer_now
double dr_timer_now()
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:800
libflac
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:25
drflac_open_memory
DRFLAC_API drflac * drflac_open_memory(const void *pData, size_t dataSize, const drflac_allocation_callbacks *pAllocationCallbacks)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:56697
decode_profiling_file
drflac_result decode_profiling_file(const char *pFilePath)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:631
decode_test__read_and_compare_pcm_frame_chunks_s32
drflac_result decode_test__read_and_compare_pcm_frame_chunks_s32(libflac *pLibFlac, drflac *pFlac, drflac_uint64 pcmFrameChunkSize)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:54
drflac_open_file_and_read_pcm_frames_f32
DRFLAC_API float * drflac_open_file_and_read_pcm_frames_f32(const char *filename, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalPCMFrameCount, const drflac_allocation_callbacks *pAllocationCallbacks)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:59154
open_and_read_test_file_s16
drflac_result open_and_read_test_file_s16(libflac *pLibFlac, const char *pFilePath)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:507
FILE_NAME_WIDTH
#define FILE_NAME_WIDTH
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:6
DRFLAC_FALSE
#define DRFLAC_FALSE
Definition: porcupine/demo/c/dr_libs/dr_flac.h:272
drflac_int16
signed short drflac_int16
Definition: porcupine/demo/c/dr_libs/dr_flac.h:243
drflac_read_pcm_frames_s16
DRFLAC_API drflac_uint64 drflac_read_pcm_frames_s16(drflac *pFlac, drflac_uint64 framesToRead, drflac_int16 *pBufferOut)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:58123
dr_argv_is_set
dr_bool32 dr_argv_is_set(int argc, char **argv, const char *value)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:681
drflac_open_file_and_read_pcm_frames_s32
DRFLAC_API drflac_int32 * drflac_open_file_and_read_pcm_frames_s32(const char *filename, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalPCMFrameCount, const drflac_allocation_callbacks *pAllocationCallbacks)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:59118
drflac::currentPCMFrame
drflac_uint64 currentPCMFrame
Definition: porcupine/demo/c/dr_libs/dr_flac.h:734
dr_path_file_name
const char * dr_path_file_name(const char *path)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:296
drflac_free
DRFLAC_API void drflac_free(void *p, const drflac_allocation_callbacks *pAllocationCallbacks)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:59227
open_and_read_test_file_s32
drflac_result open_and_read_test_file_s32(libflac *pLibFlac, const char *pFilePath)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:419
drflac_open_file
DRFLAC_API drflac * drflac_open_file(const char *pFileName, const drflac_allocation_callbacks *pAllocationCallbacks)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:56601
libflac::decodeTimeInSeconds
double decodeTimeInSeconds
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:33
libflac::channels
drflac_uint32 channels
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:30
drflac::maxBlockSizeInPCMFrames
drflac_uint16 maxBlockSizeInPCMFrames
Definition: porcupine/demo/c/dr_libs/dr_flac.h:713
DEFAULT_SOURCE_DIR
#define DEFAULT_SOURCE_DIR
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:10
drflac_bool32
drflac_uint32 drflac_bool32
Definition: porcupine/demo/c/dr_libs/dr_flac.h:270
libflac::pPCMFrames
drflac_int32 * pPCMFrames
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:27
decode_test__read_and_compare_pcm_frames_s16
drflac_result decode_test__read_and_compare_pcm_frames_s16(libflac *pLibFlac, drflac *pFlac, drflac_uint64 pcmFrameCount, drflac_int16 *pPCMFrames_libflac, drflac_int16 *pPCMFrames_drflac)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:172
decode_test__read_and_compare_pcm_frame_chunks_f32
drflac_result decode_test__read_and_compare_pcm_frame_chunks_f32(libflac *pLibFlac, drflac *pFlac, drflac_uint64 pcmFrameChunkSize)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:134
DRFLAC_TRUE
#define DRFLAC_TRUE
Definition: porcupine/demo/c/dr_libs/dr_flac.h:271
decode_test_file
drflac_result decode_test_file(const char *pFilePath)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:319
dr_printf_fixed
int dr_printf_fixed(int width, const char *const format,...)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:729
dr_file_iterator::absolutePath
char absolutePath[256]
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:382
drflac_read_pcm_frames_s32
DRFLAC_API drflac_uint64 drflac_read_pcm_frames_s32(drflac *pFlac, drflac_uint64 framesToRead, drflac_int32 *pBufferOut)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:57386
libflac_uninit
void libflac_uninit(libflac *pDecoder)
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:215
decode_test_directory
drflac_result decode_test_directory(const char *pDirectoryPath)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:377
decode_test
drflac_result decode_test()
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:404
decode_test_file_s32
drflac_result decode_test_file_s32(libflac *pLibFlac, drflac *pFlac)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:253
decode_test__read_and_compare_pcm_frame_chunks_s16
drflac_result decode_test__read_and_compare_pcm_frame_chunks_s16(libflac *pLibFlac, drflac *pFlac, drflac_uint64 pcmFrameChunkSize)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:214
python.test_porcupine.argv
argv
Definition: test_porcupine.py:158
drflac_uint32
unsigned int drflac_uint32
Definition: porcupine/demo/c/dr_libs/dr_flac.h:246
NUMBER_WIDTH
#define NUMBER_WIDTH
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:7
print_cpu_caps
void print_cpu_caps()
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:312
drflac_close
DRFLAC_API void drflac_close(drflac *pFlac)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:56763
decode_profiling
drflac_result decode_profiling()
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:731
drflac_seek_to_pcm_frame
DRFLAC_API drflac_bool32 drflac_seek_to_pcm_frame(drflac *pFlac, drflac_uint64 pcmFrameIndex)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:58917
dr_file_iterator_begin
dr_file_iterator * dr_file_iterator_begin(const char *pFolderPath, dr_file_iterator *pState)
Definition: porcupine/demo/c/dr_libs/tests/common/dr_common.c:391
drflac_open_file_and_read_pcm_frames_s16
DRFLAC_API drflac_int16 * drflac_open_file_and_read_pcm_frames_s16(const char *filename, unsigned int *channels, unsigned int *sampleRate, drflac_uint64 *totalPCMFrameCount, const drflac_allocation_callbacks *pAllocationCallbacks)
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:59136
DRFLAC_SUCCESS
#define DRFLAC_SUCCESS
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:51357
drflac_int32
signed int drflac_int32
Definition: porcupine/demo/c/dr_libs/dr_flac.h:245
open_and_read_test
drflac_result open_and_read_test()
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:616
libflac_read_pcm_frames_s32
drflac_uint64 libflac_read_pcm_frames_s32(libflac *pDecoder, drflac_uint64 framesToRead, drflac_int32 *pBufferOut)
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:224
drflac_result
drflac_int32 drflac_result
Definition: porcupine/demo/c/pvrecorder/src/miniaudio/extras/miniaudio_split/miniaudio.c:51356
libflac::currentPCMFrame
drflac_uint64 currentPCMFrame
Definition: porcupine/demo/c/dr_libs/tests/flac/dr_flac_common.c:32
decode_test_file_f32
drflac_result decode_test_file_f32(libflac *pLibFlac, drflac *pFlac)
Definition: rhino/demo/c/dr_libs/tests/flac/dr_flac_decoding.c:275


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:52