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