annotate src/libsndfile-1.0.25/programs/sndfile-cmp.c @ 169:223a55898ab9 tip default

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