Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 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 | |
| 17 | package com.android.bluetoothmidiservice; |
| 18 | |
| 19 | import android.media.midi.MidiReceiver; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | import java.io.IOException; |
| 23 | |
| 24 | /** |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 25 | * This is an abstract base class that decodes a BLE-MIDI packet |
| 26 | * buffer and passes it to a {@link android.media.midi.MidiReceiver} |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 27 | */ |
| 28 | public class BluetoothPacketDecoder extends PacketDecoder { |
| 29 | |
| 30 | private static final String TAG = "BluetoothPacketDecoder"; |
| 31 | |
| 32 | private final byte[] mBuffer; |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 33 | private int mBytesInBuffer; |
Mike Lockwood | ff00180 | 2015-04-21 09:33:09 -0700 | [diff] [blame] | 34 | private MidiBtleTimeTracker mTimeTracker; |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 35 | |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 36 | private int mLowTimestamp; |
| 37 | private long mNanoTimestamp; |
| 38 | |
| 39 | private static final int TIMESTAMP_MASK_HIGH = 0x1F80; // top 7 bits |
| 40 | private static final int TIMESTAMP_MASK_LOW = 0x7F; // bottom 7 bits |
| 41 | private static final int HEADER_TIMESTAMP_MASK = 0x3F; // bottom 6 bits |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 42 | |
| 43 | public BluetoothPacketDecoder(int maxPacketSize) { |
| 44 | mBuffer = new byte[maxPacketSize]; |
| 45 | } |
| 46 | |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 47 | private void flushOutput(MidiReceiver receiver) { |
| 48 | if (mBytesInBuffer > 0) { |
| 49 | try { |
| 50 | receiver.send(mBuffer, 0, mBytesInBuffer, mNanoTimestamp); |
| 51 | } catch (IOException e) { |
| 52 | // ??? |
| 53 | } |
| 54 | mBytesInBuffer = 0; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // NOTE: this code allows running status across packets, |
| 59 | // although the specification does not allow that. |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 60 | @Override |
| 61 | public void decodePacket(byte[] buffer, MidiReceiver receiver) { |
Mike Lockwood | ff00180 | 2015-04-21 09:33:09 -0700 | [diff] [blame] | 62 | if (mTimeTracker == null) { |
| 63 | mTimeTracker = new MidiBtleTimeTracker(System.nanoTime()); |
| 64 | } |
| 65 | |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 66 | int length = buffer.length; |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 67 | if (length < 1) { |
| 68 | Log.e(TAG, "empty packet"); |
| 69 | return; |
| 70 | } |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 71 | |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 72 | byte header = buffer[0]; |
Phil Burk | a576959 | 2020-03-27 17:56:57 -0700 | [diff] [blame^] | 73 | // Check for the header bit 7. |
| 74 | // Ignore the reserved bit 6. |
| 75 | if ((header & 0x80) != 0x80) { |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 76 | Log.e(TAG, "packet does not start with header"); |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | // shift bits 0 - 5 to bits 7 - 12 |
Mike Lockwood | ff00180 | 2015-04-21 09:33:09 -0700 | [diff] [blame] | 81 | int highTimestamp = (header & HEADER_TIMESTAMP_MASK) << 7; |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 82 | boolean lastWasTimestamp = false; |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 83 | int previousLowTimestamp = 0; |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 84 | int currentTimestamp = highTimestamp | mLowTimestamp; |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 85 | |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 86 | // Iterate through the rest of the packet, separating MIDI data from timestamps. |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 87 | for (int i = 1; i < buffer.length; i++) { |
| 88 | byte b = buffer[i]; |
| 89 | |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 90 | // Is this a timestamp byte? |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 91 | if ((b & 0x80) != 0 && !lastWasTimestamp) { |
| 92 | lastWasTimestamp = true; |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 93 | mLowTimestamp = b & TIMESTAMP_MASK_LOW; |
| 94 | |
| 95 | // If the low timestamp byte wraps within the packet then |
| 96 | // increment the high timestamp byte. |
| 97 | if (mLowTimestamp < previousLowTimestamp) { |
Mike Lockwood | ff00180 | 2015-04-21 09:33:09 -0700 | [diff] [blame] | 98 | highTimestamp = (highTimestamp + 0x0080) & TIMESTAMP_MASK_HIGH; |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 99 | } |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 100 | previousLowTimestamp = mLowTimestamp; |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 101 | |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 102 | // If the timestamp advances then send any pending data. |
| 103 | int newTimestamp = highTimestamp | mLowTimestamp; |
Mike Lockwood | ff00180 | 2015-04-21 09:33:09 -0700 | [diff] [blame] | 104 | if (newTimestamp != currentTimestamp) { |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 105 | // Send previous message separately since it has a different timestamp. |
| 106 | flushOutput(receiver); |
Mike Lockwood | ff00180 | 2015-04-21 09:33:09 -0700 | [diff] [blame] | 107 | currentTimestamp = newTimestamp; |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 108 | } |
Mike Lockwood | ff00180 | 2015-04-21 09:33:09 -0700 | [diff] [blame] | 109 | |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 110 | // Calculate MIDI nanosecond timestamp from BLE timestamp. |
Mike Lockwood | ff00180 | 2015-04-21 09:33:09 -0700 | [diff] [blame] | 111 | long now = System.nanoTime(); |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 112 | mNanoTimestamp = mTimeTracker.convertTimestampToNanotime(currentTimestamp, now); |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 113 | } else { |
| 114 | lastWasTimestamp = false; |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 115 | // Flush if full before adding more data. |
| 116 | if (mBytesInBuffer == mBuffer.length) { |
| 117 | flushOutput(receiver); |
| 118 | } |
| 119 | mBuffer[mBytesInBuffer++] = b; |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Phil Burk | 51aaf2e | 2020-02-20 14:23:39 -0800 | [diff] [blame] | 123 | flushOutput(receiver); |
Mike Lockwood | f0a41d1 | 2015-03-24 08:27:11 -0700 | [diff] [blame] | 124 | } |
| 125 | } |