To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Revision:

root / main.cpp @ 1:3e65e0344413

History | View | Annotate | Download (6.36 KB)

1

    
2
#include "MIDIFileReader.h"
3

    
4
#include <iostream>
5

    
6
using namespace std;
7
using namespace MIDIConstants;
8

    
9
int main(int argc, char **argv)
10
{
11
    if (argc != 2) {
12
        cerr << "Usage: midifile <file.mid>" << endl;
13
        return 1;
14
    }
15

    
16
    QString filename = argv[1];
17

    
18
    MIDIFileReader fr(filename);
19
    if (!fr.isOK()) {
20
        std::cerr << "Error: " << fr.getError().toStdString() << std::endl;
21
        return 1;
22
    }
23

    
24
    MIDIComposition c = fr.load();
25

    
26
    switch (fr.getFormat()) {
27
    case MIDI_SINGLE_TRACK_FILE: cout << "Format: MIDI Single Track File" << endl; break;
28
    case MIDI_SIMULTANEOUS_TRACK_FILE: cout << "Format: MIDI Simultaneous Track File" << endl; break;
29
    case MIDI_SEQUENTIAL_TRACK_FILE: cout << "Format: MIDI Sequential Track File" << endl; break;
30
    default: cout << "Format: Unknown MIDI file format?" << endl; break;
31
    }
32

    
33
    cout << "Tracks: " << c.size() << endl;
34

    
35
    int td = fr.getTimingDivision();
36
    if (td < 32768) {
37
        cout << "Timing division: " << fr.getTimingDivision() << " ppq" << endl;
38
    } else {
39
        int frames = 256 - (td >> 8);
40
        int subframes = td & 0xff;
41
        cout << "SMPTE timing: " << frames << " fps, " << subframes << " subframes" << endl;
42
    }
43

    
44
    for (MIDIComposition::const_iterator i = c.begin(); i != c.end(); ++i) {
45
        
46
        cout << "Start of track: " << i.key()+1 << endl;
47

    
48
        for (MIDITrack::const_iterator j = i->begin(); j != i->end(); ++j) {
49

    
50
            unsigned int t = j->getTime();
51
            int ch = j->getChannelNumber();
52

    
53
            if (j->isMeta()) {
54
                int code = j->getMetaEventCode();
55
                string name;
56
                bool printable = true;
57
                switch (code) {
58

    
59
                case MIDI_END_OF_TRACK:
60
                    cout << t << ": End of track" << endl;
61
                    break;
62

    
63
                case MIDI_TEXT_EVENT: name = "Text"; break;
64
                case MIDI_COPYRIGHT_NOTICE: name = "Copyright"; break;
65
                case MIDI_TRACK_NAME: name = "Track name"; break;
66
                case MIDI_INSTRUMENT_NAME: name = "Instrument name"; break;
67
                case MIDI_LYRIC: name = "Lyric"; break;
68
                case MIDI_TEXT_MARKER: name = "Text marker"; break;
69
                case MIDI_SEQUENCE_NUMBER: name = "Sequence number"; printable = false; break;
70
                case MIDI_CHANNEL_PREFIX_OR_PORT: name = "Channel prefix or port"; printable = false; break;
71
                case MIDI_CUE_POINT: name = "Cue point"; break;
72
                case MIDI_CHANNEL_PREFIX: name = "Channel prefix"; printable = false; break;
73
                case MIDI_SEQUENCER_SPECIFIC: name = "Sequencer specific"; printable = false; break;
74
                case MIDI_SMPTE_OFFSET: name = "SMPTE offset"; printable = false; break;
75

    
76
                case MIDI_SET_TEMPO:
77
                {
78
                    int m0 = j->getMetaMessage()[0];
79
                    int m1 = j->getMetaMessage()[1];
80
                    int m2 = j->getMetaMessage()[2];
81
                    long tempo = (((m0 << 8) + m1) << 8) + m2;
82

    
83
                    cout << t << ": Tempo: " << 60000000.0 / double(tempo) << endl;
84
                }
85
                    break;
86

    
87
                case MIDI_TIME_SIGNATURE:
88
                {
89
                    int numerator = j->getMetaMessage()[0];
90
                    int denominator = 1 << (int)j->getMetaMessage()[1];
91
                    
92
                    cout << t << ": Time signature: " << numerator << "/" << denominator << endl;
93
                }
94

    
95
                case MIDI_KEY_SIGNATURE:
96
                {
97
                    int accidentals = j->getMetaMessage()[0];
98
                    int isMinor = j->getMetaMessage()[1];
99
                    bool isSharp = accidentals < 0 ? false : true;
100
                    accidentals = accidentals < 0 ? -accidentals : accidentals;
101
                    cout << t << ": Key signature: " << accidentals << " "
102
                         << (isSharp ?
103
                             (accidentals > 1 ? "sharps" : "sharp") :
104
                             (accidentals > 1 ? "flats" : "flat"))
105
                         << (isMinor ? ", minor" : ", major") << endl;
106
                }
107

    
108
                }
109
                
110

    
111
                if (name != "") {
112
                    if (printable) {
113
                        cout << t << ": File meta event: code " << code
114
                             << ": " << name << ": \"" << j->getMetaMessage()
115
                             << "\"" << endl;
116
                    } else {
117
                        cout << t << ": File meta event: code " << code
118
                             << ": " << name << ": ";
119
                        for (int k = 0; k < j->getMetaMessage().length(); ++k) {
120
                            cout << (int)j->getMetaMessage()[k] << " ";
121
                        }
122
                    }
123
                }
124
                continue;
125
            }
126

    
127
            switch (j->getMessageType()) {
128
                
129
            case MIDI_NOTE_ON:
130
                cout << t << ": Note: channel " << ch
131
                     << " duration " << j->getDuration()
132
                     << " pitch " << j->getPitch()
133
                     << " velocity " << j->getVelocity() << endl;
134
                break;
135

    
136
            case MIDI_POLY_AFTERTOUCH:
137
                cout << t << ": Polyphonic aftertouch: channel " << ch
138
                     << " pitch " << j->getPitch()
139
                     << " pressure " << j->getData2() << endl;
140
                break;
141
                
142
            case MIDI_CTRL_CHANGE:
143
            {
144
                int controller = j->getData1();
145
                string name;
146
                switch (controller) {
147
                case MIDI_CONTROLLER_BANK_MSB: name = "Bank select MSB"; break;
148
                case MIDI_CONTROLLER_VOLUME: name = "Volume"; break;
149
                case MIDI_CONTROLLER_BANK_LSB: name = "Bank select LSB"; break;
150
                case MIDI_CONTROLLER_MODULATION: name = "Modulation wheel"; break;
151
                case MIDI_CONTROLLER_PAN: name = "Pan"; break;
152
                case MIDI_CONTROLLER_SUSTAIN: name = "Sustain"; break;
153
                case MIDI_CONTROLLER_RESONANCE: name = "Resonance"; break;
154
                case MIDI_CONTROLLER_RELEASE: name = "Release"; break;
155
                case MIDI_CONTROLLER_ATTACK: name = "Attack"; break;
156
                case MIDI_CONTROLLER_FILTER: name = "Filter"; break;
157
                case MIDI_CONTROLLER_REVERB: name = "Reverb"; break;
158
                case MIDI_CONTROLLER_CHORUS: name = "Chorus"; break;
159
                case MIDI_CONTROLLER_NRPN_1: name = "NRPN 1"; break;
160
                case MIDI_CONTROLLER_NRPN_2: name = "NRPN 2"; break;
161
                case MIDI_CONTROLLER_RPN_1: name = "RPN 1"; break;
162
                case MIDI_CONTROLLER_RPN_2: name = "RPN 2"; break;
163
                case MIDI_CONTROLLER_SOUNDS_OFF: name = "All sounds off"; break;
164
                case MIDI_CONTROLLER_RESET: name = "Reset"; break;
165
                case MIDI_CONTROLLER_LOCAL: name = "Local"; break;
166
                case MIDI_CONTROLLER_ALL_NOTES_OFF: name = "All notes off"; break;
167
                }
168
                cout << t << ": Controller change: channel " << ch
169
                     << " controller " << j->getData1();
170
                if (name != "") cout << " (" << name << ")";
171
                cout << " value " << j->getData2() << endl;
172
            }
173
                break;
174

    
175
            case MIDI_PROG_CHANGE:
176
                cout << t << ": Program change: channel " << ch
177
                     << " program " << j->getData1() << endl;
178
                break;
179

    
180
            case MIDI_CHNL_AFTERTOUCH:
181
                cout << t << ": Channel aftertouch: channel " << ch
182
                     << " pressure " << j->getData1() << endl;
183
                break;
184

    
185
            case MIDI_PITCH_BEND:
186
                cout << t << ": Pitch bend: channel " << ch
187
                     << " value " << (int)j->getData2() * 128 + (int)j->getData1() << endl;
188
                break;
189

    
190
            case MIDI_SYSTEM_EXCLUSIVE:
191
                cout << t << ": System exclusive: code "
192
                     << (int)j->getMessageType() << " message length " <<
193
                    j->getMetaMessage().length() << endl;
194
                break;
195
                
196

    
197
            }
198

    
199
            
200
        }
201

    
202

    
203
    }
204

    
205
}
206

    
207