audiomatch.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. static int64_t fsize(FILE *f){
  26. int64_t end, pos= ftell(f);
  27. fseek(f, 0, SEEK_END);
  28. end = ftell(f);
  29. fseek(f, pos, SEEK_SET);
  30. return end;
  31. }
  32. int main(int argc, char **argv){
  33. FILE *f[2];
  34. int i, pos;
  35. int siglen, datlen;
  36. int bestpos = 0;
  37. double bestc=0;
  38. double sigamp= 0;
  39. int16_t *signal, *data;
  40. int maxshift= 16384;
  41. if (argc < 3) {
  42. printf("audiomatch <testfile> <reffile>\n");
  43. printf("WAV headers are skipped automatically.\n");
  44. return 1;
  45. }
  46. f[0] = fopen(argv[1], "rb");
  47. f[1] = fopen(argv[2], "rb");
  48. if (!f[0] || !f[1]) {
  49. fprintf(stderr, "Could not open input files.\n");
  50. return 1;
  51. }
  52. for (i = 0; i < 2; i++) {
  53. uint8_t p[100];
  54. if (fread(p, 1, 12, f[i]) != 12)
  55. return 1;
  56. if (!memcmp(p, "RIFF", 4) &&
  57. !memcmp(p + 8, "WAVE", 4)) {
  58. if (fread(p, 1, 8, f[i]) != 8)
  59. return 1;
  60. while (memcmp(p, "data", 4)) {
  61. int s = p[4] | p[5] << 8 | p[6] << 16 | p[7] << 24;
  62. fseek(f[i], s, SEEK_CUR);
  63. if (fread(p, 1, 8, f[i]) != 8)
  64. return 1;
  65. }
  66. } else {
  67. fseek(f[i], -12, SEEK_CUR);
  68. }
  69. }
  70. datlen = fsize(f[0]) - ftell(f[0]);
  71. siglen = fsize(f[1]) - ftell(f[1]);
  72. data = malloc(datlen * sizeof(*data));
  73. signal = malloc(siglen * sizeof(*signal));
  74. fread(data , 1, datlen, f[0]);
  75. fread(signal, 1, siglen, f[1]);
  76. datlen /= 2;
  77. siglen /= 2;
  78. for(i=0; i<siglen; i++){
  79. signal[i] = ((uint8_t*)(signal + i))[0] + 256*((uint8_t*)(signal + i))[1];
  80. sigamp += signal[i] * signal[i];
  81. }
  82. for(i=0; i<datlen; i++)
  83. data[i] = ((uint8_t*)(data + i))[0] + 256*((uint8_t*)(data + i))[1];
  84. for(pos = 0; pos<maxshift; pos = pos < 0 ? -pos: -pos-1){
  85. int64_t c= 0;
  86. int testlen = FFMIN(siglen, datlen-pos);
  87. for(i=FFMAX(0, -pos); i<testlen; i++){
  88. int j= pos+i;
  89. c += signal[i] * data[j];
  90. }
  91. if(fabs(c) > sigamp * 0.94)
  92. maxshift = FFMIN(maxshift, fabs(pos)+32);
  93. if(fabs(c)>fabs(bestc)){
  94. bestc= c;
  95. bestpos = pos;
  96. }
  97. }
  98. printf("presig: %d postsig:%d c:%7.4f lenerr:%d\n", bestpos, datlen - siglen - bestpos, bestc / sigamp, datlen - siglen);
  99. return 0;
  100. }