Chris@0: /* Chris@0: ** Copyright (C) 2009-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 "common.h" Chris@0: Chris@0: #define BUFFER_LEN 4096 Chris@0: #define MAX_INPUTS 16 Chris@0: Chris@0: Chris@0: typedef struct Chris@0: { SNDFILE * infile [MAX_INPUTS] ; Chris@0: SNDFILE * outfile ; Chris@0: Chris@0: union Chris@0: { double d [BUFFER_LEN] ; Chris@0: int i [BUFFER_LEN] ; Chris@0: } din ; Chris@0: Chris@0: union Chris@0: Chris@0: { double d [MAX_INPUTS * BUFFER_LEN] ; Chris@0: int i [MAX_INPUTS * BUFFER_LEN] ; Chris@0: } dout ; Chris@0: Chris@0: int channels ; Chris@0: } STATE ; Chris@0: Chris@0: Chris@0: static void usage_exit (void) ; Chris@0: static void interleave_int (STATE * state) ; Chris@0: static void interleave_double (STATE * state) ; Chris@0: Chris@0: Chris@0: int Chris@0: main (int argc, char **argv) Chris@0: { STATE state ; Chris@0: SF_INFO sfinfo ; Chris@0: int k, double_merge = 0 ; Chris@0: Chris@0: if (argc < 5) Chris@0: { if (argc > 1) Chris@0: puts ("\nError : need at least 2 input files.") ; Chris@0: usage_exit () ; Chris@0: } ; Chris@0: Chris@0: if (strcmp (argv [argc - 2], "-o") != 0) Chris@0: { puts ("\nError : second last command line parameter should be '-o'.\n") ; Chris@0: usage_exit () ; Chris@0: } ; Chris@0: Chris@0: if (argc - 3 > MAX_INPUTS) Chris@0: { printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: memset (&state, 0, sizeof (state)) ; Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: Chris@0: for (k = 1 ; k < argc - 2 ; k++) Chris@0: { Chris@0: if ((state.infile [k - 1] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [k], sf_strerror (NULL)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.channels != 1) Chris@0: { printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv [k], sfinfo.channels) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: switch (sfinfo.format & SF_FORMAT_SUBMASK) Chris@0: { case SF_FORMAT_FLOAT : Chris@0: case SF_FORMAT_DOUBLE : Chris@0: case SF_FORMAT_VORBIS : Chris@0: double_merge = 1 ; Chris@0: break ; Chris@0: Chris@0: default : Chris@0: break ; Chris@0: } ; Chris@0: Chris@0: state.channels ++ ; Chris@0: } ; Chris@0: Chris@0: sfinfo.channels = state.channels ; Chris@0: sfinfo.format = sfe_file_type_of_ext (argv [argc - 1], sfinfo.format) ; Chris@0: Chris@0: if ((state.outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL) Chris@0: { printf ("Not able to open output file '%s'\n%s\n", argv [argc - 1], sf_strerror (NULL)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (double_merge) Chris@0: interleave_double (&state) ; Chris@0: else Chris@0: interleave_int (&state) ; Chris@0: Chris@0: for (k = 0 ; k < MAX_INPUTS ; k++) Chris@0: if (state.infile [k] != NULL) Chris@0: sf_close (state.infile [k]) ; Chris@0: sf_close (state.outfile) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*------------------------------------------------------------------------------ Chris@0: */ Chris@0: Chris@0: Chris@0: static void Chris@0: usage_exit (void) Chris@0: { puts ("\nUsage : sndfile-interleave ... -o \n") ; Chris@0: puts ("Merge two or more mono files into a single multi-channel file.\n") ; Chris@0: printf ("Using %s.\n\n", sf_version_string ()) ; Chris@0: exit (0) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: Chris@0: static void Chris@0: interleave_int (STATE * state) Chris@0: { int max_read_len, read_len ; Chris@0: int ch, k ; Chris@0: Chris@0: do Chris@0: { max_read_len = 0 ; Chris@0: Chris@0: for (ch = 0 ; ch < state->channels ; ch ++) Chris@0: { read_len = sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ; Chris@0: if (read_len < BUFFER_LEN) Chris@0: memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ; Chris@0: Chris@0: for (k = 0 ; k < read_len ; k++) Chris@0: state->dout.i [k * state->channels + ch] = state->din.i [k] ; Chris@0: Chris@0: max_read_len = MAX (max_read_len, read_len) ; Chris@0: } ; Chris@0: Chris@0: sf_writef_int (state->outfile, state->dout.i, max_read_len) ; Chris@0: } Chris@0: while (max_read_len > 0) ; Chris@0: Chris@0: } /* interleave_int */ Chris@0: Chris@0: Chris@0: static void Chris@0: interleave_double (STATE * state) Chris@0: { int max_read_len, read_len ; Chris@0: int ch, k ; Chris@0: Chris@0: do Chris@0: { max_read_len = 0 ; Chris@0: Chris@0: for (ch = 0 ; ch < state->channels ; ch ++) Chris@0: { read_len = sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ; Chris@0: if (read_len < BUFFER_LEN) Chris@0: memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ; Chris@0: Chris@0: for (k = 0 ; k < read_len ; k++) Chris@0: state->dout.d [k * state->channels + ch] = state->din.d [k] ; Chris@0: Chris@0: max_read_len = MAX (max_read_len, read_len) ; Chris@0: } ; Chris@0: Chris@0: sf_writef_double (state->outfile, state->dout.d, max_read_len) ; Chris@0: } Chris@0: while (max_read_len > 0) ; Chris@0: Chris@0: } /* interleave_double */