aboutsummaryrefslogtreecommitdiff
path: root/core/qmpmidiread.cpp
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2017-02-08 00:02:14 +0800
committerGravatar Chris Xiong <chirs241097@gmail.com> 2017-02-08 00:02:14 +0800
commit1afedc4cc39c1dcbe49f8c99843a1732bf22fa28 (patch)
tree5ab62319767ef25c9c6cc7eaa248a813b1d665c0 /core/qmpmidiread.cpp
parenta00e60541816a99a348add1a84da662bc1a4122d (diff)
downloadQMidiPlayer-1afedc4cc39c1dcbe49f8c99843a1732bf22fa28.tar.xz
First steps for the file reader API.
API additions and changes. Fixed wrong button shown when started from file.
Diffstat (limited to 'core/qmpmidiread.cpp')
-rw-r--r--core/qmpmidiread.cpp199
1 files changed, 123 insertions, 76 deletions
diff --git a/core/qmpmidiread.cpp b/core/qmpmidiread.cpp
index dc58ecc..4ac2ac8 100644
--- a/core/qmpmidiread.cpp
+++ b/core/qmpmidiread.cpp
@@ -12,29 +12,29 @@ const char* GM1SysX={"\xF0\x7E\x7F\x09\x01\xF7"};
const char* GM2SysX={"\xF0\x7E\x7F\x09\x03\xF7"};
const char* GSSysEx={"\xF0\x41\x10\x42\x12\x40\x00\x7F\x00\x41\xF7"};
const char* XGSysEx={"\xF0\x43\x10\x4C\x00\x00\x7E\x00\xF7"};
-bool cmp(SEvent *a,SEvent *b){return a->time-b->time?a->time<b->time:a->iid<b->iid;}
-void CMidiFile::error(int fatal,const char* format,...)
+bool cmp(SEvent a,SEvent b){return a.time-b.time?a.time<b.time:a.iid<b.iid;}
+void CSMFReader::error(int fatal,const char* format,...)
{
va_list ap;
va_start(ap,format);vfprintf(stderr,format,ap);va_end(ap);
fprintf(stderr," at %#lx\n",ftell(f));
if(fatal)throw std::runtime_error("fatal error");
}
-uint32_t CMidiFile::readSW()
+uint32_t CSMFReader::readSW()
{
byteread+=2;
uint32_t ret=0;
for(int i=0;i<2;++i){ret<<=8;ret|=((uint32_t)fgetc(f))&0xFF;}
return ret;
}
-uint32_t CMidiFile::readDW()
+uint32_t CSMFReader::readDW()
{
byteread+=4;
uint32_t ret=0;
for(int i=0;i<4;++i){ret<<=8;ret|=((uint32_t)fgetc(f))&0xFF;}
return ret;
}
-uint32_t CMidiFile::readVL()
+uint32_t CSMFReader::readVL()
{
uint32_t ret=0,t,c=0;
do
@@ -46,7 +46,7 @@ uint32_t CMidiFile::readVL()
byteread+=c;
return ret;
}
-int CMidiFile::eventReader()//returns 0 if End of Track encountered
+int CSMFReader::eventReader()//returns 0 if End of Track encountered
{
uint32_t delta=readVL();curt+=delta;
char type=fgetc(f);++byteread;uint32_t p1,p2;
@@ -56,37 +56,34 @@ int CMidiFile::eventReader()//returns 0 if End of Track encountered
{
case 0x80://Note Off
p1=fgetc(f);p2=fgetc(f);byteread+=2;
- eventList.push_back(new SEvent(curid,curt,type,p1,p2));
+ ret->eventList.push_back(SEvent(curid,curt,type,p1,p2));
break;
case 0x90://Note On
p1=fgetc(f);p2=fgetc(f);byteread+=2;
if(p2)
- {
- ++notes;
- eventList.push_back(new SEvent(curid,curt,type,p1,p2));
- }
+ ret->eventList.push_back(SEvent(curid,curt,type,p1,p2));
else
- eventList.push_back(new SEvent(curid,curt,(type&0x0F)|0x80,p1,p2));
+ ret->eventList.push_back(SEvent(curid,curt,(type&0x0F)|0x80,p1,p2));
break;
case 0xA0://Note Aftertouch
p1=fgetc(f);p2=fgetc(f);byteread+=2;
- eventList.push_back(new SEvent(curid,curt,type,p1,p2));
+ ret->eventList.push_back(SEvent(curid,curt,type,p1,p2));
break;
case 0xB0://Controller Change
p1=fgetc(f);p2=fgetc(f);byteread+=2;
- eventList.push_back(new SEvent(curid,curt,type,p1,p2));
+ ret->eventList.push_back(SEvent(curid,curt,type,p1,p2));
break;
case 0xC0://Patch Change
p1=fgetc(f);++byteread;
- eventList.push_back(new SEvent(curid,curt,type,p1,0));
+ ret->eventList.push_back(SEvent(curid,curt,type,p1,0));
break;
case 0xD0://Channel Aftertouch
p1=fgetc(f);++byteread;
- eventList.push_back(new SEvent(curid,curt,type,p1,0));
+ ret->eventList.push_back(SEvent(curid,curt,type,p1,0));
break;
case 0xE0://Pitch wheel
p1=fgetc(f);p2=fgetc(f);byteread+=2;
- eventList.push_back(new SEvent(curid,curt,type,(p1|(p2<<7))&0x3FFF,0));
+ ret->eventList.push_back(SEvent(curid,curt,type,(p1|(p2<<7))&0x3FFF,0));
break;
case 0xF0:
if((type&0x0F)==0x0F)//Meta Event
@@ -107,7 +104,7 @@ int CMidiFile::eventReader()//returns 0 if End of Track encountered
break;
case 0x51://Set Tempo
p1=readDW();p1&=0x00FFFFFF;
- eventList.push_back(new SEvent(curid,curt,type,metatype,p1));
+ ret->eventList.push_back(SEvent(curid,curt,type,metatype,p1));
break;
case 0x54://SMTPE offset, not handled.
fgetc(f);fgetc(f);fgetc(f);
@@ -117,12 +114,12 @@ int CMidiFile::eventReader()//returns 0 if End of Track encountered
case 0x58://Time signature
fgetc(f);++byteread;
p1=readDW();
- eventList.push_back(new SEvent(curid,curt,type,metatype,p1));
+ ret->eventList.push_back(SEvent(curid,curt,type,metatype,p1));
break;
case 0x59://Key signature
fgetc(f);++byteread;
p1=readSW();
- eventList.push_back(new SEvent(curid,curt,type,metatype,p1));
+ ret->eventList.push_back(SEvent(curid,curt,type,metatype,p1));
break;
case 0x01:case 0x02:case 0x03:
case 0x04:case 0x05:case 0x06:
@@ -134,16 +131,16 @@ int CMidiFile::eventReader()//returns 0 if End of Track encountered
{
++byteread;if(str)str[c]=fgetc(f);else fgetc(f);
}
- if(str)str[c]='\0';eventList.push_back(new SEvent(curid,curt,type,metatype,0,str));
- if(str&&metatype==0x03&&!title)
+ if(str)str[c]='\0';ret->eventList.push_back(SEvent(curid,curt,type,metatype,0,str));
+ if(str&&metatype==0x03&&!ret->title)
{
- title=new char[len+8];
- strcpy(title,str);
+ ret->title=new char[len+8];
+ strcpy(ret->title,str);
}
- if(str&&metatype==0x02&&!copyright)
+ if(str&&metatype==0x02&&!ret->copyright)
{
- copyright=new char[len+8];
- strcpy(copyright,str);
+ ret->copyright=new char[len+8];
+ strcpy(ret->copyright,str);
}
if(len<=1024&&len>0)delete[] str;
}
@@ -159,11 +156,11 @@ int CMidiFile::eventReader()//returns 0 if End of Track encountered
for(c=1;c<len;++c){++byteread;str[c]=fgetc(f);}
}
else for(c=0;c<len;++c){++byteread;str[c]=fgetc(f);}
- eventList.push_back(new SEvent(curid,curt,type,len,0,str));
- if(!strcmp(str,GM1SysX))std=1;
- if(!strcmp(str,GM2SysX))std=2;
- if(!strcmp(str,GSSysEx))std=3;
- if(!strcmp(str,XGSysEx))std=4;
+ ret->eventList.push_back(SEvent(curid,curt,type,len,0,str));
+ if(!strcmp(str,GM1SysX))ret->std=1;
+ if(!strcmp(str,GM2SysX))ret->std=2;
+ if(!strcmp(str,GSSysEx))ret->std=3;
+ if(!strcmp(str,XGSysEx))ret->std=4;
delete[] str;
}
else error(0,"W: Unknown event type %#x",type);
@@ -172,16 +169,15 @@ int CMidiFile::eventReader()//returns 0 if End of Track encountered
error(0,"W: Unknown event type %#x",type);
}
lasttype=type;++curid;
- if(eventList.size())
+ if(ret->eventList.size())
{
- SEvent* le=eventList[eventList.size()-1];
- SEventCallBackData cbd(le->type,le->p1,le->p2,le->time);
- for(int i=0;i<16;++i)if(eventReaderCB[i])
- eventReaderCB[i]->callBack(&cbd,eventReaderCBuserdata[i]);
+ SEvent& le=ret->eventList[ret->eventList.size()-1];
+ SEventCallBackData cbd(le.type,le.p1,le.p2,le.time);
+ CMidiPlayer::getInstance()->callEventReaderCB(cbd);
}
return 1;
}
-void CMidiFile::trackChunkReader()
+void CSMFReader::trackChunkReader()
{
int chnklen=readDW();byteread=0;curt=0;curid=0;
while(/*byteread<chnklen&&*/eventReader());
@@ -195,16 +191,16 @@ void CMidiFile::trackChunkReader()
error(1,"E: Read past end of track.");
}*/
}
-void CMidiFile::headerChunkReader()
+void CSMFReader::headerChunkReader()
{
int chnklen=readDW();byteread=0;
if(chnklen<6)error(1,"E: Header chunk too short.");
if(chnklen>6)error(0,"W: Header chunk length longer than expected. Ignoring extra bytes.");
- fmt=readSW();trk=readSW();divs=readSW();
- if(divs&0x8000)error(1,"E: SMTPE format is not supported.");
+ fmt=readSW();trk=readSW();ret->divs=readSW();
+ if(ret->divs&0x8000)error(1,"E: SMTPE format is not supported.");
for(;byteread<chnklen;++byteread){fgetc(f);}
}
-int CMidiFile::chunkReader(int hdrXp)
+int CSMFReader::chunkReader(int hdrXp)
{
char hdr[6];
if(!fgets(hdr,5,f))error(1,"E: Unexpected EOF.");
@@ -219,55 +215,106 @@ int CMidiFile::chunkReader(int hdrXp)
}
else return trackChunkReader(),1;
}
-void CMidiFile::dumpEvents()
+CSMFReader::CSMFReader()
{
- for(uint32_t i=0;i<eventList.size();++i)
- if(eventList[i]->str)
- printf("type %x #%d @%d p1 %d p2 %d str %s\n",eventList[i]->type,
- eventList[i]->iid,eventList[i]->time,eventList[i]->p1,eventList[i]->p2,eventList[i]->str);
- else
- printf("type %x #%d @%d p1 %d p2 %d\n",eventList[i]->type,
- eventList[i]->iid,eventList[i]->time,eventList[i]->p1,eventList[i]->p2);
+ f=NULL;
}
-CMidiFile::CMidiFile(const char* fn,CMidiPlayer* par)
+CMidiFile* CSMFReader::readFile(const char* fn)
{
- title=copyright=NULL;notes=0;std=0;valid=1;
- memcpy(eventReaderCB,par->eventReaderCB,sizeof(eventReaderCB));
- memcpy(eventReaderCBuserdata,par->eventReaderCBuserdata,sizeof(eventReaderCBuserdata));
+ ret=new CMidiFile;
+ ret->title=ret->copyright=NULL;ret->std=0;ret->valid=1;
try
{
if(!(f=fopen(fn,"rb")))throw (fprintf(stderr,"E: file %s doesn't exist!\n",fn),std::runtime_error("File doesn't exist"));
chunkReader(1);
for(uint32_t i=0;i<trk;i+=chunkReader(0));
- fclose(f);
- std::sort(eventList.begin(),eventList.end(),cmp);
- par->maxtk=eventList[eventList.size()-1]->time;
+ fclose(f);f=NULL;
+ std::sort(ret->eventList.begin(),ret->eventList.end(),cmp);
}
- catch(std::runtime_error&){fprintf(stderr,"E: %s is not a supported file.\n",fn);valid=0;}
+ catch(std::runtime_error&){fprintf(stderr,"E: %s is not a supported file.\n",fn);ret->valid=0;fclose(f);f=NULL;}
+ return ret;
}
-CMidiFile::~CMidiFile()
+CSMFReader::~CSMFReader()
{
- for(uint32_t i=0;i<eventList.size();++i)delete eventList[i];eventList.clear();
- if(title)delete[] title;if(copyright)delete[] copyright;
}
-const SEvent* CMidiFile::getEvent(uint32_t id){return id<eventList.size()?eventList[id]:NULL;}
-uint32_t CMidiFile::getEventCount(){return eventList.size();}
-uint32_t CMidiFile::getDivision(){return divs;}
-uint32_t CMidiFile::getNoteCount(){return notes;}
-uint32_t CMidiFile::getStandard(){return std;}
-bool CMidiFile::isValid(){return valid;}
-const char* CMidiFile::getTitle(){return title;}
-const char* CMidiFile::getCopyright(){return copyright;}
-void CMidiFile::discardLastEvent()
+void CSMFReader::discardCurrentEvent()
{
if(eventdiscarded)return;eventdiscarded=1;
- delete eventList[eventList.size()-1];eventList.pop_back();
+ ret->eventList.pop_back();
+}
+void CSMFReader::commitEventChange(SEventCallBackData d)
+{
+ ret->eventList[ret->eventList.size()-1].time=d.time;
+ ret->eventList[ret->eventList.size()-1].type=d.type;
+ ret->eventList[ret->eventList.size()-1].p1=d.p1;
+ ret->eventList[ret->eventList.size()-1].p2=d.p2;
+}
+
+void CMidiFileReaderCollection::destructFile(CMidiFile*& f)
+{
+ if(!f)return;
+ if(f->copyright)delete[] f->copyright;
+ if(f->title)delete[] f->title;
+ delete f;
+ f=NULL;
+}
+CMidiFileReaderCollection::CMidiFileReaderCollection()
+{
+ file=NULL;readers.clear();currentReader=NULL;
+ registerReader(new CSMFReader(),"Default SMF Reader");
}
-void CMidiFile::commitEventChange(SEventCallBackData d)
+CMidiFileReaderCollection::~CMidiFileReaderCollection()
{
- eventList[eventList.size()-1]->time=d.time;
- eventList[eventList.size()-1]->type=d.type;
- eventList[eventList.size()-1]->p1=d.p1;
- eventList[eventList.size()-1]->p2=d.p2;
+ if(file)destructFile(file);
+ delete readers[0].first;
+}
+void CMidiFileReaderCollection::registerReader(IMidiFileReader* reader,std::string name)
+{
+ for(unsigned i=0;i<readers.size();++i)
+ if(readers[i].second==name)return;
+ readers.push_back(std::make_pair(reader,name));
+}
+void CMidiFileReaderCollection::unregisterReader(std::string name)
+{
+ for(auto i=readers.begin();i!=readers.end();++i)
+ if(i->second==name)
+ {
+ readers.erase(i);
+ return;
+ }
+}
+void CMidiFileReaderCollection::readFile(const char* fn)
+{
+ if(file)destructFile(file);
+ for(unsigned i=0;i<readers.size();++i)
+ {
+ currentReader=readers[i].first;
+ CMidiFile* t=readers[i].first->readFile(fn);
+ if(t->valid){file=t;break;}
+ else destructFile(t);
+ }
+ currentReader=NULL;
+ if(file)
+ maxtk=file->eventList[file->eventList.size()-1].time;
}
+void CMidiFileReaderCollection::destructFile()
+{destructFile(file);}
+IMidiFileReader* CMidiFileReaderCollection::getCurrentReader()
+{return currentReader;}
+bool CMidiFileReaderCollection::isValid()
+{return file&&file->valid;}
+const char* CMidiFileReaderCollection::getTitle()
+{return file?file->title:NULL;}
+const char* CMidiFileReaderCollection::getCopyright()
+{return file?file->copyright:NULL;}
+const SEvent* CMidiFileReaderCollection::getEvent(uint32_t id)
+{return file?&(file->eventList[id]):NULL;}
+uint32_t CMidiFileReaderCollection::getEventCount()
+{return file?file->eventList.size():0;}
+uint32_t CMidiFileReaderCollection::getDivision()
+{return file?file->divs:0;}
+uint32_t CMidiFileReaderCollection::getMaxTick()
+{return maxtk;}
+uint32_t CMidiFileReaderCollection::getStandard()
+{return file?file->std:0;}