annotate src/libsndfile-1.0.27/programs/sndfile-cmp.c @ 83:ae30d91d2ffe

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