8 #define BUFFER_LEN 1024
10 static void usage_exit (char *progname
) ;
11 static int is_data_really_float (SNDFILE
*sndfile
) ;
12 static void fix_file (char *filename
) ;
13 static off_t
file_size (char *filename
) ;
15 static int buffer
[BUFFER_LEN
] ;
18 main (int argc
, char *argv
[])
21 int k
, data_is_float
, converted
= 0 ;
23 puts ("\nCooledit Fixer.\n---------------") ;
26 usage_exit (argv
[0]) ;
28 for (k
= 1 ; k
< argc
; k
++)
29 { if ((sndfile
= sf_open (argv
[k
], SFM_READ
, &sfinfo
)) == NULL
)
30 { /*-printf ("Failed to open : %s\n", argv [k]) ;-*/
34 if (sfinfo
.format
!= (SF_FORMAT_WAV
| SF_FORMAT_PCM_32
))
35 { /*-printf ("%-50s : not a 32 bit PCM WAV file.\n", argv [k]) ;-*/
40 data_is_float
= is_data_really_float (sndfile
) ;
44 if (data_is_float
== SF_FALSE
)
45 { /*-printf ("%-50s : not a Cooledit abomination.\n", argv [k]) ;-*/
54 puts ("\nNo files converted.") ;
63 usage_exit (char *progname
)
66 if ((cptr
= strrchr (progname
, '/')))
68 if ((cptr
= strrchr (progname
, '\\')))
71 printf ("\n Usage : %s <filename>\n", progname
) ;
73 "Fix broken files created by Syntrillium's Cooledit. These files are \n"
74 "marked as containing PCM data but actually contain floating point \n"
75 "data. Only the broken files created by Cooledit are processed. All \n"
76 "other files remain untouched.\n"
78 "More than one file may be included on the command line. \n"
85 is_data_really_float (SNDFILE
*sndfile
)
89 fptr
= (float *) buffer
;
91 while ((readcount
= sf_read_int (sndfile
, buffer
, BUFFER_LEN
)) > 0)
92 { for (k
= 0 ; k
< readcount
; k
++)
93 { if (buffer
[k
] == 0)
96 if (fabs (fptr
[k
]) > 32768.0)
102 } /* is_data_really_float */
105 fix_file (char *filename
)
106 { static char newfilename
[512] ;
108 SNDFILE
*infile
, *outfile
;
111 float *fptr
, normfactor
;
114 printf ("\nFixing : %s\n", filename
) ;
116 if ((infile
= sf_open (filename
, SFM_READ
, &sfinfo
)) == NULL
)
117 { printf ("Not able to open input file %s\n", filename
) ;
121 if (strlen (filename
) >= sizeof (newfilename
) - 1)
122 { puts ("Error : Path name too long.\n") ;
126 strncpy (newfilename
, filename
, sizeof (newfilename
)) ;
127 newfilename
[sizeof (newfilename
) - 1] = 0 ;
129 if ((cptr
= strrchr (newfilename
, '/')) == NULL
)
130 cptr
= strrchr (newfilename
, '\\') ;
134 strncat (newfilename
, "fixed.wav", sizeof (newfilename
) - strlen (newfilename
) - 1) ;
137 strncpy (newfilename
, "fixed.wav", sizeof (newfilename
) - 1) ;
139 newfilename
[sizeof (newfilename
) - 1] = 0 ;
141 printf (" Output : %s\n", newfilename
) ;
143 sfinfo
.format
= SF_FORMAT_WAV
| SF_FORMAT_FLOAT
;
145 if ((outfile
= sf_open (newfilename
, SFM_WRITE
, &sfinfo
)) == NULL
)
146 { printf ("Not able to output open file %s\n", filename
) ;
150 /* Find the file peak. sf-command (SFC_CALC_SIGNAL_MAX) cannot be used. */
152 fptr
= (float *) buffer
;
156 while ((readcount
= sf_read_int (infile
, buffer
, BUFFER_LEN
)) > 0)
157 { for (k
= 0 ; k
< readcount
; k
++)
158 if (fabs (fptr
[k
]) > normfactor
)
159 normfactor
= fabs (fptr
[k
]) ;
162 printf (" Peak : %g\n", normfactor
) ;
164 normfactor
= 1.0 / normfactor
;
166 sf_seek (infile
, 0, SEEK_SET
) ;
168 while ((readcount
= sf_read_int (infile
, buffer
, BUFFER_LEN
)) > 0)
169 { for (k
= 0 ; k
< readcount
; k
++)
170 fptr
[k
] *= normfactor
;
171 sf_write_float (outfile
, fptr
, readcount
) ;
177 if (abs (file_size (filename
) - file_size (newfilename
)) > 50)
178 { puts ("Error : file size mismatch.\n") ;
182 printf (" Renaming : %s\n", filename
) ;
184 if (remove (filename
) != 0)
185 { perror ("rename") ;
189 if (rename (newfilename
, filename
) != 0)
190 { perror ("rename") ;
198 file_size (char *filename
)
201 if (stat (filename
, &buf
) != 0)