cc247823bcc570f8d85d8fb42e4696ecadfd30ee
2 ** Copyright (C) 1999-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.
42 #define BUFFER_LEN (1<<16)
45 static void concat_data_fp (SNDFILE
*wfile
, SNDFILE
*rofile
, int channels
) ;
46 static void concat_data_int (SNDFILE
*wfile
, SNDFILE
*rofile
, int channels
) ;
49 usage_exit (const char *progname
)
51 printf ("\nUsage : %s <infile1> <infile2> ... <outfile>\n\n", progname
) ;
53 " Create a new output file <outfile> containing the concatenated\n"
54 " audio data from froms <infile1> <infile2> ....\n"
56 " The joined file will be encoded in the same format as the data\n"
57 " in infile1, with all the data in subsequent files automatically\n"
58 " converted to the correct encoding.\n"
60 " The only restriction is that the two files must have the same\n"
61 " number of channels.\n"
68 main (int argc
, char *argv
[])
69 { const char *progname
, *outfilename
;
70 SNDFILE
*outfile
, **infiles
;
71 SF_INFO sfinfo_out
, sfinfo_in
;
72 void (*func
) (SNDFILE
*, SNDFILE
*, int) ;
75 progname
= program_name (argv
[0]) ;
78 usage_exit (progname
) ;
84 outfilename
= argv
[argc
] ;
86 if ((infiles
= calloc (argc
, sizeof (SNDFILE
*))) == NULL
)
87 { printf ("\nError : Malloc failed.\n\n") ;
91 memset (&sfinfo_in
, 0, sizeof (sfinfo_in
)) ;
93 if ((infiles
[0] = sf_open (argv
[0], SFM_READ
, &sfinfo_in
)) == NULL
)
94 { printf ("\nError : failed to open file '%s'.\n\n", argv
[0]) ;
98 sfinfo_out
= sfinfo_in
;
100 for (k
= 1 ; k
< argc
; k
++)
101 { if ((infiles
[k
] = sf_open (argv
[k
], SFM_READ
, &sfinfo_in
)) == NULL
)
102 { printf ("\nError : failed to open file '%s'.\n\n", argv
[k
]) ;
106 if (sfinfo_in
.channels
!= sfinfo_out
.channels
)
107 { printf ("\nError : File '%s' has %d channels (should have %d).\n\n", argv
[k
], sfinfo_in
.channels
, sfinfo_out
.channels
) ;
112 if ((outfile
= sf_open (outfilename
, SFM_WRITE
, &sfinfo_out
)) == NULL
)
113 { printf ("\nError : Not able to open input file %s.\n", outfilename
) ;
114 puts (sf_strerror (NULL
)) ;
118 if ((sfinfo_out
.format
& SF_FORMAT_SUBMASK
) == SF_FORMAT_DOUBLE
||
119 (sfinfo_out
.format
& SF_FORMAT_SUBMASK
) == SF_FORMAT_FLOAT
)
120 func
= concat_data_fp
;
122 func
= concat_data_int
;
124 for (k
= 0 ; k
< argc
; k
++)
125 { func (outfile
, infiles
[k
], sfinfo_out
.channels
) ;
126 sf_close (infiles
[k
]) ;
135 concat_data_fp (SNDFILE
*wfile
, SNDFILE
*rofile
, int channels
)
136 { static double data
[BUFFER_LEN
] ;
137 int frames
, readcount
;
139 frames
= BUFFER_LEN
/ channels
;
142 sf_seek (wfile
, 0, SEEK_END
) ;
144 while (readcount
> 0)
145 { readcount
= sf_readf_double (rofile
, data
, frames
) ;
146 sf_writef_double (wfile
, data
, readcount
) ;
150 } /* concat_data_fp */
153 concat_data_int (SNDFILE
*wfile
, SNDFILE
*rofile
, int channels
)
154 { static int data
[BUFFER_LEN
] ;
155 int frames
, readcount
;
157 frames
= BUFFER_LEN
/ channels
;
160 sf_seek (wfile
, 0, SEEK_END
) ;
162 while (readcount
> 0)
163 { readcount
= sf_readf_int (rofile
, data
, frames
) ;
164 sf_writef_int (wfile
, data
, readcount
) ;
168 } /* concat_data_int */