| 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 |
import java.io.IOException; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class MidiEventScheduler extends EventScheduler { |
| 27 |
private static final String TAG = "MidiEventScheduler"; |
| 28 |
|
| 29 |
|
| 30 |
private final static int POOL_EVENT_SIZE = 16; |
| 31 |
private MidiReceiver mReceiver = new SchedulingReceiver(); |
| 32 |
|
| 33 |
private class SchedulingReceiver extends MidiReceiver { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
@Override |
| 39 |
public void onSend(byte[] msg, int offset, int count, long timestamp) |
| 40 |
throws IOException { |
| 41 |
MidiEvent event = createScheduledEvent(msg, offset, count, timestamp); |
| 42 |
if (event != null) { |
| 43 |
add(event); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
@Override |
| 48 |
public void onFlush() { |
| 49 |
MidiEventScheduler.this.flush(); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
public static class MidiEvent extends SchedulableEvent { |
| 54 |
public int count = 0; |
| 55 |
public byte[] data; |
| 56 |
|
| 57 |
private MidiEvent(int count) { |
| 58 |
super(0); |
| 59 |
data = new byte[count]; |
| 60 |
} |
| 61 |
|
| 62 |
private MidiEvent(byte[] msg, int offset, int count, long timestamp) { |
| 63 |
super(timestamp); |
| 64 |
data = new byte[count]; |
| 65 |
System.arraycopy(msg, offset, data, 0, count); |
| 66 |
this.count = count; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public String toString() { |
| 71 |
String text = "Event: "; |
| 72 |
for (int i = 0; i < count; i++) { |
| 73 |
text += data[i] + ", "; |
| 74 |
} |
| 75 |
return text; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
private MidiEvent createScheduledEvent(byte[] msg, int offset, int count, |
| 83 |
long timestamp) { |
| 84 |
MidiEvent event; |
| 85 |
if (count > POOL_EVENT_SIZE) { |
| 86 |
event = new MidiEvent(msg, offset, count, timestamp); |
| 87 |
} else { |
| 88 |
event = (MidiEvent) removeEventfromPool(); |
| 89 |
if (event == null) { |
| 90 |
event = new MidiEvent(POOL_EVENT_SIZE); |
| 91 |
} |
| 92 |
System.arraycopy(msg, offset, event.data, 0, count); |
| 93 |
event.count = count; |
| 94 |
event.setTimestamp(timestamp); |
| 95 |
} |
| 96 |
return event; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@Override |
| 105 |
public void addEventToPool(SchedulableEvent event) { |
| 106 |
|
| 107 |
if (event instanceof MidiEvent) { |
| 108 |
MidiEvent midiEvent = (MidiEvent) event; |
| 109 |
if (midiEvent.data.length == POOL_EVENT_SIZE) { |
| 110 |
super.addEventToPool(event); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public MidiReceiver getReceiver() { |
| 120 |
return mReceiver; |
| 121 |
} |
| 122 |
|
| 123 |
} |