2 ** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
10 ** * Redistributions of source code must retain the above copyright
11 ** notice, this list of conditions and the following disclaimer.
12 ** * Redistributions in binary form must reproduce the above copyright
13 ** notice, this list of conditions and the following disclaimer in
14 ** the documentation and/or other materials provided with the
16 ** * Neither the author nor the names of any contributors may be used
17 ** to endorse or promote products derived from this software without
18 ** specific prior written permission.
20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 #define BLOCK_SIZE 512
43 print_usage (char *progname
)
44 { printf ("\nUsage : %s <input file> <output file>\n", progname
) ;
46 " Where the output file will contain a line for each frame\n"
47 " and a column for each channel.\n"
53 convert_to_text (SNDFILE
* infile
, FILE * outfile
, int channels
)
54 { float buf
[channels
* BLOCK_SIZE
] ;
57 while ((readcount
= sf_readf_float (infile
, buf
, BLOCK_SIZE
)) > 0)
58 { for (k
= 0 ; k
< readcount
; k
++)
59 { for (m
= 0 ; m
< channels
; m
++)
60 fprintf (outfile
, " % 12.10f", buf
[k
* channels
+ m
]) ;
61 fprintf (outfile
, "\n") ;
66 } /* convert_to_text */
69 main (int argc
, char * argv
[])
70 { char *progname
, *infilename
, *outfilename
;
71 SNDFILE
*infile
= NULL
;
72 FILE *outfile
= NULL
;
75 progname
= strrchr (argv
[0], '/') ;
76 progname
= progname
? progname
+ 1 : argv
[0] ;
79 { print_usage (progname
) ;
83 infilename
= argv
[1] ;
84 outfilename
= argv
[2] ;
86 if (strcmp (infilename
, outfilename
) == 0)
87 { printf ("Error : Input and output filenames are the same.\n\n") ;
88 print_usage (progname
) ;
92 if (infilename
[0] == '-')
93 { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename
) ;
94 print_usage (progname
) ;
98 if (outfilename
[0] == '-')
99 { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename
) ;
100 print_usage (progname
) ;
104 if ((infile
= sf_open (infilename
, SFM_READ
, &sfinfo
)) == NULL
)
105 { printf ("Not able to open input file %s.\n", infilename
) ;
106 puts (sf_strerror (NULL
)) ;
110 /* Open the output file. */
111 if ((outfile
= fopen (outfilename
, "w")) == NULL
)
112 { printf ("Not able to open output file %s : %s\n", outfilename
, sf_strerror (NULL
)) ;
116 fprintf (outfile
, "# Converted from file %s.\n", infilename
) ;
117 fprintf (outfile
, "# Channels %d, Sample rate %d\n", sfinfo
.channels
, sfinfo
.samplerate
) ;
119 convert_to_text (infile
, outfile
, sfinfo
.channels
) ;