1 /*
2 ** Copyright (C) 2002-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 <math.h>
37 
38 #include <sndfile.h>
39 
40 #define	BUFFER_LEN			4096
41 
42 static void encode_file (const char *infilename, const char *outfilename, int filetype) ;
43 
44 int
main(int argc,char ** argv)45 main (int argc, char **argv)
46 {
47 	if (argc != 2)
48 	{	puts ("\nEncode a single input file into a number of different output ") ;
49 		puts ("encodings. These output encodings can then be moved to another ") ;
50 		puts ("OS for testing.\n") ;
51 		puts ("    Usage : generate <filename>\n") ;
52 		exit (1) ;
53 		} ;
54 
55 	/* A couple of standard WAV files. Make sure Win32 plays these. */
56 	encode_file (argv [1], "pcmu8.wav"	, SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
57 	encode_file (argv [1], "pcm16.wav"	, SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
58 	encode_file (argv [1], "imaadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
59 	encode_file (argv [1], "msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
60 	encode_file (argv [1], "gsm610.wav"	, SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
61 
62 	/* Soundforge W64. */
63 	encode_file (argv [1], "pcmu8.w64"	, SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
64 	encode_file (argv [1], "pcm16.w64"	, SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
65 	encode_file (argv [1], "imaadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ;
66 	encode_file (argv [1], "msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ;
67 	encode_file (argv [1], "gsm610.w64"	, SF_FORMAT_W64 | SF_FORMAT_GSM610) ;
68 
69 	return 0 ;
70 } /* main */
71 
72 /*============================================================================================
73 **	Helper functions and macros.
74 */
75 
76 #define PUT_DOTS(k)					\
77 			{	while (k--)			\
78 					putchar ('.') ;	\
79 				putchar (' ') ;		\
80 				}
81 
82 /*========================================================================================
83 */
84 
85 static void
encode_file(const char * infilename,const char * outfilename,int filetype)86 encode_file (const char *infilename, const char *outfilename, int filetype)
87 {	static float buffer [BUFFER_LEN] ;
88 
89 	SNDFILE		*infile, *outfile ;
90 	SF_INFO		sfinfo ;
91 	int			k, readcount ;
92 
93 	printf ("    %s -> %s ", infilename, outfilename) ;
94 	fflush (stdout) ;
95 
96 	k = 16 - strlen (outfilename) ;
97 	PUT_DOTS (k) ;
98 
99 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
100 
101 	if (! (infile = sf_open (infilename, SFM_READ, &sfinfo)))
102 	{	printf ("Error : could not open file : %s\n", infilename) ;
103 		puts (sf_strerror (NULL)) ;
104 		exit (1) ;
105 		}
106 
107 	sfinfo.format = filetype ;
108 
109 	if (! sf_format_check (&sfinfo))
110 	{	sf_close (infile) ;
111 		printf ("Invalid encoding\n") ;
112 		return ;
113 		} ;
114 
115 	if (! (outfile = sf_open (outfilename, SFM_WRITE, &sfinfo)))
116 	{	printf ("Error : could not open file : %s\n", outfilename) ;
117 		puts (sf_strerror (NULL)) ;
118 		exit (1) ;
119 		} ;
120 
121 	while ((readcount = sf_read_float (infile, buffer, BUFFER_LEN)) > 0)
122 		sf_write_float (outfile, buffer, readcount) ;
123 
124 	sf_close (infile) ;
125 	sf_close (outfile) ;
126 
127 	printf ("ok\n") ;
128 
129 	return ;
130 } /* encode_file */
131 
132