Alexandre Lision | 7c6f4a6 | 2013-09-05 13:27:01 -0400 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (C) 2008-2011 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 | |
| 34 | #include "sfconfig.h" |
| 35 | |
| 36 | #include <stdio.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | #include <inttypes.h> |
| 40 | |
| 41 | #include <sndfile.h> |
| 42 | |
| 43 | #include "common.h" |
| 44 | |
| 45 | /* Length of comparison data buffers in units of items */ |
| 46 | #define BUFLEN 65536 |
| 47 | |
| 48 | static const char * progname = NULL ; |
| 49 | static char * filename1 = NULL, * filename2 = NULL ; |
| 50 | |
| 51 | static int |
| 52 | comparison_error (const char * what, sf_count_t frame_offset) |
| 53 | { char buffer [128] = "" ; |
| 54 | |
| 55 | if (frame_offset >= 0) |
| 56 | snprintf (buffer, sizeof (buffer), " (at frame offset %" PRId64 ")", frame_offset) ; |
| 57 | |
| 58 | printf ("%s: %s of files %s and %s differ%s.\n", progname, what, filename1, filename2, buffer) ; |
| 59 | return 1 ; |
| 60 | } /* comparison_error */ |
| 61 | |
| 62 | static int |
| 63 | compare (void) |
| 64 | { |
| 65 | double buf1 [BUFLEN], buf2 [BUFLEN] ; |
| 66 | SF_INFO sfinfo1, sfinfo2 ; |
| 67 | SNDFILE * sf1 = NULL, * sf2 = NULL ; |
| 68 | sf_count_t items, i, nread1, nread2, offset = 0 ; |
| 69 | int retval = 0 ; |
| 70 | |
| 71 | memset (&sfinfo1, 0, sizeof (SF_INFO)) ; |
| 72 | sf1 = sf_open (filename1, SFM_READ, &sfinfo1) ; |
| 73 | if (sf1 == NULL) |
| 74 | { printf ("Error opening %s.\n", filename1) ; |
| 75 | retval = 1 ; |
| 76 | goto out ; |
| 77 | } ; |
| 78 | |
| 79 | memset (&sfinfo2, 0, sizeof (SF_INFO)) ; |
| 80 | sf2 = sf_open (filename2, SFM_READ, &sfinfo2) ; |
| 81 | if (sf2 == NULL) |
| 82 | { printf ("Error opening %s.\n", filename2) ; |
| 83 | retval = 1 ; |
| 84 | goto out ; |
| 85 | } ; |
| 86 | |
| 87 | if (sfinfo1.samplerate != sfinfo2.samplerate) |
| 88 | { retval = comparison_error ("Samplerates", -1) ; |
| 89 | goto out ; |
| 90 | } ; |
| 91 | |
| 92 | if (sfinfo1.channels != sfinfo2.channels) |
| 93 | { retval = comparison_error ("Number of channels", -1) ; |
| 94 | goto out ; |
| 95 | } ; |
| 96 | |
| 97 | /* Calculate the framecount that will fit in our data buffers */ |
| 98 | items = BUFLEN / sfinfo1.channels ; |
| 99 | |
| 100 | while ( (nread1 = sf_readf_double (sf1, buf1, items)) > 0) |
| 101 | { nread2 = sf_readf_double (sf2, buf2, nread1) ; |
| 102 | if (nread2 != nread1) |
| 103 | { retval = comparison_error ("PCM data lengths", -1) ; |
| 104 | goto out ; |
| 105 | } ; |
| 106 | for (i = 0 ; i < nread1 * sfinfo1.channels ; i++) |
| 107 | { if (buf1 [i] != buf2 [i]) |
| 108 | { retval = comparison_error ("PCM data", offset + i / sfinfo1.channels) ; |
| 109 | goto out ; |
| 110 | } ; |
| 111 | } ; |
| 112 | offset += nread1 ; |
| 113 | } ; |
| 114 | |
| 115 | if ( (nread2 = sf_readf_double (sf2, buf2, nread1)) != 0) |
| 116 | { retval = comparison_error ("PCM data lengths", -1) ; |
| 117 | goto out ; |
| 118 | } ; |
| 119 | |
| 120 | out : |
| 121 | sf_close (sf1) ; |
| 122 | sf_close (sf2) ; |
| 123 | |
| 124 | return retval ; |
| 125 | } /* compare */ |
| 126 | |
| 127 | static void |
| 128 | print_version (void) |
| 129 | { char buffer [256] ; |
| 130 | |
| 131 | sf_command (NULL, SFC_GET_LIB_VERSION, buffer, sizeof (buffer)) ; |
| 132 | printf ("\nVersion : %s\n\n", buffer) ; |
| 133 | } /* print_version */ |
| 134 | |
| 135 | static void |
| 136 | usage_exit (void) |
| 137 | { |
| 138 | print_version () ; |
| 139 | |
| 140 | printf ("Usage : %s <filename> <filename>\n", progname) ; |
| 141 | printf (" Compare the PCM data of two sound files.\n\n") ; |
| 142 | exit (0) ; |
| 143 | } /* usage_exit */ |
| 144 | |
| 145 | int |
| 146 | main (int argc, char *argv []) |
| 147 | { |
| 148 | progname = program_name (argv [0]) ; |
| 149 | |
| 150 | if (argc != 3) |
| 151 | { usage_exit () ; |
| 152 | return 1 ; |
| 153 | } ; |
| 154 | |
| 155 | filename1 = argv [argc - 2] ; |
| 156 | filename2 = argv [argc - 1] ; |
| 157 | |
| 158 | if (strcmp (filename1, filename2) == 0) |
| 159 | { printf ("Error : Input filenames are the same.\n\n") ; |
| 160 | usage_exit () ; |
| 161 | return 1 ; |
| 162 | } ; |
| 163 | |
| 164 | return compare () ; |
| 165 | } /* main */ |