1 | /* |
---|
2 | Audio File Library |
---|
3 | |
---|
4 | Copyright (C) 2001, Silicon Graphics, Inc. |
---|
5 | Copyright (C) 1998, Michael Pruett <michael@68k.org> |
---|
6 | |
---|
7 | This program is free software; you can redistribute it and/or |
---|
8 | modify it under the terms of the GNU General Public License as |
---|
9 | published by the Free Software Foundation; either version 2 of |
---|
10 | the License, or (at your option) any later version. |
---|
11 | |
---|
12 | This program is distributed in the hope that it will be |
---|
13 | useful, but WITHOUT ANY WARRANTY; without even the implied |
---|
14 | warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
---|
15 | PURPOSE. See the GNU General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU General Public |
---|
18 | License along with this program; if not, write to the Free |
---|
19 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
---|
20 | MA 02111-1307, USA. |
---|
21 | */ |
---|
22 | |
---|
23 | /* |
---|
24 | sfconvert.c |
---|
25 | |
---|
26 | sfconvert is a program which can convert various parameters of |
---|
27 | sound files. |
---|
28 | |
---|
29 | The real IRIX version has a lot of options. Mine can only |
---|
30 | convert the file format. I'm working on expanding the |
---|
31 | capabilities of this command. |
---|
32 | */ |
---|
33 | |
---|
34 | #ifdef __USE_SGI_HEADERS__ |
---|
35 | #include <dmedia/audiofile.h> |
---|
36 | #else |
---|
37 | #include <audiofile.h> |
---|
38 | #endif |
---|
39 | |
---|
40 | #include <stdio.h> |
---|
41 | #include <stdlib.h> |
---|
42 | #include <string.h> |
---|
43 | |
---|
44 | typedef int bool; |
---|
45 | |
---|
46 | #ifndef FALSE |
---|
47 | #define FALSE 0 |
---|
48 | #endif |
---|
49 | |
---|
50 | #ifndef TRUE |
---|
51 | #define TRUE 1 |
---|
52 | #endif |
---|
53 | |
---|
54 | #define BUFFER_FRAME_COUNT 65536 |
---|
55 | |
---|
56 | void usageerror (void); |
---|
57 | void printfileinfo (char *filename); |
---|
58 | int copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid, |
---|
59 | AFframecount totalFrameCount); |
---|
60 | |
---|
61 | int main (int argc, char **argv) |
---|
62 | { |
---|
63 | int i = 1; |
---|
64 | char *infilename, *outfilename; |
---|
65 | int fileFormat, outFileFormat = AF_FILE_UNKNOWN; |
---|
66 | |
---|
67 | AFfilehandle infile, outfile; |
---|
68 | AFfilesetup outfilesetup; |
---|
69 | int sampleFormat, sampleWidth, channelCount; |
---|
70 | double sampleRate; |
---|
71 | int outSampleFormat = -1, outSampleWidth = -1, |
---|
72 | outChannelCount = -1; |
---|
73 | double outMaxAmp = 1.0; |
---|
74 | |
---|
75 | AFframecount totalFrames; |
---|
76 | |
---|
77 | if (argc < 3) |
---|
78 | usageerror(); |
---|
79 | |
---|
80 | infilename = argv[1]; |
---|
81 | outfilename = argv[2]; |
---|
82 | |
---|
83 | i = 3; |
---|
84 | |
---|
85 | while (i < argc) |
---|
86 | { |
---|
87 | if (!strcmp(argv[i], "format")) |
---|
88 | { |
---|
89 | if (i + 1 >= argc) |
---|
90 | usageerror(); |
---|
91 | if (!strcmp(argv[i+1], "aiff")) |
---|
92 | outFileFormat = AF_FILE_AIFF; |
---|
93 | else if (!strcmp(argv[i+1], "aifc")) |
---|
94 | outFileFormat = AF_FILE_AIFFC; |
---|
95 | else if (!strcmp(argv[i+1], "wave")) |
---|
96 | outFileFormat = AF_FILE_WAVE; |
---|
97 | else if (!strcmp(argv[i+1], "next")) |
---|
98 | outFileFormat = AF_FILE_NEXTSND; |
---|
99 | else if (!strcmp(argv[i+1], "bics")) |
---|
100 | outFileFormat = AF_FILE_BICSF; |
---|
101 | else |
---|
102 | { |
---|
103 | fprintf(stderr, "sfconvert: Unknown format %s.\n", argv[i+1]); |
---|
104 | exit(EXIT_FAILURE); |
---|
105 | } |
---|
106 | |
---|
107 | i++; |
---|
108 | } |
---|
109 | else if (!strcmp(argv[i], "channels")) |
---|
110 | { |
---|
111 | if (i + 1 >= argc) |
---|
112 | usageerror(); |
---|
113 | |
---|
114 | outChannelCount = atoi(argv[i+1]); |
---|
115 | if (outChannelCount < 1) |
---|
116 | usageerror(); |
---|
117 | } |
---|
118 | else if (!strcmp(argv[i], "float")) |
---|
119 | { |
---|
120 | if (i + 1 >= argc) |
---|
121 | usageerror(); |
---|
122 | |
---|
123 | outSampleFormat = AF_SAMPFMT_FLOAT; |
---|
124 | outSampleWidth = 32; |
---|
125 | outMaxAmp = atof(argv[i+1]); |
---|
126 | |
---|
127 | i++; |
---|
128 | } |
---|
129 | else if (!strcmp(argv[i], "integer")) |
---|
130 | { |
---|
131 | if (i + 2 >= argc) |
---|
132 | usageerror(); |
---|
133 | |
---|
134 | outSampleWidth = atoi(argv[i+1]); |
---|
135 | if (outSampleWidth < 1 || outSampleWidth > 32) |
---|
136 | usageerror(); |
---|
137 | |
---|
138 | if (!strcmp(argv[i+2], "2scomp")) |
---|
139 | outSampleFormat = AF_SAMPFMT_TWOSCOMP; |
---|
140 | else if (!strcmp(argv[i+2], "unsigned")) |
---|
141 | outSampleFormat = AF_SAMPFMT_UNSIGNED; |
---|
142 | else |
---|
143 | usageerror(); |
---|
144 | |
---|
145 | i++; |
---|
146 | } |
---|
147 | else |
---|
148 | { |
---|
149 | printf("Unrecognized command %s\n", argv[i]); |
---|
150 | } |
---|
151 | |
---|
152 | i++; |
---|
153 | } |
---|
154 | |
---|
155 | infile = afOpenFile(infilename, "r", AF_NULL_FILESETUP); |
---|
156 | if (infile == AF_NULL_FILEHANDLE) |
---|
157 | { |
---|
158 | printf("Could not open file '%s' for reading.\n", infilename); |
---|
159 | return 1; |
---|
160 | } |
---|
161 | |
---|
162 | /* Get audio format parameters from input file. */ |
---|
163 | fileFormat = afGetFileFormat(infile, NULL); |
---|
164 | totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK); |
---|
165 | channelCount = afGetChannels(infile, AF_DEFAULT_TRACK); |
---|
166 | sampleRate = afGetRate(infile, AF_DEFAULT_TRACK); |
---|
167 | afGetSampleFormat(infile, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth); |
---|
168 | |
---|
169 | /* Initialize output audio format parameters. */ |
---|
170 | outfilesetup = afNewFileSetup(); |
---|
171 | |
---|
172 | if (outFileFormat == -1) |
---|
173 | outFileFormat = fileFormat; |
---|
174 | |
---|
175 | if (outSampleFormat == -1 || outSampleWidth == -1) |
---|
176 | { |
---|
177 | outSampleFormat = sampleFormat; |
---|
178 | outSampleWidth = sampleWidth; |
---|
179 | } |
---|
180 | |
---|
181 | if (outChannelCount == -1) |
---|
182 | outChannelCount = channelCount; |
---|
183 | |
---|
184 | afInitFileFormat(outfilesetup, outFileFormat); |
---|
185 | afInitSampleFormat(outfilesetup, AF_DEFAULT_TRACK, outSampleFormat, |
---|
186 | outSampleWidth); |
---|
187 | afInitChannels(outfilesetup, AF_DEFAULT_TRACK, outChannelCount); |
---|
188 | afInitRate(outfilesetup, AF_DEFAULT_TRACK, sampleRate); |
---|
189 | |
---|
190 | outfile = afOpenFile(outfilename, "w", outfilesetup); |
---|
191 | if (outfile == AF_NULL_FILEHANDLE) |
---|
192 | { |
---|
193 | printf("Could not open file '%s' for writing.\n", outfilename); |
---|
194 | return 1; |
---|
195 | } |
---|
196 | |
---|
197 | /* |
---|
198 | Set the output file's virtual audio format parameters |
---|
199 | to match the audio format parameters of the input file. |
---|
200 | */ |
---|
201 | afSetVirtualChannels(outfile, AF_DEFAULT_TRACK, channelCount); |
---|
202 | afSetVirtualSampleFormat(outfile, AF_DEFAULT_TRACK, sampleFormat, |
---|
203 | sampleWidth); |
---|
204 | |
---|
205 | afFreeFileSetup(outfilesetup); |
---|
206 | |
---|
207 | copyaudiodata(infile, outfile, AF_DEFAULT_TRACK, totalFrames); |
---|
208 | |
---|
209 | afCloseFile(infile); |
---|
210 | afCloseFile(outfile); |
---|
211 | |
---|
212 | printfileinfo(infilename); |
---|
213 | putchar('\n'); |
---|
214 | printfileinfo(outfilename); |
---|
215 | |
---|
216 | return EXIT_SUCCESS; |
---|
217 | } |
---|
218 | |
---|
219 | void usageerror (void) |
---|
220 | { |
---|
221 | printf("usage: sfconvert infile outfile [ options ... ] [ output keywords ... ]\n"); |
---|
222 | printf("\n"); |
---|
223 | |
---|
224 | printf("Where keywords specify format of input or output soundfile:\n"); |
---|
225 | printf(" byteorder e endian (e is big or little)\n"); |
---|
226 | printf(" channels n n-channel file (1 or 2)\n"); |
---|
227 | printf(" format f file format f (see below)\n"); |
---|
228 | printf(" integer n s n-bit integer file, where s is one of\n"); |
---|
229 | printf(" 2scomp: 2's complement signed data\n"); |
---|
230 | printf(" unsigned: unsigned data\n"); |
---|
231 | printf(" float m floating point file, maxamp m (usually 1.0)\n"); |
---|
232 | printf("\n"); |
---|
233 | |
---|
234 | printf("Currently supported formats are:\n"); |
---|
235 | printf("\n"); |
---|
236 | printf(" aiff Audio Interchange File Format\n"); |
---|
237 | printf(" aifc AIFF-C File Format\n"); |
---|
238 | printf(" next NeXT/Sun Format\n"); |
---|
239 | printf(" wave MS RIFF WAVE Format\n"); |
---|
240 | printf(" bics Berkeley/IRCAM/CARL Sound File Format\n"); |
---|
241 | printf("\n"); |
---|
242 | |
---|
243 | exit(EXIT_FAILURE); |
---|
244 | } |
---|
245 | |
---|
246 | /* |
---|
247 | Copy audio data from one file to another. This function |
---|
248 | assumes that the virtual sample formats of the two files |
---|
249 | match. |
---|
250 | */ |
---|
251 | int copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid, |
---|
252 | AFframecount totalFrameCount) |
---|
253 | { |
---|
254 | AFframecount totalFramesWritten = 0; |
---|
255 | void *buffer; |
---|
256 | int frameSize; |
---|
257 | bool ok = TRUE, done = FALSE; |
---|
258 | |
---|
259 | frameSize = afGetVirtualFrameSize(infile, trackid, 1); |
---|
260 | |
---|
261 | buffer = malloc(BUFFER_FRAME_COUNT * frameSize); |
---|
262 | |
---|
263 | while (done == FALSE) |
---|
264 | { |
---|
265 | AFframecount framesToRead = BUFFER_FRAME_COUNT; |
---|
266 | AFframecount framesRead, framesWritten; |
---|
267 | |
---|
268 | framesRead = afReadFrames(infile, trackid, buffer, |
---|
269 | framesToRead); |
---|
270 | |
---|
271 | if (framesRead < 0) |
---|
272 | { |
---|
273 | fprintf(stderr, "Bad read of audio track data.\n"); |
---|
274 | ok = FALSE; |
---|
275 | done = TRUE; |
---|
276 | } |
---|
277 | |
---|
278 | framesWritten = afWriteFrames(outfile, trackid, buffer, |
---|
279 | framesRead); |
---|
280 | |
---|
281 | if (framesWritten < 0) |
---|
282 | { |
---|
283 | fprintf(stderr, "Bad write of audio track data.\n"); |
---|
284 | ok = FALSE; |
---|
285 | done = TRUE; |
---|
286 | } |
---|
287 | else |
---|
288 | { |
---|
289 | totalFramesWritten += framesWritten; |
---|
290 | } |
---|
291 | |
---|
292 | if (totalFramesWritten == totalFrameCount) |
---|
293 | done = TRUE; |
---|
294 | } |
---|
295 | |
---|
296 | free(buffer); |
---|
297 | |
---|
298 | return ok; |
---|
299 | } |
---|