~ubuntu-branches/ubuntu/gutsy/audacity/gutsy-backports

« back to all changes in this revision

Viewing changes to lib-src/libsndfile/examples/cooledit-fixer.c

  • Committer: Bazaar Package Importer
  • Author(s): John Dong
  • Date: 2008-02-18 21:58:19 UTC
  • mfrom: (13.1.2 hardy)
  • Revision ID: james.westby@ubuntu.com-20080218215819-tmbcf1rx238r8gdv
Tags: 1.3.4-1.1ubuntu1~gutsy1
Automated backport upload; no source changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
** Copyright (C) 2002-2005 Erik de Castro Lopo <erikd@mega-nerd.com>
3
 
**
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.
8
 
**
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.
13
 
**
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.
17
 
*/
18
 
 
19
 
#include <stdio.h>
20
 
#include <stdlib.h>
21
 
#include <string.h>
22
 
#include <math.h>
23
 
#include <sys/stat.h>
24
 
 
25
 
#include <sndfile.h>
26
 
 
27
 
#define BUFFER_LEN      1024
28
 
 
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) ;
33
 
 
34
 
static  int buffer [BUFFER_LEN] ;
35
 
 
36
 
int
37
 
main (int argc, char *argv [])
38
 
{       SNDFILE *sndfile ;
39
 
        SF_INFO sfinfo ;
40
 
        int k, data_is_float, converted = 0 ;
41
 
 
42
 
        puts ("\nCooledit Fixer.\n---------------") ;
43
 
 
44
 
        if (argc < 2)
45
 
                usage_exit (argv [0]) ;
46
 
 
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]) ;-*/
50
 
                        continue ;
51
 
                        } ;
52
 
 
53
 
                if (sfinfo.format != (SF_FORMAT_WAV | SF_FORMAT_PCM_32))
54
 
                {       /*-printf ("%-50s : not a 32 bit PCM WAV file.\n", argv [k]) ;-*/
55
 
                        sf_close (sndfile) ;
56
 
                        continue ;
57
 
                        } ;
58
 
 
59
 
                data_is_float = is_data_really_float (sndfile) ;
60
 
 
61
 
                sf_close (sndfile) ;
62
 
 
63
 
                if (data_is_float == SF_FALSE)
64
 
                {       /*-printf ("%-50s : not a Cooledit abomination.\n", argv [k]) ;-*/
65
 
                        continue ;
66
 
                        } ;
67
 
 
68
 
                fix_file (argv [k]) ;
69
 
                converted ++ ;
70
 
                } ;
71
 
 
72
 
        if (converted == 0)
73
 
                puts ("\nNo files converted.") ;
74
 
 
75
 
        puts ("") ;
76
 
 
77
 
        return 0 ;
78
 
} /* main */
79
 
 
80
 
 
81
 
static void
82
 
usage_exit (char *progname)
83
 
{       char *cptr ;
84
 
 
85
 
        if ((cptr = strrchr (progname, '/')))
86
 
                progname = cptr + 1 ;
87
 
        if ((cptr = strrchr (progname, '\\')))
88
 
                progname = cptr + 1 ;
89
 
 
90
 
        printf ("\n    Usage : %s <filename>\n", progname) ;
91
 
        puts ("\n"
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"
96
 
                "\n"
97
 
                "More than one file may be included on the command line. \n"
98
 
                ) ;
99
 
 
100
 
        exit (1) ;
101
 
} /* usage_exit */
102
 
 
103
 
static int
104
 
is_data_really_float (SNDFILE *sndfile)
105
 
{       float   *fptr ;
106
 
        int     k, readcount ;
107
 
 
108
 
        fptr = (float *) buffer ;
109
 
 
110
 
        while ((readcount = sf_read_int (sndfile, buffer, BUFFER_LEN)) > 0)
111
 
        {       for (k = 0 ; k < readcount ; k++)
112
 
                {       if (buffer [k] == 0)
113
 
                                continue ;
114
 
 
115
 
                        if (fabs (fptr [k]) > 32768.0)
116
 
                                return SF_FALSE ;
117
 
                        } ;
118
 
                } ;
119
 
 
120
 
        return SF_TRUE ;
121
 
} /* is_data_really_float */
122
 
 
123
 
