C Programming Column
by Al Stevens


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

short getsample(FILE**ch)
{
    short sample = 0;
    if (*ch)    {
        if (feof(*ch))  {
            fclose(*ch);
            *ch = NULL;
        }
        else
            fread(&sample, sizeof(short), 1, *ch);
    }
    return sample;        
} 
void downsample(FILE* ch1, char* fn)
{
    FILE* fo = fopen(fn, "wb");
    if (fo != NULL) {
        while (ch1)  {
            short sample;
            sample = getsample(&ch1);
            fwrite(&sample, sizeof(short), 1, fo);
            sample = getsample(&ch1);
            sample = getsample(&ch1);
        }
        fclose(fo);
    }
}
int main(int argc, char* argv[])
{
    if (argc > 1)   {
        FILE *fi;
        char* fn = argv[1];
        fi = fopen(fn, "rb");
        if (fi) {
            strcpy(fn + strlen(fn) - 3, "pcm");
            fseek(fi, 44, SEEK_SET);
            downsample(fi, fn);
        }
    }
    return 0;
}

Listing Two
// mix six 14700/1/16 pcms into one 44100/2/16 Hz tune.wav
#include <stdio.h>

short getsample(FILE**ch)
{
    short sample = 0;
    if (*ch)    {
        if (feof(*ch))  {
            fclose(*ch);
            *ch = NULL;
        }
        else
            fread(&sample, sizeof(short), 1, *ch);
    }
    return sample;        
} 
void mix(FILE* ch1, FILE* ch2, FILE* ch3, FILE* ch4, FILE* ch5, FILE* ch6)
{
    FILE* fo = fopen("tune.wav", "wb");
    if (fo != NULL)  {
        struct wavhdr {
            char riff[4];
            int wfchsize;
            char wave[4];
            char fmt[4];
            int fmtchsize;
            short tag;
            short nch;
            int rate;
            int bps;
            short align;
            short sampsize;
            char data[4];
            int datasize;
        } wh = {
            "RIFF",
            sizeof(struct wavhdr)-8,
            "WAVE",
            "fmt ",
            16,
            1,
            2,
            44100,
            176400,
            4,
            16,
            "data",
            0
        };
        fwrite(&wh, sizeof wh, 1, fo);
        while (ch1 || ch2 || ch3 || ch4 || ch5 || ch6)  {
            short sample;
            sample = getsample(&ch1);
            fwrite(&sample, sizeof(short), 1, fo);
            sample = getsample(&ch2);
            fwrite(&sample, sizeof(short), 1, fo);
            sample = getsample(&ch3);
            fwrite(&sample, sizeof(short), 1, fo);
            sample = getsample(&ch4);
            fwrite(&sample, sizeof(short), 1, fo);          
            sample = getsample(&ch5);
            fwrite(&sample, sizeof(short), 1, fo);
            sample = getsample(&ch6);
            fwrite(&sample, sizeof(short), 1, fo);
            wh.wfchsize += sizeof(short) * 6;
            wh.datasize += sizeof(short) * 6;
        }
        fseek(fo, 0, SEEK_SET);
        fwrite(&wh, sizeof wh, 1, fo);
        fclose(fo);
    }
}
int main()
{
    FILE *ftrombone, *fcornet, *fclarinet, *fpiano, *fdrums, *fbass;
    ftrombone = fopen("trombone.pcm", "rb");
    fcornet   = fopen("cornet.pcm",   "rb");
    fclarinet = fopen("clarinet.pcm", "rb");
    fpiano    = fopen("piano.pcm",    "rb");
    fdrums    = fopen("drums.pcm",    "rb");
    fbass     = fopen("bass.pcm",     "rb");
    mix(ftrombone, fcornet, fclarinet, fpiano, fdrums, fbass);
    return 0;
}





1

