2 ** Copyright (C) 2009 Erik de Castro Lopo <erikd@mega-nerd.com>
4 ** All rights reserved.
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are
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
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.
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.
40 #define BUFFER_LEN 4096
45 { SNDFILE * infile [MAX_INPUTS] ;
49 { double d [BUFFER_LEN] ;
55 { double d [MAX_INPUTS * BUFFER_LEN] ;
56 int i [MAX_INPUTS * BUFFER_LEN] ;
63 static void usage_exit (void) ;
64 static void interleave_int (STATE * state) ;
65 static void interleave_double (STATE * state) ;
69 main (int argc, char **argv)
72 int k, double_merge = 0 ;
75 { puts ("\nError : need at least 2 input files.") ;
79 if (strcmp (argv [argc - 2], "-o") != 0)
80 { puts ("\nError : second last command line parameter should be '-o'.\n") ;
84 if (argc - 2 > MAX_INPUTS)
85 { printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS) ;
89 memset (&state, 0, sizeof (state)) ;
90 memset (&sfinfo, 0, sizeof (sfinfo)) ;
92 for (k = 1 ; k < argc - 2 ; k++)
94 if ((state.infile [k - 1] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL)
95 { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [k], sf_strerror (NULL)) ;
99 if (sfinfo.channels != 1)
100 { printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv [k], sfinfo.channels) ;
104 switch (sfinfo.format & SF_FORMAT_SUBMASK)
105 { case SF_FORMAT_FLOAT :
106 case SF_FORMAT_DOUBLE :
107 case SF_FORMAT_VORBIS :
118 sfinfo.channels = state.channels ;
119 sfinfo.format = sfe_file_type_of_ext (argv [argc - 1], sfinfo.format) ;
121 if ((state.outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
122 { printf ("Not able to open output file '%s'\n%s\n", argv [argc - 1], sf_strerror (NULL)) ;
127 interleave_double (&state) ;
129 interleave_int (&state) ;
131 for (k = 0 ; k < MAX_INPUTS ; k++)
132 if (state.infile [k] != NULL)
133 sf_close (state.infile [k]) ;
134 sf_close (state.outfile) ;
139 /*------------------------------------------------------------------------------
145 { puts ("\nUsage : sndfile-interleave <input 1> <input 2> ... -o <output file>\n") ;
146 puts ("Merge two mono files to one stereo file\n") ;
152 interleave_int (STATE * state)
153 { int max_read_len, read_len ;
159 for (ch = 0 ; ch < state->channels ; ch ++)
160 { read_len = sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ;
161 if (read_len < BUFFER_LEN)
162 memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ;
164 for (k = 0 ; k < read_len ; k++)
165 state->dout.i [k * state->channels + ch] = state->din.i [k] ;
167 max_read_len = MAX (max_read_len, read_len) ;
170 sf_writef_int (state->outfile, state->dout.i, max_read_len) ;
172 while (max_read_len > 0) ;
174 } /* interleave_int */
178 interleave_double (STATE * state)
179 { int max_read_len, read_len ;
185 for (ch = 0 ; ch < state->channels ; ch ++)
186 { read_len = sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ;
187 if (read_len < BUFFER_LEN)
188 memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ;
190 for (k = 0 ; k < read_len ; k++)
191 state->dout.d [k * state->channels + ch] = state->din.d [k] ;
193 max_read_len = MAX (max_read_len, read_len) ;
196 sf_writef_double (state->outfile, state->dout.d, max_read_len) ;
198 while (max_read_len > 0) ;
200 } /* interleave_double */