MidiReceiver.java revision b6f50d357bd3d4d296be6bb047f5ce93a79cbca1
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 * Instructs the receiver to discard all pending events. 46 * @throws IOException 47 */ 48 public void flush() throws IOException { 49 } 50 51 /** 52 * Returns the maximum size of a message this receiver can receive. 53 * Defaults to {@link java.lang.Integer#MAX_VALUE} unless overridden. 54 * @return maximum message size 55 */ 56 public int getMaxMessageSize() { 57 return Integer.MAX_VALUE; 58 } 59 60 /** 61 * Called to send MIDI data to the receiver 62 * Data will get split into multiple calls to {@link #onReceive} if count exceeds 63 * {@link #getMaxMessageSize}. 64 * 65 * @param msg a byte array containing the MIDI data 66 * @param offset the offset of the first byte of the data in the array to be sent 67 * @param count the number of bytes of MIDI data in the array to be sent 68 * @throws IOException 69 */ 70 public void send(byte[] msg, int offset, int count) throws IOException { 71 sendWithTimestamp(msg, offset, count, System.nanoTime()); 72 } 73 74 /** 75 * Called to send MIDI data to the receiver to be handled at a specified time in the future 76 * Data will get split into multiple calls to {@link #onReceive} if count exceeds 77 * {@link #getMaxMessageSize}. 78 * 79 * @param msg a byte array containing the MIDI data 80 * @param offset the offset of the first byte of the data in the array to be sent 81 * @param count the number of bytes of MIDI data in the array to be sent 82 * @param timestamp the timestamp of the message (based on {@link java.lang.System#nanoTime} 83 * @throws IOException 84 */ 85 public void sendWithTimestamp(byte[] msg, int offset, int count, long timestamp) 86 throws IOException { 87 int messageSize = getMaxMessageSize(); 88 while (count > 0) { 89 int length = (count > messageSize ? messageSize : count); 90 onReceive(msg, offset, length, timestamp); 91 offset += length; 92 count -= length; 93 } 94 } 95} 96