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