MidiPortImpl.java revision eebc98ff18c1ee92dff3fcd505158ea161d552be
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 19/** 20 * This class contains utilities for socket communication between a 21 * MidiInputPort and MidiOutputPort 22 */ 23/* package */ class MidiPortImpl { 24 private static final String TAG = "MidiPort"; 25 26 /** 27 * Maximum size of a packet that can pass through our ParcelFileDescriptor. 28 */ 29 public static final int MAX_PACKET_SIZE = 1024; 30 31 /** 32 * size of message timestamp in bytes 33 */ 34 private static final int TIMESTAMP_SIZE = 8; 35 36 /** 37 * Maximum amount of MIDI data that can be included in a packet 38 */ 39 public static final int MAX_PACKET_DATA_SIZE = MAX_PACKET_SIZE - TIMESTAMP_SIZE; 40 41 /** 42 * Utility function for packing a MIDI message to be sent through our ParcelFileDescriptor 43 * 44 * message byte array contains variable length MIDI message. 45 * messageSize is size of variable length MIDI message 46 * timestamp is message timestamp to pack 47 * dest is buffer to pack into 48 * returns size of packed message 49 */ 50 public static int packMessage(byte[] message, int offset, int size, long timestamp, 51 byte[] dest) { 52 if (size + TIMESTAMP_SIZE > MAX_PACKET_SIZE) { 53 size = MAX_PACKET_SIZE - TIMESTAMP_SIZE; 54 } 55 // message data goes first 56 System.arraycopy(message, offset, dest, 0, size); 57 58 // followed by timestamp 59 for (int i = 0; i < TIMESTAMP_SIZE; i++) { 60 dest[size++] = (byte)timestamp; 61 timestamp >>= 8; 62 } 63 64 return size; 65 } 66 67 /** 68 * Utility function for unpacking a MIDI message received from our ParcelFileDescriptor 69 * returns the offset of the MIDI message in packed buffer 70 */ 71 public static int getMessageOffset(byte[] buffer, int bufferLength) { 72 // message is at the beginning 73 return 0; 74 } 75 76 /** 77 * Utility function for unpacking a MIDI message received from our ParcelFileDescriptor 78 * returns size of MIDI data in packed buffer 79 */ 80 public static int getMessageSize(byte[] buffer, int bufferLength) { 81 // message length is total buffer length minus size of the timestamp 82 return bufferLength - TIMESTAMP_SIZE; 83 } 84 85 /** 86 * Utility function for unpacking a MIDI message received from our ParcelFileDescriptor 87 * unpacks timestamp from packed buffer 88 */ 89 public static long getMessageTimeStamp(byte[] buffer, int bufferLength) { 90 // timestamp is at end of the packet 91 int offset = bufferLength; 92 long timestamp = 0; 93 94 for (int i = 0; i < TIMESTAMP_SIZE; i++) { 95 int b = (int)buffer[--offset] & 0xFF; 96 timestamp = (timestamp << 8) | b; 97 } 98 return timestamp; 99 } 100} 101