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_CHANNELS 16 Chris@0: Chris@0: Chris@0: typedef struct Chris@0: { SNDFILE * infile ; Chris@0: SNDFILE * outfile [MAX_CHANNELS] ; Chris@0: Chris@0: union Chris@0: { double d [MAX_CHANNELS * BUFFER_LEN] ; Chris@0: int i [MAX_CHANNELS * BUFFER_LEN] ; Chris@0: } din ; Chris@0: Chris@0: union Chris@0: { double d [BUFFER_LEN] ; Chris@0: int i [BUFFER_LEN] ; Chris@0: } dout ; Chris@0: Chris@0: int channels ; Chris@0: } STATE ; Chris@0: Chris@0: static void usage_exit (void) ; Chris@0: Chris@0: static void deinterleave_int (STATE * state) ; Chris@0: static void deinterleave_double (STATE * state) ; Chris@0: Chris@0: int Chris@0: main (int argc, char **argv) Chris@0: { STATE state ; Chris@0: SF_INFO sfinfo ; Chris@0: char pathname [512], ext [32], *cptr ; Chris@0: int ch, double_split ; Chris@0: Chris@0: if (argc != 2) Chris@0: { if (argc != 1) Chris@0: puts ("\nError : need a single input file.\n") ; Chris@0: usage_exit () ; Chris@0: } ; Chris@0: Chris@0: memset (&state, 0, sizeof (state)) ; Chris@0: memset (&sfinfo, 0, sizeof (sfinfo)) ; Chris@0: Chris@0: if ((state.infile = sf_open (argv [1], SFM_READ, &sfinfo)) == NULL) Chris@0: { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [1], sf_strerror (NULL)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: if (sfinfo.channels < 2) Chris@0: { printf ("\nError : Input file '%s' only has one channel.\n", argv [1]) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: state.channels = sfinfo.channels ; Chris@0: sfinfo.channels = 1 ; Chris@0: Chris@0: snprintf (pathname, sizeof (pathname), "%s", argv [1]) ; Chris@0: if ((cptr = strrchr (pathname, '.')) == NULL) Chris@0: ext [0] = 0 ; Chris@0: else Chris@0: { snprintf (ext, sizeof (ext), "%s", cptr) ; Chris@0: cptr [0] = 0 ; Chris@0: } ; Chris@0: Chris@0: printf ("Input file : %s\n", pathname) ; Chris@0: puts ("Output files :") ; Chris@0: Chris@0: for (ch = 0 ; ch < state.channels ; ch++) Chris@0: { char filename [520] ; Chris@0: Chris@0: snprintf (filename, sizeof (filename), "%s_%02d%s", pathname, ch, ext) ; Chris@0: Chris@0: if ((state.outfile [ch] = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL) Chris@0: { printf ("Not able to open output file '%s'\n%s\n", filename, sf_strerror (NULL)) ; Chris@0: exit (1) ; Chris@0: } ; Chris@0: Chris@0: printf (" %s\n", filename) ; 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_split = 1 ; Chris@0: break ; Chris@0: Chris@0: default : Chris@0: double_split = 0 ; Chris@0: break ; Chris@0: } ; Chris@0: Chris@0: if (double_split) Chris@0: deinterleave_double (&state) ; Chris@0: else Chris@0: deinterleave_int (&state) ; Chris@0: Chris@0: sf_close (state.infile) ; Chris@0: for (ch = 0 ; ch < MAX_CHANNELS ; ch++) Chris@0: if (state.outfile [ch] != NULL) Chris@0: sf_close (state.outfile [ch]) ; Chris@0: Chris@0: return 0 ; Chris@0: } /* main */ Chris@0: Chris@0: /*------------------------------------------------------------------------------ Chris@0: */ Chris@0: Chris@0: static void Chris@0: usage_exit (void) Chris@0: { puts ("\nUsage : sndfile-deinterleave \n") ; Chris@0: puts ( Chris@0: "Split a mutli-channel file into a set of mono files.\n" Chris@0: "\n" Chris@0: "If the input file is named 'a.wav', the output files will be named\n" Chris@0: "a_00.wav, a_01.wav and so on.\n" Chris@0: ) ; Chris@0: printf ("Using %s.\n\n", sf_version_string ()) ; Chris@0: exit (0) ; Chris@0: } /* usage_exit */ Chris@0: Chris@0: static void Chris@0: deinterleave_int (STATE * state) Chris@0: { int read_len ; Chris@0: int ch, k ; Chris@0: Chris@0: do Chris@0: { read_len = sf_readf_int (state->infile, state->din.i, BUFFER_LEN) ; Chris@0: Chris@0: for (ch = 0 ; ch < state->channels ; ch ++) Chris@0: { for (k = 0 ; k < read_len ; k++) Chris@0: state->dout.i [k] = state->din.i [k * state->channels + ch] ; Chris@0: sf_write_int (state->outfile [ch], state->dout.i, read_len) ; Chris@0: } ; Chris@0: } Chris@0: while (read_len > 0) ; Chris@0: Chris@0: } /* deinterleave_int */ Chris@0: Chris@0: static void Chris@0: deinterleave_double (STATE * state) Chris@0: { int read_len ; Chris@0: int ch, k ; Chris@0: Chris@0: do Chris@0: { read_len = sf_readf_double (state->infile, state->din.d, BUFFER_LEN) ; Chris@0: Chris@0: for (ch = 0 ; ch < state->channels ; ch ++) Chris@0: { for (k = 0 ; k < read_len ; k++) Chris@0: state->dout.d [k] = state->din.d [k * state->channels + ch] ; Chris@0: sf_write_double (state->outfile [ch], state->dout.d, read_len) ; Chris@0: } ; Chris@0: } Chris@0: while (read_len > 0) ; Chris@0: Chris@0: } /* deinterleave_double */