static void
124
 
fix_file (char *filename)
125
 
{       static  char    newfilename [512] ;
126
 
 
127
 
        SNDFILE *infile, *outfile ;
128
 
        SF_INFO sfinfo ;
129
 
        int             readcount, k ;
130
 
        float   *fptr, normfactor ;
131
 
        char    *cptr ;
132
 
 
133
 
        printf ("\nFixing : %s\n", filename) ;
134
 
 
135
 
        if ((infile = sf_open (filename, SFM_READ, &sfinfo)) == NULL)
136
 
        {       printf ("Not able to open input file %s\n", filename) ;
137
 
                exit (1) ;
138
 
                } ;
139
 
 
140
 
        if (strlen (filename) >= sizeof (newfilename) - 1)
141
 
        {       puts ("Error : Path name too long.\n") ;
142
 
                exit (1) ;
143
 
                } ;
144
 
 
145
 
        strncpy (newfilename, filename, sizeof (newfilename)) ;
146
 
        newfilename [sizeof (newfilename) - 1] = 0 ;
147
 
 
148
 
        if ((cptr = strrchr (newfilename, '/')) == NULL)
149
 
                cptr = strrchr (newfilename, '\\') ;
150
 
 
151
 
        if (cptr)
152
 
        {       cptr [1] = 0 ;
153
 
                strncat (newfilename, "fixed.wav", sizeof (newfilename) - strlen (newfilename) - 1) ;
154
 
                }
155
 
        else
156
 
                strncpy (newfilename, "fixed.wav", sizeof (newfilename) - 1) ;
157
 
 
158
 
        newfilename [sizeof (newfilename) - 1] = 0 ;
159
 
 
160
 
        printf ("    Output   : %s\n", newfilename) ;
161
 
 
162
 
        sfinfo.format = SF_FORMAT_WAV | SF_FORMAT_FLOAT ;
163
 
 
164
 
        if ((outfile = sf_open (newfilename, SFM_WRITE, &sfinfo)) == NULL)
165
 
        {       printf ("Not able to output open file %s\n", filename) ;
166
 
                exit (1) ;
167
 
                } ;
168
 
 
169
 
        /* Find the file peak. sf-command (SFC_CALC_SIGNAL_MAX) cannot be used. */
170
 
 
171
 
        fptr = (float *) buffer ;
172
 
 
173
 
        normfactor = 0.0 ;
174
 
 
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]) ;
179
 
                } ;
180
 
 
181
 
        printf ("    Peak     : %g\n", normfactor) ;
182
 
 
183
 
        normfactor = 1.0 / normfactor ;
184
 
 
185
 
        sf_seek (infile, 0, SEEK_SET) ;
186
 
 
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) ;
191
 
                } ;
192
 
 
193
 
        sf_close (infile) ;
194
 
        sf_close (outfile) ;
195
 
 
196
 
        if (abs (file_size (filename) - file_size (newfilename)) > 50)
197
 
        {       puts ("Error : file size mismatch.\n") ;
198
 
                exit (1) ;
199
 
                } ;
200
 
 
201
 
        printf ("    Renaming : %s\n", filename) ;
202
 
 
203
 
        if (remove (filename) != 0)
204
 
        {       perror ("rename") ;
205
 
                exit (1) ;
206
 
                } ;
207
 
 
208
 
        if (rename (newfilename, filename) != 0)
209
 
        {       perror ("rename") ;
210
 
                exit (1) ;
211
 
                } ;
212
 
 
213
 
        return ;
214
 
} /* fix_file */
215
 
 
216
 
static off_t
217
 
file_size (char *filename)
218
 
{       struct stat buf ;
219
 
 
220
 
        if (stat (filename, &buf) != 0)
221
 
        {       perror ("stat") ;
222
 
                exit (1) ;
223
 
                } ;
224
 
 
225
 
        return buf.st_size ;
226
 
} /* file_size */
227
 
/*
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.
231
 
**
232
 
** arch-tag: 5475655e-3898-40ff-969b-c8ab2351b0e4
233
 
*/