cue_test.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #include "sfconfig.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <sndfile.h>
  6. static void *
  7. get_cues (const char *filename, double *sr)
  8. {
  9. SNDFILE *file;
  10. SF_INFO sfinfo;
  11. unsigned int err, size;
  12. uint32_t count = 0;
  13. SF_CUES_VAR(0) *info;
  14. if ((file = sf_open(filename, SFM_READ, &sfinfo)) == NULL)
  15. {
  16. printf("can't open file '%s'\n", filename);
  17. exit(1);
  18. }
  19. printf("\n---- get cues of file '%s'\n", filename);
  20. if ((err = sf_command(file, SFC_GET_CUE_COUNT, &count, sizeof(uint32_t))) == SF_FALSE)
  21. {
  22. if (sf_error(file))
  23. {
  24. printf("can't get cue info size for file '%s' (arg size %lu), err %s\n",
  25. filename, sizeof(uint32_t), sf_strerror(file));
  26. exit(2);
  27. }
  28. else
  29. printf("no cue info for file '%s'\n", filename);
  30. return NULL;
  31. }
  32. size = sizeof(*info) + count * sizeof(SF_CUE_POINT);
  33. printf("number of cues %d size %d\n", count, size);
  34. if (!(info = malloc(size)))
  35. return NULL;
  36. if (sf_command(file, SFC_GET_CUE, info, size) == SF_FALSE)
  37. {
  38. printf("can't get cue info of size %d for file '%s' error %s\n",
  39. size, filename, sf_strerror(file));
  40. exit(3);
  41. }
  42. *sr = sfinfo.samplerate;
  43. sf_close(file);
  44. return info;
  45. }
  46. static void
  47. test_cues (const char *filename)
  48. {
  49. unsigned int i;
  50. double sr;
  51. SF_CUES_VAR(0) *info = get_cues(filename, &sr);
  52. if (info == NULL)
  53. exit(1);
  54. for (i = 0; i < info->cue_count; i++)
  55. {
  56. int pos = info->cue_points[i].position;
  57. double t = (double) pos / sr;
  58. double expected = i < 8 ? (double) i / 3. : 10. / 3.;
  59. double error = (double) fabs(t - expected);
  60. printf("cue %02d: markerID %02d position %6d offset %6d (time %.3f expected %.3f diff %f) label '%s'\n",
  61. i, info->cue_points[i].indx, pos, info->cue_points[i].sample_offset, t, expected, error, info->cue_points[i].name);
  62. if (error > 0.025)
  63. exit(4);
  64. }
  65. free(info);
  66. }
  67. static void
  68. print_cues (const char *filename)
  69. {
  70. unsigned int i;
  71. double sr;
  72. SF_CUES_VAR(0) *info = get_cues(filename, &sr);
  73. if (info == NULL)
  74. exit(1);
  75. for (i = 0; i < info->cue_count; i++)
  76. {
  77. int pos = info->cue_points[i].position;
  78. int indx = info->cue_points[i].indx;
  79. int cstart = info->cue_points[i].chunk_start;
  80. int bstart = info->cue_points[i].block_start;
  81. int offset = info->cue_points[i].sample_offset;
  82. const char *name = info->cue_points[i].name;
  83. double t = (double) pos / sr;
  84. if (cstart != 0 || bstart != 0)
  85. printf("cue %02d time %7.3f: markerID %02d position %8d chunk_start %d block_start %d offset %8d label '%s'\n",
  86. i, t, indx, pos, offset, cstart, bstart, name);
  87. else
  88. printf("cue %02d time %7.3f: markerID %02d position %8d offset %8d label '%s'\n",
  89. i, t, indx, pos, offset, name);
  90. }
  91. free(info);
  92. }
  93. int
  94. main (int argc, char **argv)
  95. {
  96. int i;
  97. if (argc > 1)
  98. for (i = 1; i < argc; i++)
  99. print_cues(argv[i]);
  100. else
  101. {
  102. test_cues("clickpluck24.wav");
  103. test_cues("clickpluck.wav");
  104. test_cues("clickpluck.aiff");
  105. }
  106. return 0;
  107. }