diff options
author | Chris Xiong <chirs241097@gmail.com> | 2020-05-12 00:58:40 +0800 |
---|---|---|
committer | Chris Xiong <chirs241097@gmail.com> | 2020-05-12 00:58:40 +0800 |
commit | a7407edaf81c685d4a389785a405a53a5de4b148 (patch) | |
tree | e6e4f2d85f2172c94ac83a6e8f01e1e90a78ede9 /midifmt-plugin | |
parent | 437b7b16c322a8e53ac55a5f831098494d9a7073 (diff) | |
download | QMidiPlayer-a7407edaf81c685d4a389785a405a53a5de4b148.tar.xz |
Format EVERYTHING.
Hopefully this will make the source code less horrendous and actually
readable.
The command used was:
```
astyle --suffix=none --style=allman --attach-extern-c --attach-closing-while --indent-switches --indent-after-parens --pad-oper --pad-header --unpad-paren --align-pointer=name --recursive './*.cpp,*.hpp'
```
Diffstat (limited to 'midifmt-plugin')
-rw-r--r-- | midifmt-plugin/midifmtplugin.cpp | 183 | ||||
-rw-r--r-- | midifmt-plugin/midifmtplugin.hpp | 64 |
2 files changed, 138 insertions, 109 deletions
diff --git a/midifmt-plugin/midifmtplugin.cpp b/midifmt-plugin/midifmtplugin.cpp index 62a9af1..39d6d4f 100644 --- a/midifmt-plugin/midifmtplugin.cpp +++ b/midifmt-plugin/midifmtplugin.cpp @@ -2,115 +2,140 @@ #include <algorithm> #include <stdexcept> #include "midifmtplugin.hpp" -qmpPluginAPI* qmpMidiFmtPlugin::api=nullptr; +qmpPluginAPI *qmpMidiFmtPlugin::api = nullptr; uint32_t CMidiStreamReader::readDWLE() { - uint32_t ret=0; - for(uint32_t i=0;i<4;++i)ret|=((uint32_t)fgetc(f))<<(i<<3); - return ret; + uint32_t ret = 0; + for (uint32_t i = 0; i < 4; ++i) + ret |= ((uint32_t)fgetc(f)) << (i << 3); + return ret; } bool CMidiStreamReader::RIFFHeaderReader() { - char hdr[9]; - fread(hdr,1,4,f); - if(strncmp(hdr,"RIFF",4))return false; - fseek(f,4,SEEK_CUR); - fread(hdr,1,8,f); - if(strncmp(hdr,"MIDSfmt ",8))return false; - if(readDWLE()!=0x0C)return false; - ret->divs=readDWLE(); - readDWLE(); - fmt=readDWLE(); - return true; + char hdr[9]; + fread(hdr, 1, 4, f); + if (strncmp(hdr, "RIFF", 4)) + return false; + fseek(f, 4, SEEK_CUR); + fread(hdr, 1, 8, f); + if (strncmp(hdr, "MIDSfmt ", 8)) + return false; + if (readDWLE() != 0x0C) + return false; + ret->divs = readDWLE(); + readDWLE(); + fmt = readDWLE(); + return true; } bool CMidiStreamReader::midsBodyReader() { - char buf[9]; - fread(buf,1,4,f); - if(strncmp(buf,"data",4))return false; - readDWLE();//size - uint32_t cblocks=readDWLE(); - uint32_t curid=0,cts=0; - for(uint32_t i=0;i<cblocks;++i) - { - readDWLE(); - uint32_t blocksz=readDWLE(),cpos=ftell(f); - while(ftell(f)-cpos<blocksz) - { - cts+=readDWLE(); - if(!(fmt&1))readDWLE(); - uint32_t e=readDWLE(); - SEvent ev; - if(e>>24==1)//set tempo - { - char s[3]={'\0'}; - for(int i=0;i<3;++i) - s[i]=(e>>(8*(2-i)))&0xff; - ev=SEvent(curid,cts,0xFF,0x51,0); - ev.str=std::string(s,3); - } - else if(e>>24==0)//midishortmsg - ev=SEvent(curid,cts,e&0xFF,(e>>8)&0xFF,(e>>16)&0xFF); - else return false; - ret->tracks.back().appendEvent(ev);eventdiscarded=0; - qmpMidiFmtPlugin::api->callEventReaderCB(ev); - if(eventdiscarded)ret->tracks.back().eventList.pop_back(); - ++curid; - } - } - return true; + char buf[9]; + fread(buf, 1, 4, f); + if (strncmp(buf, "data", 4)) + return false; + readDWLE();//size + uint32_t cblocks = readDWLE(); + uint32_t curid = 0, cts = 0; + for (uint32_t i = 0; i < cblocks; ++i) + { + readDWLE(); + uint32_t blocksz = readDWLE(), cpos = ftell(f); + while (ftell(f) - cpos < blocksz) + { + cts += readDWLE(); + if (!(fmt & 1))readDWLE(); + uint32_t e = readDWLE(); + SEvent ev; + if (e >> 24 == 1) //set tempo + { + char s[3] = {'\0'}; + for (int i = 0; i < 3; ++i) + s[i] = (e >> (8 * (2 - i))) & 0xff; + ev = SEvent(curid, cts, 0xFF, 0x51, 0); + ev.str = std::string(s, 3); + } + else if (e >> 24 == 0) //midishortmsg + ev = SEvent(curid, cts, e & 0xFF, (e >> 8) & 0xFF, (e >> 16) & 0xFF); + else return false; + ret->tracks.back().appendEvent(ev); + eventdiscarded = 0; + qmpMidiFmtPlugin::api->callEventReaderCB(ev); + if (eventdiscarded) + ret->tracks.back().eventList.pop_back(); + ++curid; + } + } + return true; } -CMidiFile* CMidiStreamReader::readFile(const char *fn) +CMidiFile *CMidiStreamReader::readFile(const char *fn) { - ret=new CMidiFile; - ret->title=ret->copyright=nullptr;ret->std=0;ret->valid=1; - ret->tracks.push_back(CMidiTrack()); - try - { - if(!(f=fopen(fn,"rb")))throw std::runtime_error("File doesn't exist"); - if(!RIFFHeaderReader())throw std::runtime_error("Wrong RIFF header"); - if(!midsBodyReader())throw std::runtime_error("MIDS data error"); - }catch(std::runtime_error& e) - { - fprintf(stderr,"CMidiStreamReader E: %s is not a supported file. Cause: %s.\n",fn,e.what()); - ret->valid=0;if(f)fclose(f);f=nullptr; - } - return ret; + ret = new CMidiFile; + ret->title = ret->copyright = nullptr; + ret->std = 0; + ret->valid = 1; + ret->tracks.push_back(CMidiTrack()); + try + { + if (!(f = fopen(fn, "rb"))) + throw std::runtime_error("File doesn't exist"); + if (!RIFFHeaderReader()) + throw std::runtime_error("Wrong RIFF header"); + if (!midsBodyReader()) + throw std::runtime_error("MIDS data error"); + } + catch (std::runtime_error &e) + { + fprintf(stderr, "CMidiStreamReader E: %s is not a supported file. Cause: %s.\n", fn, e.what()); + ret->valid = 0; + if (f) + fclose(f); + f = nullptr; + } + return ret; } void CMidiStreamReader::discardCurrentEvent() { - eventdiscarded=1; + eventdiscarded = 1; } void CMidiStreamReader::commitEventChange(SEvent d) { - ret->tracks.back().eventList.back().time=d.time; - ret->tracks.back().eventList.back().type=d.type; - ret->tracks.back().eventList.back().p1=d.p1; - ret->tracks.back().eventList.back().p2=d.p2; + ret->tracks.back().eventList.back().time = d.time; + ret->tracks.back().eventList.back().type = d.type; + ret->tracks.back().eventList.back().p1 = d.p1; + ret->tracks.back().eventList.back().p2 = d.p2; } CMidiStreamReader::CMidiStreamReader() { - ret=nullptr;f=nullptr; + ret = nullptr; + f = nullptr; } CMidiStreamReader::~CMidiStreamReader() { } qmpMidiFmtPlugin::qmpMidiFmtPlugin(qmpPluginAPI *_api) -{api=_api;} +{ + api = _api; +} qmpMidiFmtPlugin::~qmpMidiFmtPlugin() -{api=nullptr;} +{ + api = nullptr; +} void qmpMidiFmtPlugin::init() { - api->registerFileReader(mdsreader=new CMidiStreamReader,"MIDS reader"); + api->registerFileReader(mdsreader = new CMidiStreamReader, "MIDS reader"); } void qmpMidiFmtPlugin::deinit() { - api->unregisterFileReader("MIDS reader"); - delete mdsreader; + api->unregisterFileReader("MIDS reader"); + delete mdsreader; +} +const char *qmpMidiFmtPlugin::pluginGetName() +{ + return "QMidiPlayer extra midi formats plugin"; +} +const char *qmpMidiFmtPlugin::pluginGetVersion() +{ + return PLUGIN_VERSION; } -const char* qmpMidiFmtPlugin::pluginGetName() -{return "QMidiPlayer extra midi formats plugin";} -const char* qmpMidiFmtPlugin::pluginGetVersion() -{return PLUGIN_VERSION;} diff --git a/midifmt-plugin/midifmtplugin.hpp b/midifmt-plugin/midifmtplugin.hpp index 038a2a7..b4eeddb 100644 --- a/midifmt-plugin/midifmtplugin.hpp +++ b/midifmt-plugin/midifmtplugin.hpp @@ -4,42 +4,46 @@ #include <cstdio> #include "../include/qmpcorepublic.hpp" -class CMidiStreamReader:public qmpFileReader +class CMidiStreamReader: public qmpFileReader { - private: - CMidiFile* ret; - FILE* f; - int eventdiscarded,fmt; - uint32_t readDWLE(); - bool RIFFHeaderReader(); - bool midsBodyReader(); - public: - CMidiStreamReader(); - ~CMidiStreamReader(); - CMidiFile* readFile(const char *fn); - void discardCurrentEvent(); - void commitEventChange(SEvent d); +private: + CMidiFile *ret; + FILE *f; + int eventdiscarded, fmt; + uint32_t readDWLE(); + bool RIFFHeaderReader(); + bool midsBodyReader(); +public: + CMidiStreamReader(); + ~CMidiStreamReader(); + CMidiFile *readFile(const char *fn); + void discardCurrentEvent(); + void commitEventChange(SEvent d); }; -class qmpMidiFmtPlugin:public qmpPluginIntf +class qmpMidiFmtPlugin: public qmpPluginIntf { - private: - CMidiStreamReader* mdsreader; - public: - static qmpPluginAPI* api; - qmpMidiFmtPlugin(qmpPluginAPI* _api); - ~qmpMidiFmtPlugin(); - void init(); - void deinit(); - const char* pluginGetName(); - const char* pluginGetVersion(); +private: + CMidiStreamReader *mdsreader; +public: + static qmpPluginAPI *api; + qmpMidiFmtPlugin(qmpPluginAPI *_api); + ~qmpMidiFmtPlugin(); + void init(); + void deinit(); + const char *pluginGetName(); + const char *pluginGetVersion(); }; -extern "C"{ - EXPORTSYM qmpPluginIntf* qmpPluginGetInterface(qmpPluginAPI* api) - {return new qmpMidiFmtPlugin(api);} - EXPORTSYM const char* qmpPluginGetAPIRev() - {return QMP_PLUGIN_API_REV;} +extern "C" { + EXPORTSYM qmpPluginIntf *qmpPluginGetInterface(qmpPluginAPI *api) + { + return new qmpMidiFmtPlugin(api); + } + EXPORTSYM const char *qmpPluginGetAPIRev() + { + return QMP_PLUGIN_API_REV; + } } #endif // MIDIFMTPLUGIN_HPP |