• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 void
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 [BLOCK_SIZE] ;
66 	sf_count_t frames ;
67 	int k, m, readcount ;
68 
69 	frames = BLOCK_SIZE / channels ;
70 
71 	while ((readcount = sf_readf_float (infile, buf, frames)) > 0)
72 	{	for (k = 0 ; k < readcount ; k++)
73 		{	for (m = 0 ; m < channels ; m++)
74 				if (full_precision)
75 					fprintf (outfile, " %.*e", OP_DBL_Digs - 1, buf [k * channels + m]) ;
76 				else
77 					fprintf (outfile, " % 12.10f", buf [k * channels + m]) ;
78 			fprintf (outfile, "\n") ;
79 			} ;
80 		} ;
81 
82 	return ;
83 } /* convert_to_text */
84 
85 int
main(int argc,char * argv[])86 main (int argc, char * argv [])
87 {	char 		*progname, *infilename, *outfilename ;
88 	SNDFILE		*infile = NULL ;
89 	FILE		*outfile = NULL ;
90 	SF_INFO		sfinfo ;
91 	int		full_precision = 0 ;
92 
93 	progname = strrchr (argv [0], '/') ;
94 	progname = progname ? progname + 1 : argv [0] ;
95 
96 	switch (argc)
97 	{	case 4 :
98 			if (!strcmp ("--full-precision", argv [3]))
99 			{	print_usage (progname) ;
100 				return 1 ;
101 				} ;
102 			full_precision = 1 ;
103 			argv++ ;
104 		case 3 :
105 			break ;
106 		default:
107 			print_usage (progname) ;
108 			return 1 ;
109 		} ;
110 
111 	infilename = argv [1] ;
112 	outfilename = argv [2] ;
113 
114 	if (strcmp (infilename, outfilename) == 0)
115 	{	printf ("Error : Input and output filenames are the same.\n\n") ;
116 		print_usage (progname) ;
117 		return 1 ;
118 		} ;
119 
120 	if (infilename [0] == '-')
121 	{	printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
122 		print_usage (progname) ;
123 		return 1 ;
124 		} ;
125 
126 	if (outfilename [0] == '-')
127 	{	printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
128 		print_usage (progname) ;
129 		return 1 ;
130 		} ;
131 
132 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
133 
134 	if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
135 	{	printf ("Not able to open input file %s.\n", infilename) ;
136 		puts (sf_strerror (NULL)) ;
137 		return 1 ;
138 		} ;
139 
140 	/* Open the output file. */
141 	if ((outfile = fopen (outfilename, "w")) == NULL)
142 	{	printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
143 		return 1 ;
144 		} ;
145 
146 	fprintf (outfile, "# Converted from file %s.\n", infilename) ;
147 	fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ;
148 
149 	convert_to_text (infile, outfile, sfinfo.channels, full_precision) ;
150 
151 	sf_close (infile) ;
152 	fclose (outfile) ;
153 
154 	return 0 ;
155 } /* main */
156 
157