2 ** Copyright (C) 2009-2011 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 ;
76 puts ("\nError : need at least 2 input files.") ;
80 if (strcmp (argv
[argc
- 2], "-o") != 0)
81 { puts ("\nError : second last command line parameter should be '-o'.\n") ;
85 if (argc
- 3 > MAX_INPUTS
)
86 { printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS
) ;
90 memset (&state
, 0, sizeof (state
)) ;
91 memset (&sfinfo
, 0, sizeof (sfinfo
)) ;
93 for (k
= 1 ; k
< argc
- 2 ; k
++)
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
)) ;
100 if (sfinfo
.channels
!= 1)
101 { printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv
[k
], sfinfo
.channels
) ;
105 switch (sfinfo
.format
& SF_FORMAT_SUBMASK
)
106 { case SF_FORMAT_FLOAT
:
107 case SF_FORMAT_DOUBLE
:
108 case SF_FORMAT_VORBIS
:
119 sfinfo
.channels
= state
.channels
;
120 sfinfo
.format
= sfe_file_type_of_ext (argv
[argc
- 1], sfinfo
.format
) ;
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
)) ;
128 interleave_double (&state
) ;
130 interleave_int (&state
) ;
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
) ;
140 /*------------------------------------------------------------------------------
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 ()) ;
154 interleave_int (STATE
* state
)
155 { int max_read_len
, read_len
;
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
)) ;
166 for (k
= 0 ; k
< read_len
; k
++)
167 state
->dout
.i
[k
* state
->channels
+ ch
] = state
->din
.i
[k
] ;
169 max_read_len
= MAX (max_read_len
, read_len
) ;
172 sf_writef_int (state
->outfile
, state
->dout
.i
, max_read_len
) ;
174 while (max_read_len
> 0) ;
176 } /* interleave_int */
180 interleave_double (STATE
* state
)
181 { int max_read_len
, read_len
;
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
)) ;
192 for (k
= 0 ; k
< read_len
; k
++)
193 state
->dout
.d
[k
* state
->channels
+ ch
] = state
->din
.d
[k
] ;
195 max_read_len
= MAX (max_read_len
, read_len
) ;
198 sf_writef_double (state
->outfile
, state
->dout
.d
, max_read_len
) ;
200 while (max_read_len
> 0) ;
202 } /* interleave_double */