1/*
2 * Copyright (C) 2015 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 com.android.internal.midi;
18
19import android.media.midi.MidiReceiver;
20//import android.util.Log;
21
22import java.io.IOException;
23
24/**
25 * Convert stream of bytes to discrete messages.
26 *
27 * Parses the incoming bytes and then posts individual messages to the receiver
28 * specified in the constructor. Short messages of 1-3 bytes will be complete.
29 * System Exclusive messages may be posted in pieces.
30 *
31 * Resolves Running Status and
32 * interleaved System Real-Time messages.
33 */
34public 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     * @see android.midi.MidiReceiver#onSend(byte[], int, int, long)
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) { // status byte?
68                if (currentInt < 0xF0) { // channel message?
69                    mRunningStatus = currentByte;
70                    mCount = 1;
71                    mNeeded = MidiConstants.getBytesPerMessage(currentByte) - 1;
72                } else if (currentInt < 0xF8) { // system common?
73                    if (currentInt == 0xF0 /* SysEx Start */) {
74                        // Log.i(TAG, "SysEx Start");
75                        mInSysEx = true;
76                        sysExStartOffset = offset;
77                    } else if (currentInt == 0xF7 /* SysEx End */) {
78                        // Log.i(TAG, "SysEx End");
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 { // real-time?
92                    // Single byte message interleaved with other data.
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 { // data byte
101                if (!mInSysEx) {
102                    mBuffer[mCount++] = currentByte;
103                    if (--mNeeded == 0) {
104                        if (mRunningStatus != 0) {
105                            mBuffer[0] = mRunningStatus;
106                        }
107                        mReceiver.send(mBuffer, 0, mCount, timestamp);
108                        mNeeded = MidiConstants.getBytesPerMessage(mBuffer[0]) - 1;
109                        mCount = 1;
110                    }
111                }
112            }
113            ++offset;
114        }
115
116        // send any accumulatedSysEx data
117        if (sysExStartOffset >= 0 && sysExStartOffset < offset) {
118            mReceiver.send(data, sysExStartOffset,
119                    offset - sysExStartOffset, timestamp);
120        }
121    }
122
123}
124