sndfile-cmp.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. ** Copyright (C) 2008-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
  3. ** Copyright (C) 2008 Conrad Parker <conrad@metadecks.org>
  4. **
  5. ** All rights reserved.
  6. **
  7. ** Redistribution and use in source and binary forms, with or without
  8. ** modification, are permitted provided that the following conditions are
  9. ** met:
  10. **
  11. ** * Redistributions of source code must retain the above copyright
  12. ** notice, this list of conditions and the following disclaimer.
  13. ** * Redistributions in binary form must reproduce the above copyright
  14. ** notice, this list of conditions and the following disclaimer in
  15. ** the documentation and/or other materials provided with the
  16. ** distribution.
  17. ** * Neither the author nor the names of any contributors may be used
  18. ** to endorse or promote products derived from this software without
  19. ** specific prior written permission.
  20. **
  21. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  23. ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  24. ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  25. ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  28. ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  29. ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  30. ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  31. ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include "sfconfig.h"
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include <inttypes.h>
  38. #include <sndfile.h>
  39. #include "common.h"
  40. /* Length of comparison data buffers in units of items */
  41. #define BUFLEN 65536
  42. static const char * progname = NULL ;
  43. static char * filename1 = NULL, * filename2 = NULL ;
  44. static int
  45. comparison_error (const char * what, sf_count_t frame_offset)
  46. { char buffer [128] = "" ;
  47. if (frame_offset >= 0)
  48. snprintf (buffer, sizeof (buffer), " (at frame offset %" PRId64 ")", frame_offset) ;
  49. printf ("%s: %s of files %s and %s differ%s.\n", progname, what, filename1, filename2, buffer) ;
  50. return 1 ;
  51. } /* comparison_error */
  52. static int
  53. compare (void)
  54. {
  55. double buf1 [BUFLEN], buf2 [BUFLEN] ;
  56. SF_INFO sfinfo1, sfinfo2 ;
  57. SNDFILE * sf1 = NULL, * sf2 = NULL ;
  58. sf_count_t items, i, nread1, nread2, offset = 0 ;
  59. int retval = 0 ;
  60. memset (&sfinfo1, 0, sizeof (SF_INFO)) ;
  61. sf1 = sf_open (filename1, SFM_READ, &sfinfo1) ;
  62. if (sf1 == NULL)
  63. { printf ("Error opening %s.\n", filename1) ;
  64. retval = 1 ;
  65. goto out ;
  66. } ;
  67. memset (&sfinfo2, 0, sizeof (SF_INFO)) ;
  68. sf2 = sf_open (filename2, SFM_READ, &sfinfo2) ;
  69. if (sf2 == NULL)
  70. { printf ("Error opening %s.\n", filename2) ;
  71. retval = 1 ;
  72. goto out ;
  73. } ;
  74. if (sfinfo1.samplerate != sfinfo2.samplerate)
  75. { retval = comparison_error ("Samplerates", -1) ;
  76. goto out ;
  77. } ;
  78. if (sfinfo1.channels != sfinfo2.channels)
  79. { retval = comparison_error ("Number of channels", -1) ;
  80. goto out ;
  81. } ;
  82. /* Calculate the framecount that will fit in our data buffers */
  83. items = BUFLEN / sfinfo1.channels ;
  84. while ((nread1 = sf_readf_double (sf1, buf1, items)) > 0)
  85. { nread2 = sf_readf_double (sf2, buf2, nread1) ;
  86. if (nread2 != nread1)
  87. { retval = comparison_error ("PCM data lengths", -1) ;
  88. goto out ;
  89. } ;
  90. for (i = 0 ; i < nread1 * sfinfo1.channels ; i++)
  91. { if (buf1 [i] != buf2 [i])
  92. { retval = comparison_error ("PCM data", offset + i / sfinfo1.channels) ;
  93. goto out ;
  94. } ;
  95. } ;
  96. offset += nread1 ;
  97. } ;
  98. if ((nread2 = sf_readf_double (sf2, buf2, items)) != 0)
  99. { retval = comparison_error ("PCM data lengths", -1) ;
  100. goto out ;
  101. } ;
  102. out :
  103. sf_close (sf1) ;
  104. sf_close (sf2) ;
  105. return retval ;
  106. } /* compare */
  107. static void
  108. print_version (void)
  109. { char buffer [256] ;
  110. sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ;
  111. printf ("\nVersion : %s\n\n", buffer) ;
  112. } /* print_version */
  113. static void
  114. usage_exit (void)
  115. {
  116. print_version () ;
  117. printf ("Usage : %s <filename> <filename>\n", progname) ;
  118. printf (" Compare the PCM data of two sound files.\n\n") ;
  119. exit (0) ;
  120. } /* usage_exit */
  121. int
  122. main (int argc, char *argv [])
  123. {
  124. progname = program_name (argv [0]) ;
  125. if (argc != 3)
  126. { usage_exit () ;
  127. return 1 ;
  128. } ;
  129. filename1 = argv [argc - 2] ;
  130. filename2 = argv [argc - 1] ;
  131. if (strcmp (filename1, filename2) == 0)
  132. { printf ("Error : Input filenames are the same.\n\n") ;
  133. usage_exit () ;
  134. return 1 ;
  135. } ;
  136. return compare () ;
  137. } /* main */