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