midiparse.c++ 1.75 KB
#include <stdio.h>
#include <stdlib.h>
#include <bstring.h>
#include <assert.h>
#include <getopt.h>
#include <math.h>

#include "event.h"
#include "midifile.h"
#include "seqfile.h"
#include "time.h"

static char usage[] = "-verbose <filename>";

main(int argc, char **argv)
{
    int c;
    extern char *optarg;
    extern int  optind;
    int         errflg=0;
    char        *ifile;
    char        *ofile=0;
    int         verbose=0;
    
    while ((c = getopt(argc, argv, "vo:")) != EOF) {        
        switch (c) {
	    case 'v':
                verbose = 1;
		break;
            case 'o':
                ofile = optarg;
                break;
            case '?':
                errflg++;
                break;
        }
    }

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

    ifile = argv[optind++];
    if (optind != argc) {
	fprintf(stderr, "warning: only first file (%s) used, rest ignored\n",
	   ifile);
    }

    if (ofile == 0)
        ofile = "tmp.seq";
    
    MIDIFile *midifile = new MIDIFile;
    if (!midifile) exit(1);
    midifile->Open(ifile, "r");
    
    SeqFile *seqfile = new SeqFile(midifile->GetDivision());
    if (!seqfile) exit(1);
    seqfile->Open(ofile, "w");
    
    // merge all event data into one track. Note: this is not very
    // efficient at this point ( in fact, its probably pretty bad),
    // but it does the job.
    int tracks = midifile->GetTrackCount();
    for (int i = 0; i < tracks; i++) {
        Event *event;
        while ((event = midifile->GetNextEvent(i)) != 0){
            seqfile->AddEvent(event);
        }
    }

    if (verbose)
        seqfile->Print();

    seqfile->Write();

    midifile->Close();
    seqfile->Close();
}