Chris@40
|
1 /*
|
Chris@40
|
2 ** Copyright (C) 1999-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 <ctype.h>
|
Chris@40
|
37
|
Chris@40
|
38 #include <sndfile.h>
|
Chris@40
|
39
|
Chris@40
|
40 #include "common.h"
|
Chris@40
|
41
|
Chris@40
|
42 #define BUFFER_LEN (1 << 16)
|
Chris@40
|
43
|
Chris@40
|
44
|
Chris@40
|
45 static void concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels) ;
|
Chris@40
|
46 static void concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels) ;
|
Chris@40
|
47
|
Chris@40
|
48 static void
|
Chris@40
|
49 usage_exit (const char *progname)
|
Chris@40
|
50 {
|
Chris@40
|
51 printf ("\nUsage : %s <infile1> <infile2> ... <outfile>\n\n", progname) ;
|
Chris@40
|
52 puts (
|
Chris@40
|
53 " Create a new output file <outfile> containing the concatenated\n"
|
Chris@40
|
54 " audio data from froms <infile1> <infile2> ....\n"
|
Chris@40
|
55 "\n"
|
Chris@40
|
56 " The joined file will be encoded in the same format as the data\n"
|
Chris@40
|
57 " in infile1, with all the data in subsequent files automatically\n"
|
Chris@40
|
58 " converted to the correct encoding.\n"
|
Chris@40
|
59 "\n"
|
Chris@40
|
60 " The only restriction is that the two files must have the same\n"
|
Chris@40
|
61 " number of channels.\n"
|
Chris@40
|
62 ) ;
|
Chris@40
|
63
|
Chris@40
|
64 exit (1) ;
|
Chris@40
|
65 } /* usage_exit */
|
Chris@40
|
66
|
Chris@40
|
67 int
|
Chris@40
|
68 main (int argc, char *argv [])
|
Chris@40
|
69 { const char *progname, *outfilename ;
|
Chris@40
|
70 SNDFILE *outfile, **infiles ;
|
Chris@40
|
71 SF_INFO sfinfo_out, sfinfo_in ;
|
Chris@40
|
72 void (*func) (SNDFILE*, SNDFILE*, int) ;
|
Chris@40
|
73 int k ;
|
Chris@40
|
74
|
Chris@40
|
75 progname = program_name (argv [0]) ;
|
Chris@40
|
76
|
Chris@40
|
77 if (argc < 4)
|
Chris@40
|
78 usage_exit (progname) ;
|
Chris@40
|
79
|
Chris@40
|
80 argv ++ ;
|
Chris@40
|
81 argc -- ;
|
Chris@40
|
82
|
Chris@40
|
83 argc -- ;
|
Chris@40
|
84 outfilename = argv [argc] ;
|
Chris@40
|
85
|
Chris@40
|
86 if ((infiles = calloc (argc, sizeof (SNDFILE*))) == NULL)
|
Chris@40
|
87 { printf ("\nError : Malloc failed.\n\n") ;
|
Chris@40
|
88 exit (1) ;
|
Chris@40
|
89 } ;
|
Chris@40
|
90
|
Chris@40
|
91 memset (&sfinfo_in, 0, sizeof (sfinfo_in)) ;
|
Chris@40
|
92
|
Chris@40
|
93 if ((infiles [0] = sf_open (argv [0], SFM_READ, &sfinfo_in)) == NULL)
|
Chris@40
|
94 { printf ("\nError : failed to open file '%s'.\n\n", argv [0]) ;
|
Chris@40
|
95 exit (1) ;
|
Chris@40
|
96 } ;
|
Chris@40
|
97
|
Chris@40
|
98 sfinfo_out = sfinfo_in ;
|
Chris@40
|
99
|
Chris@40
|
100 for (k = 1 ; k < argc ; k++)
|
Chris@40
|
101 { if ((infiles [k] = sf_open (argv [k], SFM_READ, &sfinfo_in)) == NULL)
|
Chris@40
|
102 { printf ("\nError : failed to open file '%s'.\n\n", argv [k]) ;
|
Chris@40
|
103 exit (1) ;
|
Chris@40
|
104 } ;
|
Chris@40
|
105
|
Chris@40
|
106 if (sfinfo_in.channels != sfinfo_out.channels)
|
Chris@40
|
107 { printf ("\nError : File '%s' has %d channels (should have %d).\n\n", argv [k], sfinfo_in.channels, sfinfo_out.channels) ;
|
Chris@40
|
108 exit (1) ;
|
Chris@40
|
109 } ;
|
Chris@40
|
110 } ;
|
Chris@40
|
111
|
Chris@40
|
112 if ((outfile = sf_open (outfilename, SFM_WRITE, &sfinfo_out)) == NULL)
|
Chris@40
|
113 { printf ("\nError : Not able to open input file %s.\n", outfilename) ;
|
Chris@40
|
114 puts (sf_strerror (NULL)) ;
|
Chris@40
|
115 exit (1) ;
|
Chris@40
|
116 } ;
|
Chris@40
|
117
|
Chris@40
|
118 if ((sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_DOUBLE ||
|
Chris@40
|
119 (sfinfo_out.format & SF_FORMAT_SUBMASK) == SF_FORMAT_FLOAT)
|
Chris@40
|
120 func = concat_data_fp ;
|
Chris@40
|
121 else
|
Chris@40
|
122 func = concat_data_int ;
|
Chris@40
|
123
|
Chris@40
|
124 for (k = 0 ; k < argc ; k++)
|
Chris@40
|
125 { func (outfile, infiles [k], sfinfo_out.channels) ;
|
Chris@40
|
126 sf_close (infiles [k]) ;
|
Chris@40
|
127 } ;
|
Chris@40
|
128
|
Chris@40
|
129 sf_close (outfile) ;
|
Chris@40
|
130 free (infiles) ;
|
Chris@40
|
131
|
Chris@40
|
132 return 0 ;
|
Chris@40
|
133 } /* main */
|
Chris@40
|
134
|
Chris@40
|
135 static void
|
Chris@40
|
136 concat_data_fp (SNDFILE *wfile, SNDFILE *rofile, int channels)
|
Chris@40
|
137 { static double data [BUFFER_LEN] ;
|
Chris@40
|
138 int frames, readcount ;
|
Chris@40
|
139
|
Chris@40
|
140 frames = BUFFER_LEN / channels ;
|
Chris@40
|
141 readcount = frames ;
|
Chris@40
|
142
|
Chris@40
|
143 sf_seek (wfile, 0, SEEK_END) ;
|
Chris@40
|
144
|
Chris@40
|
145 while (readcount > 0)
|
Chris@40
|
146 { readcount = sf_readf_double (rofile, data, frames) ;
|
Chris@40
|
147 sf_writef_double (wfile, data, readcount) ;
|
Chris@40
|
148 } ;
|
Chris@40
|
149
|
Chris@40
|
150 return ;
|
Chris@40
|
151 } /* concat_data_fp */
|
Chris@40
|
152
|
Chris@40
|
153 static void
|
Chris@40
|
154 concat_data_int (SNDFILE *wfile, SNDFILE *rofile, int channels)
|
Chris@40
|
155 { static int data [BUFFER_LEN] ;
|
Chris@40
|
156 int frames, readcount ;
|
Chris@40
|
157
|
Chris@40
|
158 frames = BUFFER_LEN / channels ;
|
Chris@40
|
159 readcount = frames ;
|
Chris@40
|
160
|
Chris@40
|
161 sf_seek (wfile, 0, SEEK_END) ;
|
Chris@40
|
162
|
Chris@40
|
163 while (readcount > 0)
|
Chris@40
|
164 { readcount = sf_readf_int (rofile, data, frames) ;
|
Chris@40
|
165 sf_writef_int (wfile, data, readcount) ;
|
Chris@40
|
166 } ;
|
Chris@40
|
167
|
Chris@40
|
168 return ;
|
Chris@40
|
169 } /* concat_data_int */
|
Chris@40
|
170
|