annotate src/libsndfile-1.0.27/programs/sndfile-deinterleave.c @ 40:1df64224f5ac

Current libsndfile source
author Chris Cannam
date Tue, 18 Oct 2016 13:22:47 +0100
parents
children
rev   line source
Chris@40 1 /*
Chris@40 2 ** Copyright (C) 2009-2014 Erik de Castro Lopo <erikd@mega-nerd.com>
Chris@40 3 **
Chris@40 4 ** All rights reserved.
Chris@40 5 **
Chris@40 6 ** Redistribution and use in source and binary forms, with or without
Chris@40 7 ** modification, are permitted provided that the following conditions are
Chris@40 8 ** met:
Chris@40 9 **
Chris@40 10 ** * Redistributions of source code must retain the above copyright
Chris@40 11 ** notice, this list of conditions and the following disclaimer.
Chris@40 12 ** * Redistributions in binary form must reproduce the above copyright
Chris@40 13 ** notice, this list of conditions and the following disclaimer in
Chris@40 14 ** the documentation and/or other materials provided with the
Chris@40 15 ** distribution.
Chris@40 16 ** * Neither the author nor the names of any contributors may be used
Chris@40 17 ** to endorse or promote products derived from this software without
Chris@40 18 ** specific prior written permission.
Chris@40 19 **
Chris@40 20 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@40 21 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
Chris@40 22 ** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
Chris@40 23 ** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
Chris@40 24 ** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Chris@40 25 ** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Chris@40 26 ** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Chris@40 27 ** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Chris@40 28 ** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
Chris@40 29 ** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Chris@40 30 ** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@40 31 */
Chris@40 32
Chris@40 33 #include <stdio.h>
Chris@40 34 #include <stdlib.h>
Chris@40 35 #include <string.h>
Chris@40 36 #include <sndfile.h>
Chris@40 37
Chris@40 38 #include "common.h"
Chris@40 39
Chris@40 40 #define BUFFER_LEN 4096
Chris@40 41 #define MAX_CHANNELS 16
Chris@40 42
Chris@40 43
Chris@40 44 typedef struct
Chris@40 45 { SNDFILE * infile ;
Chris@40 46 SNDFILE * outfile [MAX_CHANNELS] ;
Chris@40 47
Chris@40 48 union
Chris@40 49 { double d [MAX_CHANNELS * BUFFER_LEN] ;
Chris@40 50 int i [MAX_CHANNELS * BUFFER_LEN] ;
Chris@40 51 } din ;
Chris@40 52
Chris@40 53 union
Chris@40 54 { double d [BUFFER_LEN] ;
Chris@40 55 int i [BUFFER_LEN] ;
Chris@40 56 } dout ;
Chris@40 57
Chris@40 58 int channels ;
Chris@40 59 } STATE ;
Chris@40 60
Chris@40 61 static void usage_exit (void) ;
Chris@40 62
Chris@40 63 static void deinterleave_int (STATE * state) ;
Chris@40 64 static void deinterleave_double (STATE * state) ;
Chris@40 65
Chris@40 66 int
Chris@40 67 main (int argc, char **argv)
Chris@40 68 { STATE state ;
Chris@40 69 SF_INFO sfinfo ;
Chris@40 70 char pathname [512], ext [32], *cptr ;
Chris@40 71 int ch, double_split ;
Chris@40 72
Chris@40 73 if (argc != 2)
Chris@40 74 { if (argc != 1)
Chris@40 75 puts ("\nError : need a single input file.\n") ;
Chris@40 76 usage_exit () ;
Chris@40 77 } ;
Chris@40 78
Chris@40 79 memset (&state, 0, sizeof (state)) ;
Chris@40 80 memset (&sfinfo, 0, sizeof (sfinfo)) ;
Chris@40 81
Chris@40 82 if ((state.infile = sf_open (argv [1], SFM_READ, &sfinfo)) == NULL)
Chris@40 83 { printf ("\nError : Not able to open input file '%s'\n%s\n", argv [1], sf_strerror (NULL)) ;
Chris@40 84 exit (1) ;
Chris@40 85 } ;
Chris@40 86
Chris@40 87 if (sfinfo.channels < 2)
Chris@40 88 { printf ("\nError : Input file '%s' only has one channel.\n", argv [1]) ;
Chris@40 89 exit (1) ;
Chris@40 90 } ;
Chris@40 91
Chris@40 92 state.channels = sfinfo.channels ;
Chris@40 93 sfinfo.channels = 1 ;
Chris@40 94
Chris@40 95 snprintf (pathname, sizeof (pathname), "%s", argv [1]) ;
Chris@40 96 if ((cptr = strrchr (pathname, '.')) == NULL)
Chris@40 97 ext [0] = 0 ;
Chris@40 98 else
Chris@40 99 { snprintf (ext, sizeof (ext), "%s", cptr) ;
Chris@40 100 cptr [0] = 0 ;
Chris@40 101 } ;
Chris@40 102
Chris@40 103 printf ("Input file : %s\n", pathname) ;
Chris@40 104 puts ("Output files :") ;
Chris@40 105
Chris@40 106 for (ch = 0 ; ch < state.channels ; ch++)
Chris@40 107 { char filename [520] ;
Chris@40 108
Chris@40 109 snprintf (filename, sizeof (filename), "%s_%02d%s", pathname, ch, ext) ;
Chris@40 110
Chris@40 111 if ((state.outfile [ch] = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
Chris@40 112 { printf ("Not able to open output file '%s'\n%s\n", filename, sf_strerror (NULL)) ;
Chris@40 113 exit (1) ;
Chris@40 114 } ;
Chris@40 115
Chris@40 116 printf (" %s\n", filename) ;
Chris@40 117 } ;
Chris@40 118
Chris@40 119 switch (sfinfo.format & SF_FORMAT_SUBMASK)
Chris@40 120 { case SF_FORMAT_FLOAT :
Chris@40 121 case SF_FORMAT_DOUBLE :
Chris@40 122 case SF_FORMAT_VORBIS :
Chris@40 123 double_split = 1 ;
Chris@40 124 break ;
Chris@40 125
Chris@40 126 default :
Chris@40 127 double_split = 0 ;
Chris@40 128 break ;
Chris@40 129 } ;
Chris@40 130
Chris@40 131 if (double_split)
Chris@40 132 deinterleave_double (&state) ;
Chris@40 133 else
Chris@40 134 deinterleave_int (&state) ;
Chris@40 135
Chris@40 136 sf_close (state.infile) ;
Chris@40 137 for (ch = 0 ; ch < MAX_CHANNELS ; ch++)
Chris@40 138 if (state.outfile [ch] != NULL)
Chris@40 139 sf_close (state.outfile [ch]) ;
Chris@40 140
Chris@40 141 return 0 ;
Chris@40 142 } /* main */
Chris@40 143
Chris@40 144 /*------------------------------------------------------------------------------
Chris@40 145 */
Chris@40 146
Chris@40 147 static void
Chris@40 148 usage_exit (void)
Chris@40 149 { puts ("\nUsage : sndfile-deinterleave <filename>\n") ;
Chris@40 150 puts (
Chris@40 151 "Split a mutli-channel file into a set of mono files.\n"
Chris@40 152 "\n"
Chris@40 153 "If the input file is named 'a.wav', the output files will be named\n"
Chris@40 154 "a_00.wav, a_01.wav and so on.\n"
Chris@40 155 ) ;
Chris@40 156 printf ("Using %s.\n\n", sf_version_string ()) ;
Chris@40 157 exit (1) ;
Chris@40 158 } /* usage_exit */
Chris@40 159
Chris@40 160 static void
Chris@40 161 deinterleave_int (STATE * state)
Chris@40 162 { int read_len ;
Chris@40 163 int ch, k ;
Chris@40 164
Chris@40 165 do
Chris@40 166 { read_len = sf_readf_int (state->infile, state->din.i, BUFFER_LEN) ;
Chris@40 167
Chris@40 168 for (ch = 0 ; ch < state->channels ; ch ++)
Chris@40 169 { for (k = 0 ; k < read_len ; k++)
Chris@40 170 state->dout.i [k] = state->din.i [k * state->channels + ch] ;
Chris@40 171 sf_write_int (state->outfile [ch], state->dout.i, read_len) ;
Chris@40 172 } ;
Chris@40 173 }
Chris@40 174 while (read_len > 0) ;
Chris@40 175
Chris@40 176 } /* deinterleave_int */
Chris@40 177
Chris@40 178 static void
Chris@40 179 deinterleave_double (STATE * state)
Chris@40 180 { int read_len ;
Chris@40 181 int ch, k ;
Chris@40 182
Chris@40 183 do
Chris@40 184 { read_len = sf_readf_double (state->infile, state->din.d, BUFFER_LEN) ;
Chris@40 185
Chris@40 186 for (ch = 0 ; ch < state->channels ; ch ++)
Chris@40 187 { for (k = 0 ; k < read_len ; k++)
Chris@40 188 state->dout.d [k] = state->din.d [k * state->channels + ch] ;
Chris@40 189 sf_write_double (state->outfile [ch], state->dout.d, read_len) ;
Chris@40 190 } ;
Chris@40 191 }
Chris@40 192 while (read_len > 0) ;
Chris@40 193
Chris@40 194 } /* deinterleave_double */