2 ** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 ** Copyright (C) 2008 Conrad Parker <conrad@metadecks.org>
5 ** All rights reserved.
7 ** Redistribution and use in source and binary forms, with or without
8 ** modification, are permitted provided that the following conditions are
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
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.
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.
45 /* Length of comparison data buffers in units of items */
48 static const char * progname
= NULL
;
49 static char * filename1
= NULL
, * filename2
= NULL
;
52 comparison_error (const char * what
, sf_count_t frame_offset
)
53 { char buffer
[128] = "" ;
55 if (frame_offset
>= 0)
56 snprintf (buffer
, sizeof (buffer
), " (at frame offset %" PRId64
")", frame_offset
) ;
58 printf ("%s: %s of files %s and %s differ%s.\n", progname
, what
, filename1
, filename2
, buffer
) ;
60 } /* comparison_error */
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 ;
71 memset (&sfinfo1
, 0, sizeof (SF_INFO
)) ;
72 sf1
= sf_open (filename1
, SFM_READ
, &sfinfo1
) ;
74 { printf ("Error opening %s.\n", filename1
) ;
79 memset (&sfinfo2
, 0, sizeof (SF_INFO
)) ;
80 sf2
= sf_open (filename2
, SFM_READ
, &sfinfo2
) ;
82 { printf ("Error opening %s.\n", filename2
) ;
87 if (sfinfo1
.samplerate
!= sfinfo2
.samplerate
)
88 { retval
= comparison_error ("Samplerates", -1) ;
92 if (sfinfo1
.channels
!= sfinfo2
.channels
)
93 { retval
= comparison_error ("Number of channels", -1) ;
97 /* Calculate the framecount that will fit in our data buffers */
98 items
= BUFLEN
/ sfinfo1
.channels
;
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) ;
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
) ;
115 if ( (nread2
= sf_readf_double (sf2
, buf2
, nread1
)) != 0)
116 { retval
= comparison_error ("PCM data lengths", -1) ;
129 { char buffer
[256] ;
131 sf_command (NULL
, SFC_GET_LIB_VERSION
, buffer
, sizeof (buffer
)) ;
132 printf ("\nVersion : %s\n\n", buffer
) ;
133 } /* print_version */
140 printf ("Usage : %s <filename> <filename>\n", progname
) ;
141 printf (" Compare the PCM data of two sound files.\n\n") ;
146 main (int argc
, char *argv
[])
148 progname
= program_name (argv
[0]) ;
155 filename1
= argv
[argc
- 2] ;
156 filename2
= argv
[argc
- 1] ;
158 if (strcmp (filename1
, filename2
) == 0)
159 { printf ("Error : Input filenames are the same.\n\n") ;