Guest User

Load MIDI File

a guest
Jun 23rd, 2012
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.01 KB | None | 0 0
  1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <vector>
  7.  
  8. using namespace std;
  9.  
  10. struct MidiEvent
  11. {
  12.     unsigned long delay;
  13.     unsigned char status;
  14.     unsigned char pitch;
  15.     unsigned char volume;
  16. };
  17.  
  18. struct MidiTrack
  19. {
  20.     char trackName[256];
  21.     vector<MidiEvent> events;
  22. };
  23.  
  24. void LoadMidiFile(const wchar_t *fileName, vector<MidiTrack> *outTracks);
  25. template <class T> void endswap(T &objp);
  26. template <class T> void fileRead(std::ifstream &ifstr, T &outVar);
  27.  
  28. vector<MidiEvent> midiTrack;
  29. vector<MidiTrack> midiTracks;
  30.  
  31. int main(int argc, char** argv) {
  32.  
  33.    printf("Commands:\n"
  34.             "quit\n"
  35.             "load fileName\n"
  36.             "\n");
  37.  
  38.     while (1)
  39.     {
  40.         wstring wstr;
  41.         getline(wcin, wstr);
  42.  
  43.         if (wstr == L"quit") break;
  44.         if (wstr.substr(0, 4) == L"load")
  45.         {
  46.             wstring fn;
  47.             fn = wstr.substr(wstr.find(' ')+1);
  48.             LoadMidiFile(fn.c_str(), &midiTracks);
  49.         }
  50.     }
  51.    return 0;
  52. }
  53.  
  54. void LoadMidiFile(const wchar_t *fileName, vector<MidiTrack> *outTracks)
  55. {
  56.     wstring formattedFileName = fileName;
  57.     wstring::size_type findQuote = 0;
  58.     while((findQuote = formattedFileName.find('\"', findQuote)) != formattedFileName.npos)
  59.     {
  60.         formattedFileName.erase(findQuote, 1);
  61.     }
  62.  
  63.     ifstream ifs(formattedFileName.c_str(), ifstream::binary | ifstream::in);
  64.  
  65.     if(ifs.fail())
  66.     {
  67.         wcout << L"Error: Failed to open \"" << fileName << "\"" << endl;
  68.         if(ifs.is_open()) ifs.close();
  69.         return;
  70.     }
  71.  
  72.     outTracks->erase(outTracks->begin(), outTracks->end());
  73.  
  74.     unsigned long fileType;
  75.     unsigned long headerSize;
  76.     unsigned short midiType;
  77.     unsigned short numTracks;
  78.     unsigned short ppq;
  79.  
  80.     fileRead(ifs, fileType);
  81.     fileRead(ifs, headerSize);
  82.     fileRead(ifs, midiType);
  83.     fileRead(ifs, numTracks);
  84.     fileRead(ifs, ppq);
  85.  
  86.     cout << "Number of Tracks in File: " << dec << numTracks << endl;
  87.  
  88.     for(int i=0; i<numTracks; i++)
  89.     {
  90.         // Track Header
  91.         unsigned long startMark;
  92.         unsigned long cbTrackSize;
  93.  
  94.         fileRead(ifs, startMark);
  95.         fileRead(ifs, cbTrackSize);
  96.  
  97.         cout << "Reading in track number " << dec << i << ", press any key to start." << endl;
  98.         _getch();
  99.  
  100.         MidiTrack midiTrack;
  101.  
  102.         unsigned char runningState = 0;
  103.         bool endOfTrack = false;
  104.  
  105.         while(!endOfTrack)
  106.         {
  107.             MidiEvent midiEvent;
  108.  
  109.             midiEvent.delay = 0;
  110.             midiEvent.pitch = 0;
  111.             midiEvent.status = 0;
  112.             midiEvent.volume = 0;
  113.  
  114.             unsigned char nextByte;
  115.             //unsigned long delay = 0;
  116.             fileRead(ifs, nextByte);
  117.  
  118.             // Get the delay time (time offset) till next event
  119.             // as long as the most significant bit is set, another byte will follow. Add the number and shift it ony byte to the left to make space for next byte
  120.  
  121.             while(nextByte > 0x80)
  122.             {
  123.                 midiEvent.delay += (nextByte & 127); //delay |= (nextByte & 127);
  124.                 midiEvent.delay <<= 8; // delay <<= 8;
  125.                 fileRead(ifs, nextByte);
  126.             }
  127.             midiEvent.delay += (nextByte & 127); // delay |= (nextByte & 127);
  128.  
  129.             cout << "File Offset: 0x";
  130.             cout.fill('0');
  131.             cout.width(6);
  132.             cout << hex << uppercase << ifs.tellg();
  133.             cout << " Delay: 0x";
  134.             cout.fill('0');
  135.             cout.width(6);
  136.             cout << midiEvent.delay;
  137.  
  138.             fileRead(ifs, nextByte);
  139.  
  140.             if(nextByte == 0xFF) // indicates a meta event, the next two bytes have data in them --- Byte 1 == Meta Event Type, Byte 2 = Meta Event Value
  141.             {
  142.                 fileRead(ifs, nextByte); // contains meta event type
  143.  
  144.                 if(nextByte == 0x01) // Text Event
  145.                 {
  146.                     cout << " TextEvent: ";
  147.                     fileRead(ifs, nextByte); // contains length
  148.                     int stringLen = nextByte;
  149.  
  150.                     for(int j = 0; j < stringLen; j++)
  151.                     {
  152.                         fileRead(ifs, nextByte);
  153.                         cout << nextByte;
  154.                     }
  155.                     cout << endl;
  156.                 }
  157.                 else if(nextByte == 0x03) // track name
  158.                 {
  159.                     fileRead(ifs, nextByte); // this byte contains size/length of track name which follows
  160.                     int cbTrackName = nextByte;
  161.                     char *trackName = new char[cbTrackName+1];
  162.                     trackName[cbTrackName] = '\0';
  163.                     for(int j=0; j < cbTrackName; j++)
  164.                     {
  165.                         fileRead(ifs, nextByte);
  166.                         trackName[j] = nextByte;
  167.                     }
  168.                     strcpy_s(midiTrack.trackName, trackName);
  169.                     cout << " TrackName #" << dec << i << " Name: " << midiTrack.trackName << endl;
  170.                     delete[] trackName;
  171.                 }
  172.                 else if(nextByte == 0x2F) // end of track
  173.                 {
  174.                     cout << " EndOfTrack" << endl;
  175.                     endOfTrack = true;
  176.                     ifs.ignore(1);
  177.                 }
  178.                 else
  179.                 {
  180.                     cout << " Unknown MetaEvent (0x" << uppercase << hex << static_cast<int>(nextByte);
  181.                     fileRead(ifs, nextByte);
  182.                     cout << "): skip " << dec << static_cast<int>(nextByte) << " Bytes" << endl;
  183.                     ifs.ignore(nextByte);
  184.                 }
  185.                 continue;
  186.             }
  187.             else if(nextByte == 0xF0) //SysEx Messages, we dont care about them, so we skip all the bytes until 0xF7 which indicates the end of the SysEx message
  188.             {
  189.                 cout << " SysEx Message" << endl;
  190.                 fileRead(ifs, nextByte);
  191.                 while(nextByte != 0xF7)
  192.                 {
  193.                     fileRead(ifs, nextByte);
  194.                 }
  195.                 continue;
  196.             }
  197.             else // MIDI Event
  198.             {
  199.                 // Get the status byte. If the next byte has the most significant bit set, its a status event, so update the runningState variable,
  200.                 // else, it's data, so we use the state saved in runningState.
  201.                 cout << " MidiEvent: 0x" << hex << uppercase << static_cast<int>(nextByte) << endl;
  202.                 if(runningState == 0 || (nextByte >= 0x80))
  203.                 {
  204.                     runningState = nextByte;
  205.                 }
  206.  
  207.                 midiEvent.status = runningState;
  208.  
  209.                 fileRead(ifs, nextByte);
  210.                 midiEvent.pitch = nextByte;
  211.  
  212.                 fileRead(ifs, nextByte);
  213.                 midiEvent.volume = nextByte;
  214.  
  215.                 midiTrack.events.push_back(midiEvent);
  216.             }
  217.         }
  218.         cout << "Track read, next one coming right up!" << endl;
  219.         outTracks->push_back(midiTrack);
  220.     }
  221.  
  222.     ifs.close();
  223.  
  224.     cout << outTracks->size() << " tracks read." << endl;
  225. }
  226.  
  227. template <class T>
  228. void endswap(T &objp)
  229. {
  230.   unsigned char *memp = reinterpret_cast<unsigned char*>(&objp);
  231.   std::reverse(memp, memp + sizeof(T));
  232. }
  233.  
  234. template <class T>
  235. void fileRead(std::ifstream &ifstr, T &outVar)
  236. {
  237.     ifstr.read(reinterpret_cast<char*>(&outVar), sizeof(outVar));
  238.     endswap(outVar);
  239. }
Advertisement
Add Comment
Please, Sign In to add comment