Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <conio.h>
- #include <stdio.h>
- #include <string>
- #include <iostream>
- #include <fstream>
- #include <vector>
- using namespace std;
- struct MidiEvent
- {
- unsigned long delay;
- unsigned char status;
- unsigned char pitch;
- unsigned char volume;
- };
- struct MidiTrack
- {
- char trackName[256];
- vector<MidiEvent> events;
- };
- void LoadMidiFile(const wchar_t *fileName, vector<MidiTrack> *outTracks);
- template <class T> void endswap(T &objp);
- template <class T> void fileRead(std::ifstream &ifstr, T &outVar);
- vector<MidiEvent> midiTrack;
- vector<MidiTrack> midiTracks;
- int main(int argc, char** argv) {
- printf("Commands:\n"
- "quit\n"
- "load fileName\n"
- "\n");
- while (1)
- {
- wstring wstr;
- getline(wcin, wstr);
- if (wstr == L"quit") break;
- if (wstr.substr(0, 4) == L"load")
- {
- wstring fn;
- fn = wstr.substr(wstr.find(' ')+1);
- LoadMidiFile(fn.c_str(), &midiTracks);
- }
- }
- return 0;
- }
- void LoadMidiFile(const wchar_t *fileName, vector<MidiTrack> *outTracks)
- {
- wstring formattedFileName = fileName;
- wstring::size_type findQuote = 0;
- while((findQuote = formattedFileName.find('\"', findQuote)) != formattedFileName.npos)
- {
- formattedFileName.erase(findQuote, 1);
- }
- ifstream ifs(formattedFileName.c_str(), ifstream::binary | ifstream::in);
- if(ifs.fail())
- {
- wcout << L"Error: Failed to open \"" << fileName << "\"" << endl;
- if(ifs.is_open()) ifs.close();
- return;
- }
- outTracks->erase(outTracks->begin(), outTracks->end());
- unsigned long fileType;
- unsigned long headerSize;
- unsigned short midiType;
- unsigned short numTracks;
- unsigned short ppq;
- fileRead(ifs, fileType);
- fileRead(ifs, headerSize);
- fileRead(ifs, midiType);
- fileRead(ifs, numTracks);
- fileRead(ifs, ppq);
- cout << "Number of Tracks in File: " << dec << numTracks << endl;
- for(int i=0; i<numTracks; i++)
- {
- // Track Header
- unsigned long startMark;
- unsigned long cbTrackSize;
- fileRead(ifs, startMark);
- fileRead(ifs, cbTrackSize);
- cout << "Reading in track number " << dec << i << ", press any key to start." << endl;
- _getch();
- MidiTrack midiTrack;
- unsigned char runningState = 0;
- bool endOfTrack = false;
- while(!endOfTrack)
- {
- MidiEvent midiEvent;
- midiEvent.delay = 0;
- midiEvent.pitch = 0;
- midiEvent.status = 0;
- midiEvent.volume = 0;
- unsigned char nextByte;
- //unsigned long delay = 0;
- fileRead(ifs, nextByte);
- // Get the delay time (time offset) till next event
- // 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
- while(nextByte > 0x80)
- {
- midiEvent.delay += (nextByte & 127); //delay |= (nextByte & 127);
- midiEvent.delay <<= 8; // delay <<= 8;
- fileRead(ifs, nextByte);
- }
- midiEvent.delay += (nextByte & 127); // delay |= (nextByte & 127);
- cout << "File Offset: 0x";
- cout.fill('0');
- cout.width(6);
- cout << hex << uppercase << ifs.tellg();
- cout << " Delay: 0x";
- cout.fill('0');
- cout.width(6);
- cout << midiEvent.delay;
- fileRead(ifs, nextByte);
- 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
- {
- fileRead(ifs, nextByte); // contains meta event type
- if(nextByte == 0x01) // Text Event
- {
- cout << " TextEvent: ";
- fileRead(ifs, nextByte); // contains length
- int stringLen = nextByte;
- for(int j = 0; j < stringLen; j++)
- {
- fileRead(ifs, nextByte);
- cout << nextByte;
- }
- cout << endl;
- }
- else if(nextByte == 0x03) // track name
- {
- fileRead(ifs, nextByte); // this byte contains size/length of track name which follows
- int cbTrackName = nextByte;
- char *trackName = new char[cbTrackName+1];
- trackName[cbTrackName] = '\0';
- for(int j=0; j < cbTrackName; j++)
- {
- fileRead(ifs, nextByte);
- trackName[j] = nextByte;
- }
- strcpy_s(midiTrack.trackName, trackName);
- cout << " TrackName #" << dec << i << " Name: " << midiTrack.trackName << endl;
- delete[] trackName;
- }
- else if(nextByte == 0x2F) // end of track
- {
- cout << " EndOfTrack" << endl;
- endOfTrack = true;
- ifs.ignore(1);
- }
- else
- {
- cout << " Unknown MetaEvent (0x" << uppercase << hex << static_cast<int>(nextByte);
- fileRead(ifs, nextByte);
- cout << "): skip " << dec << static_cast<int>(nextByte) << " Bytes" << endl;
- ifs.ignore(nextByte);
- }
- continue;
- }
- 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
- {
- cout << " SysEx Message" << endl;
- fileRead(ifs, nextByte);
- while(nextByte != 0xF7)
- {
- fileRead(ifs, nextByte);
- }
- continue;
- }
- else // MIDI Event
- {
- // Get the status byte. If the next byte has the most significant bit set, its a status event, so update the runningState variable,
- // else, it's data, so we use the state saved in runningState.
- cout << " MidiEvent: 0x" << hex << uppercase << static_cast<int>(nextByte) << endl;
- if(runningState == 0 || (nextByte >= 0x80))
- {
- runningState = nextByte;
- }
- midiEvent.status = runningState;
- fileRead(ifs, nextByte);
- midiEvent.pitch = nextByte;
- fileRead(ifs, nextByte);
- midiEvent.volume = nextByte;
- midiTrack.events.push_back(midiEvent);
- }
- }
- cout << "Track read, next one coming right up!" << endl;
- outTracks->push_back(midiTrack);
- }
- ifs.close();
- cout << outTracks->size() << " tracks read." << endl;
- }
- template <class T>
- void endswap(T &objp)
- {
- unsigned char *memp = reinterpret_cast<unsigned char*>(&objp);
- std::reverse(memp, memp + sizeof(T));
- }
- template <class T>
- void fileRead(std::ifstream &ifstr, T &outVar)
- {
- ifstr.read(reinterpret_cast<char*>(&outVar), sizeof(outVar));
- endswap(outVar);
- }
Advertisement
Add Comment
Please, Sign In to add comment