Chris@0: /* Chris@0: ** Copyright (C) 1999-2011 Erik de Castro Lopo Chris@0: ** Chris@0: ** All rights reserved. Chris@0: ** Chris@0: ** Redistribution and use in source and binary forms, with or without Chris@0: ** modification, are permitted provided that the following conditions are Chris@0: ** met: Chris@0: ** Chris@0: ** * Redistributions of source code must retain the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer. Chris@0: ** * Redistributions in binary form must reproduce the above copyright Chris@0: ** notice, this list of conditions and the following disclaimer in Chris@0: ** the documentation and/or other materials provided with the Chris@0: ** distribution. Chris@0: ** * Neither the author nor the names of any contributors may be used Chris@0: ** to endorse or promote products derived from this software without Chris@0: ** specific prior written permission. Chris@0: ** Chris@0: ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS Chris@0: ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED Chris@0: ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR Chris@0: ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR Chris@0: ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, Chris@0: ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, Chris@0: ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; Chris@0: ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, Chris@0: ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR Chris@0: ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF Chris@0: ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: #include Chris@0: Chris@0: #include Chris@0: Chris@0: #include "common.h" Chris@0: Chris@0: #define BUFFER_LEN (1<<16) Chris@0: Chris@0: Chris@0: static void concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels) ; Chris@0: static void concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels) ; Chris@0: Chris@0: static void Chris@0: usage_exit (const char *progname) Chris@0: { Chris@0: printf ("\nUsage : %s ... \n\n", progname) ; Chris@0: puts ( Chris@0: " Create a new output file containing the concatenated\n" Chris@0: " audio data from froms ....\n" Chris@0: "\n" Chris@0: " The joined file will be encoded in the same format as the data\n" Chris@0: " in infile1, with all the data in subsequent files automatically\n" Chris@0: " converted to the correct encoding.\n" Chris@0: "\n" Chris@0: " The only restriction is that the two files must have the same\n" Chris@0: " number of channels.\n" Chris@0: ) ; Chris@0: Chris@0: exit (0) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: int Chris@0: main (int argc, char *argv []) Chris@0: { const char *progname, *outfilename ; Chris@0: SNDFILE *outfile, **infiles ; Chris@0: SF_INFO sfinfo_out, sfinfo_in ; Chris@0: void (*func) (SNDFILE*, SNDFILE*, int) ; Chris@0: int k ; Chris@0: Chris@0: progname = program_name (argv [0]) ; Chris@0: Chris@0: if (argc < 4) Chris@0: usage_exit (progname) ; Chris@0: Chris@0: argv ++ ; Chris@0: argc -- ; Chris@0: Chris@0: argc -- ; Chris@0: outfilename = argv [argc] ; Chris@0: Chris@0: if ((infiles = calloc (argc, sizeof (SNDFILE*))) == NULL) Chris@0: { printf ("\nError : Malloc failed.\n\n") ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: memset (&sfinfo_in, 0, sizeof (sfinfo_in)) ; Chris@0: Chris@0: if ((infiles [0] = sf_open (argv [0], SFM_READ, &sfinfo_in)) == NULL) Chris@0: { printf ("\nError : failed to open file '%s'.\n\n", argv [0]) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: sfinfo_out = sfinfo_in ; Chris@0: Chris@0: for (k = 1 ; k < argc ; k++) Chris@0: { if ((infiles [k] = sf_open (argv [k], SFM_READ, &sfinfo_in)) == NULL) Chris@0: { printf ("\nError : failed to open file '%s'.\n\n", argv [k]) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo_in.channels != sfinfo_out.channels) Chris@0: { printf ("\nError : File '%s' has %d channels (should have %d).\n\n", argv [k], sfinfo_in.channels, sfinfo_out.channels) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: } ; Chris@0: Chris@0: if ((outfile = sf_open (outfilename, SFM_WRITE, &sfinfo_out)) == NULL) Chris@0: { printf ("\nError : Not able to open input file %s.\n", outfilename) ; Chris@0: puts (sf_strerror (NULL)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if ((sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DOUBLE || Chris@0: (sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT) Chris@0: func = concat_data_fp ; Chris@0: else Chris@0: func = concat_data_int ; Chris@0: Chris@0: for (k = 0 ; k < argc ; k++) Chris@0: { func (outfile, infiles [k], sfinfo_out.channels) ; Chris@0: sf_close (infiles [k]) ; Chris@0: } ; Chris@0: Chris@0: sf_close (outfile) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: static void Chris@0: concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels) Chris@0: { static double data [BUFFER_LEN] ; Chris@0: int frames, readcount ; Chris@0: Chris@0: frames = BUFFER_LEN / channels ; Chris@0: readcount = frames ; Chris@0: Chris@0: sf_seek (wfile, 0, SEEK_END) ; Chris@0: Chris@0: while (readcount > 0) Chris@0: { readcount = sf_readf_double (rofile, data, frames) ; Chris@0: sf_writef_double (wfile, data, readcount) ; Chris@0: } ; Chris@0: Chris@0: return ; Chris@0: } /* concat_data_fp */ Chris@0: Chris@0: static void Chris@0: concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels) Chris@0: { static int data [BUFFER_LEN] ; Chris@0: int frames, readcount ; Chris@0: Chris@0: frames = BUFFER_LEN / channels ; Chris@0: readcount = frames ; Chris@0: Chris@0: sf_seek (wfile, 0, SEEK_END) ; Chris@0: Chris@0: while (readcount > 0) Chris@0: { readcount = sf_readf_int (rofile, data, frames) ; Chris@0: sf_writef_int (wfile, data, readcount) ; Chris@0: } ; Chris@0: Chris@0: return ; Chris@0: } /* concat_data_int */ Chris@0: