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
|
/* allegroconvert.cpp -- convert from allegro to standard midi files */
/* CHANGE LOG
04 apr 03 -- added options to remove tempo track, retaining either
the original beats or original timing (RBD)
*/
#include <fstream>
#include "allegro.h"
using namespace std;
void midi_fail(char *msg)
{
printf("Failure: %s\n", msg);
exit(1);
}
void *midi_alloc(size_t s) { return malloc(s); }
void midi_free(void *a) { free(a); }
void print_help()
{
printf("%s%s%s%s%s%s%s%s",
"Usage: allegroconvert [-m] [-a] [-t tempo] [-f] <filename>\n",
" Use -m for midifile->allegro, -a for allegro->midifile.\n",
" .mid extension implies -m, .gro implies -a\n",
" If tempo (a float) is specified after -t, the tempo track\n",
" is deleted and replaced by a fixed tempo.\n",
" If -f (flatten) is specified, output a flat tempo=60,\n",
" preserving the original timing of all midi events.\n",
" Do not use both -t and -f.\n");
exit(-1);
}
// process -- perform tempo changing and flattening operations
//
void process(Alg_seq_ptr seq, bool tempo_flag, double tempo,
bool flatten_flag)
{
// the tempo changing operation replaces the tempo track with
// a fixed tempo. This changes the timing of the result.
if (tempo_flag) {
seq->convert_to_beats(); // preserve beats
} else if (flatten_flag) {
seq->convert_to_seconds(); // preserve timing
} else return;
// the following finishes both tempo and flatten processing...
seq->get_time_map()->beats.len = 1; // remove contents of tempo map
seq->get_time_map()->last_tempo = tempo / 60.0; // set the new fixed tempo
// (allegro uses beats/second so divide bpm by 60)
seq->get_time_map()->last_tempo_flag = true;
}
int main(int argc, char *argv[])
{
if (argc < 2) {
print_help();
}
char *filename = argv[argc - 1];
char outfilename[256];
bool midifile = false;
bool allegrofile = false;
bool flatten_flag = false;
double tempo = 60.0; // default is used for -f (flatten)
bool tempo_flag = false;
char *ext = NULL;
int i = 1;
while (i < argc - 1) {
if (argv[i][0] != '-') print_help();
if (argv[i][1] == 'm') midifile = true;
else if (argv[i][1] == 'a') allegrofile = true;
else if (argv[i][1] == 't') {
i++;
if (i >= argc - 1) print_help(); // expected tempo
tempo = atof(argv[i]);
tempo_flag = true;
} else if (argv[i][1] == 'f') {
flatten_flag = true;
} else print_help();
i++;
}
// Do not use both -t and -f:
if (tempo_flag & flatten_flag) print_help();
int len = strlen(filename);
if (!midifile && !allegrofile) {
if (len < 4) print_help(); // no extension, need -m or -a
ext = filename + len - 4;
if (strcmp(ext, ".mid") == 0) midifile = true;
else if (strcmp(ext, ".gro") == 0) allegrofile = true;
else print_help();
} else if (len > 4) {
ext = filename + len - 4;
}
Alg_seq_ptr seq;
strcpy(outfilename, filename);
if (midifile) {
seq = new Alg_seq(filename, true);
process(seq, tempo_flag, tempo, flatten_flag);
if (ext && strcmp(ext, ".mid") == 0) {
ext = outfilename + strlen(outfilename) - 4;
} else {
ext = outfilename + strlen(outfilename);
}
strcpy(ext, ".gro");
seq->write(outfilename);
} else if (allegrofile) {
seq = new Alg_seq(filename, false);
process(seq, tempo_flag, tempo, flatten_flag);
if (ext && strcmp(ext, ".gro") == 0) {
ext = outfilename + strlen(outfilename) - 4;
} else {
ext = outfilename + strlen(outfilename);
}
strcpy(ext, ".mid");
seq->smf_write(outfilename);
}
int events = 0;
for (i = 0; i < seq->track_list.length(); i++) {
events += seq->track_list[i].length();
}
printf("%ld tracks, %d events\n", seq->track_list.length(), events);
printf("wrote %s\n", outfilename);
/* DELETE THE DATA */
delete seq;
return 0;
}
|