libsndfile source files.
[Faustine.git] / interpretor / libsndfile-1.0.25 / examples / sndfile-to-text.c
1 /*
2 ** Copyright (C) 2008-2011 Erik de Castro Lopo <erikd@mega-nerd.com>
3 **
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
8 ** met:
9 **
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
15 ** distribution.
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.
19 **
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.
31 */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37
38 #include <sndfile.h>
39
40 #define BLOCK_SIZE 512
41
42 static void
43 print_usage (char *progname)
44 { printf ("\nUsage : %s <input file> <output file>\n", progname) ;
45 puts ("\n"
46 " Where the output file will contain a line for each frame\n"
47 " and a column for each channel.\n"
48 ) ;
49
50 } /* print_usage */
51
52 static void
53 convert_to_text (SNDFILE * infile, FILE * outfile, int channels)
54 { float buf [channels * BLOCK_SIZE] ;
55 int k, m, readcount ;
56
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") ;
62 } ;
63 } ;
64
65 return ;
66 } /* convert_to_text */
67
68 int
69 main (int argc, char * argv [])
70 { char *progname, *infilename, *outfilename ;
71 SNDFILE *infile = NULL ;
72 FILE *outfile = NULL ;
73 SF_INFO sfinfo ;
74
75 progname = strrchr (argv [0], '/') ;
76 progname = progname ? progname + 1 : argv [0] ;
77
78 if (argc != 3)
79 { print_usage (progname) ;
80 return 1 ;
81 } ;
82
83 infilename = argv [1] ;
84 outfilename = argv [2] ;
85
86 if (strcmp (infilename, outfilename) == 0)
87 { printf ("Error : Input and output filenames are the same.\n\n") ;
88 print_usage (progname) ;
89 return 1 ;
90 } ;
91
92 if (infilename [0] == '-')
93 { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
94 print_usage (progname) ;
95 return 1 ;
96 } ;
97
98 if (outfilename [0] == '-')
99 { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
100 print_usage (progname) ;
101 return 1 ;
102 } ;
103
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)) ;
107 return 1 ;
108 } ;
109
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)) ;
113 return 1 ;
114 } ;
115
116 fprintf (outfile, "# Converted from file %s.\n", infilename) ;
117 fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ;
118
119 convert_to_text (infile, outfile, sfinfo.channels) ;
120
121 sf_close (infile) ;
122 fclose (outfile) ;
123
124 return 0 ;
125 } /* main */
126