| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.midi; |
| 18 |
|
| 19 |
import android.media.midi.MidiReceiver; |
| 20 |
|
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class MidiFramer extends MidiReceiver { |
| 35 |
|
| 36 |
public String TAG = "MidiFramer"; |
| 37 |
private MidiReceiver mReceiver; |
| 38 |
private byte[] mBuffer = new byte[3]; |
| 39 |
private int mCount; |
| 40 |
private byte mRunningStatus; |
| 41 |
private int mNeeded; |
| 42 |
private boolean mInSysEx; |
| 43 |
|
| 44 |
public MidiFramer(MidiReceiver receiver) { |
| 45 |
mReceiver = receiver; |
| 46 |
} |
| 47 |
|
| 48 |
public static String formatMidiData(byte[] data, int offset, int count) { |
| 49 |
String text = "MIDI+" + offset + " : "; |
| 50 |
for (int i = 0; i < count; i++) { |
| 51 |
text += String.format("0x%02X, ", data[offset + i]); |
| 52 |
} |
| 53 |
return text; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
@Override |
| 60 |
public void onSend(byte[] data, int offset, int count, long timestamp) |
| 61 |
throws IOException { |
| 62 |
int sysExStartOffset = (mInSysEx ? offset : -1); |
| 63 |
|
| 64 |
for (int i = 0; i < count; i++) { |
| 65 |
final byte currentByte = data[offset]; |
| 66 |
final int currentInt = currentByte & 0xFF; |
| 67 |
if (currentInt >= 0x80) { |
| 68 |
if (currentInt < 0xF0) { |
| 69 |
mRunningStatus = currentByte; |
| 70 |
mCount = 1; |
| 71 |
mNeeded = MidiConstants.getBytesPerMessage(currentByte) - 1; |
| 72 |
} else if (currentInt < 0xF8) { |
| 73 |
if (currentInt == 0xF0 ) { |
| 74 |
|
| 75 |
mInSysEx = true; |
| 76 |
sysExStartOffset = offset; |
| 77 |
} else if (currentInt == 0xF7 ) { |
| 78 |
|
| 79 |
if (mInSysEx) { |
| 80 |
mReceiver.send(data, sysExStartOffset, |
| 81 |
offset - sysExStartOffset + 1, timestamp); |
| 82 |
mInSysEx = false; |
| 83 |
sysExStartOffset = -1; |
| 84 |
} |
| 85 |
} else { |
| 86 |
mBuffer[0] = currentByte; |
| 87 |
mRunningStatus = 0; |
| 88 |
mCount = 1; |
| 89 |
mNeeded = MidiConstants.getBytesPerMessage(currentByte) - 1; |
| 90 |
} |
| 91 |
} else { |
| 92 |
|
| 93 |
if (mInSysEx) { |
| 94 |
mReceiver.send(data, sysExStartOffset, |
| 95 |
offset - sysExStartOffset, timestamp); |
| 96 |
sysExStartOffset = offset + 1; |
| 97 |
} |
| 98 |
mReceiver.send(data, offset, 1, timestamp); |
| 99 |
} |
| 100 |
} else { |
| 101 |
if (!mInSysEx) { |
| 102 |
|
| 103 |
|
| 104 |
if (mNeeded <= 0) { |
| 105 |
break; |
| 106 |
} |
| 107 |
|
| 108 |
mBuffer[mCount++] = currentByte; |
| 109 |
if (--mNeeded == 0) { |
| 110 |
if (mRunningStatus != 0) { |
| 111 |
mBuffer[0] = mRunningStatus; |
| 112 |
} |
| 113 |
mReceiver.send(mBuffer, 0, mCount, timestamp); |
| 114 |
mNeeded = MidiConstants.getBytesPerMessage(mBuffer[0]) - 1; |
| 115 |
mCount = 1; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
++offset; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
if (sysExStartOffset >= 0 && sysExStartOffset < offset) { |
| 124 |
mReceiver.send(data, sysExStartOffset, |
| 125 |
offset - sysExStartOffset, timestamp); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
} |