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