MidiReader::TempoReaderImpl::TempoReaderImpl(MidiFile const & file) : timeFormat_(file.getTimeFormat()) , current_(0) { file.findAllTempoEvents(sequence_); count_ = sequence_.getNumEvents(); }
void setTempo(MidiFile& midifile, int index, double& tempo) { double newtempo = 0.0; static int count = 0; count++; vector<double> event; event.assign(7, unused); MidiEvent& mididata = midifile[0][index]; int microseconds = 0; microseconds = microseconds | (mididata[3] << 16); microseconds = microseconds | (mididata[4] << 8); microseconds = microseconds | (mididata[5] << 0); newtempo = 60.0 / microseconds * 1000000.0; if (count <= 1) { tempo = newtempo; } else if (tempo != newtempo) { if (verboseQ) { cout << getTime(midifile[0][index].tick, tempo, midifile.getTicksPerQuarterNote()) << "\t" << "tempo\t" << newtempo << endl; } else { legend_opcode[OP_TEMPO/1000] = 1; event[0] = getTime(midifile[0][index].tick, tempo, midifile.getTicksPerQuarterNote()); event[1] = OP_TEMPO; event[2] = newtempo; matlabarray.push_back(event); } } tempo = newtempo; }
void printMidiHeader(MidiFile& midifile) { // print MIDI file header marker cout << "+M +T +h +d\t\t\t; MIDI file header chunk marker" << endl; // print the number of bytes in the MIDI file to follow (always 6): cout << "4'6\t\t\t\t; bytes in header to follow" << endl; // print the format (0 = single track, 1 = multitrack) // The MidiFile class does not exactly keep track of this value. // It will presume that a single track file is a type-0 MIDI file // (type-1 MIDI files can theoretically have a single track, but not // usually). if (midifile.getTrackCount() == 0) { cout << "2'0\t\t\t\t; format: Type-0 (single track)"; } else { cout << "2'1\t\t\t\t; format: Type-1 (multi-track)"; } cout << endl; // print track count cout << "2'" << midifile.getTrackCount(); cout << "\t\t\t\t; track count: "; cout << midifile.getTrackCount() << " track"; if (midifile.getTrackCount() != 1) { cout << "s"; } cout << endl; // print the ticks per quarter note. The ticks per quarter note // can be SMPTE or regular. Assuming regular at the moment. int ticks = midifile.getTicksPerQuarterNote(); cout << "2'" << ticks << "\t\t\t\t; ticks per quarter note" << endl; }
int main(int argc, char **argv) { MIDI_io midi_io; // PortMidi wrapper instance PmEvent event; // PortMidi event MidiFile midifile; char midiEvent[3]; std::string filename="uncomposer.midi"; bool event_read; int input_device=0; bool use_default_devices=false; unsigned long prev_timestamp=0,new_timestamp=0,delay=0; unsigned char cmd,data1,data2; midi_io.list_devices(); midifile.set_bpm(120); if(argc>1) filename=argv[1]; midifile.open(filename.c_str()); if(!use_default_devices){ std::cout << "\nGive input device number: "; cin >> input_device; midi_io.set_input_device(input_device); }
bool VGMSeq::SaveAsMidi(const std::wstring & filepath) { MidiFile* midi = this->ConvertToMidi(); if (!midi) return false; bool result = midi->SaveMidiFile(filepath); delete midi; return result; }
void printAscii(MidiFile& file, Options& options) { if (options.getBoolean("hex")) { file.writeHex(cout); } else if (options.getBoolean("no-comments")) { file.writeBinasc(cout); } else { cout << file; } }
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; }
void convertMidiFile(MidiFile& midifile) { if (type0Q) { midifile.joinTracks(); } midifile.deltaTicks(); printMidiHeader(midifile); int trackcount = midifile.getTrackCount(); for (int i=0; i<trackcount; i++) { printTrack(midifile, i); } }
void MidiStorage::readMidiData(const char *filePath) { File *filePtr = new File (filePath); bool fileExists = filePtr->existsAsFile(); int NumEvents; if (fileExists) { ScopedPointer<FileInputStream> fileInputStream = filePtr->createInputStream(); //delete the stream! MidiFile fileMIDI; const MidiMessageSequence *midiSequence; MidiMessageSequence::MidiEventHolder *midiEvent; fileMIDI.readFrom(*fileInputStream); // read the MIDI file fileMIDI.convertTimestampTicksToSeconds(); midiSequence = fileMIDI.getTrack(1); //Track indices start from 1; Also we want only one track to exist NumEvents = midiSequence->getNumEvents(); noteNumber = new float[NumEvents]; timeStamps = new float[NumEvents]; for (int i=0; i<NumEvents; i++) { midiEvent = midiSequence->getEventPointer(i); //Make sure the index doesn't exceed your no. of timeStamps. It starts from 0. if ( midiEvent->message.isNoteOn() ) { noteNumber[i] = midiEvent->message.getNoteNumber(); timeStamps[i] = midiEvent->message.getTimeStamp(); } else { noteNumber[i] = -1.f; timeStamps[i] = midiEvent->message.getTimeStamp(); } } } else { Logger::writeToLog("Error in Reading Midi File - It doesn't exist"); } midiLen = NumEvents; //std::cout<<"noteNumber: "<<midiLen<<std::endl; FileRW::fileWrite( noteNumber, NumEvents, (char *)"/Users/Rithesh/Documents/Learn C++/ASE/notes/Matlab_ASE/midiIn.txt"); FileRW::fileWrite( timeStamps, NumEvents, (char *)"/Users/Rithesh/Documents/Learn C++/ASE/notes/Matlab_ASE/tStmpIn.txt"); }
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; }
int getTrackByteCount(MidiFile& midifile, int track) { int sum = 0; int i; int eventcount = midifile.getEventCount(track); MidiEvent event; for (i=0; i<eventcount; i++) { event = midifile.getEvent(track, i); sum += getVlvSize(event.tick); sum += event.size(); } return sum; }
double getTime(int ticks, MidiFile& midifile) { int tpq = midifile.getTicksPerQuarterNote(); switch (timetype) { case TICK: return ticks; case BEAT: return (double)ticks/tpq; case SEC: return midifile.getTimeInSeconds(ticks); case MSEC: return 1000 * midifile.getTimeInSeconds(ticks); } return 0.0; }
void AddDrumTrack(MidiFile& midifile, int* data, int instrument, int ticks) { vector<uchar> midievent; // temporary storage for MIDI events midievent.resize(3); // set the size of the array to 3 bytes midievent[2] = 64; // set the loudness to a constant value int notestate = 0; // 0 = off, 1 = on int i = 0; int actiontime; int track = midifile.addTrack(); // Add a track to the MIDI file while (data[i] >= 0) { switch (data[i]) { case 'x': case 'X': if (notestate) { // turn off previous note midievent[0] = 0x89; midievent[1] = instrument; actiontime = ticks * i - 1; midifile.addEvent(track, actiontime, midievent); } // turn on current note midievent[0] = 0x99; midievent[1] = instrument; actiontime = ticks * i; midifile.addEvent(track, actiontime, midievent); notestate = 1; break; case '0': case 'o': case 'O': // turn off previous note if (notestate) { midievent[0] = 0x89; midievent[1] = instrument; actiontime = ticks * i - 1; midifile.addEvent(track, actiontime, midievent); notestate = 0; } break; } i++; } if (notestate) { // turn off last note midievent[0] = 0x89; midievent[1] = instrument; actiontime = ticks * i; midifile.addEvent(track, actiontime, midievent); } }
int main(int argc, char* argv[]) { checkOptions(options, argc, argv); HumdrumFile infile; infile.read(options.getArg(1)); MidiFile midifile; createMidiFile(midifile, infile); if (strcmp(filename, "") == 0) { cout << midifile; } else { midifile.write(filename); } return 0; }
int main(int argc, char** argv) { int status; MidiFile inputfile; Options options(argc, argv); checkOptions(options); status = inputfile.read(options.getArg(1)); if (status == 0) { cout << "Syntax error in file: " << options.getArg(1) << "\n"; } processMidiFile(inputfile); return 0; }
static void findTempoEvents (MidiFile& midiFile, StringPairArray& midiMetadata) { MidiMessageSequence tempoEvents; midiFile.findAllTempoEvents (tempoEvents); const int numTempoEvents = tempoEvents.getNumEvents(); MemoryOutputStream tempoSequence; for (int i = 0; i < numTempoEvents; ++i) { const double tempo = getTempoFromTempoMetaEvent (tempoEvents.getEventPointer (i)); if (tempo > 0.0) { if (i == 0) midiMetadata.set (CoreAudioFormat::tempo, String (tempo)); if (numTempoEvents > 1) tempoSequence << String (tempo) << ',' << tempoEvents.getEventTime (i) << ';'; } } if (tempoSequence.getDataSize() > 0) midiMetadata.set ("tempo sequence", tempoSequence.toUTF8()); }
static void findTimeSigEvents (MidiFile& midiFile, StringPairArray& midiMetadata) { MidiMessageSequence timeSigEvents; midiFile.findAllTimeSigEvents (timeSigEvents); const int numTimeSigEvents = timeSigEvents.getNumEvents(); MemoryOutputStream timeSigSequence; for (int i = 0; i < numTimeSigEvents; ++i) { int numerator, denominator; timeSigEvents.getEventPointer(i)->message.getTimeSignatureInfo (numerator, denominator); String timeSigString; timeSigString << numerator << '/' << denominator; if (i == 0) midiMetadata.set (CoreAudioFormat::timeSig, timeSigString); if (numTimeSigEvents > 1) timeSigSequence << timeSigString << ',' << timeSigEvents.getEventTime (i) << ';'; } if (timeSigSequence.getDataSize() > 0) midiMetadata.set ("time signature sequence", timeSigSequence.toUTF8()); }
static void findKeySigEvents (MidiFile& midiFile, StringPairArray& midiMetadata) { MidiMessageSequence keySigEvents; midiFile.findAllKeySigEvents (keySigEvents); const int numKeySigEvents = keySigEvents.getNumEvents(); MemoryOutputStream keySigSequence; for (int i = 0; i < numKeySigEvents; ++i) { const MidiMessage& message (keySigEvents.getEventPointer (i)->message); const int key = jlimit (0, 14, message.getKeySignatureNumberOfSharpsOrFlats() + 7); const bool isMajor = message.isKeySignatureMajorKey(); static const char* majorKeys[] = { "Cb", "Gb", "Db", "Ab", "Eb", "Bb", "F", "C", "G", "D", "A", "E", "B", "F#", "C#" }; static const char* minorKeys[] = { "Ab", "Eb", "Bb", "F", "C", "G", "D", "A", "E", "B", "F#", "C#", "G#", "D#", "A#" }; String keySigString (isMajor ? majorKeys[key] : minorKeys[key]); if (! isMajor) keySigString << 'm'; if (i == 0) midiMetadata.set (CoreAudioFormat::keySig, keySigString); if (numKeySigEvents > 1) keySigSequence << keySigString << ',' << keySigEvents.getEventTime (i) << ';'; } if (keySigSequence.getDataSize() > 0) midiMetadata.set ("key signature sequence", keySigSequence.toUTF8()); }
void PianoPanel::MIDIExtraValidateWindow() { wxString notes = Choice_Piano_Notes_Source->GetStringSelection(); if (notes == "MIDI File") { wxString file = TextCtrl_Piano_File->GetValue(); MidiFile midifile; if (file == "" || !wxFile::Exists(file) || midifile.read(file) == 0) { // invalid midi file TextCtrl_Piano_File->SetBackgroundColour(*wxRED); Choice_Piano_MIDITrack_APPLYLAST->Enable(false); BitmapButton_Piano_MIDITrack_APPLYLAST->Enable(false); } else { std::string selection = std::string(Choice_Piano_MIDITrack_APPLYLAST->GetStringSelection().c_str()); int tracks = midifile.getNumTracks(); Choice_Piano_MIDITrack_APPLYLAST->Clear(); Choice_Piano_MIDITrack_APPLYLAST->Append("All"); if (selection == "All") { Choice_Piano_MIDITrack_APPLYLAST->Select(0); } for (int i = 1; i <= tracks; i++) { if (MIDITrackContainsNotes(i-1, &midifile)) { std::string n = std::string(wxString::Format("%i", i).c_str()); Choice_Piano_MIDITrack_APPLYLAST->Append(n); if (selection == n) { Choice_Piano_MIDITrack_APPLYLAST->Select(i); } } } if (Choice_Piano_MIDITrack_APPLYLAST->GetStringSelection() == "") { Choice_Piano_MIDITrack_APPLYLAST->Select(0); } Choice_Piano_MIDITrack_APPLYLAST->Enable(true); BitmapButton_Piano_MIDITrack_APPLYLAST->Enable(true); } } }
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 getNoteOnDeltas(vector<int>& noteondeltas, MidiFile& midifile) { int i; for (i=0; i<midifile.getNumTracks(); i++) { addNoteOnEvents(noteondeltas, midifile, i); } // sort note ons here. sortArray(noteondeltas); }
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; }
void printLegend(MidiFile& midifile) { cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"; cout << "%% DATA LEGEND %%\n"; cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"; cout << "%%Filename: " << midifile.getFilename() << endl; cout << "%%Ticks per quarter note: " << midifile.getTicksPerQuarterNote() << "\n"; cout << "%%" << endl; cout << "%% Meaning of columns:" << endl; cout << "%%(1) note start in beats (quarter notes)." << endl; cout << "%%(2) note duration in beats (quarter notes)." << endl; cout << "%%(3) MIDI channel (indexed from 1)." << endl; cout << "%%(4) MIDI pitch (60 = C4)" << endl; cout << "%%(5) MIDI velocity (60 = C4)" << endl; cout << "%%(6) note start in seconds." << endl; cout << "%%(7) note duration in seconds." << endl; cout << "%%(8) MIDI file track." << endl; cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"; }
void printMidiAsSkini(MidiFile& midifile) { int tpq = midifile.getTicksPerQuarterNote(); int oldticks = 0; // absolute ticks of last event int ticks = 0; // absolute ticks of current event double tempo = 120.0; // time units will be in seconds MidiEvent event; // temporary event for printing double curtime = 0.0; // current time in seconds int i; for (i=0; i<midifile.getNumEvents(0); i++) { oldticks = ticks; event = midifile.getEvent(0, i); ticks = event.tick; if (i>0) { curtime += (ticks - oldticks) * 60.0 / tempo / tpq; } processEvent(event, tempo, curtime); } }
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 printTrack(MidiFile& midifile, int track) { cout << endl; // first print track header cout << "+M +T +r +k\t\t\t; Track chunk marker" << endl; // print number of bytes in track int trackbytes = getTrackByteCount(midifile, track); cout << "4'" << trackbytes << "\t\t\t\t; number of bytes to follow in track" << endl; cout << endl; // print the list of events in the track MidiEvent event; int eventcount = midifile.getEventCount(track); for (int i=0; i<eventcount; i++) { event = midifile.getEvent(track, i); printMidiEvent(event); } }
int main(int argc, char** argv) { MidiFile midifile; Options options; checkOptions(options, argc, argv); int filecount = options.getArgCount(); if (filecount == 0) { midifile.read(cin); printAscii(midifile, options); } else { for (int i=1; i<=filecount; i++) { midifile.read(options.getArg(i)); printAscii(midifile, options); if (i < filecount) { cout << "\n\n\n"; } } } return 0; }
int main(int argc, char* argv[]) { checkOptions(options, argc, argv); MidiFile midifile; int numinputs = options.getArgCount(); for (int i=0; i < numinputs || i==0; i++) { midifile.clear(); if (options.getArgCount() < 1) { midifile.read(cin); } else { midifile.read(options.getArg(i+1)); } if (options.getArgCount() > 1) { cout << options.getArg(i+1) << "\t"; } cout << getTotalDuration(midifile); cout << endl; } }
int main(int argc, char* argv[]) { checkOptions(options, argc, argv); MidiFile outfile; outfile.joinTracks(); outfile.deltaTicks(); int i; int initQ = 0; for (i=1; i<=options.getArgCount(); i++) { appendMidi(outfile, options.getArg(i).data(), seconds, initQ++); } // insert an end-of track Meta Event int tpq = outfile.getTicksPerQuarterNote(); MidiEvent mfevent; mfevent.tick = tpq; mfevent.track = 0; mfevent.resize(3); mfevent[0] = 0xff; mfevent[1] = 0x2f; mfevent[2] = 0; outfile.addEvent(mfevent); if (binaryQ) { outfile.write(cout); } else { cout << outfile; } return 0; }
//============================================================================== static StringPairArray parseMidiChunk (InputStream& input, int64 size) { const int64 originalPosition = input.getPosition(); MemoryBlock midiBlock; input.readIntoMemoryBlock (midiBlock, (ssize_t) size); MemoryInputStream midiInputStream (midiBlock, false); StringPairArray midiMetadata; MidiFile midiFile; if (midiFile.readFrom (midiInputStream)) { midiMetadata.set (CoreAudioFormat::midiDataBase64, midiBlock.toBase64Encoding()); findTempoEvents (midiFile, midiMetadata); findTimeSigEvents (midiFile, midiMetadata); } input.setPosition (originalPosition + size); return midiMetadata; }