Git init
[external/libsndfile.git] / programs / sndfile-interleave.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_INPUTS      16
42
43
44 typedef struct
45 {       SNDFILE * infile [MAX_INPUTS] ;
46         SNDFILE * outfile ;
47
48         union
49         {       double  d [BUFFER_LEN] ;
50                 int             i [BUFFER_LEN] ;
51         } din ;
52
53         union
54
55         {       double  d [MAX_INPUTS * BUFFER_LEN] ;
56                 int             i [MAX_INPUTS * BUFFER_LEN] ;
57         } dout ;
58
59         int channels ;
60 } STATE ;
61
62
63 static void usage_exit (void) ;
64 static void interleave_int (STATE * state) ;
65 static void interleave_double (STATE * state) ;
66
67
68 int
69 main (int argc, char **argv)
70 {       STATE state ;
71         SF_INFO sfinfo ;
72         int k, double_merge = 0 ;
73
74         if (argc < 5)
75         {       puts ("\nError : need at least 2 input files.") ;
76                 usage_exit () ;
77                 } ;
78
79         if (strcmp (argv [argc - 2], "-o") != 0)
80         {       puts ("\nError : second last command line parameter should be '-o'.\n") ;
81                 usage_exit () ;
82                 } ;
83
84         if (argc - 2 > MAX_INPUTS)
85         {       printf ("\nError : Cannot handle more than %d input channels.\n\n", MAX_INPUTS) ;
86                 exit (1) ;
87                 } ;
88
89         memset (&state, 0, sizeof (state)) ;
90         memset (&sfinfo, 0, sizeof (sfinfo)) ;
91
92         for (k = 1 ; k < argc - 2 ; k++)
93         {
94                 if ((state.infile [k - 1] = sf_open (argv [k], SFM_READ, &sfinfo)) == NULL)
95                 {       printf ("\nError : Not able to open input file '%s'\n%s\n", argv [k], sf_strerror (NULL)) ;
96                         exit (1) ;
97                         } ;
98
99                 if (sfinfo.channels != 1)
100                 {       printf ("\bError : Input file '%s' should be mono (has %d channels).\n", argv [k], sfinfo.channels) ;
101                         exit (1) ;
102                         } ;
103
104                 switch (sfinfo.format & SF_FORMAT_SUBMASK)
105                 {       case SF_FORMAT_FLOAT :
106                         case SF_FORMAT_DOUBLE :
107                         case SF_FORMAT_VORBIS :
108                                 double_merge = 1 ;
109                                 break ;
110
111                         default :
112                                 break ;
113                         } ;
114
115                 state.channels ++ ;
116                 } ;
117
118         sfinfo.channels = state.channels ;
119         sfinfo.format = sfe_file_type_of_ext (argv [argc - 1], sfinfo.format) ;
120
121         if ((state.outfile = sf_open (argv [argc - 1], SFM_WRITE, &sfinfo)) == NULL)
122         {       printf ("Not able to open output file '%s'\n%s\n", argv [argc - 1], sf_strerror (NULL)) ;
123                 exit (1) ;
124                 } ;
125
126         if (double_merge)
127                 interleave_double (&state) ;
128         else
129                 interleave_int (&state) ;
130
131         for (k = 0 ; k < MAX_INPUTS ; k++)
132                 if (state.infile [k] != NULL)
133                         sf_close (state.infile [k]) ;
134         sf_close (state.outfile) ;
135
136         return 0 ;
137 } /* main */
138
139 /*------------------------------------------------------------------------------
140 */
141
142
143 static void
144 usage_exit (void)
145 {       puts ("\nUsage : sndfile-interleave <input 1> <input 2> ... -o <output file>\n") ;
146         puts ("Merge two mono files to one stereo file\n") ;
147         exit (0) ;
148 } /* usage_exit */
149
150
151 static void
152 interleave_int (STATE * state)
153 {       int max_read_len, read_len ;
154         int ch, k ;
155
156         do
157         {       max_read_len = 0 ;
158
159                 for (ch = 0 ; ch < state->channels ; ch ++)
160                 {       read_len = sf_read_int (state->infile [ch], state->din.i, BUFFER_LEN) ;
161                         if (read_len < BUFFER_LEN)
162                                 memset (state->din.i + read_len, 0, sizeof (state->din.i [0]) * (BUFFER_LEN - read_len)) ;
163
164                         for (k = 0 ; k < read_len ; k++)
165                                 state->dout.i [k * state->channels + ch] = state->din.i [k] ;
166
167                         max_read_len = MAX (max_read_len, read_len) ;
168                         } ;
169
170                 sf_writef_int (state->outfile, state->dout.i, max_read_len) ;
171                 }
172         while (max_read_len > 0) ;
173
174 } /* interleave_int */
175
176
177 static void
178 interleave_double (STATE * state)
179 {       int max_read_len, read_len ;
180         int ch, k ;
181
182         do
183         {       max_read_len = 0 ;
184
185                 for (ch = 0 ; ch < state->channels ; ch ++)
186                 {       read_len = sf_read_double (state->infile [ch], state->din.d, BUFFER_LEN) ;
187                         if (read_len < BUFFER_LEN)
188                                 memset (state->din.d + read_len, 0, sizeof (state->din.d [0]) * (BUFFER_LEN - read_len)) ;
189
190                         for (k = 0 ; k < read_len ; k++)
191                                 state->dout.d [k * state->channels + ch] = state->din.d [k] ;
192
193                         max_read_len = MAX (max_read_len, read_len) ;
194                         } ;
195
196                 sf_writef_double (state->outfile, state->dout.d, max_read_len) ;
197                 }
198         while (max_read_len > 0) ;
199
200 } /* interleave_double */