Home | History | Annotate | Download | only in midi
      1 /*
      2  * Copyright (C) 2015 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.internal.midi;
     18 
     19 /**
     20  * MIDI related constants and static methods.
     21  */
     22 public final class MidiConstants {
     23     public static final byte STATUS_COMMAND_MASK = (byte) 0xF0;
     24     public static final byte STATUS_CHANNEL_MASK = (byte) 0x0F;
     25 
     26     // Channel voice messages.
     27     public static final byte STATUS_NOTE_OFF = (byte) 0x80;
     28     public static final byte STATUS_NOTE_ON = (byte) 0x90;
     29     public static final byte STATUS_POLYPHONIC_AFTERTOUCH = (byte) 0xA0;
     30     public static final byte STATUS_CONTROL_CHANGE = (byte) 0xB0;
     31     public static final byte STATUS_PROGRAM_CHANGE = (byte) 0xC0;
     32     public static final byte STATUS_CHANNEL_PRESSURE = (byte) 0xD0;
     33     public static final byte STATUS_PITCH_BEND = (byte) 0xE0;
     34 
     35     // System Common Messages.
     36     public static final byte STATUS_SYSTEM_EXCLUSIVE = (byte) 0xF0;
     37     public static final byte STATUS_MIDI_TIME_CODE = (byte) 0xF1;
     38     public static final byte STATUS_SONG_POSITION = (byte) 0xF2;
     39     public static final byte STATUS_SONG_SELECT = (byte) 0xF3;
     40     public static final byte STATUS_TUNE_REQUEST = (byte) 0xF6;
     41     public static final byte STATUS_END_SYSEX = (byte) 0xF7;
     42 
     43     // System Real-Time Messages
     44     public static final byte STATUS_TIMING_CLOCK = (byte) 0xF8;
     45     public static final byte STATUS_START = (byte) 0xFA;
     46     public static final byte STATUS_CONTINUE = (byte) 0xFB;
     47     public static final byte STATUS_STOP = (byte) 0xFC;
     48     public static final byte STATUS_ACTIVE_SENSING = (byte) 0xFE;
     49     public static final byte STATUS_RESET = (byte) 0xFF;
     50 
     51     /** Number of bytes in a message nc from 8c to Ec */
     52     public final static int CHANNEL_BYTE_LENGTHS[] = { 3, 3, 3, 3, 2, 2, 3 };
     53 
     54     /** Number of bytes in a message Fn from F0 to FF */
     55     public final static int SYSTEM_BYTE_LENGTHS[] = { 1, 2, 3, 2, 1, 1, 1, 1, 1,
     56             1, 1, 1, 1, 1, 1, 1 };
     57 
     58 
     59     /**
     60      * MIDI messages, except for SysEx, are 1,2 or 3 bytes long.
     61      * You can tell how long a MIDI message is from the first status byte.
     62      * Do not call this for SysEx, which has variable length.
     63      * @param statusByte
     64      * @return number of bytes in a complete message or zero if data byte passed
     65      */
     66     public static int getBytesPerMessage(byte statusByte) {
     67         // Java bytes are signed so we need to mask off the high bits
     68         // to get a value between 0 and 255.
     69         int statusInt = statusByte & 0xFF;
     70         if (statusInt >= 0xF0) {
     71             // System messages use low nibble for size.
     72             return SYSTEM_BYTE_LENGTHS[statusInt & 0x0F];
     73         } else if(statusInt >= 0x80) {
     74             // Channel voice messages use high nibble for size.
     75             return CHANNEL_BYTE_LENGTHS[(statusInt >> 4) - 8];
     76         } else {
     77             return 0; // data byte
     78         }
     79     }
     80 
     81 
     82     /**
     83      * @param msg
     84      * @param offset
     85      * @param count
     86      * @return true if the entire message is ActiveSensing commands
     87      */
     88     public static boolean isAllActiveSensing(byte[] msg, int offset,
     89             int count) {
     90         // Count bytes that are not active sensing.
     91         int goodBytes = 0;
     92         for (int i = 0; i < count; i++) {
     93             byte b = msg[offset + i];
     94             if (b != MidiConstants.STATUS_ACTIVE_SENSING) {
     95                 goodBytes++;
     96             }
     97         }
     98         return (goodBytes == 0);
     99     }
    100 
    101     // Returns true if this command can be used for running status
    102     public static boolean allowRunningStatus(byte command) {
    103         // only Channel Voice and Channel Mode commands can use running status
    104         return (command >= STATUS_NOTE_OFF && command < STATUS_SYSTEM_EXCLUSIVE);
    105     }
    106 
    107     // Returns true if this command cancels running status
    108     public static boolean cancelsRunningStatus(byte command) {
    109         // System Common messages cancel running status
    110         return (command >= STATUS_SYSTEM_EXCLUSIVE && command <= STATUS_END_SYSEX);
    111     }
    112 }
    113