1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/*
** Copyright (C) 2002-2005 Erik de Castro Lopo <erikd@mega-nerd.com>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <sys/stat.h>
#include <sndfile.h>
#define BUFFER_LEN 1024
static void usage_exit (char *progname) ;
static int is_data_really_float (SNDFILE *sndfile) ;
static void fix_file (char *filename) ;
static off_t file_size (char *filename) ;
static int buffer [BUFFER_LEN] ;
int
main (int argc, char *argv [])
{ SNDFILE *sndfile ;
SF_INFO sfinfo ;
int k, data_is_float, converted = 0 ;
puts ("\nCooledit Fixer.\n---------------") ;
if (argc < 2)
usage_exit (argv [0]) ;
for (k = 1 ; k < argc ; k++)
{ if ((sndfile = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL)
{ /*-printf ("Failed to open : %s\n", argv [k]) ;-*/
continue ;
} ;
if (sfinfo.format != (SF_FORMAT_WAV | SF_FORMAT_PCM_32))
{ /*-printf ("%-50s : not a 32 bit PCM WAV file.\n", argv [k]) ;-*/
sf_close (sndfile) ;
continue ;
} ;
data_is_float = is_data_really_float (sndfile) ;
sf_close (sndfile) ;
if (data_is_float == SF_FALSE)
{ /*-printf ("%-50s : not a Cooledit abomination.\n", argv [k]) ;-*/
continue ;
} ;
fix_file (argv [k]) ;
converted ++ ;
} ;
if (converted == 0)
puts ("\nNo files converted.") ;
puts ("") ;
return 0 ;
} /* main */
static void
usage_exit (char *progname)
{ char *cptr ;
if ((cptr = strrchr (progname, '/')))
progname = cptr + 1 ;
if ((cptr = strrchr (progname, '\\')))
progname = cptr + 1 ;
printf ("\n Usage : %s <filename>\n", progname) ;
puts ("\n"
"Fix broken files created by Syntrillium's Cooledit. These files are \n"
"marked as containing PCM data but actually contain floating point \n"
"data. Only the broken files created by Cooledit are processed. All \n"
"other files remain untouched.\n"
"\n"
"More than one file may be included on the command line. \n"
) ;
exit (1) ;
} /* usage_exit */
static int
is_data_really_float (SNDFILE *sndfile)
{ float *fptr ;
int k, readcount ;
fptr = (float *) buffer ;
while ((readcount = sf_read_int (sndfile, buffer, BUFFER_LEN)) > 0)
{ for (k = 0 ; k < readcount ; k++)
{ if (buffer [k] == 0)
continue ;
if (fabs (fptr [k]) > 32768.0)
return SF_FALSE ;
} ;
} ;
return SF_TRUE ;
} /* is_data_really_float */
static void
fix_file (char *filename)
{ static char newfilename [512] ;
SNDFILE *infile, *outfile ;
SF_INFO sfinfo ;
int readcount, k ;
float *fptr, normfactor ;
char *cptr ;
printf ("\nFixing : %s\n", filename) ;
if ((infile = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
{ printf ("Not able to open input file %s\n", filename) ;
exit (1) ;
} ;
if (strlen (filename) >= sizeof (newfilename) - 1)
{ puts ("Error : Path name too long.\n") ;
exit (1) ;
} ;
strncpy (newfilename, filename, sizeof (newfilename)) ;
newfilename [sizeof (newfilename) - 1] = 0 ;
if ((cptr = strrchr (newfilename, '/')) == NULL)
cptr = strrchr (newfilename, '\\') ;
if (cptr)
{ cptr [1] = 0 ;
strncat (newfilename, "fixed.wav", sizeof (newfilename) - strlen (newfilename) - 1) ;
}
else
strncpy (newfilename, "fixed.wav", sizeof (newfilename) - 1) ;
newfilename [sizeof (newfilename) - 1] = 0 ;
printf (" Output : %s\n", newfilename) ;
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT ;
if ((outfile = sf_open (newfilename, SFM_WRITE, &sfinfo)) == NULL)
{ printf ("Not able to output open file %s\n", filename) ;
exit (1) ;
} ;
/* Find the file peak. sf-command (SFC_CALC_SIGNAL_MAX) cannot be used. */
fptr = (float *) buffer ;
normfactor = 0.0 ;
while ((readcount = sf_read_int (infile, buffer, BUFFER_LEN)) > 0)
{ for (k = 0 ; k < readcount ; k++)
if (fabs (fptr [k]) > normfactor)
normfactor = fabs (fptr [k]) ;
} ;
printf (" Peak : %g\n", normfactor) ;
normfactor = 1.0 / normfactor ;
sf_seek (infile, 0, SEEK_SET) ;
while ((readcount = sf_read_int (infile, buffer, BUFFER_LEN)) > 0)
{ for (k = 0 ; k < readcount ; k++)
fptr [k] *= normfactor ;
sf_write_float (outfile, fptr, readcount) ;
} ;
sf_close (infile) ;
sf_close (outfile) ;
if (abs (file_size (filename) - file_size (newfilename)) > 50)
{ puts ("Error : file size mismatch.\n") ;
exit (1) ;
} ;
printf (" Renaming : %s\n", filename) ;
if (remove (filename) != 0)
{ perror ("rename") ;
exit (1) ;
} ;
if (rename (newfilename, filename) != 0)
{ perror ("rename") ;
exit (1) ;
} ;
return ;
} /* fix_file */
static off_t
file_size (char *filename)
{ struct stat buf ;
if (stat (filename, &buf) != 0)
{ perror ("stat") ;
exit (1) ;
} ;
return buf.st_size ;
} /* file_size */
/*
** Do not edit or modify anything in this comment block.
** The arch-tag line is a file identity tag for the GNU Arch
** revision control system.
**
** arch-tag: 5475655e-3898-40ff-969b-c8ab2351b0e4
*/
|