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