annotate src/libsndfile-1.0.25/programs/sndfile-cmp.c @ 23:619f715526df sv_v2.1

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