Implement stdin, stdout and stderr to Faustine.
[Faustine.git] / interpreter / lib / src / libsndfile-1.0.25 / programs / sndfile-interleave.c
1 /*
2 ** Copyright (C) 2009-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 <sndfile.h>
37
38 #include "common.h"
39
40 #define BUFFER_LEN 4096
41 #define MAX_INPUTS 16
42
43
44 typedef struct
45 { SNDFILE * infile [MAX_INPUTS] ;
46 SNDFILE * outfile ;
47
48 union
49 { double d [BUFFER_LEN] ;
50 int i [BUFFER_LEN] ;
51 } din ;
52
53 union
54
55 { double d [MAX_INPUTS * BUFFER_LEN] ;
56 int i [MAX_INPUTS * BUFFER_LEN] ;
57 } dout ;
58
59 int channels ;
60 } STATE ;
61
62
63 static void usage_exit (void) ;
64 static void interleave_int (STATE * state) ;
65 static void interleave_double (STATE * state) ;
66
67
68 int
69 main (int argc, char **argv)
70 { STATE state ;
71 SF_INFO sfinfo ;
72 int k, double_merge = 0 ;
73
74 if (argc < 5)
75 { if (argc > 1)
76 puts ("\nError : need at least 2 input files.") ;
77 usage_exit () ;
78 } ;
79
80 if (strcmp (argv [argc - 2], "-o") != 0)
81 { puts ("\nError : second last command line parameter should be '-o'.\n") ;
82 usage_exit () ;
83 } ;
84
85 if (argc - 3 > MAX_INPUTS)
86 { printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS) ;
87 exit (1) ;
88 } ;
89
90 memset (&state, 0, sizeof (state)) ;
91 memset (&sfinfo, 0, sizeof (sfinfo)) ;
92
93 for (k = 1 ; k < argc - 2 ; k++)
94 {
95 if ((state.infile [k - 1] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL)
96 { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [k], sf_strerror (NULL)) ;
97 exit (1) ;
98 } ;
99
100 if (sfinfo.channels != 1)
101 { printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv [k], sfinfo.channels) ;
102 exit (1) ;
103 } ;
104
105 switch (sfinfo.format & SF_FORMAT_SUBMASK)
106 { case SF_FORMAT_FLOAT :
107 case SF_FORMAT_DOUBLE :
108 case SF_FORMAT_VORBIS :
109 double_merge = 1 ;
110 break ;
111
112 default :
113 break ;
114 } ;
115
116 state.channels ++ ;
117 } ;
118
119 sfinfo.channels = state.channels ;
120 sfinfo.format = sfe_file_type_of_ext (argv [argc - 1], sfinfo.format) ;
121
122 if ((state.outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
123 { printf ("Not able to open output file '%s'\n%s\n", argv [argc - 1], sf_strerror (NULL)) ;
124 exit (1) ;
125 } ;
126
127 if (double_merge)
128 interleave_double (&state) ;
129 else
130 interleave_int (&state) ;
131
132 for (k = 0 ; k < MAX_INPUTS ; k++)
133 if (state.infile [k] != NULL)
134 sf_close (state.infile [k]) ;
135 sf_close (state.outfile) ;
136
137 return 0 ;
138 } /* main */
139
140 /*------------------------------------------------------------------------------
141 */
142
143
144 static void
145 usage_exit (void)
146 { puts ("\nUsage : sndfile-interleave <input 1> <input 2> ... -o <output file>\n") ;
147 puts ("Merge two or more mono files into a single multi-channel file.\n") ;
148 printf ("Using %s.\n\n", sf_version_string ()) ;
149 exit (0) ;
150 } /* usage_exit */
151
152
153 static void
154 interleave_int (STATE * state)
155 { int max_read_len, read_len ;
156 int ch, k ;
157
158 do
159 { max_read_len = 0 ;
160
161 for (ch = 0 ; ch < state->channels ; ch ++)
162 { read_len = sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ;
163 if (read_len < BUFFER_LEN)
164 memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ;
165
166 for (k = 0 ; k < read_len ; k++)
167 state->dout.i [k * state->channels + ch] = state->din.i [k] ;
168
169 max_read_len = MAX (max_read_len, read_len) ;
170 } ;
171
172 sf_writef_int (state->outfile, state->dout.i, max_read_len) ;
173 }
174 while (max_read_len > 0) ;
175
176 } /* interleave_int */
177
178
179 static void
180 interleave_double (STATE * state)
181 { int max_read_len, read_len ;
182 int ch, k ;
183
184 do
185 { max_read_len = 0 ;
186
187 for (ch = 0 ; ch < state->channels ; ch ++)
188 { read_len = sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ;
189 if (read_len < BUFFER_LEN)
190 memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ;
191
192 for (k = 0 ; k < read_len ; k++)
193 state->dout.d [k * state->channels + ch] = state->din.d [k] ;
194
195 max_read_len = MAX (max_read_len, read_len) ;
196 } ;
197
198 sf_writef_double (state->outfile, state->dout.d, max_read_len) ;
199 }
200 while (max_read_len > 0) ;
201
202 } /* interleave_double */