1 /*
2 ** Copyright (C) 2008-2016 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 #include <float.h>
38
39 #include <sndfile.h>
40
41 #define BLOCK_SIZE 4096
42
43 #ifdef DBL_DECIMAL_DIG
44 #define OP_DBL_Digs (DBL_DECIMAL_DIG)
45 #else
46 #ifdef DECIMAL_DIG
47 #define OP_DBL_Digs (DECIMAL_DIG)
48 #else
49 #define OP_DBL_Digs (DBL_DIG + 3)
50 #endif
51 #endif
52
53 static void
print_usage(char *progname)54 print_usage (char *progname)
55 { printf ("\nUsage : %s [--full-precision] <input file> <output file>\n", progname) ;
56 puts ("\n"
57 " Where the output file will contain a line for each frame\n"
58 " and a column for each channel.\n"
59 ) ;
60
61 } /* print_usage */
62
63 static int
convert_to_text(SNDFILE * infile, FILE * outfile, int channels, int full_precision)64 convert_to_text (SNDFILE * infile, FILE * outfile, int channels, int full_precision)
65 { float *buf ;
66 sf_count_t frames ;
67 int k, m, readcount ;
68
69 buf = malloc (BLOCK_SIZE * sizeof (float)) ;
70 if (buf == NULL)
71 { printf ("Error : Out of memory.\n\n") ;
72 return 1 ;
73 } ;
74
75 frames = BLOCK_SIZE / channels ;
76
77 while ((readcount = (int) sf_readf_float (infile, buf, frames)) > 0)
78 { for (k = 0 ; k < readcount ; k++)
79 { for (m = 0 ; m < channels ; m++)
80 if (full_precision)
81 fprintf (outfile, " %.*e", OP_DBL_Digs - 1, buf [k * channels + m]) ;
82 else
83 fprintf (outfile, " % 12.10f", buf [k * channels + m]) ;
84 fprintf (outfile, "\n") ;
85 } ;
86 } ;
87
88 free (buf) ;
89
90 return 0 ;
91 } /* convert_to_text */
92
93 int
main(int argc, char * argv [])94 main (int argc, char * argv [])
95 { char *progname, *infilename, *outfilename ;
96 SNDFILE *infile = NULL ;
97 FILE *outfile = NULL ;
98 SF_INFO sfinfo ;
99 int full_precision = 0 ;
100 int ret = 1 ;
101
102 progname = strrchr (argv [0], '/') ;
103 progname = progname ? progname + 1 : argv [0] ;
104
105 switch (argc)
106 { case 4 :
107 if (!strcmp ("--full-precision", argv [3]))
108 { print_usage (progname) ;
109 goto cleanup ;
110 } ;
111 full_precision = 1 ;
112 argv++ ;
113 case 3 :
114 break ;
115 default:
116 print_usage (progname) ;
117 goto cleanup ;
118 } ;
119
120 infilename = argv [1] ;
121 outfilename = argv [2] ;
122
123 if (strcmp (infilename, outfilename) == 0)
124 { printf ("Error : Input and output filenames are the same.\n\n") ;
125 print_usage (progname) ;
126 goto cleanup ;
127 } ;
128
129 if (infilename [0] == '-')
130 { printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
131 print_usage (progname) ;
132 goto cleanup ;
133 } ;
134
135 if (outfilename [0] == '-')
136 { printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
137 print_usage (progname) ;
138 goto cleanup ;
139 } ;
140
141 memset (&sfinfo, 0, sizeof (sfinfo)) ;
142
143 if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
144 { printf ("Not able to open input file %s.\n", infilename) ;
145 puts (sf_strerror (NULL)) ;
146 goto cleanup ;
147 } ;
148
149 /* Open the output file. */
150 if ((outfile = fopen (outfilename, "w")) == NULL)
151 { printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
152 goto cleanup ;
153 } ;
154
155 fprintf (outfile, "# Converted from file %s.\n", infilename) ;
156 fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ;
157
158 ret = convert_to_text (infile, outfile, sfinfo.channels, full_precision) ;
159
160 cleanup :
161
162 sf_close (infile) ;
163 if (outfile != NULL)
164 fclose (outfile) ;
165
166 return ret ;
167 } /* main */
168
169