Git init
[external/libsndfile.git] / examples / generate.c
1 /*
2 ** Copyright (C) 2002-2009 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 "sfconfig.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <math.h>
39
40 #include <sndfile.h>
41
42 #define BUFFER_LEN                      4096
43
44 static void encode_file (const char *infilename, const char *outfilename, int filetype) ;
45
46 int
47 main (int argc, char **argv)
48 {
49         if (argc != 2)
50         {       puts ("\nEncode a single input file into a number of different output ") ;
51                 puts ("encodings. These output encodings can then be moved to another ") ;
52                 puts ("OS for testing.\n") ;
53                 puts ("    Usage : generate <filename>\n") ;
54                 exit (1) ;
55                 } ;
56
57         /* A couple of standard WAV files. Make sure Win32 plays these. */
58         encode_file (argv [1], "pcmu8.wav"      , SF_FORMAT_WAV | SF_FORMAT_PCM_U8) ;
59         encode_file (argv [1], "pcm16.wav"      , SF_FORMAT_WAV | SF_FORMAT_PCM_16) ;
60         encode_file (argv [1], "imaadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_MS_ADPCM) ;
61         encode_file (argv [1], "msadpcm.wav", SF_FORMAT_WAV | SF_FORMAT_IMA_ADPCM) ;
62         encode_file (argv [1], "gsm610.wav"     , SF_FORMAT_WAV | SF_FORMAT_GSM610) ;
63
64         /* Soundforge W64. */
65         encode_file (argv [1], "pcmu8.w64"      , SF_FORMAT_W64 | SF_FORMAT_PCM_U8) ;
66         encode_file (argv [1], "pcm16.w64"      , SF_FORMAT_W64 | SF_FORMAT_PCM_16) ;
67         encode_file (argv [1], "imaadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_MS_ADPCM) ;
68         encode_file (argv [1], "msadpcm.w64", SF_FORMAT_W64 | SF_FORMAT_IMA_ADPCM) ;
69         encode_file (argv [1], "gsm610.w64"     , SF_FORMAT_W64 | SF_FORMAT_GSM610) ;
70
71         return 0 ;
72 } /* main */
73
74 /*============================================================================================
75 **      Helper functions and macros.
76 */
77
78 #define PUT_DOTS(k)                                     \
79                         {       while (k--)                     \
80                                         putchar ('.') ; \
81                                 putchar (' ') ;         \
82                                 }
83
84 /*========================================================================================
85 */
86
87 static void
88 encode_file (const char *infilename, const char *outfilename, int filetype)
89 {       static float buffer [BUFFER_LEN] ;
90
91         SNDFILE         *infile, *outfile ;
92         SF_INFO         sfinfo ;
93         int                     k, readcount ;
94
95         printf ("    %s -> %s ", infilename, outfilename) ;
96         fflush (stdout) ;
97
98         k = 16 - strlen (outfilename) ;
99         PUT_DOTS (k) ;
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