MidiReceiver.java revision 81b9f7d325a552c54e793b51f571ae3d65b26e94
1/* 2 * Copyright (C) 2014 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 17package android.media.midi; 18 19import java.io.IOException; 20 21/** 22 * Interface for sending and receiving data to and from a MIDI device. 23 */ 24abstract public class MidiReceiver { 25 /** 26 * Called to pass MIDI data to the receiver. 27 * May fail if count exceeds {@link #getMaxMessageSize}. 28 * 29 * NOTE: the msg array parameter is only valid within the context of this call. 30 * The msg bytes should be copied by the receiver rather than retaining a reference 31 * to this parameter. 32 * Also, modifying the contents of the msg array parameter may result in other receivers 33 * in the same application receiving incorrect values in their {link #onReceive} method. 34 * 35 * @param msg a byte array containing the MIDI data 36 * @param offset the offset of the first byte of the data in the array to be processed 37 * @param count the number of bytes of MIDI data in the array to be processed 38 * @param timestamp the timestamp of the message (based on {@link java.lang.System#nanoTime} 39 * @throws IOException 40 */ 41 abstract public void onReceive(byte[] msg, int offset, int count, long timestamp) 42 throws IOException; 43 44 /** 45 * Returns the maximum size of a message this receiver can receive. 46 * Defaults to {@link java.lang.Integer#MAX_VALUE} unless overridden. 47 * @return maximum message size 48 */ 49 public int getMaxMessageSize() { 50 return Integer.MAX_VALUE; 51 } 52 53 /** 54 * Called to send MIDI data to the receiver 55 * Data will get split into multiple calls to {@link #onReceive} if count exceeds 56 * {@link #getMaxMessageSize}. 57 * 58 * @param msg a byte array containing the MIDI data 59 * @param offset the offset of the first byte of the data in the array to be sent 60 * @param count the number of bytes of MIDI data in the array to be sent 61 * @throws IOException 62 */ 63 public void send(byte[] msg, int offset, int count) throws IOException { 64 sendWithTimestamp(msg, offset, count, System.nanoTime()); 65 } 66 67 /** 68 * Called to send MIDI data to the receiver to be handled at a specified time in the future 69 * Data will get split into multiple calls to {@link #onReceive} if count exceeds 70 * {@link #getMaxMessageSize}. 71 * 72 * @param msg a byte array containing the MIDI data 73 * @param offset the offset of the first byte of the data in the array to be sent 74 * @param count the number of bytes of MIDI data in the array to be sent 75 * @param timestamp the timestamp of the message (based on {@link java.lang.System#nanoTime} 76 * @throws IOException 77 */ 78 public void sendWithTimestamp(byte[] msg, int offset, int count, long timestamp) 79 throws IOException { 80 int messageSize = getMaxMessageSize(); 81 while (count > 0) { 82 int length = (count > messageSize ? messageSize : count); 83 onReceive(msg, offset, length, timestamp); 84 offset += length; 85 count -= length; 86 } 87 } 88} 89