1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/*
Audio File Library
Copyright (C) 1998, 2011-2012, Michael Pruett <michael@68k.org>
Copyright (C) 2001, Silicon Graphics, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
sfconvert is a program which can convert various parameters of
sound files.
*/
#include "config.h"
#ifdef __USE_SGI_HEADERS__
#include <dmedia/audiofile.h>
#else
#include <audiofile.h>
#endif
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "printinfo.h"
void printversion (void);
void printusage (void);
void usageerror (void);
bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid);
int firstBitSet(int x)
{
int position=0;
while (x!=0)
{
x>>=1;
++position;
}
return position;
}
#ifndef __has_builtin
#define __has_builtin(x) 0
#endif
bool multiplyCheckOverflow(int a, int b, int *result)
{
#if (defined __GNUC__ && __GNUC__ >= 5) || ( __clang__ && __has_builtin(__builtin_mul_overflow))
return __builtin_mul_overflow(a, b, result);
#else
if (firstBitSet(a)+firstBitSet(b)>31) // int is signed, so we can't use 32 bits
return true;
*result = a * b;
return false;
#endif
}
int main (int argc, char **argv)
{
if (argc == 2)
{
if (!strcmp(argv[1], "--version") || !strcmp(argv[1], "-v"))
{
printversion();
exit(EXIT_SUCCESS);
}
if (!strcmp(argv[1], "--help") || !strcmp(argv[1], "-h"))
{
printusage();
exit(EXIT_SUCCESS);
}
}
if (argc < 3)
usageerror();
const char *inFileName = argv[1];
const char *outFileName = argv[2];
int outFileFormat = AF_FILE_UNKNOWN;
int outSampleFormat = -1, outSampleWidth = -1, outChannelCount = -1;
int outCompression = AF_COMPRESSION_NONE;
double outMaxAmp = 1.0;
int i = 3;
while (i < argc)
{
if (!strcmp(argv[i], "format"))
{
if (i + 1 >= argc)
usageerror();
if (!strcmp(argv[i+1], "aiff"))
outFileFormat = AF_FILE_AIFF;
else if (!strcmp(argv[i+1], "aifc"))
outFileFormat = AF_FILE_AIFFC;
else if (!strcmp(argv[i+1], "wave"))
outFileFormat = AF_FILE_WAVE;
else if (!strcmp(argv[i+1], "next"))
outFileFormat = AF_FILE_NEXTSND;
else if (!strcmp(argv[i+1], "bics"))
outFileFormat = AF_FILE_BICSF;
else if (!strcmp(argv[i+1], "smp"))
outFileFormat = AF_FILE_SAMPLEVISION;
else if (!strcmp(argv[i+1], "voc"))
outFileFormat = AF_FILE_VOC;
else if (!strcmp(argv[i+1], "nist"))
outFileFormat = AF_FILE_NIST_SPHERE;
else if (!strcmp(argv[i+1], "caf"))
outFileFormat = AF_FILE_CAF;
else if (!strcmp(argv[i+1], "flac"))
outFileFormat = AF_FILE_FLAC;
else
{
fprintf(stderr, "sfconvert: Unknown format %s.\n", argv[i+1]);
exit(EXIT_FAILURE);
}
// Increment for argument.
i++;
}
else if (!strcmp(argv[i], "channels"))
{
if (i + 1 >= argc)
usageerror();
outChannelCount = atoi(argv[i+1]);
if (outChannelCount < 1)
usageerror();
// Increment for argument.
i++;
}
else if (!strcmp(argv[i], "float"))
{
if (i + 1 >= argc)
usageerror();
outSampleFormat = AF_SAMPFMT_FLOAT;
outSampleWidth = 32;
outMaxAmp = atof(argv[i+1]);
// outMaxAmp is currently unused.
(void) outMaxAmp;
// Increment for argument.
i++;
}
else if (!strcmp(argv[i], "integer"))
{
if (i + 2 >= argc)
usageerror();
outSampleWidth = atoi(argv[i+1]);
if (outSampleWidth < 1 || outSampleWidth > 32)
usageerror();
if (!strcmp(argv[i+2], "2scomp"))
outSampleFormat = AF_SAMPFMT_TWOSCOMP;
else if (!strcmp(argv[i+2], "unsigned"))
outSampleFormat = AF_SAMPFMT_UNSIGNED;
else
usageerror();
// Increment for arguments.
i += 2;
}
else if (!strcmp(argv[i], "compression"))
{
if (i + 1 >= argc)
usageerror();
if (!strcmp(argv[i+1], "none"))
outCompression = AF_COMPRESSION_NONE;
else if (!strcmp(argv[i+1], "ulaw"))
outCompression = AF_COMPRESSION_G711_ULAW;
else if (!strcmp(argv[i+1], "alaw"))
outCompression = AF_COMPRESSION_G711_ALAW;
else if (!strcmp(argv[i+1], "ima"))
outCompression = AF_COMPRESSION_IMA;
else if (!strcmp(argv[i+1], "msadpcm"))
outCompression = AF_COMPRESSION_MS_ADPCM;
else if (!strcmp(argv[i+1], "flac"))
outCompression = AF_COMPRESSION_FLAC;
else if (!strcmp(argv[i+1], "alac"))
outCompression = AF_COMPRESSION_ALAC;
else
{
fprintf(stderr, "sfconvert: Unknown compression format %s.\n", argv[i+1]);
exit(EXIT_FAILURE);
}
i++;
}
else
{
printf("Unrecognized command %s\n", argv[i]);
}
i++;
}
AFfilehandle inFile = afOpenFile(inFileName, "r", AF_NULL_FILESETUP);
if (!inFile)
{
printf("Could not open file '%s' for reading.\n", inFileName);
return EXIT_FAILURE;
}
// Get audio format parameters from input file.
int fileFormat = afGetFileFormat(inFile, NULL);
int channelCount = afGetChannels(inFile, AF_DEFAULT_TRACK);
double sampleRate = afGetRate(inFile, AF_DEFAULT_TRACK);
int sampleFormat, sampleWidth;
afGetSampleFormat(inFile, AF_DEFAULT_TRACK, &sampleFormat, &sampleWidth);
// Initialize output audio format parameters.
AFfilesetup outFileSetup = afNewFileSetup();
if (outFileFormat == -1)
outFileFormat = fileFormat;
if (outSampleFormat == -1 || outSampleWidth == -1)
{
outSampleFormat = sampleFormat;
outSampleWidth = sampleWidth;
}
if (outChannelCount == -1)
outChannelCount = channelCount;
afInitFileFormat(outFileSetup, outFileFormat);
afInitCompression(outFileSetup, AF_DEFAULT_TRACK, outCompression);
afInitSampleFormat(outFileSetup, AF_DEFAULT_TRACK, outSampleFormat,
outSampleWidth);
afInitChannels(outFileSetup, AF_DEFAULT_TRACK, outChannelCount);
afInitRate(outFileSetup, AF_DEFAULT_TRACK, sampleRate);
AFfilehandle outFile = afOpenFile(outFileName, "w", outFileSetup);
if (!outFile)
{
printf("Could not open file '%s' for writing.\n", outFileName);
return EXIT_FAILURE;
}
afFreeFileSetup(outFileSetup);
/*
Set the output file's virtual audio format parameters
to match the audio format parameters of the input file.
*/
afSetVirtualChannels(outFile, AF_DEFAULT_TRACK, channelCount);
afSetVirtualSampleFormat(outFile, AF_DEFAULT_TRACK, sampleFormat,
sampleWidth);
bool success = copyaudiodata(inFile, outFile, AF_DEFAULT_TRACK);
afCloseFile(inFile);
afCloseFile(outFile);
if (!success)
{
unlink(outFileName);
return EXIT_FAILURE;
}
printfileinfo(inFileName);
putchar('\n');
printfileinfo(outFileName);
return EXIT_SUCCESS;
}
void printusage (void)
{
printf("usage: sfconvert infile outfile [ options ... ] [ output keywords ... ]\n");
printf("\n");
printf("Where keywords specify format of input or output soundfile:\n");
printf(" format f file format f (see below)\n");
printf(" compression c compression format c (see below)\n");
printf(" byteorder e endian (e is big or little)\n");
printf(" channels n n-channel file (1 or 2)\n");
printf(" integer n s n-bit integer file, where s is one of\n");
printf(" 2scomp: 2's complement signed data\n");
printf(" unsigned: unsigned data\n");
printf(" float m floating point file, maxamp m (usually 1.0)\n");
printf("\n");
printf("Currently supported file formats are:\n");
printf("\n");
printf(" aiff Audio Interchange File Format\n");
printf(" aifc AIFF-C File Format\n");
printf(" next NeXT/Sun Format\n");
printf(" wave MS RIFF WAVE Format\n");
printf(" bics Berkeley/IRCAM/CARL Sound File Format\n");
printf(" smp Sample Vision Format\n");
printf(" voc Creative Voice File\n");
printf(" nist NIST SPHERE Format\n");
printf(" caf Core Audio Format\n");
printf("\n");
printf("Currently supported compression formats are:\n");
printf("\n");
printf(" ulaw G.711 u-law\n");
printf(" alaw G.711 A-law\n");
printf(" ima IMA ADPCM\n");
printf(" msadpcm MS ADPCM\n");
printf(" flac FLAC\n");
printf(" alac Apple Lossless Audio Codec\n");
printf("\n");
}
void usageerror (void)
{
printusage();
exit(EXIT_FAILURE);
}
void printversion (void)
{
printf("sfconvert: Audio File Library version %s\n", VERSION);
}
/*
Copy audio data from one file to another. This function
assumes that the virtual sample formats of the two files
match.
*/
bool copyaudiodata (AFfilehandle infile, AFfilehandle outfile, int trackid)
{
int frameSize = afGetVirtualFrameSize(infile, trackid, 1);
if(frameSize <= 0)
return false;
int kBufferFrameCount = 65536;
int bufferSize;
while (multiplyCheckOverflow(kBufferFrameCount, frameSize, &bufferSize))
kBufferFrameCount /= 2;
void *buffer = malloc(bufferSize);
AFframecount totalFrames = afGetFrameCount(infile, AF_DEFAULT_TRACK);
AFframecount totalFramesWritten = 0;
bool success = true;
while (totalFramesWritten < totalFrames)
{
AFframecount framesToRead = totalFrames - totalFramesWritten;
if (framesToRead > kBufferFrameCount)
framesToRead = kBufferFrameCount;
AFframecount framesRead = afReadFrames(infile, trackid, buffer,
framesToRead);
if (framesRead < framesToRead)
{
fprintf(stderr, "Bad read of audio track data.\n");
success = false;
break;
}
AFframecount framesWritten = afWriteFrames(outfile, trackid, buffer,
framesRead);
if (framesWritten < framesRead)
{
fprintf(stderr, "Bad write of audio track data.\n");
success = false;
break;
}
totalFramesWritten += framesWritten;
}
free(buffer);
return success;
}
|