aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorGravatar Chris Xiong <chirs241097@gmail.com> 2019-05-25 22:40:15 +0800
committerGravatar Chris Xiong <chirs241097@gmail.com> 2019-05-25 22:40:15 +0800
commit3f72121ac41741d53e4916f1275cbd4f93259c4d (patch)
tree15bba079c86de0e241812cd657ad190eb0c2c41a /core
parenteba45fdf11a98113b439db0510b55f14845db8fb (diff)
downloadQMidiPlayer-3f72121ac41741d53e4916f1275cbd4f93259c4d.tar.xz
Massive code refactor en cours!
Less complain from compilers: - use nullptr instead of NULL - use types for event fields - explicit type casting added Stop saving parameters of meta events in p1/p2 fields of SEvent. callback_t now uses std::function, deprecating ICallback. Not recommended for daily usage, or even testing: the code refactor is still a work in progress.
Diffstat (limited to 'core')
-rw-r--r--core/qmpmidioutfluid.cpp22
-rw-r--r--core/qmpmidioutrtmidi.cpp10
-rw-r--r--core/qmpmidiplay.cpp101
-rw-r--r--core/qmpmidiplay.hpp40
-rw-r--r--core/qmpmidiread.cpp133
5 files changed, 197 insertions, 109 deletions
diff --git a/core/qmpmidioutfluid.cpp b/core/qmpmidioutfluid.cpp
index ab0ab9d..bb8f290 100644
--- a/core/qmpmidioutfluid.cpp
+++ b/core/qmpmidioutfluid.cpp
@@ -5,27 +5,27 @@
qmpMidiOutFluid::qmpMidiOutFluid()
{
settings=new_fluid_settings();
- synth=NULL;adriver=NULL;
+ synth=nullptr;adriver=nullptr;
}
qmpMidiOutFluid::~qmpMidiOutFluid()
{
delete_fluid_settings(settings);
- settings=NULL;
+ settings=nullptr;
}
void qmpMidiOutFluid::deviceInit()
{
synth=new_fluid_synth(settings);
if(!synth){fputs("Error creating fluidsynth instance!",stderr);return;}
- fluid_set_log_function(FLUID_DBG,NULL,NULL);
- fluid_set_log_function(FLUID_INFO,NULL,NULL);
- fluid_set_log_function(FLUID_WARN,NULL,NULL);
- fluid_set_log_function(FLUID_ERR,fluid_default_log_function,NULL);
- fluid_set_log_function(FLUID_PANIC,fluid_default_log_function,NULL);
+ fluid_set_log_function(FLUID_DBG,nullptr,nullptr);
+ fluid_set_log_function(FLUID_INFO,nullptr,nullptr);
+ fluid_set_log_function(FLUID_WARN,nullptr,nullptr);
+ fluid_set_log_function(FLUID_ERR,fluid_default_log_function,nullptr);
+ fluid_set_log_function(FLUID_PANIC,fluid_default_log_function,nullptr);
adriver=new_fluid_audio_driver(settings,synth);
if(!adriver)
{
fputs("Error creating fluidsynth audio driver!",stderr);
- delete_fluid_synth(synth);synth=NULL;
+ delete_fluid_synth(synth);synth=nullptr;
return;
}
fluid_synth_set_chorus(synth,3,2.0,0.3,8.0,
@@ -37,7 +37,7 @@ void qmpMidiOutFluid::deviceDeinit(bool freshsettings)
if(!synth||!adriver)return;
delete_fluid_audio_driver(adriver);
delete_fluid_synth(synth);
- synth=NULL;adriver=NULL;
+ synth=nullptr;adriver=nullptr;
if(freshsettings)
{
delete_fluid_settings(settings);
@@ -70,7 +70,7 @@ void qmpMidiOutFluid::basicMessage(uint8_t type,uint8_t p1,uint8_t p2)
void qmpMidiOutFluid::extendedMessage(uint8_t length,const char *data)
{
int rlen=0;
- fluid_synth_sysex(synth,data,length,NULL,&rlen,NULL,0);
+ fluid_synth_sysex(synth,data,length,nullptr,&rlen,nullptr,0);
}
void qmpMidiOutFluid::rpnMessage(uint8_t ch,uint16_t type,uint16_t val)
{
@@ -234,7 +234,7 @@ void qmpFileRendererFluid::renderDeinit()
delete_fluid_player(player);
delete_fluid_synth(synth);
delete_fluid_settings(settings);
- player=NULL;synth=NULL;settings=NULL;
+ player=nullptr;synth=nullptr;settings=nullptr;
}
void qmpFileRendererFluid::renderWorker()
{
diff --git a/core/qmpmidioutrtmidi.cpp b/core/qmpmidioutrtmidi.cpp
index 1b897df..2d6d41f 100644
--- a/core/qmpmidioutrtmidi.cpp
+++ b/core/qmpmidioutrtmidi.cpp
@@ -6,13 +6,13 @@
qmpMidiOutRtMidi::qmpMidiOutRtMidi(unsigned _portid)
{
portid=_portid;
- outport=NULL;
+ outport=nullptr;
}
qmpMidiOutRtMidi::~qmpMidiOutRtMidi()
{
if(!outport)return;
if(outport->isPortOpen())outport->closePort();
- delete outport;outport=NULL;
+ delete outport;outport=nullptr;
}
void qmpMidiOutRtMidi::deviceInit()
{
@@ -23,7 +23,7 @@ void qmpMidiOutRtMidi::deviceInit()
catch(RtMidiError &e)
{
printf("Cannot create RtMidi Output instance: %s\n",e.what());
- outport=NULL;
+ outport=nullptr;
}
}
void qmpMidiOutRtMidi::deviceDeinit()
@@ -92,14 +92,14 @@ void qmpMidiOutRtMidi::onUnmapped(uint8_t ch,int refcnt)
if(!refcnt&&outport)outport->closePort();
}
-RtMidiOut* qmpRtMidiManager::dummy=NULL;
+RtMidiOut* qmpRtMidiManager::dummy=nullptr;
void qmpRtMidiManager::createDevices()
{
try{dummy=new RtMidiOut();}
catch(RtMidiError &e)
{
printf("Failed to initialize the dummy device: %s\n",e.what());
- dummy=NULL;return;
+ dummy=nullptr;return;
}
for(unsigned i=0;i<dummy->getPortCount();++i)
devices.push_back(std::make_pair(new qmpMidiOutRtMidi(i),dummy->getPortName(i)));
diff --git a/core/qmpmidiplay.cpp b/core/qmpmidiplay.cpp
index 29f636f..1e0cf8c 100644
--- a/core/qmpmidiplay.cpp
+++ b/core/qmpmidiplay.cpp
@@ -10,15 +10,13 @@
#include <windows.h>
uint64_t pf;
#endif
-//debug purpose
-uint32_t ttick;
-std::chrono::high_resolution_clock::time_point ttime;
-CMidiPlayer* CMidiPlayer::ref=NULL;
+CMidiPlayer* CMidiPlayer::ref=nullptr;
bool CMidiPlayer::processEvent(const SEvent *e)
{
- SEventCallBackData cbd(e->type,e->p1,e->p2,tceptr);
for(int i=0;i<16;++i)if(eventHandlerCB[i])
- eventHandlerCB[i]->callBack(&cbd,eventHandlerCBuserdata[i]);
+ eventHandlerCB[i]->callBack((void*)e,eventHandlerCBuserdata[i]);
+ for(auto i=event_handlers.begin();i!=event_handlers.end();++i)
+ i->second.first((void*)e,i->second.second);
uint8_t ch=e->type&0x0F;
switch(e->type&0xF0)
{
@@ -56,16 +54,28 @@ bool CMidiPlayer::processEvent(const SEvent *e)
switch(e->p1)
{
case 0x51:
- ctempo=e->p2;dpt=ctempo*1000./divs;
+ {
+ ctempo=0;
+#if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__)||defined(_WIN32)
+ std::string x(e->str);
+ std::reverse(x.begin(),x.end());
+ memcpy(&ctempo,x.data(),3);
+#else
+ memcpy(&ctempo,e->str.data(),3);
+ ctempo>>=8;
+#endif
+ dpt=ctempo*1000./divs;
ttime=std::chrono::high_resolution_clock::now();
ttick=getTick();
+ }
break;
case 0x58:
- ctsn=e->p2>>24;
- ctsd=1<<((e->p2>>16)&0xFF);
+ ctsn=(uint32_t)e->str[0];
+ ctsd=1<<((uint32_t)e->str[1]);
break;
case 0x59:
- cks=e->p2;
+ if(e->str.length()==2)
+ cks=(e->str[0]<<8u)|e->str[1];
break;
case 0x01:case 0x02:case 0x03:
case 0x04:case 0x05:case 0x06:
@@ -111,14 +121,26 @@ void CMidiPlayer::processEventStub(const SEvent *e)
switch(e->p1)
{
case 0x51:
- ctempo=e->p2;dpt=ctempo*1000./divs;
+ {
+ ctempo=0;
+#if (__BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__)||defined(_WIN32)
+ std::string x(e->str);
+ std::reverse(x.begin(),x.end());
+ memcpy(&ctempo,x.data(),3);
+#else
+ memcpy(&ctempo,e->str.data(),3);
+ ctempo>>=8;
+#endif
+ dpt=ctempo*1000./divs;
ccc[0][131]=ctempo;
+ }
break;
case 0x58:
- ccc[0][132]=e->p2;
+ ccc[0][132]=(e->str[0]<<24u)|(e->str[1]<<16u);
break;
case 0x59:
- ccc[0][133]=e->p2;
+ if(e->str.length()>=2)
+ ccc[0][133]=(e->str[0]<<8u)|e->str[1];
break;
}
}
@@ -251,9 +273,10 @@ void CMidiPlayer::fileTimer2Pass()
CMidiPlayer::CMidiPlayer()
{
midiReaders=new CMidiFileReaderCollection();
- resumed=false;midiFile=NULL;internalFluid=new qmpMidiOutFluid();
+ resumed=false;midiFile=nullptr;internalFluid=new qmpMidiOutFluid();
registerMidiOutDevice(internalFluid,"Internal FluidSynth");
waitvoice=true;
+ event_handlers_id=event_read_handlers_id=file_read_finish_hooks_id=0;
memset(eventHandlerCB,0,sizeof(eventHandlerCB));
memset(eventHandlerCBuserdata,0,sizeof(eventHandlerCBuserdata));
memset(eventReaderCB,0,sizeof(eventReaderCB));
@@ -281,7 +304,7 @@ CMidiPlayer::~CMidiPlayer()
{
internalFluid->deviceDeinit();
delete internalFluid;
- internalFluid=NULL;
+ internalFluid=nullptr;
}
if(midiFile)delete midiFile;delete midiReaders;
}
@@ -307,7 +330,9 @@ bool CMidiPlayer::playerLoadFile(const char* fn)
maxtk=std::max(maxtk,i.eventList.back().time);
}
for(int i=0;i<16;++i)if(fileReadFinishCB[i])
- fileReadFinishCB[i]->callBack(NULL,fileReadFinishCBuserdata[i]);
+ fileReadFinishCB[i]->callBack(nullptr,fileReadFinishCBuserdata[i]);
+ for(auto i=file_read_finish_hooks.begin();i!=file_read_finish_hooks.end();++i)
+ i->second.first(nullptr,i->second.second);
eorder.clear();
for(size_t i=0;i<midiFile->tracks.size();++i)
for(size_t j=0;j<midiFile->tracks[i].eventList.size();++j)
@@ -343,7 +368,7 @@ void CMidiPlayer::playerInit()
void CMidiPlayer::playerDeinit()
{
tceptr=0;tcstop=1;tcpaused=0;
- delete midiFile;midiFile=NULL;
+ delete midiFile;midiFile=nullptr;
}
void CMidiPlayer::playerThread()
{
@@ -503,7 +528,7 @@ int CMidiPlayer::setEventHandlerCB(ICallBack *cb,void *userdata)
for(int i=0;i<16;++i)
{
if(eventHandlerCB[i]==cb)return i;
- if(eventHandlerCB[i]==NULL)
+ if(eventHandlerCB[i]==nullptr)
{
eventHandlerCB[i]=cb;eventHandlerCBuserdata[i]=userdata;
return i;
@@ -512,13 +537,13 @@ int CMidiPlayer::setEventHandlerCB(ICallBack *cb,void *userdata)
return -1;
}
void CMidiPlayer::unsetEventHandlerCB(int id)
-{eventHandlerCB[id]=NULL;eventHandlerCBuserdata[id]=NULL;}
+{eventHandlerCB[id]=nullptr;eventHandlerCBuserdata[id]=nullptr;}
int CMidiPlayer::setEventReaderCB(ICallBack *cb,void *userdata)
{
for(int i=0;i<16;++i)
{
if(eventReaderCB[i]==cb)return i;
- if(eventReaderCB[i]==NULL)
+ if(eventReaderCB[i]==nullptr)
{
eventReaderCB[i]=cb;eventReaderCBuserdata[i]=userdata;
return i;
@@ -527,13 +552,13 @@ int CMidiPlayer::setEventReaderCB(ICallBack *cb,void *userdata)
return -1;
}
void CMidiPlayer::unsetEventReaderCB(int id)
-{eventReaderCB[id]=NULL;eventReaderCBuserdata[id]=NULL;}
+{eventReaderCB[id]=nullptr;eventReaderCBuserdata[id]=nullptr;}
int CMidiPlayer::setFileReadFinishedCB(ICallBack *cb,void *userdata)
{
for(int i=0;i<16;++i)
{
if(fileReadFinishCB[i]==cb)return i;
- if(fileReadFinishCB[i]==NULL)
+ if(fileReadFinishCB[i]==nullptr)
{
fileReadFinishCB[i]=cb;fileReadFinishCBuserdata[i]=userdata;
return i;
@@ -542,23 +567,49 @@ int CMidiPlayer::setFileReadFinishedCB(ICallBack *cb,void *userdata)
return -1;
}
void CMidiPlayer::unsetFileReadFinishedCB(int id)
-{fileReadFinishCB[id]=NULL;fileReadFinishCBuserdata[id]=NULL;}
+{fileReadFinishCB[id]=nullptr;fileReadFinishCBuserdata[id]=nullptr;}
+int CMidiPlayer::registerEventHandler(callback_t cb,void *userdata)
+{
+ event_handlers[event_handlers_id++]=std::make_pair(cb,userdata);
+}
+void CMidiPlayer::unregisterEventHandler(int id)
+{
+ event_handlers.find(id)!=event_handlers.end()&&event_handlers.erase(id);
+}
+int CMidiPlayer::registerEventReadHandler(callback_t cb,void *userdata)
+{
+ event_read_handlers[event_read_handlers_id++]=std::make_pair(cb,userdata);
+}
+void CMidiPlayer::unregisterEventReadHandler(int id)
+{
+ event_read_handlers.find(id)!=event_read_handlers.end()&&event_read_handlers.erase(id);
+}
+int CMidiPlayer::registerFileReadFinishHook(callback_t cb,void *userdata)
+{
+ file_read_finish_hooks[file_read_finish_hooks_id++]=std::make_pair(cb,userdata);
+}
+void CMidiPlayer::unregisterFileReadFinishHook(int id)
+{
+ file_read_finish_hooks.find(id)!=file_read_finish_hooks.end()&&file_read_finish_hooks.erase(id);
+}
void CMidiPlayer::registerReader(qmpFileReader *reader,std::string name)
{midiReaders->registerReader(reader,name);}
void CMidiPlayer::unregisterReader(std::string name)
{midiReaders->unregisterReader(name);}
-void CMidiPlayer::callEventReaderCB(SEventCallBackData d)
+void CMidiPlayer::callEventReaderCB(SEvent d)
{
if((d.type&0xF0)==0x90)++notes;
for(int i=0;i<16;++i)if(eventReaderCB[i])
eventReaderCB[i]->callBack(&d,eventReaderCBuserdata[i]);
+ for(auto i=event_read_handlers.begin();i!=event_read_handlers.end();++i)
+ i->second.first(&d,i->second.second);
}
void CMidiPlayer::discardCurrentEvent()
{
if(midiReaders->getCurrentReader())
midiReaders->getCurrentReader()->discardCurrentEvent();
}
-void CMidiPlayer::commitEventChange(SEventCallBackData d)
+void CMidiPlayer::commitEventChange(SEvent d)
{
if(midiReaders->getCurrentReader())
midiReaders->getCurrentReader()->commitEventChange(d);
diff --git a/core/qmpmidiplay.hpp b/core/qmpmidiplay.hpp
index f054238..b94bb08 100644
--- a/core/qmpmidiplay.hpp
+++ b/core/qmpmidiplay.hpp
@@ -3,6 +3,8 @@
#define QMPMIDIPLAY_H
#include <cstring>
#include <cstdlib>
+#include <chrono>
+#include <unordered_map>
#include <utility>
#include <vector>
#define QMP_MAIN
@@ -17,23 +19,24 @@ class CSMFReader:public qmpFileReader
CMidiTrack* curTrack;
uint32_t fmt,trk;
FILE *f;
- int byteread,valid,eventdiscarded;
- uint32_t curt,curid;
+ bool eventdiscarded;
+ uint32_t byteread,curt,curid;
void error(int fatal,const char* format,...);
- uint32_t readSW();
- uint32_t readDW();
- uint32_t readVL();
- int eventReader();
- void trackChunkReader();
- void headerChunkReader();
- int chunkReader(int hdrXp);
+ uint8_t read_u8();
+ uint16_t read_u16();
+ uint32_t read_u32();
+ uint32_t read_varlen();
+ int read_event();
+ void read_track();
+ void read_header();
+ uint32_t read_chunk(int is_header);
public:
CSMFReader();
~CSMFReader();
CMidiFile* readFile(const char* fn);
void discardCurrentEvent();
- void commitEventChange(SEventCallBackData d);
+ void commitEventChange(SEvent d);
};
class CMidiFileReaderCollection{
private:
@@ -70,6 +73,9 @@ class CMidiPlayer
uint32_t tceptr,tcpaused,tcstop,ct;
uint32_t finished,resumed;
uint32_t pbr[16],pbv[16];
+ //playback correction
+ uint32_t ttick;
+ std::chrono::high_resolution_clock::time_point ttime;
struct SMidiDev
{
std::string name;
@@ -84,6 +90,10 @@ class CMidiPlayer
void* eventHandlerCBuserdata[16];
void* eventReaderCBuserdata[16];
void* fileReadFinishCBuserdata[16];
+ std::unordered_map<int,std::pair<callback_t,void*>> event_handlers;
+ std::unordered_map<int,std::pair<callback_t,void*>> event_read_handlers;
+ std::unordered_map<int,std::pair<callback_t,void*>> file_read_finish_hooks;
+ int event_handlers_id,event_read_handlers_id,file_read_finish_hooks_id;
static CMidiPlayer* ref;
SEvent *getEvent(int id);
@@ -152,12 +162,18 @@ class CMidiPlayer
void unsetEventReaderCB(int id);
int setFileReadFinishedCB(ICallBack *cb,void *userdata);
void unsetFileReadFinishedCB(int id);
+ int registerEventHandler(callback_t cb,void *userdata);
+ void unregisterEventHandler(int id);
+ int registerEventReadHandler(callback_t cb,void *userdata);
+ void unregisterEventReadHandler(int id);
+ int registerFileReadFinishHook(callback_t cb,void *userdata);
+ void unregisterFileReadFinishHook(int id);
void registerReader(qmpFileReader* reader,std::string name);
void unregisterReader(std::string name);
- void callEventReaderCB(SEventCallBackData d);
+ void callEventReaderCB(SEvent d);
void discardCurrentEvent();
- void commitEventChange(SEventCallBackData d);
+ void commitEventChange(SEvent d);
static CMidiPlayer* getInstance();
};
diff --git a/core/qmpmidiread.cpp b/core/qmpmidiread.cpp
index d5f20ff..7f5bdfc 100644
--- a/core/qmpmidiread.cpp
+++ b/core/qmpmidiread.cpp
@@ -8,10 +8,10 @@
#include <cstdarg>
#include <algorithm>
#include "qmpmidiplay.hpp"
-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";
+static const char* GM1SysX="\xF0\x7E\x7F\x09\x01\xF7";
+static const char* GM2SysX="\xF0\x7E\x7F\x09\x03\xF7";
+static const char* GSSysEx="\xF0\x41\x10\x42\x12\x40\x00\x7F\x00\x41\xF7";
+static const char* XGSysEx="\xF0\x43\x10\x4C\x00\x00\x7E\x00\xF7";
#define assert(x) if(!(x))this->error(false,"assertion failure @ qmpmidiread.cpp:%d",__LINE__)
void CSMFReader::error(int fatal,const char* format,...)
{
@@ -21,34 +21,56 @@ void CSMFReader::error(int fatal,const char* format,...)
if(fatal)throw std::runtime_error(bufr);
else fprintf(stderr,"CSMFReader W: %s.\n",bufr);
}
-uint32_t CSMFReader::readSW()
+uint8_t CSMFReader::read_u8()
{
- uint32_t ret=0;
- for(int i=0;i<2;++i){ret<<=8;ret|=((uint32_t)fgetc(f))&0xFF;}
+ uint8_t ret=0;
+ int t=fgetc(f);
+ if(!~t)error(1,"Unexpected EOF");
+ ret=(uint8_t)t;
return ret;
}
-uint32_t CSMFReader::readDW()
+uint16_t CSMFReader::read_u16()
+{
+ uint16_t ret=0;
+ size_t sz=fread(&ret,2,1,f);
+ if(sz<1)error(1,"Unexpected EOF");
+#if defined(_MSC_VER)&&defined(_WIN32)
+ ret=_byteswap_ushort(ret);
+#elif __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
+ ret=__builtin_bswap16(ret);
+#endif
+ return ret;
+}
+uint32_t CSMFReader::read_u32()
{
uint32_t ret=0;
- for(int i=0;i<4;++i){ret<<=8;ret|=((uint32_t)fgetc(f))&0xFF;}
+ size_t sz=fread(&ret,4,1,f);
+ if(sz<1)error(1,"Unexpected EOF");
+#if defined(_MSC_VER)&&defined(_WIN32)
+ ret=_byteswap_ulong(ret);
+#elif __BYTE_ORDER__==__ORDER_LITTLE_ENDIAN__
+ ret=__builtin_bswap32(ret);
+#endif
return ret;
}
-uint32_t CSMFReader::readVL()
+uint32_t CSMFReader::read_varlen()
{
- uint32_t ret=0,t,c=0;
+ uint32_t ret=0,c=0;
+ int t;
do
{
t=fgetc(f);
+ if(!~t)error(1,"Unexpected EOF");
if(++c>4)error(1,"Variable length type overflow");
ret<<=7;ret|=(t&0x7F);
}while(t&0x80);
return ret;
}
-int CSMFReader::eventReader()//returns 0 if End of Track encountered
+int CSMFReader::read_event()//returns 0 if End of Track encountered
{
- uint32_t delta=readVL();curt+=delta;
- char type=fgetc(f);uint32_t p1,p2;
- static char lasttype;eventdiscarded=0;
+ uint32_t delta=read_varlen();curt+=delta;
+ uint8_t type=read_u8();uint32_t p1,p2;
+ static uint8_t lasttype;eventdiscarded=false;
if(!(type&0x80)){fseek(f,-1,SEEK_CUR);type=lasttype;}
switch(type&0xF0)
{
@@ -57,19 +79,19 @@ int CSMFReader::eventReader()//returns 0 if End of Track encountered
case 0xA0://Note Aftertouch
case 0xB0://Controller Change
case 0xE0://Pitch wheel
- p1=fgetc(f);p2=fgetc(f);
+ p1=read_u8();p2=read_u8();
curTrack->appendEvent(SEvent(curid,curt,type,p1,p2));
break;
case 0xC0://Patch Change
case 0xD0://Channel Aftertouch
- p1=fgetc(f);
+ p1=read_u8();
curTrack->appendEvent(SEvent(curid,curt,type,p1,0));
break;
case 0xF0:
if((type&0x0F)==0x0F)//Meta Event
{
- char metatype=fgetc(f);
- uint32_t len=readVL();char* str=NULL;
+ uint8_t metatype=read_u8();
+ uint32_t len=read_varlen();char* str=nullptr;
if(len<=1024&&len>0)str=new char[len+8];
if(str)fread(str,1,len,f);else fseek(f,len,SEEK_CUR);
if(str)str[len]='\0';
@@ -83,26 +105,21 @@ int CSMFReader::eventReader()//returns 0 if End of Track encountered
break;
case 0x2F://End of Track
assert(len==0);
- return 0;
- break;
+ return 0;
case 0x51://Set Tempo
assert(len==3);
- p1=((str[0]&0xffu)<<16u)|(str[1]&0xffu)<<8u|(str[2]&0xffu);
- curTrack->appendEvent(SEvent(curid,curt,type,metatype,p1));
+ curTrack->appendEvent(SEvent(curid,curt,type,metatype,0,str));
break;
case 0x54://SMTPE offset, not handled.
assert(len==5);
break;
case 0x58://Time signature
assert(len==4);
- p1=((str[0]&0xffu)<<24u)|(str[1]&0xffu)<<16u|(str[2]&0xffu)<<8u|(str[3]&0xffu);
- curTrack->appendEvent(SEvent(curid,curt,type,metatype,p1));
+ curTrack->appendEvent(SEvent(curid,curt,type,metatype,0,str));
break;
case 0x59://Key signature
assert(len==2);
- if(len>=2)
- p1=(str[0]&0xffu)<<8u|(str[1]&0xffu);else p1=0;
- curTrack->appendEvent(SEvent(curid,curt,type,metatype,p1));
+ curTrack->appendEvent(SEvent(curid,curt,type,metatype,0,str));
break;
case 0x01:case 0x02:case 0x03:
case 0x04:case 0x05:case 0x06:
@@ -125,14 +142,19 @@ int CSMFReader::eventReader()//returns 0 if End of Track encountered
}
else if((type&0x0F)==0x00||(type&0x0F)==0x07)//SysEx
{
- uint32_t len=readVL(),c;char* str=NULL;
+ uint32_t len=read_varlen();char* str=nullptr;
str=new char[len+8];
if((type&0x0F)==0x00)
{
str[0]=0xF0;++len;
- for(c=1;c<len;++c){str[c]=fgetc(f);}
+ size_t sz=fread(str+1,1,len-1,f);
+ if(sz<len-1)error(1,"Unexpected EOF");
+ }
+ else
+ {
+ size_t sz=fread(str,1,len,f);
+ if(sz<len)error(1,"Unexpected EOF");
}
- else for(c=0;c<len;++c){str[c]=fgetc(f);}
curTrack->appendEvent(SEvent(curid,curt,type,len,0,str));
if(!strcmp(str,GM1SysX))ret->std=1;
if(!strcmp(str,GM2SysX))ret->std=2;
@@ -149,17 +171,16 @@ int CSMFReader::eventReader()//returns 0 if End of Track encountered
if(curTrack->eventList.size())
{
SEvent& le=curTrack->eventList.back();
- SEventCallBackData cbd(le.type,le.p1,le.p2,le.time);
- CMidiPlayer::getInstance()->callEventReaderCB(cbd);
+ CMidiPlayer::getInstance()->callEventReaderCB(le);
}
return 1;
}
-void CSMFReader::trackChunkReader()
+void CSMFReader::read_track()
{
ret->tracks.push_back(CMidiTrack());
curTrack=&ret->tracks.back();
- int chnklen=readDW();byteread=ftell(f);curt=0;curid=0;
- while(eventReader());
+ uint32_t chnklen=read_u32();byteread=ftell(f);curt=0;curid=0;
+ while(read_event());
byteread=ftell(f)-byteread;
if(byteread<chnklen)
{
@@ -169,21 +190,21 @@ void CSMFReader::trackChunkReader()
if(byteread>chnklen)
error(1,"Read past end of track");
}
-void CSMFReader::headerChunkReader()
+void CSMFReader::read_header()
{
- int chnklen=readDW();byteread=ftell(f);
+ uint32_t chnklen=read_u32();byteread=ftell(f);
if(chnklen<6)error(1,"Header chunk too short");
if(chnklen>6)error(0,"Header chunk length longer than expected. Ignoring extra bytes");
- fmt=readSW();trk=readSW();ret->divs=readSW();
+ fmt=read_u16();trk=read_u16();ret->divs=read_u16();
if(ret->divs&0x8000)error(1,"SMTPE format is not supported");
for(byteread=ftell(f)-byteread;byteread<chnklen;++byteread){fgetc(f);}
}
-int CSMFReader::chunkReader(int hdrXp)
+uint32_t CSMFReader::read_chunk(int is_header)
{
char hdr[6];
fread(hdr,1,4,f);
if(feof(f))error(1,"Unexpected EOF");
- if(hdrXp)
+ if(is_header)
{
if(!strncmp(hdr,"RIFF",4))
{
@@ -194,37 +215,37 @@ int CSMFReader::chunkReader(int hdrXp)
fread(hdr,1,4,f);
}
if(strncmp(hdr,"MThd",4)){error(1,"Wrong MIDI header.");}
- else return headerChunkReader(),0;
+ else return read_header(),0;
}
else
if(strncmp(hdr,"MTrk",4))
{
error(0,"Wrong track chunk header. Ignoring the whole chunk");
- uint32_t chnklen=readDW();fseek(f,chnklen,SEEK_CUR);return 0;
+ uint32_t chnklen=read_u32();fseek(f,chnklen,SEEK_CUR);return 0;
}
- else return trackChunkReader(),1;
+ else return read_track(),1;
return 0;
}
CSMFReader::CSMFReader()
{
- f=NULL;
+ f=nullptr;
}
CMidiFile* CSMFReader::readFile(const char* fn)
{
ret=new CMidiFile;
- ret->title=ret->copyright=NULL;ret->std=0;ret->valid=1;
+ ret->title=ret->copyright=nullptr;ret->std=0;ret->valid=1;
try
{
if(!(f=fopen(fn,"rb")))
- throw std::runtime_error("File doesn't exist");
- chunkReader(1);
- for(uint32_t i=0;i<trk;i+=chunkReader(0));
- fclose(f);f=NULL;
+ throw std::runtime_error("Can't open file");
+ read_chunk(1);
+ for(uint32_t i=0;i<trk;i+=read_chunk(0));
+ fclose(f);f=nullptr;
}
catch(std::runtime_error& e)
{
fprintf(stderr,"CSMFReader E: %s is not a supported file. Cause: %s.\n",fn,e.what());
- ret->valid=0;if(f)fclose(f);f=NULL;
+ ret->valid=0;if(f)fclose(f);f=nullptr;
}
return ret;
}
@@ -234,10 +255,10 @@ CSMFReader::~CSMFReader()
void CSMFReader::discardCurrentEvent()
{
- if(eventdiscarded)return;eventdiscarded=1;
+ if(eventdiscarded)return;eventdiscarded=true;
curTrack->eventList.pop_back();
}
-void CSMFReader::commitEventChange(SEventCallBackData d)
+void CSMFReader::commitEventChange(SEvent d)
{
curTrack->eventList.back().time=d.time;
curTrack->eventList.back().type=d.type;
@@ -247,7 +268,7 @@ void CSMFReader::commitEventChange(SEventCallBackData d)
CMidiFileReaderCollection::CMidiFileReaderCollection()
{
- readers.clear();currentReader=NULL;
+ readers.clear();currentReader=nullptr;
registerReader(new CSMFReader(),"Default SMF Reader");
}
CMidiFileReaderCollection::~CMidiFileReaderCollection()
@@ -271,7 +292,7 @@ void CMidiFileReaderCollection::unregisterReader(std::string name)
}
CMidiFile* CMidiFileReaderCollection::readFile(const char* fn)
{
- CMidiFile *file=NULL;
+ CMidiFile *file=nullptr;
for(unsigned i=0;i<readers.size();++i)
{
currentReader=readers[i].first;
@@ -280,7 +301,7 @@ CMidiFile* CMidiFileReaderCollection::readFile(const char* fn)
if(t->valid){file=t;break;}
else delete t;
}
- currentReader=NULL;
+ currentReader=nullptr;
return file;
}
qmpFileReader* CMidiFileReaderCollection::getCurrentReader()