MidiReceiver.java revision d8f6363a5d9c72f1ed22f79bc16563123dff4803
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 * Although public, this method should be considered a private implementation 27 * detail. Client code should call {@link #send} or {@link #sendWithTimestamp} 28 * instead. 29 * 30 * Called to pass MIDI data to the receiver. 31 * May fail if count exceeds {@link #getMaxMessageSize}. 32 * 33 * NOTE: the msg array parameter is only valid within the context of this call. 34 * The msg bytes should be copied by the receiver rather than retaining a reference 35 * to this parameter. 36 * Also, modifying the contents of the msg array parameter may result in other receivers 37 * in the same application receiving incorrect values in their {link #onReceive} method. 38 * 39 * @param msg a byte array containing the MIDI data 40 * @param offset the offset of the first byte of the data in the array to be processed 41 * @param count the number of bytes of MIDI data in the array to be processed 42 * @param timestamp the timestamp of the message (based on {@link java.lang.System#nanoTime} 43 * @throws IOException 44 */ 45 abstract public void onReceive(byte[] msg, int offset, int count, long timestamp) 46 throws IOException; 47 48 /** 49 * Instructs the receiver to discard all pending events. 50 * @throws IOException 51 */ 52 public void flush() throws IOException { 53 } 54 55 /** 56 * Returns the maximum size of a message this receiver can receive. 57 * Defaults to {@link java.lang.Integer#MAX_VALUE} unless overridden. 58 * @return maximum message size 59 */ 60 public int getMaxMessageSize() { 61 return Integer.MAX_VALUE; 62 } 63 64 /** 65 * Called to send MIDI data to the receiver 66 * Data will get split into multiple calls to {@link #onReceive} if count exceeds 67 * {@link #getMaxMessageSize}. 68 * 69 * @param msg a byte array containing the MIDI data 70 * @param offset the offset of the first byte of the data in the array to be sent 71 * @param count the number of bytes of MIDI data in the array to be sent 72 * @throws IOException 73 */ 74 public void send(byte[] msg, int offset, int count) throws IOException { 75 sendWithTimestamp(msg, offset, count, System.nanoTime()); 76 } 77 78 /** 79 * Called to send MIDI data to the receiver to be handled at a specified time in the future 80 * Data will get split into multiple calls to {@link #onReceive} if count exceeds 81 * {@link #getMaxMessageSize}. 82 * 83 * @param msg a byte array containing the MIDI data 84 * @param offset the offset of the first byte of the data in the array to be sent 85 * @param count the number of bytes of MIDI data in the array to be sent 86 * @param timestamp the timestamp of the message (based on {@link java.lang.System#nanoTime} 87 * @throws IOException 88 */ 89 public void sendWithTimestamp(byte[] msg, int offset, int count, long timestamp) 90 throws IOException { 91 int messageSize = getMaxMessageSize(); 92 while (count > 0) { 93 int length = (count > messageSize ? messageSize : count); 94 onReceive(msg, offset, length, timestamp); 95 offset += length; 96 count -= length; 97 } 98 } 99} 100