audiomatch.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <math.h>
  22. #include <inttypes.h>
  23. #define FFMIN(a,b) ((a) > (b) ? (b) : (a))
  24. #define FFMAX(a,b) ((a) > (b) ? (a) : (b))
  25. #define FFABS(a) ((a) >= 0 ? (a) : (-(a)))
  26. static int64_t fsize(FILE *f) {
  27. int64_t end, pos = ftell(f);
  28. fseek(f, 0, SEEK_END);
  29. end = ftell(f);
  30. fseek(f, pos, SEEK_SET);
  31. return end;
  32. }
  33. int main(int argc, char **argv) {
  34. FILE *f[2];
  35. int i, pos;
  36. int siglen, datlen;
  37. int bestpos = 0;
  38. double bestc = 0;
  39. double sigamp = 0;
  40. int16_t *signal, *data;
  41. int maxshift = 16384;
  42. if (argc < 3) {
  43. printf("audiomatch <testfile> <reffile>\n");
  44. printf("WAV headers are skipped automatically.\n");
  45. return 1;
  46. }
  47. f[0] = fopen(argv[1], "rb");
  48. f[1] = fopen(argv[2], "rb");
  49. if (!f[0] || !f[1]) {
  50. fprintf(stderr, "Could not open input files.\n");
  51. return 1;
  52. }
  53. for (i = 0; i < 2; i++) {
  54. uint8_t p[100];
  55. if (fread(p, 1, 12, f[i]) != 12)
  56. return 1;
  57. if (!memcmp(p, "RIFF", 4) &&
  58. !memcmp(p + 8, "WAVE", 4)) {
  59. if (fread(p, 1, 8, f[i]) != 8)
  60. return 1;
  61. while (memcmp(p, "data", 4)) {
  62. int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24;
  63. fseek(f[i], s, SEEK_CUR);
  64. if (fread(p, 1, 8, f[i]) != 8)
  65. return 1;
  66. }
  67. } else {
  68. fseek(f[i], -12, SEEK_CUR);
  69. }
  70. }
  71. datlen = fsize(f[0]) - ftell(f[0]);
  72. siglen = fsize(f[1]) - ftell(f[1]);
  73. data = malloc(datlen * sizeof(*data));
  74. signal = malloc(siglen * sizeof(*signal));
  75. if (fread(data , 1, datlen, f[0]) != datlen)
  76. goto read_fail;
  77. if (fread(signal, 1, siglen, f[1]) != siglen)
  78. goto read_fail;
  79. datlen /= 2;
  80. siglen /= 2;
  81. for (i = 0; i < siglen; i++) {
  82. signal[i] = ((uint8_t*)(signal + i))[0] + 256*((uint8_t*)(signal + i))[1];
  83. sigamp += signal[i] * signal[i];
  84. }
  85. for (i = 0; i < datlen; i++)
  86. data[i] = ((uint8_t*)(data + i))[0] + 256*((uint8_t*)(data + i))[1];
  87. for (pos = 0; pos < maxshift; pos = pos < 0 ? -pos: -pos-1) {
  88. int64_t c = 0;
  89. int testlen = FFMIN(siglen, datlen-pos);
  90. for (i = FFMAX(0, -pos); i < testlen; i++) {
  91. int j = pos + i;
  92. c += signal[i] * data[j];
  93. }
  94. if (FFABS(c) > sigamp * 0.94)
  95. maxshift = FFMIN(maxshift, FFABS(pos)+32);
  96. if (FFABS(c) > FFABS(bestc)) {
  97. bestc = c;
  98. bestpos = pos;
  99. }
  100. }
  101. printf("presig: %d postsig:%d c:%7.4f lenerr:%d\n", bestpos, datlen - siglen - bestpos, bestc / sigamp, datlen - siglen);
  102. free(data);
  103. free(signal);
  104. return 0;
  105. read_fail:
  106. free(data);
  107. free(signal);
  108. return 1;
  109. }