-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmidisplit.cpp
executable file
·280 lines (247 loc) · 8.71 KB
/
midisplit.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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "synth.h"
#include <map>
#include <iostream>
namespace mgnr{
void voiceTrack::pushPause(int len){
length+=len;
words.push_back(voiceWord(len,-1,0,strPool->create("")));
}
void voiceTrack::pushNote(int len,const stringPool::stringPtr & info,int tone,int v){
length+=len;
words.push_back(voiceWord(len , tone , v , info));
}
void synth::clearTracks(){
if(tracks.empty())
return;
for(auto it:tracks){
delete it;
}
tracks.clear();
}
void synth::splitTracks(){
clearTracks();
std::multimap<int,note*> sortedNotes;//对音符进行排序
for(auto it:notes){
if(it->info.empty()){//没命名
continue;
}
if(it->info.at(0)=='@'){//控制符
continue;
}
int begin = it->begin;
sortedNotes.insert(std::pair<int,note*>(begin,it));
}
for(auto it:sortedNotes){
int begin = it.second->begin;
int dur = it.second->delay;
if(begin<0 || dur<=0){//begin dur小于0是错误的音符
continue;
}
if(tracks.empty()){//没有音轨
//创建音轨
auto track = new voiceTrack(&strPool);
tracks.push_back(track);
if(begin==0){//begin=0,直接放到起始位置
}else{//先插入休止符
track->pushPause(begin);
}
track->pushNote(dur,it.second->info,it.second->tone,it.second->volume);//插入音符本身
}else{
bool successFlag = false;
//有音轨,扫描音轨,找一个空穴,插入音符
for(auto track:tracks){
if(begin > track->length){//起始时间大于音轨长度,说明需要插入先插入休止符
track->pushPause(begin - track->length);//插入休止符
track->pushNote(dur,it.second->info,it.second->tone,it.second->volume);//插入音符本身
successFlag=true;
break;
}else
if(begin == track->length){//等于,说明可以直接在后面追加
track->pushNote(dur,it.second->info,it.second->tone,it.second->volume);//插入音符本身
successFlag=true;
break;
}//否则,此音轨不可用
}
if(!successFlag){//如果没有插入成功,则新建音轨
//创建音轨
auto track = new voiceTrack(&strPool);
tracks.push_back(track);
if(begin==0){//begin=0,直接放到起始位置
}else{//先插入休止符
track->pushPause(begin);
}
track->pushNote(dur,it.second->info,it.second->tone,it.second->volume);//插入音符本身
}
}
}
int tnum=tracks.size();
//::__android_log_print(ANDROID_LOG_INFO, "mgenner","track num:%d\n",tnum);
}
static int hash(const char * str){
unsigned int seed = 131; // 31 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*str){
hash = hash * seed + (*str++);
}
return (hash & 0x7FFFFFFF);
}
void synth::toHashSeries(std::string & out){
std::vector<std::pair<int,int> > ser;
toHashSeries(ser);
out.clear();
char buf[64];
for(auto it:ser){
snprintf(buf,64,"%d %d\n",it.first,it.second);
out+=buf;
}
}
void synth::toHashSeries(std::vector<std::pair<int,int> > & out){
std::multimap<int,note*> sortedNotes;//对音符进行排序
for(auto it:notes){
if(it->info.empty()){//没命名
continue;
}
if(it->info[0]=='@'){//控制符
continue;
}
int begin = it->begin;
sortedNotes.insert(std::pair<int,note*>(begin,it));
}
std::string ser;
ser.clear();
int ntime=0;
char buf[64];
for(auto it:sortedNotes){
snprintf(buf,64,"%d-%d-%d-%d ",
(int)it.second->begin,
(int)it.second->tone,
(int)it.second->volume,
(int)it.second->delay
);
//std::cout<<ser<<std::endl;
if(it.first==ntime){
ser+=buf;
}else{
if(!ser.empty())
out.push_back(std::pair<int,int>(ntime,hash(ser.c_str())));
ntime=it.first;
ser=buf;
}
}
if(!ser.empty()){
out.push_back(std::pair<int,int>(ntime,hash(ser.c_str())));
}
}
void synth::diff(const std::string & in,std::function<void (int)> const & callback_d,std::function<void (int,int,int,int)> const & callback_s){
std::istringstream iss(in);
char buf[1024];
std::vector<std::pair<int,int> > data;
while(!iss.eof()){
bzero(buf,1024);
iss.getline(buf,1024);
if(strlen(buf)<2)
continue;
std::istringstream ibuf(buf);
int t,h;
ibuf>>t;
ibuf>>h;
data.push_back(std::pair<int,int>(t,h));
}
diff(data,callback_d,callback_s);
}
void synth::diff(const std::vector<std::pair<int,int> > & in,std::function<void (int)> const & callback_d,std::function<void (int,int,int,int)> const & callback_s){
std::vector<std::pair<int,int> > me;
toHashSeries(me);
std::vector<int> A,B,Am,Bm;
A.resize(me.size());
Am.resize(me.size());
B.resize(in.size());
Bm.resize(in.size());
for(int i=0;i<me.size();i++){
A.at(i) = me.at(i).second;
Am.at(i) = 0;
}
for(int i=0;i<in.size();i++){
B.at(i) = in.at(i).second;
Bm.at(i) = 0;
}
int id=0;
LCS(A,B,[&](int im,int iin,int len){
++id;
try{
if(len>2){
callback_s(me.at(im-len+1).first , me.at(im).first , in.at(iin-len+1).first , in.at(iin).first);
}
for(int i=0;i<len;i++){
Am.at(im -i)=id;
Bm.at(iin-i)=id;
}
}catch(...){
}
});
int last=0;
bool inArea=false;
//A,B中标0的部分就是不同的
for(int i=0;i<me.size();i++){
auto tick = me.at(i).first;
//printf("id:%d\n",Am.at(i));
if(Am.at(i) == 0){
last = 0;
if(!inArea){//差异区域开始
callback_d(tick);
}
inArea=true;
}else{
if(inArea){//刚结束差异区
}else{
if(Am.at(i) != last && i!=0){
//发现删除过
callback_d(tick);
}
}
last = Am.at(i);
inArea=false;
}
}
}
//临时类
class diff_loader: public synth{
void rebuildNoteLen()override{}
void drawNote_begin()override{}
void drawNote(int fx,int fy,int tx,int ty, int volume,const stringPool::stringPtr & info,bool selected,bool onlydisplay=false)override{}
void drawNote_end()override{}
void drawTableRaw(int from,int to,int left,int right,int t)override{}
void drawTimeCol(float p)override{}
void drawSectionCol(float p,int n)override{}
void drawTempo(float p,double t)override{}
void drawTempoPadd()override{}
void drawScroll()override{}
void scrollBuilder_onGetNoteArea()override{}
void scrollBuilder_onGetAllNotePos(note *)override{}
void scrollBuilder_onSwap()override{}
void onNoteOn(note * n,int c)override{}
void onNoteOff(note * n,int c)override{}
void onSetChannelIns(int c,int ins)override{}
void callJavaNoteOn(const char * info,int channel,int tone,int vol)override{}
void callJavaNoteOff(const char * info,int channel,int tone)override{}
void onLoadName(const stringPool::stringPtr & name)override{}
void onSelectedChange(int len)override{}
void drawDescriptions(float p,const stringPool::stringPtr & title,const std::string & content)override{}
void drawDescriptionsPadd()override{}
void callAddDescription(int tick)override{}
void callEditDescription(int tick, const stringPool::stringPtr &title, const std::string &content)override{}
void drawCaption(float p,const std::string & str)override{}
};
void synth::diff_path(
const std::string & path,
std::function<void (int)> const & callback_d,
std::function<void (int,int,int,int)> const & callback_s
){
auto targ = new diff_loader();
targ->loadMidi(path);
std::vector<std::pair<int,int> > hash;
targ->toHashSeries(hash);
this->diff(hash,callback_d,callback_s);
delete targ;
}
}