ascii2aifc.c 3.02 KB
#include <stdio.h>
#include <stdlib.h>
#include <bstring.h>
#include <assert.h>
#include <getopt.h>
#include <audiofile.h>
#include <math.h>

#define	MIN(a,b) (((a)<(b))?(a):(b))
#define	MAX(a,b) (((a)>(b))?(a):(b))

static char usage[] = "[-stereo] [-r sample rate] infile outfile";

main(int argc, char **argv)
{
    char                *ofile = NULL;
    char                *ifile = NULL;
    
    extern char         *optarg;
    extern int          optind;
    int                 errflg=0;
    int                 c;
    int                 verbose;
    
    int                 nchannels       = 1;
    double              rate            = 44100.0;
    int                 bytesPerSample  = 2;
    int                 bitsPerSample   = bytesPerSample << 3;
    
    AFfilesetup         setup;
    AFfilehandle        file;
    FILE                *asciifile;
    
    int                 count;
    short               buf[2];
    long                w0, w1;
    
    while ((c = getopt(argc, argv, "svr:")) != EOF) {        
        switch (c) {
            case 's':
                nchannels = 2;
                break;
            case 'r':
                rate = atof(optarg);
                break;
            case 'v':
                verbose = 1;
                break;
            case '?':
                errflg++;
                break;
        }
    }

    if (errflg || optind == argc) {
        (void)fprintf(stderr, "%s %s\n", argv[0], usage);
        exit (2);
    }

    /*
     * open the input file
     */
    ifile = argv[optind++];
    if (optind != argc - 1) {
        (void)fprintf(stderr, "%s %s\n", argv[0], usage);
    }

    asciifile = fopen(ifile, "r");
    if (!asciifile) {
        (void)fprintf(stderr, "error opening file %s\n", ifile);
    }
    
    /*
     * open the output file
     */
    ofile = argv[optind++];
    if (optind != argc) {
        (void)fprintf(stderr, "%s %s\n", argv[0], usage);
    }
    
    setup = AFnewfilesetup();
    AFinitchannels(setup, AF_DEFAULT_TRACK, nchannels);
    AFinitsampfmt(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, bitsPerSample);
    AFinitrate(setup, AF_DEFAULT_TRACK, rate);
    AFinitcompression(setup, AF_DEFAULT_TRACK, AF_COMPRESSION_NONE);
    AFinitfilefmt(setup, AF_FILE_AIFFC);
    file = AFopenfile(ofile, "w", setup);
    if (file == AF_NULL_FILEHANDLE) {
        printf("error opening file %s\n", ofile);
        exit(1);
    }    

    /*
     * ### not very efficient, but quick to code
     */
    while (1) {
        if (nchannels == 1) {
            count = fscanf(asciifile, "%x\n", &w0);
            buf[0] = (short)w0;
        } else {
            count = fscanf(asciifile, "%x %x\n", &w0, &w1);
            buf[0] = (short)w0;
            buf[1] = (short)w1;
        }

        if (count == EOF)
            break;

        count = AFwriteframes(file, AF_DEFAULT_TRACK, buf, 1);
        if (count <= 0) {
            printf("error writing file, file incomplete\n");
            exit(1);
        }
    }

    AFfreefilesetup(setup);
    AFclosefile(file);

    fclose(asciifile);
}