split_audio.c.html

    /* split_audio.c - Split right/left channels of an audio file       */

    /* Some audio books use one channel at a time as a means of fitting */
    /* more on one tape.  On a 90 minute tape they can fit 2*90/60=3hrs */
    /* of audio.  The drawback is that you have to listen to the book   */
    /* one speaker at a time, left and then right.  This program splits */
    /* the input audio file (obtained from the audio book) into two     */
    /* output audio files, i.e. left and right channels. The left input */
    /* channel is copied to both channels of the "left" output file and */
    /* the right input channel is copied to both channels of the "right"*/
    /* output file.                                                     */
    /*                                                                  */
    /*   Usage:  split_audio  file_name.[raw|wav]                       */
    /*                                                                  */
    /* The output file type is the same as the input type.              */

    /*  David Edwards <dee.engineering@usa.net>                         */

    // Make with: gcc [-ggdb] -o split_audio split_audio.c
    // Or with any ANSI C compiler...

    /********************** REVISION HISTORY ****************************
    11-20-00:   Original release
    12-04-00:   Changed name from split_raw to split_audio with addition
                of wav file support.
    *********************************************************************/
    #define BUFSIZE 1024    // Read in BUFSIZE bytes of file

    #include <stdio.h>
    #include <string.h>

    int main(int argc,char **argv)
    {
        FILE *fin, *fleft, *fright;
        char fnbase[BUFSIZE], fnleft[BUFSIZE], fnright[BUFSIZE];
        char *sptr;
        int i, byte;
        int Lmsb, Llsb, Rmsb, Rlsb;

    /*********************************
     Initialization Portion of Program
    **********************************/

    // Error handling
        if (argc != 2) {
          fprintf(stderr,"\nSplit an audio file into two left/right files."
    " /DEE 12-04-00 Rev 1.1\n"
    "\n   Usage:  split_audio filename.[raw|wav]\n"
    "\n   Output: filename.left.[raw|wav] and filename.right.[raw|wav]\n\n");
          return 1;
        }

        // Open the input file
        if ((fin = fopen(argv[1], "rb")) == NULL) {
            fprintf(stderr, "\nCannot open input file: %s\n",argv[1]);
            return 1;
        }

        // Get the base name (without extension) of the input file name
        strcpy(fnbase,argv[1]);
        if ((sptr=strrchr(fnbase,'.')) != NULL) {
            sptr[0]=0;
        } else {
            fprintf(stderr,"\nInput file must have a .raw or .wav extension\n");
            fclose(fin);
            return 1;
        }

        // Prepare strings for opening/creating output files
        strcpy(fnleft,fnbase);
        strcpy(fnright,fnbase);

        // Handle wav headers & setup output names
        sptr=strrchr(argv[1],'.');
        if (strncasecmp(sptr, ".RAW",4) == 0) {
            strcat(fnleft,".left.raw");
            strcat(fnright,".right.raw");

            // Open/create the left output file
            if ((fleft = fopen(fnleft, "wb")) == NULL) {
                fprintf(stderr, "\nCannot open output file: %s\n",fnleft);
                fclose(fin);
                return 1;
            }

            // Open/create the right output file
            if ((fright = fopen(fnright, "wb")) == NULL) {
                fprintf(stderr, "\nCannot open output file: %s\n",fnright);
                fclose(fin);
                return 1;
            }

        } else if (strncasecmp(sptr,".WAV",4) == 0) {
            strcat(fnleft,".left.wav");
            strcat(fnright,".right.wav");

            // Open/create the left output file
            if ((fleft = fopen(fnleft, "wb")) == NULL) {
                fprintf(stderr, "\nCannot open output file: %s\n",fnleft);
                fclose(fin);
                return 1;
            }

            // Open/create the right output file
            if ((fright = fopen(fnright, "wb")) == NULL) {
                fprintf(stderr, "\nCannot open output file: %s\n",fnright);
                fclose(fin);
                return 1;
            }

            for (i=0; i<44; i++) {
                byte=fgetc(fin);
                fputc(byte,fleft);
                fputc(byte,fright);
            }
       } else {
            fprintf(stderr,"\nInput file must have a .raw or .wav extension\n");
            fclose(fin);
            return 1;
       }

        fprintf(stdout,"\nSplitting %s into:\n\n"
    "          %s\n"
    "          %s\n\n",argv[1],fnleft,fnright);

    /*********************************
     Write the output files
     See the README file for logic
    **********************************/

        while ((Llsb=fgetc(fin)) != EOF) {
            Lmsb=fgetc(fin);
            fputc(Llsb,fleft);  /* LSB left output  */
            fputc(Lmsb,fleft);  /* MSB left output  */
            fputc(Llsb,fleft);  /* LSB right output */
            fputc(Lmsb,fleft);  /* MSB right output */

            Rlsb=fgetc(fin);
            Rmsb=fgetc(fin);
            fputc(Rlsb,fright); /* LSB left output  */
            fputc(Rmsb,fright); /* MSB left output  */
            fputc(Rlsb,fright); /* LSB right output */
            fputc(Rmsb,fright); /* MSB right output */
        }

        fclose(fin);
        fclose(fleft);
        fclose(fright);
        return 1;
    }