int main(int argc, char* argv[]) { checkOptions(options, argc, argv); MidiFile midifile; midifile.read(options.getArg(1)); midifile.absoluteTicks(); midifile.joinTracks(); printMidiAsSkini(midifile); return 0; }
int main(int argc, char** argv) { MidiFile outputfile; // create an empty MIDI file with one track outputfile.absoluteTicks(); // time information stored as absolute time // (will be coverted to delta time when written) outputfile.addTrack(2); // Add another two tracks to the MIDI file vector<uchar> midievent; // temporary storage for MIDI events midievent.resize(3); // set the size of the array to 3 bytes int tpq = 120; // default value in MIDI file is 48 outputfile.setTicksPerQuarterNote(tpq); // data to write to MIDI file: (60 = middle C) // C5 C G G A A G- F F E E D D C- int melody[50] = {72,72,79,79,81,81,79,77,77,76,76,74,74,72,-1}; int mrhythm[50] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2,-1}; // C3 C4 E C F C E C D B3 C4 A3 F G C- int bass[50] = {48,60,64,60,65,60,64,60,62,59,60,57,53,55,48,-1}; int brhythm[50]= { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2,-1}; // store a melody line in track 1 (track 0 left empty for conductor info) int i=0; int actiontime = 0; // temporary storage for MIDI event time midievent[2] = 64; // store attack/release velocity for note command while (melody[i] >= 0) { midievent[0] = 0x90; // store a note on command (MIDI channel 1) midievent[1] = melody[i]; outputfile.addEvent(1, actiontime, midievent); actiontime += tpq * mrhythm[i]; midievent[0] = 0x80; // store a note on command (MIDI channel 1) outputfile.addEvent(1, actiontime, midievent); i++; } // store a base line in track 2 i=0; actiontime = 0; // reset time for beginning of file midievent[2] = 64; while (bass[i] >= 0) { midievent[0] = 0x90; midievent[1] = bass[i]; outputfile.addEvent(2, actiontime, midievent); actiontime += tpq * brhythm[i]; midievent[0] = 0x80; outputfile.addEvent(2, actiontime, midievent); i++; } outputfile.sortTracks(); // make sure data is in correct order outputfile.write("twinkle.mid"); // write Standard MIDI File twinkle.mid return 0; }
void createMidiFile(const char* filename, vector<vector<int> >& sequence) { MidiFile midifile; midifile.absoluteTicks(); midifile.addTrack(1); int tpq = 120; double beat = 0.0; midifile.setTicksPerQuarterNote(tpq); MidiEvent tempo; tempo.setMetaTempo(60.0); tempo.track = 0; tempo.tick = 0; midifile.addEvent(tempo); int maxlen = 0; int i, j; for (i=0; i<(int)sequence.size(); i++) { if ((int)sequence[i].size() > maxlen) { maxlen = sequence[i].size(); } } vector<int> notelist; MidiEvent noteon(0x90, 0, 64); MidiEvent noteoff(0x80, 0, 64); noteon.track = 1; noteoff.track = 1; for (i=0; i<maxlen; i++) { notelist.clear(); for (j=0; j<(int)sequence.size(); j++) { if (i<(int)sequence[j].size()) { notelist.push_back(sequence[j][i]); } } for (j=0; j<(int)notelist.size(); j++) { noteon[1] = 0x7f & notelist[j]; noteoff[1] = 0x7f & notelist[j]; noteon.tick = (int)(beat * tpq + 0.5); noteoff.tick = (int)(beat * tpq + 1 * tpq + 0.5); midifile.addEvent(noteon); midifile.addEvent(noteoff); } beat += 1.0; } midifile.sortTracks(); midifile.write(filename); }
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); } }
int main(int argc, char* argv[]) { checkOptions(options, argc, argv); MidiFile midifile; midifile.read(options.getArg(1)); midifile.absoluteTicks(); vector<int> noteondeltas; noteondeltas.reserve(maxcount); noteondeltas.clear(); midifile.joinTracks(); getNoteOnDeltas(noteondeltas, midifile); if (rawQ) { cout << "// "; printID(noteondeltas); printDeltas(noteondeltas); } else { printID(noteondeltas); } return 0; }
int main(int argc, char** argv) { MidiFile outputfile; // create an empty MIDI file with one track outputfile.absoluteTicks(); // time information stored as absolute time // (will be coverted to delta time when written) outputfile.setTicksPerQuarterNote(QUARTER); int hhdata[50] = {'x', '-', 'x', '-', 'x', '-', 'x', '-', -1}; int sndata[50] = {'-', '-', 'x', '-', '-', '-', 'x', '-', -1}; int bsdata[50] = {'x', '-', '-', '-', 'x', '-', '-', '-', -1}; AddDrumTrack(outputfile, hhdata, HIGH_HAT, SIXTEENTH); AddDrumTrack(outputfile, sndata, SNARE, SIXTEENTH); AddDrumTrack(outputfile, bsdata, BASS_DRUM, SIXTEENTH); outputfile.sortTracks(); // make sure data is in correct order outputfile.write("rhythm.mid"); // write Standard MIDI File twinkle.mid return 0; }
void processMidiFile(MidiFile& midifile) { midifile.absoluteTicks(); midifile.joinTracks(); int i, j; int eventcount = midifile.getEventCount(0); int track; int timeinticks; double timeinsecs; MidiEvent *ptr; int attack; for (i=0; i<eventcount; i++) { ptr = &(midifile[0][i]); track = ptr->track; timeinticks = ptr->tick; timeinsecs = midifile.getTimeInSeconds(0, i); attack = ((*ptr)[0] & 0xf0) == 0x90; if (onsetQ && !attack) { continue; } if (onsetQ && attack) { if ((*ptr)[2] == 0) { continue; } } cout << timeinticks << "\t"; cout << timeinsecs << "\t"; cout << track << "\t"; cout << i << "\t"; for (j=0; j<(int)ptr->size(); j++) { if (j == 0) { cout << "0x" << hex << (int)(*ptr)[j] << dec << " "; } else { cout << (int)(*ptr)[j] << " "; } } cout << endl; } }
void convertMidiFile(MidiFile& midifile, vector<vector<double> >& matlab) { midifile.absoluteTicks(); midifile.joinTracks(); vector<double> event(8); 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; } int channel; for (i=0; i<midifile.getNumEvents(0); i++) { if (verboseQ) { cout << ">>> " << (int)midifile[0][i][0] << "\n"; } event.assign(event.size(), unused); command = midifile[0][i][0] & 0xf0; channel = midifile[0][i][0] & 0x0f; // check for tempo indication if (midifile[0][i][0] == 0xff && midifile[0][i][1] == 0x51) { setTempo(midifile, i, tempo); if (verboseQ) { cout << "# New Tempo: " << tempo << "\n"; } } if ((midifile[0][i][0] & 0x0f) == 0x09) { continue; } 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, tempo, midifile.getTicksPerQuarterNote()); 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, tempo, midifile.getTicksPerQuarterNote()); 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; event[7] = channel; } } 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, tempo, midifile.getTicksPerQuarterNote()) << "\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, tempo, midifile.getTicksPerQuarterNote()); 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, tempo, midifile.getTicksPerQuarterNote()) << "\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, tempo, midifile.getTicksPerQuarterNote()); 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, tempo, midifile.getTicksPerQuarterNote()) << "\t"; } else { event[0] = getTime(midifile[0][i].tick, tempo, midifile.getTicksPerQuarterNote()); } processMetaEvent(midifile, i, event); if (verboseQ) { cout << "\n"; } } if (event[1] != unused) { matlab.push_back(event); } } }