annotate src/libsndfile-1.0.27/programs/sndfile-cmp.c @ 127:7867fa7e1b6b

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