2
** Copyright (C) 2002-2005 Erik de Castro Lopo <erikd@mega-nerd.com>
4
** This program is free software; you can redistribute it and/or modify
5
** it under the terms of the GNU General Public License as published by
6
** the Free Software Foundation; either version 2 of the License, or
7
** (at your option) any later version.
9
** This program is distributed in the hope that it will be useful,
10
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
** GNU General Public License for more details.
14
** You should have received a copy of the GNU General Public License
15
** along with this program; if not, write to the Free Software
16
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27
#define BUFFER_LEN 1024
29
static void usage_exit (char *progname) ;
30
static int is_data_really_float (SNDFILE *sndfile) ;
31
static void fix_file (char *filename) ;
32
static off_t file_size (char *filename) ;
34
static int buffer [BUFFER_LEN] ;
37
main (int argc, char *argv [])
40
int k, data_is_float, converted = 0 ;
42
puts ("\nCooledit Fixer.\n---------------") ;
45
usage_exit (argv [0]) ;
47
for (k = 1 ; k < argc ; k++)
48
{ if ((sndfile = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL)
49
{ /*-printf ("Failed to open : %s\n", argv [k]) ;-*/
53
if (sfinfo.format != (SF_FORMAT_WAV | SF_FORMAT_PCM_32))
54
{ /*-printf ("%-50s : not a 32 bit PCM WAV file.\n", argv [k]) ;-*/
59
data_is_float = is_data_really_float (sndfile) ;
63
if (data_is_float == SF_FALSE)
64
{ /*-printf ("%-50s : not a Cooledit abomination.\n", argv [k]) ;-*/
73
puts ("\nNo files converted.") ;
82
usage_exit (char *progname)
85
if ((cptr = strrchr (progname, '/')))
87
if ((cptr = strrchr (progname, '\\')))
90
printf ("\n Usage : %s <filename>\n", progname) ;
92
"Fix broken files created by Syntrillium's Cooledit. These files are \n"
93
"marked as containing PCM data but actually contain floating point \n"
94
"data. Only the broken files created by Cooledit are processed. All \n"
95
"other files remain untouched.\n"
97
"More than one file may be included on the command line. \n"
104
is_data_really_float (SNDFILE *sndfile)
108
fptr = (float *) buffer ;
110
while ((readcount = sf_read_int (sndfile, buffer, BUFFER_LEN)) > 0)
111
{ for (k = 0 ; k < readcount ; k++)
112
{ if (buffer [k] == 0)
115
if (fabs (fptr [k]) > 32768.0)
121
} /* is_data_really_float */
124
fix_file (char *filename)
125
{ static char newfilename [512] ;
127
SNDFILE *infile, *outfile ;
130
float *fptr, normfactor ;
133
printf ("\nFixing : %s\n", filename) ;
135
if ((infile = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
136
{ printf ("Not able to open input file %s\n", filename) ;
140
if (strlen (filename) >= sizeof (newfilename) - 1)
141
{ puts ("Error : Path name too long.\n") ;
145
strncpy (newfilename, filename, sizeof (newfilename)) ;
146
newfilename [sizeof (newfilename) - 1] = 0 ;
148
if ((cptr = strrchr (newfilename, '/')) == NULL)
149
cptr = strrchr (newfilename, '\\') ;
153
strncat (newfilename, "fixed.wav", sizeof (newfilename) - strlen (newfilename) - 1) ;
156
strncpy (newfilename, "fixed.wav", sizeof (newfilename) - 1) ;
158
newfilename [sizeof (newfilename) - 1] = 0 ;
160
printf (" Output : %s\n", newfilename) ;
162
sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT ;
164
if ((outfile = sf_open (newfilename, SFM_WRITE, &sfinfo)) == NULL)
165
{ printf ("Not able to output open file %s\n", filename) ;
169
/* Find the file peak. sf-command (SFC_CALC_SIGNAL_MAX) cannot be used. */
171
fptr = (float *) buffer ;
175
while ((readcount = sf_read_int (infile, buffer, BUFFER_LEN)) > 0)
176
{ for (k = 0 ; k < readcount ; k++)
177
if (fabs (fptr [k]) > normfactor)
178
normfactor = fabs (fptr [k]) ;
181
printf (" Peak : %g\n", normfactor) ;
183
normfactor = 1.0 / normfactor ;
185
sf_seek (infile, 0, SEEK_SET) ;
187
while ((readcount = sf_read_int (infile, buffer, BUFFER_LEN)) > 0)
188
{ for (k = 0 ; k < readcount ; k++)
189
fptr [k] *= normfactor ;
190
sf_write_float (outfile, fptr, readcount) ;
196
if (abs (file_size (filename) - file_size (newfilename)) > 50)
197
{ puts ("Error : file size mismatch.\n") ;
201
printf (" Renaming : %s\n", filename) ;
203
if (remove (filename) != 0)
204
{ perror ("rename") ;
208
if (rename (newfilename, filename) != 0)
209
{ perror ("rename") ;
217
file_size (char *filename)
220
if (stat (filename, &buf) != 0)
228
** Do not edit or modify anything in this comment block.
229
** The arch-tag line is a file identity tag for the GNU Arch
230
** revision control system.
232
** arch-tag: 5475655e-3898-40ff-969b-c8ab2351b0e4