jdkoftinoff / jdksmidi

c++ MIDI library
http://github.com/jdkoftinoff/jdksmidi/wiki
Other
116 stars 34 forks source link

Is there already a method to get note length/duration? #4

Open cerupcat opened 11 years ago

cerupcat commented 11 years ago

Before I dive into creating my own method, I wanted to double check if there was already a way to get note length/durations when parsing or using the sequencer.

If that's not possible, is there a recommended way to implement this? Ideally I'd like to sequence a midi file and get the note lengths as it's playing back so I can visualize the length on screen.

jdkoftinoff commented 11 years ago

I had this in an ancient version of the library, and it looks like this method got dropped by accident. This code is from 1993 and may not integrate directly but the algorithm was used to find note lengths to create sheet music display:


ulong   MIDITrack::GetNoteGateTime( unsigned int event ) const
{
    ulong    gate=0;
    ulong    start=0;
    uchar   channel, note;

    // make sure buffer is valid and event is within it.

    if( !buffer || event>last_event )
        return 0;

    // make sure that event is a note ON message

    if( buffer[event].GetStatus() != M_NOTE_ON )
        return 0;

    // make sure that event has velocity>0

    if( buffer[event].GetVelocity() == 0 )
        return 0;

    start=buffer[event].GetTime();
    channel= buffer[event].GetChannel();
    note= buffer[event].GetNote();

    while( event<last_event-1 )
    {
        event++;

        if( buffer[event].GetChannel() == channel )
        {
            if( buffer[event].GetNote()==note )
            {
                if( (buffer[event].GetStatus()==M_NOTE_OFF )
                    || (buffer[event].GetStatus()==M_NOTE_ON
                    && buffer[event].GetVelocity()==0 )
                   )
                {
                    // we found the matching note off!
                    gate=buffer[event].GetTime()-start;

                    return gate;
                }
            }
        }

    }
    return 0;
}
cerupcat commented 11 years ago

Thanks for the quick response Jeff, I really appreciate it. I'll give it a test today and make any necessary adjustments. If there's any major changes, i'll post back with my result.