-
Notifications
You must be signed in to change notification settings - Fork 1
/
midi-index.cpp
93 lines (82 loc) · 2.65 KB
/
midi-index.cpp
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
#include "mainMelody.hpp"
#include "MidiFile.h"
#include "Options.h"
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
using namespace libmidi;
using namespace smf;
class channelParser:public mainMelody{
protected:
virtual bool readFile(message & m){
while(1){
if(this->eventId >= this->midifile[this->channelId].size())
return false;
if (
midifile[this->channelId][this->eventId].isNoteOn() &&
midifile[this->channelId][this->eventId].size()>=3){
m.begin=midifile[this->channelId][this->eventId].seconds;
m.delay=midifile[this->channelId][this->eventId].getDurationInSeconds();
m.note=midifile[this->channelId][this->eventId][1];
m.velocity=midifile[this->channelId][this->eventId][2];
++(this->eventId);
return true;
}else{
++(this->eventId);
}
}
}
void parseChannel(int id){
if(id!=0)
fprintf(out,"\n");
fprintf(out,"channel:%d\n",id);
this->lastn=-1;
this->reset();
this->eventId=0;
this->channelId=id;
this->noteBlockNum=0;
this->parse();
}
virtual void onNoteOn(int note,int velocity){
if(this->noteBlockNum >= 4){
fprintf(out," ");
this->noteBlockNum=0;
}
if(this->lastn==-1)
fprintf(out,"%d_",0);
else
fprintf(out,"%d_",note - this->lastn);
this->lastn=note;
++(this->noteBlockNum);
}
int lastn;
protected:
MidiFile midifile;
int eventId;
int channelId;
FILE * out;
int noteBlockNum;
public:
bool start(const char * inf,const char * outf){
out=fopen(outf,"w");
if(out==NULL)
return false;
this->midifile.read(inf);
this->midifile.doTimeAnalysis();
this->midifile.linkNotePairs();
int tracks = midifile.getTrackCount();
for (int track=0; track<tracks; track++) {
this->parseChannel(track);
}
fclose(out);
return true;
}
};
int main(int argc, char** argv){
if(argc!=3)
return 1;
channelParser P;
P.start(argv[1],argv[2]);
return 0;
}