    001
    002package ibxm;
    003
    004public class Pattern {
    005    public int num_rows;
    006
    007    private int data_offset, note_index;
    008    private byte[] pattern_data;
    009
    010    public Pattern() {
    011        num_rows = 1;
    012        set_pattern_data( new byte[ 0 ] );
    013    }
    014
    015    public void set_pattern_data( byte[] data ) {
    016        if( data != null ) {
    017            pattern_data = data;
    018        }
    019        data_offset = 0;
    020        note_index = 0;
    021    }
    022
    023    public void get_note( int[] note, int index ) {
    024        if( index < note_index ) {
    025            note_index = 0;
    026            data_offset = 0;
    027        }
    028        while( note_index <= index ) {
    029            data_offset = next_note( data_offset, note );
    030            note_index += 1;
    031        }
    032    }
    033
    034    public int next_note( int data_offset, int[] note ) {
    035        int bitmask, field;
    036        if( data_offset < 0 ) {
    037            data_offset = pattern_data.length;
    038        }
    039        bitmask = 0x80;
    040        if( data_offset < pattern_data.length ) {
    041            bitmask = pattern_data[ data_offset ] & 0xFF;
    042        }
    043        if( ( bitmask & 0x80 ) == 0x80 ) {
    044            data_offset += 1;
    045        } else {
    046            bitmask = 0x1F;
    047        }
    048        for( field = 0; field < 5; field++ ) {
    049            note[ field ] = 0;
    050            if( ( bitmask & 0x01 ) == 0x01 ) {
    051                if( data_offset < pattern_data.length ) {
    052                    note[ field ] = pattern_data[ data_offset ] & 0xFF;
    053                    data_offset += 1;
    054                }
    055            }
    056            bitmask = bitmask >> 1;
    057        }
    058        return data_offset;
    059    }
    060}
    061