1b815c7f3Sopenharmony_ci/*
2b815c7f3Sopenharmony_ci** Copyright (C) 1999-2014 Erik de Castro Lopo <erikd@mega-nerd.com>
3b815c7f3Sopenharmony_ci**
4b815c7f3Sopenharmony_ci** All rights reserved.
5b815c7f3Sopenharmony_ci**
6b815c7f3Sopenharmony_ci** Redistribution and use in source and binary forms, with or without
7b815c7f3Sopenharmony_ci** modification, are permitted provided that the following conditions are
8b815c7f3Sopenharmony_ci** met:
9b815c7f3Sopenharmony_ci**
10b815c7f3Sopenharmony_ci**     * Redistributions of source code must retain the above copyright
11b815c7f3Sopenharmony_ci**       notice, this list of conditions and the following disclaimer.
12b815c7f3Sopenharmony_ci**     * Redistributions in binary form must reproduce the above copyright
13b815c7f3Sopenharmony_ci**       notice, this list of conditions and the following disclaimer in
14b815c7f3Sopenharmony_ci**       the documentation and/or other materials provided with the
15b815c7f3Sopenharmony_ci**       distribution.
16b815c7f3Sopenharmony_ci**     * Neither the author nor the names of any contributors may be used
17b815c7f3Sopenharmony_ci**       to endorse or promote products derived from this software without
18b815c7f3Sopenharmony_ci**       specific prior written permission.
19b815c7f3Sopenharmony_ci**
20b815c7f3Sopenharmony_ci** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21b815c7f3Sopenharmony_ci** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22b815c7f3Sopenharmony_ci** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23b815c7f3Sopenharmony_ci** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24b815c7f3Sopenharmony_ci** CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25b815c7f3Sopenharmony_ci** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26b815c7f3Sopenharmony_ci** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27b815c7f3Sopenharmony_ci** OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28b815c7f3Sopenharmony_ci** WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29b815c7f3Sopenharmony_ci** OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30b815c7f3Sopenharmony_ci** ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31b815c7f3Sopenharmony_ci*/
32b815c7f3Sopenharmony_ci
33b815c7f3Sopenharmony_ci#include	<stdio.h>
34b815c7f3Sopenharmony_ci#include	<stdlib.h>
35b815c7f3Sopenharmony_ci#include	<string.h>
36b815c7f3Sopenharmony_ci#include	<ctype.h>
37b815c7f3Sopenharmony_ci
38b815c7f3Sopenharmony_ci#include	<sndfile.h>
39b815c7f3Sopenharmony_ci
40b815c7f3Sopenharmony_ci#include	"common.h"
41b815c7f3Sopenharmony_ci
42b815c7f3Sopenharmony_ci#define		BUFFER_LEN	(1 << 16)
43b815c7f3Sopenharmony_ci
44b815c7f3Sopenharmony_ci
45b815c7f3Sopenharmony_cistatic void concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels) ;
46b815c7f3Sopenharmony_cistatic void concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels) ;
47b815c7f3Sopenharmony_ci
48b815c7f3Sopenharmony_cistatic void
49b815c7f3Sopenharmony_ciusage_exit (const char *progname)
50b815c7f3Sopenharmony_ci{
51b815c7f3Sopenharmony_ci	printf ("\nUsage : %s <infile1> <infile2>  ... <outfile>\n\n", progname) ;
52b815c7f3Sopenharmony_ci	puts (
53b815c7f3Sopenharmony_ci		"    Create a new output file <outfile> containing the concatenated\n"
54b815c7f3Sopenharmony_ci		"    audio data from froms <infile1> <infile2> ....\n"
55b815c7f3Sopenharmony_ci		"\n"
56b815c7f3Sopenharmony_ci		"    The joined file will be encoded in the same format as the data\n"
57b815c7f3Sopenharmony_ci		"    in infile1, with all the data in subsequent files automatically\n"
58b815c7f3Sopenharmony_ci		"    converted to the correct encoding.\n"
59b815c7f3Sopenharmony_ci		"\n"
60b815c7f3Sopenharmony_ci		"    The only restriction is that the two files must have the same\n"
61b815c7f3Sopenharmony_ci		"    number of channels.\n"
62b815c7f3Sopenharmony_ci		) ;
63b815c7f3Sopenharmony_ci
64b815c7f3Sopenharmony_ci	exit (1) ;
65b815c7f3Sopenharmony_ci} /* usage_exit */
66b815c7f3Sopenharmony_ci
67b815c7f3Sopenharmony_ciint
68b815c7f3Sopenharmony_cimain (int argc, char *argv [])
69b815c7f3Sopenharmony_ci{	const char	*progname, *outfilename ;
70b815c7f3Sopenharmony_ci	SNDFILE		*outfile, **infiles ;
71b815c7f3Sopenharmony_ci	SF_INFO		sfinfo_out, sfinfo_in ;
72b815c7f3Sopenharmony_ci	void 		(*func) (SNDFILE*, SNDFILE*, int) ;
73b815c7f3Sopenharmony_ci	int			k ;
74b815c7f3Sopenharmony_ci
75b815c7f3Sopenharmony_ci	progname = program_name (argv [0]) ;
76b815c7f3Sopenharmony_ci
77b815c7f3Sopenharmony_ci	if (argc < 4)
78b815c7f3Sopenharmony_ci		usage_exit (progname) ;
79b815c7f3Sopenharmony_ci
80b815c7f3Sopenharmony_ci	argv ++ ;
81b815c7f3Sopenharmony_ci	argc -- ;
82b815c7f3Sopenharmony_ci
83b815c7f3Sopenharmony_ci	argc -- ;
84b815c7f3Sopenharmony_ci	outfilename = argv [argc] ;
85b815c7f3Sopenharmony_ci
86b815c7f3Sopenharmony_ci	if ((infiles = calloc (argc, sizeof (SNDFILE*))) == NULL)
87b815c7f3Sopenharmony_ci	{	printf ("\nError : Malloc failed.\n\n") ;
88b815c7f3Sopenharmony_ci		exit (1) ;
89b815c7f3Sopenharmony_ci		} ;
90b815c7f3Sopenharmony_ci
91b815c7f3Sopenharmony_ci	memset (&sfinfo_in, 0, sizeof (sfinfo_in)) ;
92b815c7f3Sopenharmony_ci
93b815c7f3Sopenharmony_ci	if ((infiles [0] = sf_open (argv [0], SFM_READ, &sfinfo_in)) == NULL)
94b815c7f3Sopenharmony_ci	{	printf ("\nError : failed to open file '%s'.\n\n", argv [0]) ;
95b815c7f3Sopenharmony_ci		exit (1) ;
96b815c7f3Sopenharmony_ci		} ;
97b815c7f3Sopenharmony_ci
98b815c7f3Sopenharmony_ci	sfinfo_out = sfinfo_in ;
99b815c7f3Sopenharmony_ci
100b815c7f3Sopenharmony_ci	for (k = 1 ; k < argc ; k++)
101b815c7f3Sopenharmony_ci	{	if ((infiles [k] = sf_open (argv [k], SFM_READ, &sfinfo_in)) == NULL)
102b815c7f3Sopenharmony_ci		{	printf ("\nError : failed to open file '%s'.\n\n", argv [k]) ;
103b815c7f3Sopenharmony_ci			exit (1) ;
104b815c7f3Sopenharmony_ci			} ;
105b815c7f3Sopenharmony_ci
106b815c7f3Sopenharmony_ci		if (sfinfo_in.channels != sfinfo_out.channels)
107b815c7f3Sopenharmony_ci		{	printf ("\nError : File '%s' has %d channels (should have %d).\n\n", argv [k], sfinfo_in.channels, sfinfo_out.channels) ;
108b815c7f3Sopenharmony_ci			exit (1) ;
109b815c7f3Sopenharmony_ci			} ;
110b815c7f3Sopenharmony_ci		} ;
111b815c7f3Sopenharmony_ci
112b815c7f3Sopenharmony_ci	if ((outfile = sf_open (outfilename, SFM_WRITE, &sfinfo_out)) == NULL)
113b815c7f3Sopenharmony_ci	{	printf ("\nError : Not able to open input file %s.\n", outfilename) ;
114b815c7f3Sopenharmony_ci		puts (sf_strerror (NULL)) ;
115b815c7f3Sopenharmony_ci		exit (1) ;
116b815c7f3Sopenharmony_ci		} ;
117b815c7f3Sopenharmony_ci
118b815c7f3Sopenharmony_ci	if ((sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DOUBLE ||
119b815c7f3Sopenharmony_ci			(sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT)
120b815c7f3Sopenharmony_ci		func = concat_data_fp ;
121b815c7f3Sopenharmony_ci	else
122b815c7f3Sopenharmony_ci		func = concat_data_int ;
123b815c7f3Sopenharmony_ci
124b815c7f3Sopenharmony_ci	for (k = 0 ; k < argc ; k++)
125b815c7f3Sopenharmony_ci	{	func (outfile, infiles [k], sfinfo_out.channels) ;
126b815c7f3Sopenharmony_ci		sf_close (infiles [k]) ;
127b815c7f3Sopenharmony_ci		} ;
128b815c7f3Sopenharmony_ci
129b815c7f3Sopenharmony_ci	sf_close (outfile) ;
130b815c7f3Sopenharmony_ci	free (infiles) ;
131b815c7f3Sopenharmony_ci
132b815c7f3Sopenharmony_ci	return 0 ;
133b815c7f3Sopenharmony_ci} /* main */
134b815c7f3Sopenharmony_ci
135b815c7f3Sopenharmony_cistatic void
136b815c7f3Sopenharmony_ciconcat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels)
137b815c7f3Sopenharmony_ci{	static double	data [BUFFER_LEN] ;
138b815c7f3Sopenharmony_ci	int		frames, readcount ;
139b815c7f3Sopenharmony_ci
140b815c7f3Sopenharmony_ci	frames = BUFFER_LEN / channels ;
141b815c7f3Sopenharmony_ci	readcount = frames ;
142b815c7f3Sopenharmony_ci
143b815c7f3Sopenharmony_ci	sf_seek (wfile, 0, SEEK_END) ;
144b815c7f3Sopenharmony_ci
145b815c7f3Sopenharmony_ci	while (readcount > 0)
146b815c7f3Sopenharmony_ci	{	readcount = (int) sf_readf_double (rofile, data, frames) ;
147b815c7f3Sopenharmony_ci		sf_writef_double (wfile, data, readcount) ;
148b815c7f3Sopenharmony_ci		} ;
149b815c7f3Sopenharmony_ci
150b815c7f3Sopenharmony_ci	return ;
151b815c7f3Sopenharmony_ci} /* concat_data_fp */
152b815c7f3Sopenharmony_ci
153b815c7f3Sopenharmony_cistatic void
154b815c7f3Sopenharmony_ciconcat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels)
155b815c7f3Sopenharmony_ci{	static int	data [BUFFER_LEN] ;
156b815c7f3Sopenharmony_ci	int		frames, readcount ;
157b815c7f3Sopenharmony_ci
158b815c7f3Sopenharmony_ci	frames = BUFFER_LEN / channels ;
159b815c7f3Sopenharmony_ci	readcount = frames ;
160b815c7f3Sopenharmony_ci
161b815c7f3Sopenharmony_ci	sf_seek (wfile, 0, SEEK_END) ;
162b815c7f3Sopenharmony_ci
163b815c7f3Sopenharmony_ci	while (readcount > 0)
164b815c7f3Sopenharmony_ci	{	readcount = (int) sf_readf_int (rofile, data, frames) ;
165b815c7f3Sopenharmony_ci		sf_writef_int (wfile, data, readcount) ;
166b815c7f3Sopenharmony_ci		} ;
167b815c7f3Sopenharmony_ci
168b815c7f3Sopenharmony_ci	return ;
169b815c7f3Sopenharmony_ci} /* concat_data_int */
170b815c7f3Sopenharmony_ci
171