void convertMidiFile(MidiFile& midifile, vector<vector<double> >& matlab) { midifile.absoluteTicks(); midifile.linkNotePairs(); midifile.joinTracks(); midifile.doTimeAnalysis(); vector<double> parameters(8); // 1: beat on time // 2: beat duration // 3: channel // 4: pitch # // 5: velocity // 6: start time (seconds) // 7: duration (seconds) // 8: track double tpq = midifile.getTicksPerQuarterNote(); double beatstart; double beatdur; double starttime; double duration; double channel; double key; double velocity; double track; for (int i=0; i<midifile[0].size(); i++) { if (!midifile[0][i].isNoteOn()) { continue; } beatstart = midifile[0][i].tick / tpq; beatdur = midifile[0][i].getTickDuration() / tpq; starttime = midifile.getTimeInSeconds(0, i); duration = midifile[0][i].getDurationInSeconds(); channel = midifile[0][i].getChannelNibble() + 1; key = midifile[0][i][1]; velocity = midifile[0][i][2]; track = midifile[0][i].track + 1; parameters[0] = beatstart; parameters[1] = beatdur; parameters[2] = channel; parameters[3] = key; parameters[4] = velocity; parameters[5] = starttime; parameters[6] = duration; parameters[7] = track; matlab.push_back(parameters); } }
double getTotalDuration(MidiFile& midifile) { midifile.doTimeAnalysis(); midifile.joinTracks(); return midifile[0].last().seconds; }
void convertMidiFile(MidiFile& midifile, vector<vector<double> >& matlab) { midifile.absoluteTicks(); midifile.joinTracks(); if (secQ || msecQ) { midifile.doTimeAnalysis(); } vector<double> event(7); vector<double> ontimes(128); vector<int> onvelocities(128); int i; for (i=0; i<128; i++) { ontimes[i] = -1.0; onvelocities[i] = -1; } double offtime = 0.0; int key = 0; int vel = 0; int command = 0; if (verboseQ) { cout << "-1\ttpq\t" << midifile.getTicksPerQuarterNote() << endl; } for (i=0; i<midifile.getNumEvents(0); i++) { event.assign(event.size(), unused); command = midifile[0][i][0] & 0xf0; if (command == 0xf0) { command = midifile[0][i][0]; } if (command == 0x90 && midifile[0][i][2] != 0) { // store note-on velocity and time key = midifile[0][i][1]; vel = midifile[0][i][2]; ontimes[key] = getTime(midifile[0][i].tick, midifile); onvelocities[key] = vel; } else if (command == 0x90 || command == 0x80) { // note off command write to output key = midifile[0][i][1]; offtime = getTime(midifile[0][i].tick, midifile); legend_opcode[OP_NOTE/1000] = 1; if (verboseQ) { cout << ontimes[key] << "\tnote" << "\tdur=" << offtime - ontimes[key] << "\tpch=" << key << "\tvel=" << onvelocities[key] << "\tch=" << (midifile[0][i][0] & 0x0f) << "\ttrack=" << midifile[0][i].track << endl; } else { event[0] = ontimes[key]; event[1] = OP_NOTE; event[2] = offtime - ontimes[key]; event[3] = key; event[4] = onvelocities[key]; event[5] = (midifile[0][i][0] & 0x0f); event[6] = midifile[0][i].track; } } else if (command == 0xb0) { legend_controller[midifile[0][i][1]] = 1; legend_opcode[OP_CONTROL/1000] = 1; if (verboseQ) { cout << getTime(midifile[0][i].tick, midifile) << "\tcontrol" << "\ttype=" << (int)midifile[0][i][1] << "\tval=" << (int)midifile[0][i][2] << "\tch=" << (midifile[0][i][0] & 0x0f) << "\ttrack=" << midifile[0][i].track << "\n"; } else { event[0] = getTime(midifile[0][i].tick, midifile); event[1] = OP_CONTROL; event[2] = (int)midifile[0][i][1]; event[3] = (int)midifile[0][i][2]; event[5] = (midifile[0][i][0] & 0x0f); event[6] = midifile[0][i].track; } } else if (command == 0xc0) { legend_instr[midifile[0][i][1]] = 1; legend_opcode[OP_INSTR/1000] = 1; if (verboseQ) { cout << getTime(midifile[0][i].tick, midifile) << "\tinstr" << "\tname=" << GMinstrument[midifile[0][i][1]] << "\tnum=" << (int)midifile[0][i][1] << "\tch=" << (midifile[0][i][0] & 0x0f) << "\ttrack=" << midifile[0][i].track << "\n"; } else { event[0] = getTime(midifile[0][i].tick, midifile); event[1] = OP_INSTR; event[2] = (int)midifile[0][i][1]; event[5] = (midifile[0][i][0] & 0x0f); event[6] = midifile[0][i].track; } } else if (command == 0xff) { if (verboseQ) { cout << getTime(midifile[0][i].tick, midifile) << "\t"; } else { event[0] = getTime(midifile[0][i].tick, midifile); } processMetaEvent(midifile, i, event); if (verboseQ) { cout << "\n"; } } /* no longer needed // check for tempo indication if (midifile[0][i][0] == 0xff && midifile[0][i][1] == 0x51) { setTempo(midifile, i, tempo); } */ if (event[1] != unused) { matlab.push_back(event); } } }
int main(int argc, char** argv) { options.setOptions(argc, argv); checkOptions(options); int status; MidiFile midifile; if (options.getArgCount()) { status = midifile.read(options.getArg(1)); } else { status = midifile.read(cin); } if (status == 0) { cerr << "Error: could not read MIDI file" << endl; exit(1); } int tpq = midifile.getTicksPerQuarterNote(); midifile.linkNotePairs(); if (joinQ) { midifile.joinTracks(); } MidiEvent* mev; double duration; if (secondsQ) { midifile.doTimeAnalysis(); } if (secondsQ) { cout << "SEC\tDUR\tTRACK\tNOTE\n"; } else if (quarterQ) { cout << "QTRS\tDUR\tTRACK\tNOTE\n"; } else { cout << "TICKS\tDUR\tTRACK\tNOTE\n"; } cout << "============================\n"; for (int track=0; track < midifile.getTrackCount(); track++) { for (int i=0; i<midifile[track].size(); i++) { mev = &midifile[track][i]; if (!mev->isNoteOn()) { continue; } if (secondsQ) { duration = mev->getDurationInSeconds(); } else { duration = mev->getTickDuration(); } if (secondsQ) { cout << mev->seconds << '\t'; cout << duration << '\t'; } else if (quarterQ) { cout << mev->tick/tpq << '\t'; cout << duration/tpq << '\t'; } else { cout << mev->tick << '\t'; cout << duration << '\t'; } cout << mev->track << '\t'; cout << mev->getKeyNumber(); cout << endl; } if (midifile.getTrackCount() > 1) { cout << endl; } } return 0; }