File: allegroconvert.cpp

package info (click to toggle)
audacity 2.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 129,312 kB
  • sloc: ansic: 373,350; cpp: 276,880; sh: 56,060; python: 18,922; makefile: 10,309; lisp: 8,365; xml: 1,888; perl: 1,798; java: 1,551; asm: 545; pascal: 395; sed: 58; awk: 35
file content (137 lines) | stat: -rwxr-xr-x 4,276 bytes parent folder | download | duplicates (9)
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;
}