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