1 /*
2 ** Copyright (C) 2009-2017 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
main(int argc,char ** argv)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 	{	if (argc != 1)
75 			puts ("\nError : need a single input file.\n") ;
76 		usage_exit () ;
77 		} ;
78 
79 	memset (&state, 0, sizeof (state)) ;
80 	memset (&sfinfo, 0, sizeof (sfinfo)) ;
81 
82 	if ((state.infile = sf_open (argv [1], SFM_READ, &sfinfo)) == NULL)
83 	{	printf ("\nError : Not able to open input file '%s'\n%s\n", argv [1], sf_strerror (NULL)) ;
84 		exit (1) ;
85 		} ;
86 
87 	if (sfinfo.channels < 2)
88 	{	printf ("\nError : Input file '%s' only has one channel.\n", argv [1]) ;
89 		exit (1) ;
90 		} ;
91 
92 	if (sfinfo.channels > MAX_CHANNELS)
93 	{	printf ("\nError : Input file '%s' has too many (%d) channels. Limit is %d.\n",
94 			argv [1], sfinfo.channels, MAX_CHANNELS) ;
95 		exit (1) ;
96 		} ;
97 
98 
99 	state.channels = sfinfo.channels ;
100 	sfinfo.channels = 1 ;
101 
102 	if (snprintf (pathname, sizeof (pathname), "%s", argv [1]) > (int) sizeof (pathname))
103 	{	printf ("\nError : Length of provided filename '%s' exceeds MAX_PATH (%d).\n", argv [1], (int) sizeof (pathname)) ;
104 		exit (1) ;
105 		} ;
106 
107 	if ((cptr = strrchr (pathname, '.')) == NULL)
108 		ext [0] = 0 ;
109 	else
110 	{	snprintf (ext, sizeof (ext), "%s", cptr) ;
111 		cptr [0] = 0 ;
112 		} ;
113 
114 	printf ("Input file : %s\n", pathname) ;
115 	puts ("Output files :") ;
116 
117 	for (ch = 0 ; ch < state.channels ; ch++)
118 	{	char filename [520] ;
119 		size_t count ;
120 
121 		count = snprintf (filename, sizeof (filename), "%s_%02d%s", pathname, ch, ext) ;
122 
123 		if (count >= sizeof (filename))
124 		{	printf ("File name truncated to %s\n", filename) ;
125 			} ;
126 
127 		if ((state.outfile [ch] = sf_open (filename, SFM_WRITE, &sfinfo)) == NULL)
128 		{	printf ("Not able to open output file '%s'\n%s\n", filename, sf_strerror (NULL)) ;
129 			exit (1) ;
130 			} ;
131 
132 		printf ("    %s\n", filename) ;
133 		} ;
134 
135 	switch (sfinfo.format & SF_FORMAT_SUBMASK)
136 	{	case SF_FORMAT_FLOAT :
137 		case SF_FORMAT_DOUBLE :
138 		case SF_FORMAT_VORBIS :
139 			double_split = 1 ;
140 			break ;
141 
142 		default :
143 			double_split = 0 ;
144 			break ;
145 		} ;
146 
147 	if (double_split)
148 		deinterleave_double (&state) ;
149 	else
150 		deinterleave_int (&state) ;
151 
152 	sf_close (state.infile) ;
153 	for (ch = 0 ; ch < MAX_CHANNELS ; ch++)
154 		if (state.outfile [ch] != NULL)
155 			sf_close (state.outfile [ch]) ;
156 
157 	return 0 ;
158 } /* main */
159 
160 /*------------------------------------------------------------------------------
161 */
162 
163 static void
usage_exit(void)164 usage_exit (void)
165 {	puts ("\nUsage : sndfile-deinterleave <filename>\n") ;
166 	puts (
167 		"Split a mutli-channel file into a set of mono files.\n"
168 		"\n"
169 		"If the input file is named 'a.wav', the output files will be named\n"
170 		"a_00.wav, a_01.wav and so on.\n"
171 		) ;
172 	printf ("Using %s.\n\n", sf_version_string ()) ;
173 	exit (1) ;
174 } /* usage_exit */
175 
176 static void
deinterleave_int(STATE * state)177 deinterleave_int (STATE * state)
178 {	int read_len ;
179 	int ch, k ;
180 
181 	do
182 	{	read_len = sf_readf_int (state->infile, state->din.i, BUFFER_LEN) ;
183 
184 		for (ch = 0 ; ch < state->channels ; ch ++)
185 		{	for (k = 0 ; k < read_len ; k++)
186 				state->dout.i [k] = state->din.i [k * state->channels + ch] ;
187 			sf_write_int (state->outfile [ch], state->dout.i, read_len) ;
188 			} ;
189 		}
190 	while (read_len > 0) ;
191 
192 } /* deinterleave_int */
193 
194 static void
deinterleave_double(STATE * state)195 deinterleave_double (STATE * state)
196 {	int read_len ;
197 	int ch, k ;
198 
199 	do
200 	{	read_len = sf_readf_double (state->infile, state->din.d, BUFFER_LEN) ;
201 
202 		for (ch = 0 ; ch < state->channels ; ch ++)
203 		{	for (k = 0 ; k < read_len ; k++)
204 				state->dout.d [k] = state->din.d [k * state->channels + ch] ;
205 			sf_write_double (state->outfile [ch], state->dout.d, read_len) ;
206 			} ;
207 		}
208 	while (read_len > 0) ;
209 
210 } /* deinterleave_double */
211