cannam@125: /* cannam@125: ** Copyright (C) 2008-2016 Erik de Castro Lopo cannam@125: ** Copyright (C) 2008 Conrad Parker cannam@125: ** cannam@125: ** All rights reserved. cannam@125: ** cannam@125: ** Redistribution and use in source and binary forms, with or without cannam@125: ** modification, are permitted provided that the following conditions are cannam@125: ** met: cannam@125: ** cannam@125: ** * Redistributions of source code must retain the above copyright cannam@125: ** notice, this list of conditions and the following disclaimer. cannam@125: ** * Redistributions in binary form must reproduce the above copyright cannam@125: ** notice, this list of conditions and the following disclaimer in cannam@125: ** the documentation and/or other materials provided with the cannam@125: ** distribution. cannam@125: ** * Neither the author nor the names of any contributors may be used cannam@125: ** to endorse or promote products derived from this software without cannam@125: ** specific prior written permission. cannam@125: ** cannam@125: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS cannam@125: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED cannam@125: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR cannam@125: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR cannam@125: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, cannam@125: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, cannam@125: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; cannam@125: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, cannam@125: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR cannam@125: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF cannam@125: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. cannam@125: */ cannam@125: cannam@125: #include "sfconfig.h" cannam@125: cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: #include cannam@125: cannam@125: #include cannam@125: cannam@125: #include "common.h" cannam@125: cannam@125: /* Length of comparison data buffers in units of items */ cannam@125: #define BUFLEN 65536 cannam@125: cannam@125: static const char * progname = NULL ; cannam@125: static char * filename1 = NULL, * filename2 = NULL ; cannam@125: cannam@125: static int cannam@125: comparison_error (const char * what, sf_count_t frame_offset) cannam@125: { char buffer [128] ; cannam@125: cannam@125: if (frame_offset >= 0) cannam@125: snprintf (buffer, sizeof (buffer), " (at frame offset %" PRId64 ")", frame_offset) ; cannam@125: else cannam@125: buffer [0] = 0 ; cannam@125: cannam@125: printf ("%s: %s of files %s and %s differ%s.\n", progname, what, filename1, filename2, buffer) ; cannam@125: return 1 ; cannam@125: } /* comparison_error */ cannam@125: cannam@125: static int cannam@125: compare (void) cannam@125: { cannam@125: double buf1 [BUFLEN], buf2 [BUFLEN] ; cannam@125: SF_INFO sfinfo1, sfinfo2 ; cannam@125: SNDFILE * sf1 = NULL, * sf2 = NULL ; cannam@125: sf_count_t items, i, nread1, nread2, offset = 0 ; cannam@125: int retval = 0 ; cannam@125: cannam@125: memset (&sfinfo1, 0, sizeof (SF_INFO)) ; cannam@125: sf1 = sf_open (filename1, SFM_READ, &sfinfo1) ; cannam@125: if (sf1 == NULL) cannam@125: { printf ("Error opening %s.\n", filename1) ; cannam@125: retval = 1 ; cannam@125: goto out ; cannam@125: } ; cannam@125: cannam@125: memset (&sfinfo2, 0, sizeof (SF_INFO)) ; cannam@125: sf2 = sf_open (filename2, SFM_READ, &sfinfo2) ; cannam@125: if (sf2 == NULL) cannam@125: { printf ("Error opening %s.\n", filename2) ; cannam@125: retval = 1 ; cannam@125: goto out ; cannam@125: } ; cannam@125: cannam@125: if (sfinfo1.samplerate != sfinfo2.samplerate) cannam@125: { retval = comparison_error ("Samplerates", -1) ; cannam@125: goto out ; cannam@125: } ; cannam@125: cannam@125: if (sfinfo1.channels != sfinfo2.channels) cannam@125: { retval = comparison_error ("Number of channels", -1) ; cannam@125: goto out ; cannam@125: } ; cannam@125: cannam@125: /* Calculate the framecount that will fit in our data buffers */ cannam@125: items = BUFLEN / sfinfo1.channels ; cannam@125: cannam@125: while ((nread1 = sf_readf_double (sf1, buf1, items)) > 0) cannam@125: { nread2 = sf_readf_double (sf2, buf2, nread1) ; cannam@125: if (nread2 != nread1) cannam@125: { retval = comparison_error ("PCM data lengths", -1) ; cannam@125: goto out ; cannam@125: } ; cannam@125: for (i = 0 ; i < nread1 * sfinfo1.channels ; i++) cannam@125: { if (buf1 [i] != buf2 [i]) cannam@125: { retval = comparison_error ("PCM data", offset + i / sfinfo1.channels) ; cannam@125: goto out ; cannam@125: } ; cannam@125: } ; cannam@125: offset += nread1 ; cannam@125: } ; cannam@125: cannam@125: if ((nread2 = sf_readf_double (sf2, buf2, items)) != 0) cannam@125: { retval = comparison_error ("PCM data lengths", -1) ; cannam@125: goto out ; cannam@125: } ; cannam@125: cannam@125: out : cannam@125: sf_close (sf1) ; cannam@125: sf_close (sf2) ; cannam@125: cannam@125: return retval ; cannam@125: } /* compare */ cannam@125: cannam@125: static void cannam@125: usage_exit (void) cannam@125: { cannam@125: printf ("Usage : %s \n", progname) ; cannam@125: printf (" Compare the PCM data of two sound files.\n\n") ; cannam@125: printf ("Using %s.\n\n", sf_version_string ()) ; cannam@125: exit (1) ; cannam@125: } /* usage_exit */ cannam@125: cannam@125: int cannam@125: main (int argc, char *argv []) cannam@125: { cannam@125: progname = program_name (argv [0]) ; cannam@125: cannam@125: if (argc != 3) cannam@125: usage_exit () ; cannam@125: cannam@125: filename1 = argv [argc - 2] ; cannam@125: filename2 = argv [argc - 1] ; cannam@125: cannam@125: if (strcmp (filename1, filename2) == 0) cannam@125: { printf ("Error : Input filenames are the same.\n\n") ; cannam@125: usage_exit () ; cannam@125: } ; cannam@125: cannam@125: return compare () ; cannam@125: } /* main */