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.example.android.common.midi;
     18 
     19 /**
     20  * MIDI related constants and static methods.
     21  * These values are defined in the MIDI Standard 1.0
     22  * available from the MIDI Manufacturers Association.
     23  */
     24 public class MidiConstants {
     25     protected final static String TAG = "MidiTools";
     26     public static final byte STATUS_COMMAND_MASK = (byte) 0xF0;
     27     public static final byte STATUS_CHANNEL_MASK = (byte) 0x0F;
     28 
     29     // Channel voice messages.
     30     public static final byte STATUS_NOTE_OFF = (byte) 0x80;
     31     public static final byte STATUS_NOTE_ON = (byte) 0x90;
     32     public static final byte STATUS_POLYPHONIC_AFTERTOUCH = (byte) 0xA0;
     33     public static final byte STATUS_CONTROL_CHANGE = (byte) 0xB0;
     34     public static final byte STATUS_PROGRAM_CHANGE = (byte) 0xC0;
     35     public static final byte STATUS_CHANNEL_PRESSURE = (byte) 0xD0;
     36     public static final byte STATUS_PITCH_BEND = (byte) 0xE0;
     37 
     38     // System Common Messages.
     39     public static final byte STATUS_SYSTEM_EXCLUSIVE = (byte) 0xF0;
     40     public static final byte STATUS_MIDI_TIME_CODE = (byte) 0xF1;
     41     public static final byte STATUS_SONG_POSITION = (byte) 0xF2;
     42     public static final byte STATUS_SONG_SELECT = (byte) 0xF3;
     43     public static final byte STATUS_TUNE_REQUEST = (byte) 0xF6;
     44     public static final byte STATUS_END_SYSEX = (byte) 0xF7;
     45 
     46     // System Real-Time Messages
     47     public static final byte STATUS_TIMING_CLOCK = (byte) 0xF8;
     48     public static final byte STATUS_START = (byte) 0xFA;
     49     public static final byte STATUS_CONTINUE = (byte) 0xFB;
     50     public static final byte STATUS_STOP = (byte) 0xFC;
     51     public static final byte STATUS_ACTIVE_SENSING = (byte) 0xFE;
     52     public static final byte STATUS_RESET = (byte) 0xFF;
     53 
     54     /** Number of bytes in a message nc from 8c to Ec */
     55     public final static int CHANNEL_BYTE_LENGTHS[] = { 3, 3, 3, 3, 2, 2, 3 };
     56 
     57     /** Number of bytes in a message Fn from F0 to FF */
     58     public final static int SYSTEM_BYTE_LENGTHS[] = { 1, 2, 3, 2, 1, 1, 1, 1, 1,
     59             1, 1, 1, 1, 1, 1, 1 };
     60 
     61     /**
     62      * MIDI messages, except for SysEx, are 1,2 or 3 bytes long.
     63      * You can tell how long a MIDI message is from the first status byte.
     64      * Do not call this for SysEx, which has variable length.
     65      * @param statusByte
     66      * @return number of bytes in a complete message, zero if data byte passed
     67      */
     68     public static int getBytesPerMessage(byte statusByte) {
     69         // Java bytes are signed so we need to mask off the high bits
     70         // to get a value between 0 and 255.
     71         int statusInt = statusByte & 0xFF;
     72         if (statusInt >= 0xF0) {
     73             // System messages use low nibble for size.
     74             return SYSTEM_BYTE_LENGTHS[statusInt & 0x0F];
     75         } else if(statusInt >= 0x80) {
     76             // Channel voice messages use high nibble for size.
     77             return CHANNEL_BYTE_LENGTHS[(statusInt >> 4) - 8];
     78         } else {
     79             return 0; // data byte
     80         }
     81     }
     82 
     83     /**
     84      * @param msg
     85      * @param offset
     86      * @param count
     87      * @return true if the entire message is ActiveSensing commands
     88      */
     89     public static boolean isAllActiveSensing(byte[] msg, int offset,
     90             int count) {
     91         // Count bytes that are not active sensing.
     92         int goodBytes = 0;
     93         for (int i = 0; i < count; i++) {
     94             byte b = msg[offset + i];
     95             if (b != MidiConstants.STATUS_ACTIVE_SENSING) {
     96                 goodBytes++;
     97             }
     98         }
     99         return (goodBytes == 0);
    100     }
    101 
    102 }
    103