int main ( int argc, char **argv )
{
#ifdef WIN32
  if ( argc>1 )
  {
    MIDIFileReadStreamFile rs ( argv[1] );
    MIDIMultiTrack tracks ( 64 );
    MIDIFileReadMultiTrack track_loader ( &tracks );
    MIDIFileRead reader ( &rs, &track_loader );
    MIDISequencerGUIEventNotifierText gui ( stdout );
    MIDISequencer seq ( &tracks, &gui );
    MIDIDriverWin32 driver ( 128 );
    MIDIManager mgr ( &driver, &gui );
    
    reader.Parse();
    
    driver.StartTimer ( 20 );
    driver.OpenMIDIOutPort ( MIDI_MAPPER );
    
    seq.GoToZero();
    mgr.SetSeq ( &seq );
    
    mgr.SetTimeOffset ( timeGetTime() );
    mgr.SeqPlay();
    
    getchar();
    mgr.SeqStop();
  }
  
  return 0;
#endif
}
void ofxThreadedMidiPlayer::init(){
    if(!bIsInited){
        isReady = false;
        string filePath = ofToDataPath(midiFileName, true);
        
        MIDIFileReadStreamFile rs ( filePath.c_str() );
        
        if ( !rs.IsValid() )
        {
            ofLogError( "ERROR OPENING FILE AT: ",  filePath);
            
        }
        
        tracks = new MIDIMultiTrack();
        MIDIFileReadMultiTrack track_loader ( tracks );
        MIDIFileRead reader ( &rs, &track_loader );
        
        int numMidiTracks = reader.ReadNumTracks();
        int midiFormat = reader.GetFormat();
        
        tracks->ClearAndResize( numMidiTracks );
        cout << "numMidiTracks: " << numMidiTracks << endl;
        cout << "midiFormat: " << midiFormat << endl;
        
        if ( reader.Parse() ){
            cout << "reader parsed!: " << endl;
        }
        
        
        //MIDISequencer seq( &tracks );
        sequencer = new MIDISequencer ( tracks );//&seq;
        musicDurationInSeconds = sequencer->GetMusicDurationInSeconds();
        
        ofLogVerbose( "musicDurationInSeconds is ", ofToString(musicDurationInSeconds));
        
        midiout=new RtMidiOut();
        if (midiout->getPortCount()){
            midiout->openPort(midiPort);
            ofLogVerbose("Using Port name: " ,   ofToString(midiout->getPortName(0)) );
            //std::cout << "Using Port name: \"" << midiout->getPortName(0)<< "\"" << std::endl;
        }
        
        currentTime = 0.0;
        nextEventTime = 0.0;
        if (!sequencer->GoToTimeMs ( currentTime )){
            ofLogError("Couldn't go to time in sequence: " ,   ofToString(currentTime) );
        }
        if ( !sequencer->GetNextEventTimeMs ( &nextEventTime ) ){
            ofLogVerbose("No next events for sequence", ofToString(nextEventTime));
        }
        max_time = (musicDurationInSeconds *1000.);
        bIsInited = true;
    }
}
int main ( int argc, char **argv )
{
  if ( argc>1 )
  {
    MIDIFileReadStreamFile rs ( argv[1] );
    MIDIMultiTrack tracks;
    MIDIFileReadMultiTrack track_loader ( &tracks );
    MIDIFileRead reader ( &rs, &track_loader );
    
    reader.Parse();
    
    DumpMIDIMultiTrack ( &tracks );
    
  }
  
  return 0;
}
int main ( int argc, char **argv )
{
    if ( argc > 1 )
    {
        MIDIFileReadStreamFile rs ( argv[1] );
        MIDIMultiTrack tracks ( 64 );
        MIDIFileReadMultiTrack track_loader ( &tracks );
        MIDIFileRead reader ( &rs, &track_loader );
//    MIDISequencerGUIEventNotifierText notifier( stdout );
//    MIDISequencer seq( &tracks, &notifier );
        MIDISequencer seq ( &tracks );
        reader.Parse();
        //DumpMIDIMultiTrack( &tracks );
        PlayDumpSequencer ( &seq );
    }

    return 0;
}
bool AdvancedSequencer::Load ( const char *fname )
{
    char realname[1024];
    strcpy ( realname, fname );
    int orignamelen = strlen ( fname );
    chain_mode = false;

    if ( orignamelen > 0 )
    {
        if ( realname[orignamelen-1] == '+' )
        {
            realname[orignamelen-1] = 0;
            chain_mode = true;
        }
    }

    MIDIFileReadStreamFile mfreader_stream ( realname );
    MIDIFileReadMultiTrack track_loader ( &tracks );
    MIDIFileRead reader ( &mfreader_stream, &track_loader );
    Stop();
    driver.AllNotesOff();
    tracks.Clear();
    seq.ResetAllTracks();

    if ( reader.Parse() )
    {
        file_loaded = true;
        Reset();
        GoToMeasure ( 0 );
        ExtractWarpPositions();
    }

    else
    {
        file_loaded = false;
    }

    return file_loaded;
}